Sha256: 6f617f794b8c71331fe658e1e2539b91338221f686ac4c4084a9729f05a4de5e
Contents?: true
Size: 1.79 KB
Versions: 7
Compression:
Stored size: 1.79 KB
Contents
module Eventsimple module Entity DEFAULT_IGNORE_PROPS = %w[id lock_version].freeze def event_driven_by(event_klass, aggregate_id:, filter_attributes: []) has_many :events, class_name: event_klass.name.to_s, foreign_key: :aggregate_id, primary_key: aggregate_id, dependent: :delete_all, inverse_of: model_name.element.to_sym, autosave: false, validate: false after_initialize :readonly! class_attribute :ignored_for_projection, default: [] class_attribute :_filter_attributes self._filter_attributes = [aggregate_id] | Array.wrap(filter_attributes) # disable automatic timestamp updates self.record_timestamps = false Eventsimple.configuration.ui_visible_models |= [self] include InstanceMethods extend ClassMethods end module InstanceMethods def projection_matches_events? reprojected = self.class.find(id).reproject attributes == reprojected.attributes end def enable_writes!(&block) was_readonly = @readonly @readonly = false if block yield @readonly = true if was_readonly end end def reproject(at: nil) event_history = at ? events.where('created_at <= ?', at).load : events.load ignore_props = (DEFAULT_IGNORE_PROPS + ignored_for_projection).map(&:to_s) assign_attributes(self.class.column_defaults.except(*ignore_props)) event_history.each do |event| event.apply_timestamps(self) event.apply(self) end self end end module ClassMethods def event_class reflect_on_all_associations(:has_many).find { |association| association.name == :events }.klass end end end end
Version data entries
7 entries across 7 versions & 1 rubygems