Module: RailsAdmin::Config::Fields
- Defined in:
- lib/rails_admin/config/fields.rb,
lib/rails_admin/config/fields/base.rb,
lib/rails_admin/config/fields/group.rb,
lib/rails_admin/config/fields/types.rb,
lib/rails_admin/config/fields/groupable.rb,
lib/rails_admin/config/fields/types/date.rb,
lib/rails_admin/config/fields/types/enum.rb,
lib/rails_admin/config/fields/types/text.rb,
lib/rails_admin/config/fields/types/time.rb,
lib/rails_admin/config/fields/association.rb,
lib/rails_admin/config/fields/types/color.rb,
lib/rails_admin/config/fields/types/float.rb,
lib/rails_admin/config/fields/types/string.rb,
lib/rails_admin/config/fields/types/boolean.rb,
lib/rails_admin/config/fields/types/decimal.rb,
lib/rails_admin/config/fields/types/integer.rb,
lib/rails_admin/config/fields/types/datetime.rb,
lib/rails_admin/config/fields/types/password.rb,
lib/rails_admin/config/fields/types/dragonfly.rb,
lib/rails_admin/config/fields/types/paperclip.rb,
lib/rails_admin/config/fields/types/timestamp.rb,
lib/rails_admin/config/fields/types/carrierwave.rb,
lib/rails_admin/config/fields/types/file_upload.rb,
lib/rails_admin/config/fields/types/has_one_association.rb,
lib/rails_admin/config/fields/types/has_many_association.rb,
lib/rails_admin/config/fields/types/belongs_to_association.rb,
lib/rails_admin/config/fields/types/polymorphic_association.rb,
lib/rails_admin/config/fields/types/has_and_belongs_to_many_association.rb
Defined Under Namespace
Modules: Groupable, Types Classes: Association, Base, Group
Constant Summary collapse
- @@default_factory =
lambda do |parent, properties, fields| # If it's an association if properties.has_key?(:parent_model) association = parent.abstract_model.associations.find {|a| a[:name].to_s == properties[:name].to_s} field = RailsAdmin::Config::Fields::Types.load("#{association[:polymorphic] ? :polymorphic : properties[:type]}_association").new(parent, properties[:name], association) field.read_only(true) if association[:read_only] fields << field # If it's a column elsif !properties.has_key?(:parent_model) fields << (field = RailsAdmin::Config::Fields::Types.load(properties[:type]).new(parent, properties[:name], properties)) # hide _type columns (handled as associations) if parent.abstract_model.belongs_to_associations.find {|a| a[:foreign_type] == properties[:name] } field.hide end # hide _id column if parent.abstract_model.belongs_to_associations.find {|a| a[:child_key] == properties[:name] } field.hide field.filterable(false) # filtering is handled on the association itself end end end
- @@registry =
Registry of field factories.
Field factory is an anonymous function that recieves the parent object, an array of field properties and an array of fields already instantiated.
If the factory returns true then that property will not be run through the rest of the registered factories. If it returns false then the arguments will be passed to the next factory.
By default a basic factory is registered which loads fields by their database column type. Also a password factory is registered which loads fields if their name is password. Third default factory is a devise specific factory which loads fields for devise user models.
[@@default_factory]
Class Method Summary collapse
-
.factory(parent) ⇒ Object
Build an array of fields by the provided parent object’s abstract_model’s property and association information.
-
.register_factory(&block) ⇒ Object
Register a field factory to be included in the factory stack.
Class Method Details
.factory(parent) ⇒ Object
Build an array of fields by the provided parent object’s abstract_model’s property and association information. Each property and association is passed to the registered field factories which will populate the fields array that will be returned.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/rails_admin/config/fields.rb', line 58 def self.factory(parent) fields = [] return fields unless parent.abstract_model.model_store_exists? # Load fields for all properties (columns) parent.abstract_model.properties.each do |properties| # Unless a previous factory has already loaded current field as well unless fields.find {|f| f.name == properties[:name] } # Loop through factories until one returns true @@registry.find {|factory| factory.call(parent, properties, fields) } end end # Load fields for all associations (relations) parent.abstract_model.associations.each do |association| # Unless a previous factory has already loaded current field as well unless fields.find {|f| f.name == association[:name] } # Loop through factories until one returns true @@registry.find {|factory| factory.call(parent, association, fields) } end end fields end |
.register_factory(&block) ⇒ Object
Register a field factory to be included in the factory stack.
Factories are invoked lifo (last in first out).
86 87 88 |
# File 'lib/rails_admin/config/fields.rb', line 86 def self.register_factory(&block) @@registry.unshift(block) end |