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, #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

#assign_ids(params, resource) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/para/attribute_field/has_many.rb', line 34

def assign_ids(params, resource)
  return unless Array === params[plural_foreign_key]

  ids = params.delete(plural_foreign_key)

  if through_reflection && through_reflection.klass.orderable?
    assign_ordered_through_reflection_ids(through_reflection, resource, ids)
  else
    resource.send(:"#{plural_foreign_key}=", ids)
  end
end

#assign_ordered_through_reflection_ids(through_reflection, resource, ids) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/para/attribute_field/has_many.rb', line 46

def assign_ordered_through_reflection_ids(through_reflection, resource, ids)
  association = resource.association(through_reflection.name)
  join_resources = association.load_target

  return association.replace([]) if ids.empty?

  new_resources = ids.each_with_index.map do |id, position|
    join_resource = join_resources.find do |res|
      res.send(through_relation_source_foreign_key) == id &&
        (!polymorphic_through_reflection? || res.send(reflection.foreign_type) == reflection.klass.name)
    end

    unless join_resource
      attributes = { through_relation_source_foreign_key => id }
      attributes[reflection.foreign_type] = reflection.klass.name if polymorphic_through_reflection?
      join_resource = association.build(attributes)
    end

    join_resource.position = position
    join_resource
  end

  association.replace(new_resources)
end

#field_nameObject



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

def field_name
  reflection.name
end

#parse_input(params, resource) ⇒ Object



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

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

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

  assign_ids(params, resource)
end

#plural_foreign_keyObject



30
31
32
# File 'lib/para/attribute_field/has_many.rb', line 30

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