Module: Yes::Core::Aggregate::HasReadModel
- Extended by:
- ActiveSupport::Concern
- Included in:
- Yes::Core::Aggregate
- Defined in:
- lib/yes/core/aggregate/has_read_model.rb
Overview
Provides read model functionality for aggregates
Instance Method Summary collapse
-
#init_revision_from_stream ⇒ Boolean
Initializes the read model’s revision column with the current event stream revision.
-
#read_model ⇒ ApplicationRecord?
Retrieves or creates a read model instance for this aggregate.
-
#rebuild_read_model(remove: true) ⇒ void
Rebuilds the read model by processing all events.
-
#remove_read_model ⇒ Object
Removes the read model instance for this aggregate.
-
#revision ⇒ Integer?
Returns the current revision number from the read model.
- #revision_column ⇒ Object
-
#update_read_model(attributes) ⇒ Boolean
Updates or creates a read model with the given attributes.
Instance Method Details
#init_revision_from_stream ⇒ Boolean
This method bypasses validations and callbacks by using update_column
Initializes the read model’s revision column with the current event stream revision
163 164 165 |
# File 'lib/yes/core/aggregate/has_read_model.rb', line 163 def init_revision_from_stream read_model.update_column(revision_column, event_revision) end |
#read_model ⇒ ApplicationRecord?
Retrieves or creates a read model instance for this aggregate
124 125 126 127 128 |
# File 'lib/yes/core/aggregate/has_read_model.rb', line 124 def read_model return nil unless self.class.read_model_enabled? @read_model ||= self.class.read_model_class.find_or_create_by(id:) end |
#rebuild_read_model(remove: true) ⇒ void
This method returns an undefined value.
Rebuilds the read model by processing all events
139 140 141 |
# File 'lib/yes/core/aggregate/has_read_model.rb', line 139 def rebuild_read_model(remove: true) Yes::Core::Aggregate::ReadModelRebuilder.new(self).call(remove:) end |
#remove_read_model ⇒ Object
Removes the read model instance for this aggregate
131 132 133 134 |
# File 'lib/yes/core/aggregate/has_read_model.rb', line 131 def remove_read_model read_model.destroy @read_model = nil end |
#revision ⇒ Integer?
Returns the current revision number from the read model
154 155 156 157 158 |
# File 'lib/yes/core/aggregate/has_read_model.rb', line 154 def revision return -1 unless self.class.read_model_enabled? read_model.send(revision_column) end |
#revision_column ⇒ Object
143 144 145 146 147 148 149 150 |
# File 'lib/yes/core/aggregate/has_read_model.rb', line 143 def revision_column return :revision unless self.class.read_model_enabled? aggregate_revision_column = "#{self.class.context.underscore}_#{self.class.aggregate.underscore}_revision" return aggregate_revision_column.to_sym if read_model.class.column_names.include?(aggregate_revision_column) :revision end |
#update_read_model(attributes) ⇒ Boolean
Updates or creates a read model with the given attributes
111 112 113 114 115 116 |
# File 'lib/yes/core/aggregate/has_read_model.rb', line 111 def update_read_model(attributes) locale = attributes.delete(:locale) || I18n.locale I18n.with_locale(locale) do read_model.update!(attributes) end end |