Class: Para::AttributeField::HasManyField

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

Direct Known Subclasses

NestedManyField

Instance Attribute Summary

Attributes inherited from Base

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

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

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

Instance Method Details

#field_nameObject



6
7
8
# File 'lib/para/attribute_field/has_many.rb', line 6

def field_name
  reflection.name
end

#parse_input(params) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/para/attribute_field/has_many.rb', line 16

def parse_input(params)
  if (ids = params[plural_foreign_key].presence) && String === ids
    # Format selectize value for Rails
    ids = params[plural_foreign_key] = ids.split(',')

    on_the_fly_creation(ids) do |resource, value|
      params[plural_foreign_key].delete(value)
      params[plural_foreign_key] << resource.id
    end
  end
end

#plural_foreign_keyObject



28
29
30
# File 'lib/para/attribute_field/has_many.rb', line 28

def plural_foreign_key
  foreign_key.to_s.pluralize
end

#value_for(instance) ⇒ Object



10
11
12
13
14
# File 'lib/para/attribute_field/has_many.rb', line 10

def value_for(instance)
  instance.send(name).map do |resource|
    resource_name(resource)
  end.join(', ')
end