Skip to content

Commit

Permalink
Read model -- example.
Browse files Browse the repository at this point in the history
  • Loading branch information
mostlyobvious committed Feb 19, 2020
1 parent 351d5e8 commit 51c06d4
Show file tree
Hide file tree
Showing 18 changed files with 177 additions and 12 deletions.
3 changes: 3 additions & 0 deletions app/assets/stylesheets/editions.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the editions controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/
11 changes: 11 additions & 0 deletions app/controllers/editions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class EditionsController < ApplicationController
def show
edition_id = params[:id]

render :show, locals: {
edition_id: edition_id,
participants: EditionParticipant.where(edition_id: edition_id),
edition_status: EditionStatus.find_by(edition_id: edition_id)
}
end
end
2 changes: 2 additions & 0 deletions app/helpers/editions_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module EditionsHelper
end
16 changes: 16 additions & 0 deletions app/models/edition_participant.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class EditionParticipant < ApplicationRecord
def self.handle_participant_event(event)
edition_id = event.data[:edition_id]
participant_id = event.data[:participant_id]

case event
when Workshops::ParticipantPersonalDataProvided
where(participant_id: participant_id).update_all(
participant_name: event.data[:name],
participant_email: event.data[:email]
)
when Workshops::ParticipantRegisteredForEdition
find_or_create_by(edition_id: edition_id, participant_id: participant_id)
end
end
end
6 changes: 6 additions & 0 deletions app/models/edition_status.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class EditionStatus < ApplicationRecord
def self.handle_confirmed_event(e)
edition_id = e.data[:edition_id]
find_or_create_by(edition_id: edition_id).update(status: "confirmed")
end
end
13 changes: 13 additions & 0 deletions app/views/editions/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h1>Edition <%= edition_id %>: <%= edition_status.status %></h1>

<h3><%= participants.size %></h3>

<table>
<% participants.each do |p| %>
<tr>
<td><%= p.participant_id %></td>
<td><%= p.participant_name %></td>
<td><%= p.participant_email %></td>
</tr>
<% end %>
</table>
24 changes: 13 additions & 11 deletions config/initializers/rails_event_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@
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(->(event) { SendOrderConfirmation.new.call(event) }, to: [OrderSubmitted])
# store.subscribe_to_all_events(->(event) { Rails.logger.info(event.type) })
# end
Rails.configuration.event_store.tap do |store|
store.subscribe(
->(e) { EditionParticipant.handle_participant_event(e) },
to: [
Workshops::ParticipantRegisteredForEdition,
Workshops::ParticipantPersonalDataProvided,
])

# 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
store.subscribe(
->(e) { EditionStatus.handle_confirmed_event(e) },
to: [
Workshops::EditionConfirmed,
])
end
end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Rails.application.routes.draw do
mount RailsEventStore::Browser => '/res' if Rails.env.development?
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html

get '/editions/:id', to: 'editions#show'
end
10 changes: 10 additions & 0 deletions db/migrate/20200219115300_create_edition_statuses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateEditionStatuses < ActiveRecord::Migration[6.0]
def change
create_table :edition_statuses do |t|
t.string :edition_id
t.string :status

t.timestamps
end
end
end
12 changes: 12 additions & 0 deletions db/migrate/20200219115312_create_edition_participants.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateEditionParticipants < ActiveRecord::Migration[6.0]
def change
create_table :edition_participants do |t|
t.string :edition_id
t.string :participant_id
t.string :participant_name
t.string :participant_email

t.timestamps
end
end
end
18 changes: 17 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,23 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_02_19_002357) do
ActiveRecord::Schema.define(version: 2020_02_19_115312) do

create_table "edition_participants", force: :cascade do |t|
t.string "edition_id"
t.string "participant_id"
t.string "participant_name"
t.string "participant_email"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end

create_table "edition_statuses", force: :cascade do |t|
t.string "edition_id"
t.string "status"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end

create_table "event_store_events", id: :string, limit: 36, force: :cascade do |t|
t.string "event_type", null: false
Expand Down
27 changes: 27 additions & 0 deletions produce.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
event_store = Rails.configuration.event_store
edition_id = "smart-pension"
stream_name = "Edition$#{edition_id}"

EditionParticipant.delete_all
EditionStatus.delete_all

event_store.publish(
Workshops::EditionConfirmed.new(data: { edition_id: edition_id }),
stream_name: stream_name
)
event_store.publish(
Workshops::ParticipantRegisteredForEdition.new(data: { edition_id: edition_id, participant_id: pid1 = SecureRandom.uuid }),
stream_name: stream_name
)
event_store.publish(
Workshops::ParticipantRegisteredForEdition.new(data: { edition_id: edition_id, participant_id: pid2 = SecureRandom.uuid }),
stream_name: stream_name
)
event_store.publish(
Workshops::ParticipantPersonalDataProvided.new(data: { participant_id: pid1, name: 'Tom', email: 't@acme' }),
stream_name: stream_name
)
event_store.publish(
Workshops::ParticipantPersonalDataProvided.new(data: { participant_id: pid2, name: 'Jerry', email: 'j@acme' }),
stream_name: stream_name
)
7 changes: 7 additions & 0 deletions test/controllers/editions_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class EditionsControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end
11 changes: 11 additions & 0 deletions test/fixtures/edition_participants.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value
11 changes: 11 additions & 0 deletions test/fixtures/edition_statuses.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value
7 changes: 7 additions & 0 deletions test/models/edition_participant_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class EditionParticipantTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
7 changes: 7 additions & 0 deletions test/models/edition_status_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class EditionStatusTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
2 changes: 2 additions & 0 deletions workshops/lib/workshops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module Workshops
ParticipantRegisteredForEdition = Class.new(RailsEventStore::Event)
VenueBookedForEdition = Class.new(RailsEventStore::Event)
TwoWeeksBeforeEditionReached = Class.new(RailsEventStore::Event)
ParticipantPersonalDataProvided = Class.new(RailsEventStore::Event)
EditionConfirmed = Class.new(RailsEventStore::Event)

ConfirmEdition = Struct.new(:edition_id)
CancelEdition = Struct.new(:edition_id)
Expand Down

0 comments on commit 51c06d4

Please sign in to comment.