class Para::AttributeField::HasManyField

Public Instance Methods

assign_ids(params, resource) click to toggle source
# 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) click to toggle source
# 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_name() click to toggle source
# File lib/para/attribute_field/has_many.rb, line 6
def field_name
  reflection.name
end
parse_input(params, resource) click to toggle source
# 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_key() click to toggle source
# File lib/para/attribute_field/has_many.rb, line 30
def plural_foreign_key
  foreign_key.to_s.pluralize
end
value_for(instance) click to toggle source
# 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