Module: ActiveRecord::Associations::ThroughAssociationScope
- Defined in:
- lib/composite_primary_keys/associations/through_association_scope.rb
Instance Method Summary collapse
- #composite_ids_hash(keys, ids) ⇒ Object
- #composite_join_clause(table1, keys1, table2, keys2) ⇒ Object
- #composite_join_predicates(table1, keys1, table2, keys2) ⇒ Object
- #construct_joins(custom_joins = nil) ⇒ Object
-
#construct_owner_attributes(reflection) ⇒ Object
Construct attributes for associate pointing to owner.
- #construct_quoted_owner_attributes(reflection) ⇒ Object
Instance Method Details
#composite_ids_hash(keys, ids) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/composite_primary_keys/associations/through_association_scope.rb', line 28 def composite_ids_hash(keys, ids) [keys].flatten.zip([ids].flatten).inject(Hash.new) do |hash, (key, value)| hash[key] = value hash end end |
#composite_join_clause(table1, keys1, table2, keys2) ⇒ Object
4 5 6 7 8 9 10 11 12 |
# File 'lib/composite_primary_keys/associations/through_association_scope.rb', line 4 def composite_join_clause(table1, keys1, table2, keys2) predicates = composite_join_predicates(table1, keys1, table2, keys2) join_clause = predicates.map do |predicate| predicate.to_sql end.join(" AND ") "(#{join_clause})" end |
#composite_join_predicates(table1, keys1, table2, keys2) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/composite_primary_keys/associations/through_association_scope.rb', line 14 def composite_join_predicates(table1, keys1, table2, keys2) attributes1 = [keys1].flatten.map do |key| table1[key] end attributes2 = [keys2].flatten.map do |key| table2[key] end [attributes1, attributes2].transpose.map do |attribute1, attribute2| attribute1.eq(attribute2) end end |
#construct_joins(custom_joins = nil) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/composite_primary_keys/associations/through_association_scope.rb', line 64 def construct_joins(custom_joins = nil) polymorphic_join = nil if @reflection.source_reflection.macro == :belongs_to reflection_primary_key = @reflection.klass.primary_key source_primary_key = @reflection.source_reflection.cpk_primary_key if @reflection.[:source_type] polymorphic_join = "AND %s.%s = %s" % [ @reflection.through_reflection.quoted_table_name, "#{@reflection.source_reflection.[:foreign_type]}", @owner.class.quote_value(@reflection.[:source_type]) ] end else reflection_primary_key = @reflection.source_reflection.cpk_primary_key source_primary_key = @reflection.through_reflection.klass.primary_key if @reflection.source_reflection.[:as] polymorphic_join = "AND %s.%s = %s" % [ @reflection.quoted_table_name, "#{@reflection.source_reflection.[:as]}_type", @owner.class.quote_value(@reflection.through_reflection.klass.name) ] end end # CPK # "INNER JOIN %s ON %s.%s = %s.%s %s #{@reflection.options[:joins]} #{custom_joins}" % [ # @reflection.through_reflection.quoted_table_name, # @reflection.quoted_table_name, reflection_primary_key, # @reflection.through_reflection.quoted_table_name, source_primary_key, # polymorphic_join # ] "INNER JOIN %s ON %s %s #{@reflection.[:joins]} #{custom_joins}" % [ @reflection.through_reflection.quoted_table_name, composite_join_clause(@reflection.klass.arel_table, reflection_primary_key, @reflection.through_reflection.klass.arel_table, source_primary_key), polymorphic_join ] end |
#construct_owner_attributes(reflection) ⇒ Object
Construct attributes for associate pointing to owner.
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/composite_primary_keys/associations/through_association_scope.rb', line 53 def construct_owner_attributes(reflection) if as = reflection.[:as] { "#{as}_id" => @owner.id, "#{as}_type" => @owner.class.base_class.name.to_s } else # CPK # { reflection.primary_key_name => @owner.id } composite_ids_hash(reflection.cpk_primary_key, @owner.id) end end |
#construct_quoted_owner_attributes(reflection) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/composite_primary_keys/associations/through_association_scope.rb', line 35 def construct_quoted_owner_attributes(reflection) if as = reflection.[:as] { "#{as}_id" => owner_quoted_id, "#{as}_type" => reflection.klass.quote_value( @owner.class.base_class.name.to_s, reflection.klass.columns_hash["#{as}_type"]) } elsif reflection.macro == :belongs_to # CPK # { reflection.klass.primary_key => @owner[reflection.primary_key_name] } composite_ids_hash(reflection.klass.primary_key, @owner.quoted_id) else # CPK #{ reflection.primary_key_name => owner_quoted_id } composite_ids_hash(reflection.cpk_primary_key, @owner.quoted_id) end end |