Class: RailsAdmin::Config::Fields::CollectionAssociation

Inherits:
Association
  • Object
show all
Defined in:
lib/rails_admin/config/fields/collection_association.rb

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 Association

#associated_model_config, #associated_model_limit, #associated_object_label_method, #associated_primary_key, #association, #dynamic_scope_relationships, #method_name, #polymorphic?, #value, #virtual?

Methods inherited from Base

#association?, #eager_load_values, #editable?, #errors, #filter_options, #generic_field_help, #generic_help, #initialize, #inverse_of, #method_name, #optional, #optional=, #optional?, #parse_value, #sort_column, #type, #type_css_class, #value, #virtual?

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_prepopulate_paramsObject



30
31
32
# File 'lib/rails_admin/config/fields/collection_association.rb', line 30

def associated_prepopulate_params
  {associated_model_config.abstract_model.param_key => {association.foreign_key => bindings[:object].try(:id)}}
end

#collection(scope = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rails_admin/config/fields/collection_association.rb', line 18

def collection(scope = nil)
  if scope
    super
  elsif associated_collection_cache_all
    selected = selected_ids
    i = 0
    super.sort_by { |a| [selected.index(a[1]) || selected.size, i += 1] }
  else
    value.map { |o| [o.send(associated_object_label_method), format_key(o.send(associated_primary_key))] }
  end
end

#form_default_valueObject



59
60
61
# File 'lib/rails_admin/config/fields/collection_association.rb', line 59

def form_default_value
  (default_value if bindings[:object].new_record? && value.empty?)
end

#form_valueObject



63
64
65
# File 'lib/rails_admin/config/fields/collection_association.rb', line 63

def form_value
  form_default_value.nil? ? selected_ids : form_default_value
end

#multiple?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/rails_admin/config/fields/collection_association.rb', line 34

def multiple?
  true
end

#parse_input(params) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rails_admin/config/fields/collection_association.rb', line 42

def parse_input(params)
  return unless associated_model_config.abstract_model.primary_key.is_a?(Array)

  if nested_form
    params[method_name].each_value do |value|
      value[:id] = associated_model_config.abstract_model.parse_id(value[:id])
    end
  elsif params[method_name].is_a?(Array)
    params[method_name] = params[method_name].map { |key| associated_model_config.abstract_model.parse_id(key) if key.present? }.compact
    if params[method_name].empty?
      # Workaround for Arel::Visitors::UnsupportedVisitError in #ids_writer, until https://github.com/rails/rails/pull/51116 is in place
      params.delete(method_name)
      params[name] = []
    end
  end
end

#selected_idsObject



38
39
40
# File 'lib/rails_admin/config/fields/collection_association.rb', line 38

def selected_ids
  value.map { |s| format_key(s.send(associated_primary_key)).to_s }
end

#widget_optionsObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rails_admin/config/fields/collection_association.rb', line 67

def widget_options
  {
    xhr: !associated_collection_cache_all,
    'edit-url': (inline_edit && bindings[:view].authorized?(:edit, associated_model_config.abstract_model) ? bindings[:view].edit_path(model_name: associated_model_config.abstract_model.to_param, id: '__ID__') : ''),
    remote_source: bindings[:view].index_path(associated_model_config.abstract_model, source_object_id: abstract_model.format_id(bindings[:object].id), source_abstract_model: abstract_model.to_param, associated_collection: name, current_action: bindings[:view].current_action, compact: true),
    scopeBy: dynamic_scope_relationships,
    sortable: !!orderable,
    removable: !!removable,
    cacheAll: !!associated_collection_cache_all,
    regional: {
      add: ::I18n.t('admin.misc.add_new'),
      chooseAll: ::I18n.t('admin.misc.chose_all'),
      clearAll: ::I18n.t('admin.misc.clear_all'),
      down: ::I18n.t('admin.misc.down'),
      remove: ::I18n.t('admin.misc.remove'),
      search: ::I18n.t('admin.misc.search'),
      up: ::I18n.t('admin.misc.up'),
    },
  }
end