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 ⇒ Object
-
#initialize(klass, table: klass.arel_table, predicate_builder: klass.predicate_builder, values: {}) ⇒ Relation
constructor
A new instance of Relation.
- #initialize_copy(other) ⇒ Object
- #initialize_copy_without_cpk ⇒ Object
- #initialize_without_cpk ⇒ Object
-
#subquery_for(key, select) ⇒ Object
CPK.
- #update_all(updates) ⇒ Object
Constructor Details
#initialize(klass, table: klass.arel_table, predicate_builder: klass.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: klass.arel_table, predicate_builder: klass.predicate_builder, values: {}) initialize_without_cpk(klass, table: table, predicate_builder: predicate_builder, values: 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 ⇒ Object
63 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 101 102 |
# File 'lib/composite_primary_keys/relation.rb', line 63 def delete_all invalid_methods = INVALID_METHODS_FOR_DELETE_ALL.select do |method| value = @values[method] method == :distinct ? value : value&.any? end if invalid_methods.any? raise ActiveRecordError.new("delete_all doesn't support #{invalid_methods.join(', ')}") end if eager_loading? relation = apply_join_dependency return relation.delete_all end stmt = Arel::DeleteManager.new # CPK if @klass.composite? stmt.from(table) arel_attributes = primary_keys.map do |key| arel_attribute(key) end.to_composite_keys subselect = subquery_for(arel_attributes, arel) stmt.wheres = [arel_attributes.in(subselect)] else stmt.from(arel.join_sources.empty? ? table : arel.source) stmt.key = arel_attribute(primary_key) stmt.wheres = arel.constraints end stmt.take(arel.limit) stmt.offset(arel.offset) stmt.order(*arel.orders) affected = @klass.connection.delete(stmt, "#{@klass} Destroy") reset affected 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 |
#subquery_for(key, select) ⇒ Object
CPK
105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/composite_primary_keys/relation.rb', line 105 def subquery_for(key, select) subselect = select.clone subselect.projections = key # Materialize subquery by adding distinct # to work with MySQL 5.7.6 which sets optimizer_switch='derived_merge=on' subselect.distinct unless select.limit || select.offset || select.orders.any? key_name = Array(key).map {|a_key| a_key.name }.join(',') Arel::SelectManager.new(subselect.as("__active_record_temp")).project(Arel.sql(key_name)) end |
#update_all(updates) ⇒ Object
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 60 61 |
# File 'lib/composite_primary_keys/relation.rb', line 19 def update_all(updates) raise ArgumentError, "Empty list of attributes to change" if updates.blank? if eager_loading? relation = apply_join_dependency return relation.update_all(updates) end stmt = Arel::UpdateManager.new # CPK if @klass.composite? stmt.table(table) arel_attributes = primary_keys.map do |key| arel_attribute(key) end.to_composite_keys subselect = subquery_for(arel_attributes, arel) stmt.wheres = [arel_attributes.in(subselect)] else stmt.table(arel.join_sources.empty? ? table : arel.source) stmt.key = arel_attribute(primary_key) stmt.wheres = arel.constraints end stmt.take(arel.limit) stmt.offset(arel.offset) stmt.order(*arel.orders) if updates.is_a?(Hash) if klass.locking_enabled? && !updates.key?(klass.locking_column) && !updates.key?(klass.locking_column.to_sym) attr = arel_attribute(klass.locking_column) updates[attr.name] = _increment_attribute(attr) end stmt.set _substitute_values(updates) else stmt.set Arel.sql(klass.sanitize_sql_for_assignment(updates, table.name)) end @klass.connection.update stmt, "#{@klass} Update All" end |