Class: Para::AttributeField::BelongsToField

Inherits:
RelationField show all
Defined in:
lib/para/attribute_field/belongs_to.rb

Direct Known Subclasses

NestedOneField

Instance Attribute Summary

Attributes inherited from Base

#field_method, #field_type, #model, #name, #options, #type

Instance Method Summary collapse

Methods inherited from RelationField

#foreign_key, #polymorphic_through_reflection?, #reflection, #through_reflection, #through_relation, #through_relation_source_foreign_key

Methods included from Helpers::ResourceName

#resource_name

Methods inherited from Base

#attribute_column_path, #determine_name_and_field_method!, #excerptable_value?, field_option, #field_options, field_types, #initialize, register, #searchable?, #type?

Constructor Details

This class inherits a constructor from Para::AttributeField::Base

Instance Method Details

#field_nameObject



8
9
10
# File 'lib/para/attribute_field/belongs_to.rb', line 8

def field_name
  reflection.name
end

#parse_input(params, resource) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/para/attribute_field/belongs_to.rb', line 22

def parse_input(params, resource)
  if reflection &&
     (id = params[reflection.foreign_key].presence) &&
     !reflection.klass.exists?(id: id)
  then
    on_the_fly_creation(id) do |resource|
      params[reflection.foreign_key] = resource.id
    end
  end
end

#relation_optionsObject



18
19
20
# File 'lib/para/attribute_field/belongs_to.rb', line 18

def relation_options
  reflection.klass.all
end

#value_for(instance) ⇒ Object



12
13
14
15
16
# File 'lib/para/attribute_field/belongs_to.rb', line 12

def value_for(instance)
  if (resource = instance.send(name))
    resource_name(resource)
  end
end