Module: Redundancy::Utils

Defined in:
lib/redundancy/utils.rb

Class Method Summary collapse

Class Method Details

.cache_column(klass, association, attribute, options) ⇒ Object

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
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
# File 'lib/redundancy/utils.rb', line 10

def self.cache_column klass, association, attribute, options
  local_klass = klass

  reflection = get_reflection local_klass, association
  raise ArgumentError, "BelongsTo or HasOne reflection required" unless
    [:has_one, :belongs_to].include? reflection.macro

  foreign_key = reflection.foreign_key
  remote_klass = reflection.klass

  inverse_association = get_inverse_association local_klass, remote_klass, options

  cache_column = options[:cache_column] || :"#{association}_#{attribute}"

  case reflection.macro
  when :belongs_to
    local_klass.redundacies << UpdateColumn.new(
      source: { association: association, attribute: attribute },
      dest: { association: nil, attribute: cache_column },
      change_if: foreign_key, klass: local_klass
    )

  when :has_one
    remote_klass.redundacies << UpdateColumn.new(
      source: { association: nil, attribute: attribute, nil_unless: foreign_key },
      dest: { association: inverse_association, attribute: cache_column },
      change_if: foreign_key, klass: remote_klass
    )

    remote_klass.redundacies << UpdatePrevColumn.new(
      source: options[:default],
      dest: { klass: local_klass, prev_id: foreign_key, attribute: cache_column },
      change_if: foreign_key, klass: remote_klass
    )

  end

  remote_klass.redundacies << UpdateColumn.new(
    source: { association: nil, attribute: attribute },
    dest: { association: inverse_association, attribute: cache_column },
    change_if: attribute, klass: remote_klass, update: true
  )
end

.cache_method(klass, association, attribute, options) ⇒ Object

Raises:

  • (ArgumentError)


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
# File 'lib/redundancy/utils.rb', line 54

def self.cache_method klass, association, attribute, options
  local_klass = klass

  reflection = get_reflection local_klass, association
  raise ArgumentError, "BelongsTo reflection required" unless
    [:belongs_to].include? reflection.macro

  foreign_key = reflection.foreign_key
  remote_klass = reflection.klass

  cache_method = options[:cache_method] || :"raw_#{attribute}"

  local_klass.redundacies << UpdateMethod.new(
    source: { attribute: cache_method },
    dest: { association: association, attribute: attribute },
    change_if: options[:change_if], klass: local_klass
  )

  local_klass.redundacies << UpdatePrevMethod.new(
    source: { attribute: cache_method },
    dest: { klass: remote_klass, prev_id: foreign_key, attribute: attribute },
    change_if: foreign_key, klass: local_klass
  )

end

.get_inverse_association(klass, reflection_klass, options) ⇒ Object

Raises:

  • (ArgumentError)


86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/redundancy/utils.rb', line 86

def self.get_inverse_association klass, reflection_klass, options
  model_name = klass.model_name
  inverse_associations = options[:inverse_of]
  inverse_associations ||= [model_name.plural, model_name.singular].map(&:to_sym)

  inverse_association = Array.wrap(inverse_associations).find do |inverse_association|
    reflection_klass.reflect_on_association(inverse_association)
  end

  raise ArgumentError, "Could not find the inverse association for #{association} (#{inverse_associations.inspect} in #{reflection_klass})" unless inverse_association
  inverse_association
end

.get_reflection(klass, association) ⇒ Object

Raises:

  • (ArgumentError)


80
81
82
83
84
# File 'lib/redundancy/utils.rb', line 80

def self.get_reflection klass, association
  reflection = klass.reflect_on_association(association)
  raise ArgumentError, "Unknown association :#{association}" unless reflection
  reflection
end