permission_list.rb revision 6d67b58472abc76a1d0d68a1d1fa32a45dc6626a
98N/A#
98N/A# Helper-Class for rendering a permission list
1600N/A#
98N/Aclass PermissionList
98N/A
919N/A attr_reader :model, :collection_path, :collection, :scope, :association
919N/A
919N/A #
919N/A # first argument: restful path to the permissions collection
919N/A # second argument: options hash that may contain the following elements:
919N/A #
919N/A # :scope => scope for the autocompleter
919N/A # :model => class that represents the permissions
919N/A # :association => name of the activerecord-association
919N/A # :collection => collection of all permissions
919N/A def initialize(collection_path, options)
919N/A @collection_path = collection_path
919N/A @editable = true
919N/A
919N/A options.each do |key,value|
919N/A case key
919N/A when :model, :collection, :association
98N/A instance_variable_set("@#{key}", value)
98N/A when :scope
98N/A value = [value] unless value.is_a?(Array)
98N/A value.each do |v|
810N/A raise "Scope '#{v}' is not a class" unless v.is_a?(Class)
810N/A end
810N/A @scope = value.map(&:to_s).join(",")
810N/A else
354N/A raise ArgumentError, "invalid option: #{key}"
354N/A end
354N/A end
354N/A
354N/A # check required attributes
354N/A for key in %w( model scope collection )
354N/A raise ArgumentError, "#{key} is not set" unless instance_variable_get("@#{key}")
354N/A end
606N/A end
810N/A
1123N/A def polymorphic?
606N/A @model.reflect_on_association(@association).options[:polymorphic]==true
354N/A end
810N/A
810N/A # path for rendering a PermissionsList instance
830N/A def to_partial_path
1026N/A 'permission_list/permission_list'
810N/A end
1029N/A
1123N/A def form_path
1123N/A partial_path :form
1123N/A end
1123N/A
1123N/A def permission_path
1565N/A partial_path model_underscore
1565N/A end
810N/A
810N/A # path to a specific partial of the permission list
810N/A def partial_path(name)
1123N/A "#{model_underscore.pluralize}/#{name}"
810N/A end
810N/A
810N/A def model_underscore
810N/A @model.name.underscore
810N/A end
810N/A
851N/A def t(key)
810N/A I18n.t(key, :scope => "permission_list.#{model_underscore}" )
851N/A end
810N/A
810N/A def to_data
810N/A {
810N/A 'data-model' => model,
810N/A 'data-scope' => scope,
810N/A 'data-polymorphic' => polymorphic? && 'true',
851N/A 'data-association' => association
810N/A }
851N/A end
1010N/A
810N/Aend