Class: Madmin::Fields::BelongsTo

Inherits:
Madmin::Field show all
Defined in:
lib/madmin/fields/belongs_to.rb

Instance Attribute Summary

Attributes inherited from Madmin::Field

#attribute_name, #model, #options, #resource

Instance Method Summary collapse

Methods inherited from Madmin::Field

#default_index_attributes, field_type, #initialize, #paginateable?, #required?, #searchable?, #to_partial_path, #value, #visible?

Constructor Details

This class inherits a constructor from Madmin::Field

Instance Method Details

#associated_resourceObject



18
19
20
21
# File 'lib/madmin/fields/belongs_to.rb', line 18

def associated_resource
  Madmin.resource_by_name(model.reflect_on_association(attribute_name).klass)
rescue MissingResource
end

#associated_resource_for(object) ⇒ Object



23
24
25
26
# File 'lib/madmin/fields/belongs_to.rb', line 23

def associated_resource_for(object)
  Madmin.resource_for(object)
rescue MissingResource
end

#index_path(format: :json) ⇒ Object



14
15
16
# File 'lib/madmin/fields/belongs_to.rb', line 14

def index_path(format: :json)
  associated_resource&.index_path(format: format)
end

#options_for_select(record) ⇒ Object



4
5
6
7
8
# File 'lib/madmin/fields/belongs_to.rb', line 4

def options_for_select(record)
  current_value = record.send(attribute_name)
  records = [current_value].compact + associated_resource.model.excluding(current_value).limit(25)
  records.map { [Madmin.resource_for(_1).display_name(_1), _1.id] }
end

#to_paramObject



10
11
12
# File 'lib/madmin/fields/belongs_to.rb', line 10

def to_param
  "#{attribute_name}_id"
end