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/inet.rb,
lib/rails_admin/config/fields/types/json.rb,
lib/rails_admin/config/fields/types/text.rb,
lib/rails_admin/config/fields/types/time.rb,
lib/rails_admin/config/fields/types/uuid.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/citext.rb,
lib/rails_admin/config/fields/types/froala.rb,
lib/rails_admin/config/fields/types/hidden.rb,
lib/rails_admin/config/fields/types/shrine.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/numeric.rb,
lib/rails_admin/config/fields/types/datetime.rb,
lib/rails_admin/config/fields/types/password.rb,
lib/rails_admin/config/fields/types/ck_editor.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/wysihtml5.rb,
lib/rails_admin/config/fields/types/serialized.rb,
lib/rails_admin/config/fields/types/simple_mde.rb,
lib/rails_admin/config/fields/types/action_text.rb,
lib/rails_admin/config/fields/types/carrierwave.rb,
lib/rails_admin/config/fields/types/code_mirror.rb,
lib/rails_admin/config/fields/types/file_upload.rb,
lib/rails_admin/config/fields/types/string_like.rb,
lib/rails_admin/config/fields/singular_association.rb,
lib/rails_admin/config/fields/types/active_storage.rb,
lib/rails_admin/config/fields/types/bson_object_id.rb,
lib/rails_admin/config/fields/collection_association.rb,
lib/rails_admin/config/fields/types/active_record_enum.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/multiple_carrierwave.rb,
lib/rails_admin/config/fields/types/multiple_file_upload.rb,
lib/rails_admin/config/fields/types/belongs_to_association.rb,
lib/rails_admin/config/fields/types/multiple_active_storage.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, CollectionAssociation, Group, SingularAssociation

Constant Summary collapse

@@default_factory =
proc do |parent, properties, fields|
  # If it's an association
  if properties.association?
    association = parent.abstract_model.associations.detect { |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 receives 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


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

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
    next if fields.detect { |f| f.name == properties.name }

    # Loop through factories until one returns true
    @@registry.detect { |factory| factory.call(parent, properties, fields) }
  end
  # Load fields for all associations (relations)
  parent.abstract_model.associations.reject { |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
    next if fields.detect { |f| f.name == association.name }

    # Loop through factories until one returns true
    @@registry.detect { |factory| factory.call(parent, association, fields) }
  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


75
76
77
# File 'lib/rails_admin/config/fields.rb', line 75

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