class RailsAdmin::Config::Fields::Association

Public Instance Methods

associated_model_config() click to toggle source

Reader for the association’s child model’s configuration

# File lib/rails_admin/config/fields/association.rb, line 103
def associated_model_config
  @associated_model_config ||= RailsAdmin.config(association.klass)
end
associated_model_limit() click to toggle source
# File lib/rails_admin/config/fields/association.rb, line 152
def associated_model_limit
  RailsAdmin.config.default_associated_collection_limit
end
associated_object_label_method() click to toggle source

Reader for the association’s child model object’s label method

# File lib/rails_admin/config/fields/association.rb, line 108
def associated_object_label_method
  @associated_object_label_method ||= associated_model_config.object_label_method
end
associated_prepopulate_params() click to toggle source

Returns params which are to be set in modals

# File lib/rails_admin/config/fields/association.rb, line 118
def associated_prepopulate_params
  {}
end
associated_primary_key() click to toggle source

Reader for associated primary key

# File lib/rails_admin/config/fields/association.rb, line 113
def associated_primary_key
  association.primary_key
end
association() click to toggle source

Reader for the association information hash

# File lib/rails_admin/config/fields/association.rb, line 11
def association
  @properties
end
collection(scope = nil) click to toggle source

Returns collection of all selectable records

# File lib/rails_admin/config/fields/association.rb, line 138
def collection(scope = nil)
  (scope || bindings[:controller].list_entries(associated_model_config, :index, associated_collection_scope, false)).
    map { |o| [o.send(associated_object_label_method), format_key(o.send(associated_primary_key)).to_s] }
end
dynamic_scope_relationships() click to toggle source

parses dynamically_scope_by and returns a Hash in the form of {[form field name in this model]: [field name in the associated model]}

# File lib/rails_admin/config/fields/association.rb, line 69
def dynamic_scope_relationships
  @dynamic_scope_relationships ||=
    Array.wrap(dynamically_scope_by).flat_map do |field|
      field.is_a?(Hash) ? field.to_a : [[field, field]]
    end.map do |field_name, target_name| # rubocop:disable Style/MultilineBlockChain
      field = section.fields.detect { |f| f.name == field_name }
      raise "Field '#{field_name}' was given for #dynamically_scope_by but not found in '#{abstract_model.model_name}'" unless field

      target_field = associated_model_config.list.fields.detect { |f| f.name == target_name }
      raise "Field '#{field_name}' was given for #dynamically_scope_by but not found in '#{associated_model_config.abstract_model.model_name}'" unless target_field
      raise "Field '#{field_name}' in '#{associated_model_config.abstract_model.model_name}' can't be used for dynamic scoping because it's not filterable" unless target_field.filterable

      [field.method_name, target_name]
    end.to_h
end
method_name() click to toggle source
# File lib/rails_admin/config/fields/association.rb, line 15
def method_name
  nested_form ? :"#{name}_attributes" : association.key_accessor
end
multiple?() click to toggle source

has many?

# File lib/rails_admin/config/fields/association.rb, line 144
def multiple?
  true
end
polymorphic?() click to toggle source

Reader whether this is a polymorphic association

# File lib/rails_admin/config/fields/association.rb, line 123
def polymorphic?
  association.polymorphic?
end
value() click to toggle source

Reader for the association’s value unformatted

# File lib/rails_admin/config/fields/association.rb, line 133
def value
  bindings[:object].send(association.name)
end
virtual?() click to toggle source
# File lib/rails_admin/config/fields/association.rb, line 148
def virtual?
  true
end

Private Instance Methods

format_key(key) click to toggle source
# File lib/rails_admin/config/fields/association.rb, line 158
def format_key(key)
  if key.is_a?(Array)
    RailsAdmin.config.composite_keys_serializer.serialize(key)
  else
    key
  end
end