Sha256: 43330d990d0e113c52db40abc47a5ff965ce8f7965277ead54a36030c87450b6

Contents?: true

Size: 1.85 KB

Versions: 2

Compression:

Stored size: 1.85 KB

Contents

module ActiveRecord
  module Sanitization
    module ClassMethods
      protected
      # Accepts a hash of SQL conditions and replaces those attributes
      # that correspond to a +composed_of+ relationship with their expanded
      # aggregate attribute values.
      # Given:
      #     class Person < ActiveRecord::Base
      #       composed_of :address, class_name: "Address",
      #         mapping: [%w(address_street street), %w(address_city city)]
      #     end
      # Then:
      #     { address: Address.new("813 abc st.", "chicago") }
      #       # => { address_street: "813 abc st.", address_city: "chicago" }
      def expand_hash_conditions_for_aggregates(attrs)
        expanded_attrs = {}
        attrs.each do |attr, value|
          if attr.is_a?(CompositePrimaryKeys::CompositeKeys)
            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

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
composite_primary_keys-8.1.0 lib/composite_primary_keys/sanitization.rb
composite_primary_keys-8.0.0 lib/composite_primary_keys/sanitization.rb