Module: ActiveRecord::Sanitization::ClassMethods

Defined in:
lib/composite_primary_keys/sanitization.rb

Instance Method Summary collapse

Instance Method Details

#expand_hash_conditions_for_aggregates(attrs) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/composite_primary_keys/sanitization.rb', line 4

def expand_hash_conditions_for_aggregates(attrs)
  expanded_attrs = {}
  attrs.each do |attr, value|
    # CPK
    # if aggregation = reflect_on_aggregation(attr.to_sym)
    if attr.is_a?(CompositePrimaryKeys::CompositeKeys)
      value = value.split('/') if value.is_a?(String)
      attr.each_with_index do |key,i|
        expanded_attrs[key] = value.respond_to?(:flatten) ? value.flatten[i] : value
      end
    elsif aggregation = reflect_on_aggregation(attr.to_sym)
      mapping = aggregation.mapping
      mapping.each do |field_attr, aggregate_attr|
        if mapping.size == 1 && !value.respond_to?(aggregate_attr)
          expanded_attrs[field_attr] = value
        else
          expanded_attrs[field_attr] = value.send(aggregate_attr)
        end
      end
    else
      expanded_attrs[attr] = value
    end
  end
  expanded_attrs
end

#quoted_idObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/composite_primary_keys/sanitization.rb', line 30

def quoted_id
  # CPK
  # self.class.quote_value(@attributes[self.class.primary_key].value_for_database)
  if self.composite?
    [self.class.primary_keys, ids].transpose.map { |attr_name,id|
      self.class.quote_value(@attributes[attr_name].value_for_database)
    }
  else
    self.class.quote_value(@attributes[self.class.primary_key].value_for_database)
  end
end