Module: ActiveModel::AttributeFilters::Common::Join::ClassMethods

Defined in:
lib/attribute-filters/common_filters/join.rb

Overview

This submodule contains class methods needed to describe attribute joining.

Instance Method Summary collapse

Instance Method Details

#join_attributes(atr_name, parameters = nil) Also known as: join_attribute, joint_attribute, joint_attributes, join_attributes_to, join_attributes_into, joins_attribute, joins_attributes_to, joins_attributes_into

This method returns an undefined value.

This method parametrizes joining operation for an attribute of the given name. It uses attribute set annotations to register parameters used when joining.

Parameters:

  • atr_name (String, Symbol)

    attribute name

  • parameters (Hash) (defaults to: nil)

    parameters hash

Options Hash (parameters):

  • :separator (String)

    separator passed to join method call

  • :join_separator (String)

    separator passed to join method call (alternative name)

  • :with (String)

    separator passed to join method call (alternative name)

  • :from (String, Array<String>)

    names of source attributes used to join

  • :join_from (String, Array<String>)

    names of source attributes used to join (alternative name)

  • :source (String, Array<String>)

    names of source attributes used to join (alternative name)

  • :sources (String, Array<String>)

    names of source attributes used to join (alternative name)

  • :compact (Boolean)

    flag that causes sources to be compacted before joining

  • :join_compact (Boolean)

    flag that causes sources to be compacted before joining (alternative name)



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/attribute-filters/common_filters/join.rb', line 67

def join_attributes(atr_name, parameters = nil)
  atr_name.is_a?(Hash) and return atr_name.each_pair { |k, v| join_attribute(k, v) }
  # process reversed notation
  p = parameters
  if atr_name.is_a?(Array)
    if p.is_a?(Hash)
      p = p.dup
      dst = [:into, :in, :destination, :join_into].find { |k| p.key?(k) }
      dst.nil? and raise ArgumentError, "you must specify destination attribute using :into => 'attribute_name'"
      p[:from] = atr_name
      return join_attribute(p.delete(dst), p)
    else
      return join_attribute(p, atr_name)
    end
  end
  # setup attribute set
  setup_attributes_that :should_be_joined, { atr_name => p },
    {
      :join_separator   => [ :with, :separator, :join_separator ],
      :join_from        => [ :from, :source, :sources, :join_from ],
      :join_compact     => [ :compact, :join_compact ]
    }, :join_from
end