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/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/hidden.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/serialized.rb,
lib/rails_admin/config/fields/types/carrierwave.rb,
lib/rails_admin/config/fields/types/file_upload.rb,
lib/rails_admin/config/fields/types/bson_object_id.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: Types Classes: Association, Base, Group

Constant Summary collapse

@@default_factory =
lambda do |parent, properties, fields|
  # If it's an association
  if properties.has_key?(:model_proc)
    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)
  else
    field = RailsAdmin::Config::Fields::Types.load(properties[:type]).new(parent, properties[:name], properties)
  end
  fields << field
  field
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.

See Also:

  • register_factory
  • RailsAdmin::Config::Fields.rails_admin/config/fields/factories/passwordrails_admin/config/fields/factories/password.rb
  • RailsAdmin::Config::Fields.rails_admin/config/fields/factories/deviserails_admin/config/fields/factories/devise.rb
[@@default_factory]

Class Method Summary collapse

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.

See Also:

  • registry


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rails_admin/config/fields.rb', line 46

def self.factory(parent)
  fields = []
  # 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.select{|a| a[:type] != :belongs_to }.each do |association| # :belongs_to are created by factory for belongs_to fields
    # 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).

See Also:

  • registry


73
74
75
# File 'lib/rails_admin/config/fields.rb', line 73

def self.register_factory(&block)
  @@registry.unshift(block)
end