Class: RailsAdmin::Config::Fields::Association

Inherits:
Base
  • Object
show all
Defined in:
lib/rails_admin/config/fields/association.rb

Direct Known Subclasses

CollectionAssociation, SingularAssociation

Constant Summary

Constants inherited from Base

Base::NAMED_INSTANCE_VARIABLES

Instance Attribute Summary

Attributes inherited from Base

#abstract_model, #defined, #name, #order, #parent, #properties, #root, #section

Instance Method Summary collapse

Methods inherited from Base

#association?, #eager_load_values, #editable?, #errors, #filter_options, #form_default_value, #form_value, #generic_field_help, #generic_help, #initialize, #inverse_of, #optional, #optional=, #optional?, #parse_input, #parse_value, #sort_column, #type, #type_css_class

Methods included from Inspectable

#inspect

Methods included from Groupable

#group

Methods included from Hideable

#hidden?, #hide, included, #show

Methods included from Configurable

#has_option?, included, #register_deprecated_instance_option, #register_instance_option

Methods included from Proxyable

#bindings, #bindings=, #with

Constructor Details

This class inherits a constructor from RailsAdmin::Config::Fields::Base

Instance Method Details

#associated_model_configObject

Reader for the association’s child model’s configuration



103
104
105
# File 'lib/rails_admin/config/fields/association.rb', line 103

def associated_model_config
  @associated_model_config ||= RailsAdmin.config(association.klass)
end

#associated_model_limitObject



152
153
154
# File 'lib/rails_admin/config/fields/association.rb', line 152

def associated_model_limit
  RailsAdmin.config.default_associated_collection_limit
end

#associated_object_label_methodObject

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



108
109
110
# 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_paramsObject

Returns params which are to be set in modals



118
119
120
# File 'lib/rails_admin/config/fields/association.rb', line 118

def associated_prepopulate_params
  {}
end

#associated_primary_keyObject

Reader for associated primary key



113
114
115
# File 'lib/rails_admin/config/fields/association.rb', line 113

def associated_primary_key
  association.primary_key
end

#associationObject

Reader for the association information hash



11
12
13
# File 'lib/rails_admin/config/fields/association.rb', line 11

def association
  @properties
end

#collection(scope = nil) ⇒ Object

Returns collection of all selectable records



138
139
140
141
# 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_relationshipsObject

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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_nameObject



15
16
17
# File 'lib/rails_admin/config/fields/association.rb', line 15

def method_name
  nested_form ? :"#{name}_attributes" : association.key_accessor
end

#multiple?Boolean

has many?

Returns:

  • (Boolean)


144
145
146
# File 'lib/rails_admin/config/fields/association.rb', line 144

def multiple?
  true
end

#polymorphic?Boolean

Reader whether this is a polymorphic association

Returns:

  • (Boolean)


123
124
125
# File 'lib/rails_admin/config/fields/association.rb', line 123

def polymorphic?
  association.polymorphic?
end

#valueObject

Reader for the association’s value unformatted



133
134
135
# File 'lib/rails_admin/config/fields/association.rb', line 133

def value
  bindings[:object].send(association.name)
end

#virtual?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/rails_admin/config/fields/association.rb', line 148

def virtual?
  true
end