This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ContentHasChanged.new(data: {content: new_content}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| test 'change advertisement content' do | |
| advertisement_id = SecureRandom.random_number | |
| author_id = SecureRandom.random_number | |
| new_content = "Random content: #{SecureRandom.hex}" | |
| command_bus.(PublishAdvertisement.new(advertisement_id, author_id)) | |
| assert_events( | |
| "Advertisement$#{advertisement_id}", | |
| ContentHasChanged.new(data: {content: new_content}) | |
| ) do |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Advertisements | |
| class Advertisement | |
| include AggregateRoot | |
| AlreadyPublished = Class.new(StandardError) | |
| NotPublished = Class.new(StandardError) | |
| NotAnAuthorOfAdvertisement = Class.new(StandardError) | |
| def initialize(id) | |
| @id = id |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Advertisements | |
| class AdvertisementRepository | |
| def initialize(event_store = Rails.configuration.event_store) | |
| @repository = AggregateRoot::Repository.new(event_store) | |
| end | |
| def with_advertisement(advertisement_id, &block) | |
| stream_name = "Advertisement$#{advertisement_id}" | |
| repository.with_aggregate(Advertisement.new(advertisement_id), stream_name, &block) | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Rails.configuration.command_bus.tap do |bus| | |
| bus.register(Advertisements::PublishAdvertisement, Advertisements::OnPublishAdvertisement.new) | |
| bus.register(Advertisements::ChangeContent, Advertisements::OnChangeContent.new) | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Advertisements | |
| class OnPublishAdvertisement | |
| def call(command) | |
| repository = AdvertisementRepository::new | |
| repository.with_advertisement(command.advertisement_id) do |advertisement| | |
| advertisement.publish(command.author_id) | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Advertisements | |
| class ContentHasChanged < RailsEventStore::Event; end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Advertisements | |
| class ChangeContent | |
| attr_accessor :advertisement_id, :content, :author_id | |
| def initialize(advertisement_id, content, author_id) | |
| @advertisement_id = advertisement_id | |
| @content = content | |
| @author_id = author_id | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Advertisements | |
| class PublishAdvertisement | |
| attr_accessor :advertisement_id | |
| attr_accessor :author_id | |
| def initialize(advertisement_id, author_id) | |
| @advertisement_id = advertisement_id | |
| @author_id = author_id | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Rails.autoloaders.each do |autoloader| | |
| autoloader.collapse(Rails.root.join("advertisements/lib/advertisements/events")) | |
| autoloader.collapse(Rails.root.join("advertisements/lib/advertisements/commands")) | |
| end |
NewerOlder