Module: ActiveRecord::Persistence::ClassMethods
- Defined in:
- lib/composite_primary_keys/persistence.rb
Instance Method Summary collapse
-
#_delete_record(constraints) ⇒ Object
:nodoc:.
-
#_update_record(values, constraints) ⇒ Object
:nodoc:.
- #delete(id_or_array) ⇒ Object
Instance Method Details
#_delete_record(constraints) ⇒ Object
:nodoc:
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/composite_primary_keys/persistence.rb', line 42 def _delete_record(constraints) # :nodoc: # CPK if self.composite? && constraints[primary_key] primary_key_values = constraints.delete(primary_key) primary_key.each_with_index do |key, i| constraints[key] = primary_key_values[i] end end constraints = _substitute_values(constraints).map { |attr, bind| attr.eq(bind) } dm = Arel::DeleteManager.new dm.from(arel_table) dm.wheres = constraints connection.delete(dm, "#{self} Destroy") end |
#_update_record(values, constraints) ⇒ Object
:nodoc:
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/composite_primary_keys/persistence.rb', line 24 def _update_record(values, constraints) # :nodoc: # CPK if self.composite? && constraints[primary_key] primary_key_values = constraints.delete(primary_key) primary_key.each_with_index do |key, i| constraints[key] = primary_key_values[i] end end constraints = _substitute_values(constraints).map { |attr, bind| attr.eq(bind) } um = arel_table.where( constraints.reduce(&:and) ).compile_update(_substitute_values(values), primary_key) connection.update(um, "#{self} Update") end |
#delete(id_or_array) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/composite_primary_keys/persistence.rb', line 4 def delete(id_or_array) # CPK if self.composite? id_or_array = if id_or_array.is_a?(CompositePrimaryKeys::CompositeKeys) [id_or_array] else Array(id_or_array) end # Delete should return the number of deleted records id_or_array.map do |id| # Is the passed in id actually a record? id = id.kind_of?(::ActiveRecord::Base) ? id.id : id delete_by(cpk_id_predicate(self.arel_table, self.primary_key, id)) end.sum else delete_by(primary_key => id_or_array) end end |