Sha256: 3b05e680c34aa4f0dc1f79dbb6fd5c8acb15eb436f8843625696563e96eb260a

Contents?: true

Size: 1.78 KB

Versions: 3

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

require 'rails_admin/config/fields/association'

module RailsAdmin
  module Config
    module Fields
      module Types
        class BelongsToAssociation < RailsAdmin::Config::Fields::Association
          RailsAdmin::Config::Fields::Types.register(self)

          register_instance_option :filter_operators do
            %w[_discard like not_like is starts_with ends_with] + (required? ? [] : %w[_separator _present _blank])
          end

          register_instance_option :formatted_value do
            (o = value) && o.send(associated_model_config.object_label_method)
          end

          register_instance_option :sortable do
            @sortable ||= abstract_model.adapter_supports_joins? && associated_model_config.abstract_model.properties.collect(&:name).include?(associated_model_config.object_label_method) ? associated_model_config.object_label_method : {abstract_model.table_name => method_name}
          end

          register_instance_option :searchable do
            @searchable ||= associated_model_config.abstract_model.properties.collect(&:name).include?(associated_model_config.object_label_method) ? [associated_model_config.object_label_method, {abstract_model.model => method_name}] : {abstract_model.model => method_name}
          end

          register_instance_option :partial do
            nested_form ? :form_nested_one : :form_filtering_select
          end

          register_instance_option :eager_load do
            true
          end

          def selected_id
            bindings[:object].safe_send(association.key_accessor)
          end

          def method_name
            nested_form ? :"#{name}_attributes" : super
          end

          def multiple?
            false
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rails_admin-3.2.0.beta lib/rails_admin/config/fields/types/belongs_to_association.rb
rails_admin-3.1.4 lib/rails_admin/config/fields/types/belongs_to_association.rb
rails_admin-3.1.3 lib/rails_admin/config/fields/types/belongs_to_association.rb