Class: Para::AttributeField::BelongsToField

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

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

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