Module: ActiveDenormalize::Associations::Extension
- Defined in:
- lib/active_denormalize/associations/extension.rb
Constant Summary collapse
- OPTIONS =
[:denormalize].freeze
Class Method Summary collapse
- .add_after_commit_callback(model, reflection) ⇒ Object
- .build(model, reflection) ⇒ Object
- .define_accessors(model, reflection) ⇒ Object
- .valid_options ⇒ Object
Class Method Details
.add_after_commit_callback(model, reflection) ⇒ Object
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 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/active_denormalize/associations/extension.rb', line 78 def self.add_after_commit_callback(model, reflection) denormalized_column_prefix = model.model_name.singular denormalized_primary_key = "#{denormalized_column_prefix}_#{model.primary_key}" model.before_destroy do if (target = public_send(reflection.name)) primary_key = public_send(self.class.primary_key) denormalized_primary_key_id = target.public_send(denormalized_primary_key) # The record being destroyed is currently denormalized on its association and needs to be replaced if denormalized_primary_key_id == primary_key replacement = target.association(model.model_name.collection).scope.denormalize.where.not(id: primary_key).order(created_at: :desc).first if replacement.present? replacement.denormalize(target) else target.public_send("clear_denormalized_#{denormalized_column_prefix}") end end end end model.after_create_commit do if denormalize? && (target = public_send(reflection.name)) denormalize(target) end end model.after_update_commit do if (target = public_send(reflection.name)) primary_key = public_send(self.class.primary_key) denormalized_primary_key_id = target.public_send(denormalized_primary_key) # After an update of a denormalized record, check the association to see if it is still the current denormalized record or if it has changed. denormalize(target) if denormalized_primary_key_id == primary_key end end end |
.build(model, reflection) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/active_denormalize/associations/extension.rb', line 8 def self.build(model, reflection) return unless reflection.[:denormalize] define_accessors(model, reflection) add_after_commit_callback(model, reflection) end |
.define_accessors(model, reflection) ⇒ 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/active_denormalize/associations/extension.rb', line 19 def self.define_accessors(model, reflection) denormalized_column_prefix = model.model_name.singular target_mixin = reflection.klass.generated_association_methods target_mixin.class_eval do define_method("clear_denormalized_#{denormalized_column_prefix}") do denormalized_columns = model.denormalized_column_mapping.keys & self.class.column_names cleared_denormalized_attributes = denormalized_columns.index_with { |_column| nil } update!(cleared_denormalized_attributes) end end model.class_eval do # Needed to lookup the next valid record to denormalize when destroying the latest record. scope :denormalize, -> {} def denormalize? true end def denormalize(target) denormalized_columns = self.class.denormalized_column_mapping.keys & target.class.column_names attributes = denormalized_columns.to_h do |denormalized_column| column = self.class.denormalized_column_mapping[denormalized_column] if column == "denormalized_at" [denormalized_column, Time.zone.now] else value = send(column) if (enum_mapping = defined_enums[column]) value = enum_mapping.fetch(value) end [denormalized_column, value] end end if attributes.empty? Rails.logger.debug { "No denormalized columns found for #{target.class}" } else Rails.logger.debug { "Denormalizing #{target.class} #{target.id} with #{attributes}" } target.update!(attributes) end end def self.denormalized_column_mapping @denormalized_column_mapping ||= begin prefix = model_name.singular mapping = column_names.index_by do |column_name| "#{prefix}_#{column_name}" end mapping["#{prefix}_denormalized_at"] = "denormalized_at" mapping end end end end |
.valid_options ⇒ Object
15 16 17 |
# File 'lib/active_denormalize/associations/extension.rb', line 15 def self. OPTIONS end |