Class: ActiveRecord::Relation

Inherits:
Object
  • Object
show all
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

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_supportObject



15
16
17
# File 'lib/composite_primary_keys/relation.rb', line 15

def add_cpk_support
  extend CompositePrimaryKeys::CompositeRelation
end

#delete_allObject



53
54
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
83
84
85
86
# File 'lib/composite_primary_keys/relation.rb', line 53

def delete_all
  invalid_methods = INVALID_METHODS_FOR_DELETE_ALL.select do |method|
    value = get_value(method)
    SINGLE_VALUE_METHODS.include?(method) ? 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
  stmt.from(table)

  # CPK
  if has_join_values? && @klass.composite?
    arel_attributes = primary_key.map do |key|
      arel_attribute(key)
    end.to_composite_keys
    @klass.connection.join_to_delete(stmt, arel, arel_attributes)
  elsif has_join_values? || has_limit_or_offset?
    @klass.connection.join_to_delete(stmt, arel, arel_attribute(primary_key))
  else
    stmt.wheres = arel.constraints
  end

  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_cpkObject



9
# File 'lib/composite_primary_keys/relation.rb', line 9

alias :initialize_copy_without_cpk :initialize_copy

#initialize_without_cpkObject



3
# File 'lib/composite_primary_keys/relation.rb', line 3

alias :initialize_without_cpk :initialize

#update_all(updates) ⇒ Object

Raises:

  • (ArgumentError)


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
# 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

  stmt.set Arel.sql(@klass.send(:sanitize_sql_for_assignment, updates))
  stmt.table(table)

  if has_join_values?
    # 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, "#{@klass} Update All"
end