class RailsAdmin::Config::Fields::Types::PolymorphicAssociation

Public Instance Methods

associated_model_config() click to toggle source
# File lib/rails_admin/config/fields/types/polymorphic_association.rb, line 54
def associated_model_config
  @associated_model_config ||= association.klass.collect { |type| RailsAdmin.config(type) }.reject(&:excluded?)
end
collection(_scope = nil) click to toggle source
# File lib/rails_admin/config/fields/types/polymorphic_association.rb, line 58
def collection(_scope = nil)
  if value
    [[formatted_value, selected_id]]
  else
    [[]]
  end
end
parse_input(params) click to toggle source
# File lib/rails_admin/config/fields/types/polymorphic_association.rb, line 108
def parse_input(params)
  if (type_value = params[association.foreign_type.to_sym]).present?
    config = associated_model_config.find { |c| type_value == c.abstract_model.model.name }
    params[association.foreign_type.to_sym] = config.abstract_model.base_class.name if config
  end
end
selected_type() click to toggle source
# File lib/rails_admin/config/fields/types/polymorphic_association.rb, line 104
def selected_type
  bindings[:object].send(type_column)
end
type_collection() click to toggle source
# File lib/rails_admin/config/fields/types/polymorphic_association.rb, line 70
def type_collection
  associated_model_config.collect do |config|
    [config.label, config.abstract_model.model.name]
  end
end
type_column() click to toggle source
# File lib/rails_admin/config/fields/types/polymorphic_association.rb, line 66
def type_column
  association.foreign_type.to_s
end
type_urls() click to toggle source
# File lib/rails_admin/config/fields/types/polymorphic_association.rb, line 76
def type_urls
  types = associated_model_config.collect do |config|
    [config.abstract_model.model.name, config.abstract_model.to_param]
  end
  ::Hash[*types.collect { |v| [v[0], bindings[:view].index_path(v[1])] }.flatten]
end
value() click to toggle source

Reader for field’s value

# File lib/rails_admin/config/fields/types/polymorphic_association.rb, line 84
def value
  bindings[:object].send(association.name)
end
widget_options() click to toggle source
# File lib/rails_admin/config/fields/types/polymorphic_association.rb, line 100
def widget_options
  widget_options_for_types[selected_type.try(:downcase)] || {float_left: false}
end
widget_options_for_types() click to toggle source
# File lib/rails_admin/config/fields/types/polymorphic_association.rb, line 88
def widget_options_for_types
  type_collection.inject({}) do |options, model|
    options.merge(
      model.second.downcase.gsub('::', '-') => {
        xhr: true,
        remote_source: bindings[:view].index_path(model.second.underscore, source_object_id: bindings[:object].id, source_abstract_model: abstract_model.to_param, current_action: bindings[:view].current_action, compact: true),
        float_left: false,
      },
    )
  end
end