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

Included in:
ActiveModel::AttributeFilters::Common
Defined in:
lib/attribute-filters/common_filters/join.rb

Overview

Joins attributes.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Methods included from FilteringRegistration

#filtering_method

Instance Method Details

#join_attributesObject

Joins attributes and writes the results into other attribute.

The attrubutes to be destination for joins are taken from the attribute set called should_be_joined.

The pattern used to join a string and the optional separator argument should be set using the model’s class method join_attribute.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/attribute-filters/common_filters/join.rb', line 26

def join_attributes
  filter_attrs_from_set(:should_be_joined, :process_all, :process_blank) do |atr_val, atr_name, set_obj|
    from, compact = set_obj.annotation(atr_name, :join_from, :join_compact)
    if from.blank?
      next atr_val if !atr_val.is_a?(Array)
      from = [ atr_name ]
    elsif !from.is_a?(Array)
      from = [ from ]
    end
    vals = AttributeSet::Query.new(from, self).values
    separator = set_obj.has_annotation?(atr_name, :join_separator) ?
                set_obj.annotation(atr_name, :join_separator) : " "
    if compact && vals.respond_to?(:compact)
      vals.compact.join(separator)
    elsif vals.respond_to?(:join)
      vals.join(separator)
    else
      vals
    end
  end
end