Class: ActiveRecord::Relation
- Inherits:
-
Object
- Object
- ActiveRecord::Relation
- Defined in:
- lib/composite_primary_keys/relation.rb
Instance Method Summary collapse
- #add_cpk_support ⇒ Object
- #add_cpk_where_values_hash ⇒ Object
-
#initialize(klass, table) ⇒ Relation
constructor
A new instance of Relation.
- #initialize_copy(other) ⇒ Object
- #initialize_copy_without_cpk ⇒ Object
- #initialize_without_cpk ⇒ Object
Constructor Details
#initialize(klass, table) ⇒ Relation
Returns a new instance of Relation.
65 66 67 68 69 |
# File 'lib/composite_primary_keys/relation.rb', line 65 def initialize(klass, table) initialize_without_cpk(klass, table) add_cpk_support if klass.composite? add_cpk_where_values_hash end |
Instance Method Details
#add_cpk_support ⇒ Object
3 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/composite_primary_keys/relation.rb', line 3 def add_cpk_support class << self include CompositePrimaryKeys::ActiveRecord::Batches include CompositePrimaryKeys::ActiveRecord::Calculations include CompositePrimaryKeys::ActiveRecord::FinderMethods include CompositePrimaryKeys::ActiveRecord::QueryMethods def delete(id_or_array) ::ActiveRecord::IdentityMap.remove_by_id(self.symbolized_base_class, id_or_array) if ::ActiveRecord::IdentityMap.enabled? # Without CPK: # where(primary_key => id_or_array).delete_all id_or_array = if id_or_array.kind_of?(CompositePrimaryKeys::CompositeKeys) [id_or_array] else Array(id_or_array) end id_or_array.each do |id| where(cpk_id_predicate(table, self.primary_key, id)).delete_all end end def destroy(id_or_array) # Without CPK: #if id.is_a?(Array) # id.map { |one_id| destroy(one_id) } #else # find(id).destroy #end id_or_array = if id_or_array.kind_of?(CompositePrimaryKeys::CompositeKeys) [id_or_array] else Array(id_or_array) end id_or_array.each do |id| where(cpk_id_predicate(table, self.primary_key, id)).each do |record| record.destroy end end end end end |
#add_cpk_where_values_hash ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/composite_primary_keys/relation.rb', line 49 def add_cpk_where_values_hash class << self def where_values_hash # CPK adds this so that it finds the Equality nodes beneath the And node: nodes_from_and = with_default_scope.where_values.grep(Arel::Nodes::And).map {|and_node| and_node.children.grep(Arel::Nodes::Equality) }.flatten equalities = (nodes_from_and + with_default_scope.where_values.grep(Arel::Nodes::Equality)).find_all { |node| node.left.relation.name == table_name } Hash[equalities.map { |where| [where.left.name, where.right] }] end end end |
#initialize_copy(other) ⇒ Object
72 73 74 75 |
# File 'lib/composite_primary_keys/relation.rb', line 72 def initialize_copy(other) initialize_copy_without_cpk(other) add_cpk_support if klass.composite? end |
#initialize_copy_without_cpk ⇒ Object
71 |
# File 'lib/composite_primary_keys/relation.rb', line 71 alias :initialize_copy_without_cpk :initialize_copy |
#initialize_without_cpk ⇒ Object
64 |
# File 'lib/composite_primary_keys/relation.rb', line 64 alias :initialize_without_cpk :initialize |