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, #required?, #searchable?, #to_partial_path, #value, #visible?

Constructor Details

This class inherits a constructor from Madmin::Field

Instance Method Details

#associated_resourceObject



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

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

#index_pathObject



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

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

#options_for_select(record) ⇒ Object



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

def options_for_select(record)
  records = if (record = record.send(attribute_name))
    [record]
  else
    associated_resource.model.first(25)
  end

  records.map { [Madmin.resource_for(_1).display_name(_1), _1.id] }
end

#to_paramObject



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

def to_param
  "#{attribute_name}_id"
end