Class: ActiveRecord::Relation
- Inherits:
-
Object
- Object
- ActiveRecord::Relation
- Defined in:
- lib/composite_primary_keys/relation.rb,
lib/composite_primary_keys/relation/where_clause.rb
Defined Under Namespace
Classes: WhereClause
Instance Method Summary collapse
- #add_cpk_support ⇒ Object
- #delete_all(conditions = nil) ⇒ Object
-
#initialize(klass, table, predicate_builder, values = {}) ⇒ Relation
constructor
A new instance of Relation.
- #initialize_copy(other) ⇒ Object
- #initialize_copy_without_cpk ⇒ Object
- #initialize_without_cpk ⇒ Object
- #update_all(updates) ⇒ Object
Constructor Details
#initialize(klass, table, predicate_builder, values = {}) ⇒ Relation
Returns a new instance of Relation.
4 5 6 7 |
# File 'lib/composite_primary_keys/relation.rb', line 4 def initialize(klass, table, predicate_builder, values = {}) initialize_without_cpk(klass, table, predicate_builder, values) add_cpk_support if klass && klass.composite? end |
Instance Method Details
#add_cpk_support ⇒ Object
15 16 17 |
# File 'lib/composite_primary_keys/relation.rb', line 15 def add_cpk_support extend CompositePrimaryKeys::CompositeRelation end |
#delete_all(conditions = nil) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/composite_primary_keys/relation.rb', line 85 def delete_all(conditions = nil) invalid_methods = INVALID_METHODS_FOR_DELETE_ALL.select { |method| if MULTI_VALUE_METHODS.include?(method) send("#{method}_values").any? elsif SINGLE_VALUE_METHODS.include?(method) send("#{method}_value") elsif CLAUSE_METHODS.include?(method) send("#{method}_clause").any? end } if invalid_methods.any? raise ActiveRecordError.new("delete_all doesn't support #{invalid_methods.join(', ')}") end if conditions ActiveSupport::Deprecation.warn(<<-MESSAGE.squish) Passing conditions to delete_all is deprecated and will be removed in Rails 5.1. To achieve the same use where(conditions).delete_all. MESSAGE where(conditions).delete_all else stmt = Arel::DeleteManager.new stmt.from(table) # CPK if joins_values.any? && @klass.composite? arel_attributes = Array(primary_key).map do |key| arel_attribute(key) end.to_composite_keys @klass.connection.join_to_delete(stmt, arel, arel_attributes) elsif joins_values.any? @klass.connection.join_to_delete(stmt, arel, arel_attribute(primary_key)) else stmt.wheres = arel.constraints end affected = @klass.connection.delete(stmt, 'SQL', bound_attributes) reset affected end end |
#initialize_copy(other) ⇒ Object
10 11 12 13 |
# File 'lib/composite_primary_keys/relation.rb', line 10 def initialize_copy(other) initialize_copy_without_cpk(other) add_cpk_support if klass.composite? end |
#initialize_copy_without_cpk ⇒ Object
9 |
# File 'lib/composite_primary_keys/relation.rb', line 9 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_all(updates) ⇒ Object
55 56 57 58 59 60 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 55 def update_all(updates) raise ArgumentError, "Empty list of attributes to change" if updates.blank? stmt = Arel::UpdateManager.new stmt.set Arel.sql(@klass.send(:sanitize_sql_for_assignment, updates)) stmt.table(table) if joins_values.any? # CPK #@klass.connection.join_to_update(stmt, arel, arel_attribute(primary_key)) if primary_key.kind_of?(Array) attributes = primary_key.map do |key| arel_attribute(key) end @klass.connection.join_to_update(stmt, arel, attributes.to_composite_keys) else @klass.connection.join_to_update(stmt, arel, arel_attribute(primary_key)) end else stmt.key = arel_attribute(primary_key) stmt.take(arel.limit) stmt.order(*arel.orders) stmt.wheres = arel.constraints end @klass.connection.update stmt, 'SQL', bound_attributes end |