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, values = {}) ⇒ Relation
constructor
A new instance of Relation.
- #initialize_copy(other) ⇒ Object
- #initialize_copy_without_cpk ⇒ Object
- #initialize_without_cpk ⇒ Object
-
#update_record(values, id, id_was) ⇒ Object
:nodoc:.
Constructor Details
#initialize(klass, table, values = {}) ⇒ Relation
Returns a new instance of Relation.
4 5 6 7 8 |
# File 'lib/composite_primary_keys/relation.rb', line 4 def initialize(klass, table, values = {}) initialize_without_cpk(klass, table, values) add_cpk_support if klass && klass.composite? add_cpk_where_values_hash end |
Instance Method Details
#add_cpk_support ⇒ Object
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 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/composite_primary_keys/relation.rb', line 16 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) # 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
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/composite_primary_keys/relation.rb', line 61 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: #equalities = where_values.grep(Arel::Nodes::Equality).find_all { |node| # node.left.relation.name == table_name # } nodes_from_and = where_values.grep(Arel::Nodes::And).map {|and_node| and_node.children.grep(Arel::Nodes::Equality) }.flatten equalities = (nodes_from_and + where_values.grep(Arel::Nodes::Equality)).find_all { |node| node.left.relation.name == table_name } binds = Hash[bind_values.find_all(&:first).map { |column, v| [column.name, v] }] Hash[equalities.map { |where| name = where.left.name [name, binds.fetch(name.to_s) { where.right }] }] end end end |
#initialize_copy(other) ⇒ Object
11 12 13 14 |
# File 'lib/composite_primary_keys/relation.rb', line 11 def initialize_copy(other) initialize_copy_without_cpk(other) add_cpk_support if klass.composite? end |
#initialize_copy_without_cpk ⇒ Object
10 |
# File 'lib/composite_primary_keys/relation.rb', line 10 alias :initialize_copy_without_cpk :initialize_copy |
#initialize_without_cpk ⇒ Object
3 |
# File 'lib/composite_primary_keys/relation.rb', line 3 alias :initialize_without_cpk :initialize |
#update_record(values, id, id_was) ⇒ Object
:nodoc:
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/composite_primary_keys/relation.rb', line 84 def update_record(values, id, id_was) # :nodoc: substitutes, binds = substitute_values values # CPK um = if self.composite? relation = @klass.unscoped.where(cpk_id_predicate(@klass.arel_table, @klass.primary_key, id_was || id)) relation.arel.compile_update(substitutes, @klass.primary_key) else @klass.unscoped.where(@klass.arel_table[@klass.primary_key].eq(id_was || id)).arel.compile_update(substitutes, @klass.primary_key) end @klass.connection.update( um, 'SQL', binds) end |