event_sourced_record 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5b7ec38af25ac7107acc8351ccbc38b7a7844296
4
- data.tar.gz: c55c45d7a1ed2e00a85b23da21451eba99854cb8
3
+ metadata.gz: fb56334d7517be8d0816a7f12b91a6c2daf32d6f
4
+ data.tar.gz: e7fc402917fad1bb62c5ab3aa25d3f750b824637
5
5
  SHA512:
6
- metadata.gz: c289c63573fce64a567ff6acb8283d810f0ad141bd7b28044457ffb34cce09fba2483fceebda9450c951459b3bdd5050630d16e8313c6824c88ca6b06a9896fb
7
- data.tar.gz: c647debe68752c5146dce9f11427477b1233715ef47a6e286fb3757b99907bd6de14b1394a5acefaa35233d78e5a5e4dd248398de351816243e0ce0ac7c5f51f
6
+ metadata.gz: 7a26c10327b28148eb26819aeda00f56481bd528bedcddd37465b839fcd06da7d08893b99d1aa8821211b84e5491b306f86c02c46cbfce57a6f00f43ef6883e7
7
+ data.tar.gz: 2e0608be3b44eda77080c19edbc27d59f11272ffaccf09e433b17147b6bc883672e601d9fbef96746053112d2761ded6aeebd149ad984de58b0da1935e9b4bb8
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ sudo: false
2
+
3
+ cache: bundler
4
+
5
+ rvm:
6
+ - 1.9
7
+ - 2.1
8
+
9
+ gemfile:
10
+ - ci/ar32.gemfile
11
+ - ci/ar41.gemfile
12
+ - ci/ar42.gemfile
data/Getting_Started.md CHANGED
@@ -66,6 +66,8 @@ You might never end up showing this model to end-users, but in fact it's the aut
66
66
  class SubscriptionEvent < ActiveRecord::Base
67
67
  include EventSourcedRecord::Event
68
68
 
69
+ serialize :data
70
+
69
71
  belongs_to :subscription,
70
72
  foreign_key: 'subscription_uuid', primary_key: 'uuid'
71
73
 
data/README.md CHANGED
@@ -49,6 +49,8 @@ The event model is an ActiveRecord model but it can act significantly different
49
49
  class SubscriptionEvent < ActiveRecord::Base
50
50
  include EventSourcedRecord::Event
51
51
 
52
+ serialize :data
53
+
52
54
  belongs_to :subscription,
53
55
  foreign_key: 'subscription_uuid', primary_key: 'uuid'
54
56
 
@@ -73,6 +75,11 @@ The easiest way to create these records is with the scopes that are automaticall
73
75
  bottles_per_shipment: 1, bottles_purchased: 6, user_id: current_user.id
74
76
  )
75
77
 
78
+ The event attributes are stored in the `data` column, which by default is
79
+ serialized to a text field. If you'd prefer to use e.g. JSONB with PostgreSQL
80
+ 9.4+, just remove the call to `serialize` and modify the migration to use your
81
+ preferred type.
82
+
76
83
  ## Projection
77
84
 
78
85
  The projection is the ActiveRecord model that is generated deterministically with the data in the timestamped events combined with the logic in the calculator. Projections shouldn't have any code for modifying themselves, as that will be done externally. Accordingly, projections end up being fairly small classes:
data/ci/ar32.gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activerecord', '~> 3.2.0'
4
+
5
+ # Specify your gem's dependencies in event_sourced_record.gemspec
6
+ gemspec path: '../'
data/ci/ar41.gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activerecord', '~> 4.1.0'
4
+ gem 'rails-observers'
5
+
6
+ # Specify your gem's dependencies in event_sourced_record.gemspec
7
+ gemspec path: '../'
data/ci/ar42.gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activerecord', '~> 4.2.0'
4
+ gem 'rails-observers'
5
+
6
+ # Specify your gem's dependencies in event_sourced_record.gemspec
7
+ gemspec path: '../'
@@ -25,6 +25,7 @@ EventSourcedRecord uses Rails observers. If you are using Rails 4.0 or greater,
25
25
  MESSAGE
26
26
 
27
27
  spec.add_dependency 'activemodel'
28
+ spec.add_dependency 'activerecord-immutable', '~> 0.0.3'
28
29
 
29
30
  spec.add_development_dependency "bundler", "~> 1.6"
30
31
  spec.add_development_dependency "rake"
@@ -1,4 +1,8 @@
1
+ require 'active_record/immutable'
2
+
1
3
  module EventSourcedRecord::Event
4
+ include ActiveRecord::Immutable
5
+
2
6
  def self.included(model)
3
7
  model.cattr_accessor :_event_type_configs
4
8
  model.extend ClassMethods
@@ -8,7 +12,6 @@ module EventSourcedRecord::Event
8
12
  model.validates :event_type, presence: true
9
13
  model.validate :validate_corrent_event_type
10
14
  model.validate :validate_by_event_type
11
- model.serialize :data
12
15
  end
13
16
 
14
17
  def event_type
@@ -1,3 +1,3 @@
1
1
  module EventSourcedRecord
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -51,6 +51,8 @@ class EventSourcedRecord::ProjectionGenerator < ActiveRecord::Generators::Base
51
51
  attr_string
52
52
  }
53
53
  attr_strings << "uuid:string:uniq"
54
+ attr_strings << "created_at:datetime"
55
+ attr_strings << "updated_at:datetime"
54
56
  attr_strings.join(' ')
55
57
  end
56
58
 
@@ -2,6 +2,8 @@
2
2
  class <%= event_class_name %> < ActiveRecord::Base
3
3
  include EventSourcedRecord::Event
4
4
 
5
+ serialize :data
6
+
5
7
  belongs_to :<%= belongs_to_name %>,
6
8
  foreign_key: '<%= belongs_to_foreign_key %>', primary_key: 'uuid'
7
9
 
@@ -4,9 +4,7 @@ class <%= projection_migration_class_name %> < ActiveRecord::Migration
4
4
  <% attributes.each do |attribute| -%>
5
5
  t.<%= attribute.type %> :<%= attribute.name %><%= attribute.inject_options %>
6
6
  <% end -%>
7
- <% if options[:timestamps] %>
8
7
  t.timestamps
9
- <% end -%>
10
8
  end
11
9
  <% attributes_with_index.each do |attribute| -%>
12
10
  add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
@@ -109,4 +109,14 @@ class EventSourcedRecord::EventTest < MiniTest::Unit::TestCase
109
109
  event.event_type = 'change_settings'
110
110
  end
111
111
  end
112
+
113
+ def test_events_are_immutable
114
+ event = SubscriptionEvent.creation.create!(
115
+ bottles_per_shipment: 1, bottles_purchased: 6, user_id: 999
116
+ )
117
+
118
+ assert_raises(ActiveRecord::ReadOnlyRecord) do
119
+ event.touch
120
+ end
121
+ end
112
122
  end
@@ -23,13 +23,14 @@ class EventSourcedRecord::ProjectionGeneratorTest < Rails::Generators::TestCase
23
23
  if ar_major_version >= 4
24
24
  assert(
25
25
  @generate_calls['migration'].include?(
26
- "create_subscriptions user_id:integer bottles_per_shipment:integer bottles_left:integer uuid:string:uniq"
26
+ "create_subscriptions user_id:integer bottles_per_shipment:integer bottles_left:integer uuid:string:uniq created_at:datetime updated_at:datetime"
27
27
  ),
28
28
  @generate_calls.inspect
29
29
  )
30
30
  else
31
31
  assert_migration("db/migrate/create_subscriptions.rb") do |contents|
32
32
  assert_match(/t.integer :user_id/, contents)
33
+ assert_match(/t.timestamps/, contents)
33
34
  end
34
35
  end
35
36
  end
data/test/test_helper.rb CHANGED
@@ -14,8 +14,14 @@ require 'generators/event_sourced_record'
14
14
  #require 'generators/event_sourced_record/projection_generator'
15
15
  require 'active_record'
16
16
 
17
+ def test_db_dir
18
+ './tmp'
19
+ end
20
+
21
+ Dir.mkdir test_db_dir unless Dir.exists? test_db_dir
22
+
17
23
  ActiveRecord::Base.establish_connection(
18
- :adapter => 'sqlite3', :database => 'tmp/test.sqlite3'
24
+ :adapter => 'sqlite3', :database => "#{test_db_dir}/test.sqlite3"
19
25
  )
20
26
 
21
27
  silence_stream(STDOUT) do
@@ -49,6 +55,8 @@ end
49
55
  class SubscriptionEvent < ActiveRecord::Base
50
56
  include EventSourcedRecord::Event
51
57
 
58
+ serialize :data
59
+
52
60
  event_type :creation do
53
61
  attributes :bottles_per_shipment, :bottles_purchased, :user_id
54
62
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: event_sourced_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Francis Hwang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-17 00:00:00.000000000 Z
11
+ date: 2015-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord-immutable
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.0.3
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.0.3
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -144,12 +158,16 @@ extensions: []
144
158
  extra_rdoc_files: []
145
159
  files:
146
160
  - ".gitignore"
161
+ - ".travis.yml"
147
162
  - Appraisals
148
163
  - Gemfile
149
164
  - Getting_Started.md
150
165
  - LICENSE.txt
151
166
  - README.md
152
167
  - Rakefile
168
+ - ci/ar32.gemfile
169
+ - ci/ar41.gemfile
170
+ - ci/ar42.gemfile
153
171
  - event_sourced_record.gemspec
154
172
  - lib/event_sourced_record.rb
155
173
  - lib/event_sourced_record/calculator.rb