-
Notifications
You must be signed in to change notification settings - Fork 122
/
APP_TEMPLATE
86 lines (66 loc) · 2.33 KB
/
APP_TEMPLATE
1
2
3
4
5
6
7
8
9
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
53
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
81
82
83
84
85
86
class Profile
def initialize(options, location)
@options = options.dup
@location = location
end
private attr_reader :options, :location
def client
postgres? ? "RailsEventStore::JSONClient.new" : "RailsEventStore::Client.new"
end
def data_type
postgres? ? "jsonb" : "binary"
end
def for_existing_app
yield if existing_app?
end
private
def existing_app?
!!location
end
def postgres?
database.downcase.eql?("postgresql")
end
def database
existing_app? ? ActiveRecord::Base.connection_db_config.adapter : options.fetch(:database)
end
end
profile = Profile.new(options, ENV["LOCATION"])
gem "rails_event_store", "~> 2.15.0"
initializer "rails_event_store.rb", <<~CODE
require "rails_event_store"
require "aggregate_root"
require "arkency/command_bus"
Rails.configuration.to_prepare do
Rails.configuration.event_store = #{profile.client}
Rails.configuration.command_bus = Arkency::CommandBus.new
AggregateRoot.configure do |config|
config.default_event_store = Rails.configuration.event_store
end
# Subscribe event handlers below
Rails.configuration.event_store.tap do |store|
# store.subscribe(InvoiceReadModel.new, to: [InvoicePrinted])
# store.subscribe(lambda { |event| SendOrderConfirmation.new.call(event) }, to: [OrderSubmitted])
# store.subscribe_to_all_events(lambda { |event| Rails.logger.info(event.event_type) })
store.subscribe_to_all_events(RailsEventStore::LinkByEventType.new)
store.subscribe_to_all_events(RailsEventStore::LinkByCorrelationId.new)
store.subscribe_to_all_events(RailsEventStore::LinkByCausationId.new)
end
# Register command handlers below
# Rails.configuration.command_bus.tap do |bus|
# bus.register(PrintInvoice, Invoicing::OnPrint.new)
# bus.register(SubmitOrder, ->(cmd) { Ordering::OnSubmitOrder.new.call(cmd) })
# end
end
CODE
route "mount RailsEventStore::Browser => '/res' if Rails.env.development?"
profile.for_existing_app do
run_bundle unless run "bundle check"
end
after_bundle do
rails_command "db:create"
generate "rails_event_store_active_record:migration --data-type=#{profile.data_type}"
rails_command "db:migrate"
end
profile.for_existing_app do
run_after_bundle_callbacks if Gem::Version.new(Rails.version) < Gem::Version.new("7.1.0")
end