Class: ActiveRecord::Relation
- Inherits:
-
Object
- Object
- ActiveRecord::Relation
- Defined in:
- lib/composite_primary_keys/relation.rb
Instance Method Summary collapse
- #_update_record(values, id, id_was) ⇒ Object
- #add_cpk_support ⇒ 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
- #where_values_hash(relation_table_name = table_name) ⇒ Object
-
#where_values_hash_without_cpk ⇒ Object
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 }.
Constructor Details
#initialize(klass, table, 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, values = {}) initialize_without_cpk(klass, table, values) add_cpk_support if klass && klass.composite? end |
Instance Method Details
#_update_record(values, id, id_was) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/composite_primary_keys/relation.rb', line 45 def _update_record(values, id, id_was) 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 scope = @klass.unscoped if @klass.finder_needs_type_condition? scope.unscope!(where: @klass.inheritance_column) end scope.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 |
#add_cpk_support ⇒ Object
15 16 17 |
# File 'lib/composite_primary_keys/relation.rb', line 15 def add_cpk_support extend CompositePrimaryKeys::CompositeRelation 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 |
#where_values_hash(relation_table_name = table_name) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/composite_primary_keys/relation.rb', line 24 def where_values_hash(relation_table_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 == relation_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) { case where.right when Array then where.right.map(&:val) else where.right.val end }] }] end |
#where_values_hash_without_cpk ⇒ Object
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
}
23 |
# File 'lib/composite_primary_keys/relation.rb', line 23 alias :where_values_hash_without_cpk :where_values_hash |