Module: Redundancy::Utils
- Defined in:
- lib/redundancy/utils.rb
Class Method Summary collapse
- .cache_column(klass, association, attribute, options) ⇒ Object
- .cache_method(klass, association, attribute, options) ⇒ Object
- .get_inverse_association(klass, reflection_klass, options) ⇒ Object
- .get_reflection(klass, association) ⇒ Object
Class Method Details
.cache_column(klass, association, attribute, options) ⇒ Object
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, 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, cache_column = [: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: [: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
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 |
# File 'lib/redundancy/utils.rb', line 54 def self.cache_method klass, association, attribute, local_klass = klass reflection = get_reflection local_klass, association raise ArgumentError, "HasOne or HasMany reflection required" unless [:has_one, :has_many].include? reflection.macro foreign_key = reflection.foreign_key remote_klass = reflection.klass inverse_association = get_inverse_association local_klass, remote_klass, cache_method = [:cache_method] || :"raw_#{attribute}" remote_klass.redundacies << UpdateMethod.new( source: { attribute: cache_method }, dest: { association: inverse_association, attribute: attribute }, change_if: [:change_if], klass: remote_klass ) remote_klass.redundacies << UpdatePrevMethod.new( source: { attribute: cache_method }, dest: { klass: local_klass, prev_id: foreign_key, attribute: attribute }, change_if: foreign_key, klass: remote_klass ) end |
.get_inverse_association(klass, reflection_klass, options) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/redundancy/utils.rb', line 88 def self.get_inverse_association klass, reflection_klass, model_name = klass.model_name inverse_associations = [: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
82 83 84 85 86 |
# File 'lib/redundancy/utils.rb', line 82 def self.get_reflection klass, association reflection = klass.reflect_on_association(association) raise ArgumentError, "Unknown association :#{association}" unless reflection reflection end |