Class: Administrate::Field::BelongsTo

Inherits:
Associative show all
Defined in:
lib/administrate/field/belongs_to.rb

Direct Known Subclasses

Polymorphic

Instance Attribute Summary

Attributes inherited from Base

#attribute, #data, #options, #page, #resource

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Associative

#associated_class, associated_class, associated_class_name, #associated_class_name, #display_associated_resource, foreign_key_for, reflection

Methods inherited from Base

associative?, field_type, html_class, #html_class, #initialize, #name, #required?, searchable?, #to_partial_path, with_options

Constructor Details

This class inherits a constructor from Administrate::Field::Base

Class Method Details

.eager_load?Boolean

Returns:



16
17
18
# File 'lib/administrate/field/belongs_to.rb', line 16

def self.eager_load?
  true
end

.permitted_attribute(attr, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/administrate/field/belongs_to.rb', line 6

def self.permitted_attribute(attr, options = {})
  resource_class = options[:resource_class]
  if resource_class
    foreign_key_for(resource_class, attr)
  else
    Administrate.warn_of_missing_resource_class
    :"#{attr}_id"
  end
end

Instance Method Details

#associated_resource_optionsObject



24
25
26
27
28
# File 'lib/administrate/field/belongs_to.rb', line 24

def associated_resource_options
  candidate_resources.map do |resource|
    [display_candidate_resource(resource), resource.send(primary_key)]
  end
end

#include_blank_optionObject



34
35
36
# File 'lib/administrate/field/belongs_to.rb', line 34

def include_blank_option
  options.fetch(:include_blank, true)
end

#permitted_attributeObject



20
21
22
# File 'lib/administrate/field/belongs_to.rb', line 20

def permitted_attribute
  foreign_key
end

#selected_optionObject



30
31
32
# File 'lib/administrate/field/belongs_to.rb', line 30

def selected_option
  data && data.send(primary_key)
end