Skip to content

Commit 2ac555e

Browse files
Add from state method to transition history
1 parent 5e69852 commit 2ac555e

File tree

9 files changed

+35
-8
lines changed

9 files changed

+35
-8
lines changed

lib/generators/statesman/templates/active_record_transition_model.rb.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class <%= klass %> < <%= Statesman::Utils.rails_5_or_higher? ? 'ApplicationRecor
99
# self.updated_timestamp_column = nil
1010

1111
<%- unless Statesman::Utils.rails_4_or_higher? -%>
12-
attr_accessible :to_state, :metadata, :sort_key
12+
attr_accessible :from_state, :to_state, :metadata, :sort_key
1313

1414
<%- end -%>
1515
belongs_to :<%= parent_name %><%= class_name_option %>, inverse_of: :<%= table_name %>

lib/generators/statesman/templates/create_migration.rb.erb

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class Create<%= migration_class_name %> < ActiveRecord::Migration<%= "[#{ActiveRecord::Migration.current_version}]" if Statesman::Utils.rails_5_or_higher? %>
22
def change
33
create_table :<%= table_name %> do |t|
4+
t.string :from_state, null: false
45
t.string :to_state, null: false
56
t.text :metadata<%= ", default: #{metadata_default_value}" unless mysql? %>
67
t.integer :sort_key, null: false

lib/generators/statesman/templates/update_migration.rb.erb

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class AddStatesmanTo<%= migration_class_name %> < ActiveRecord::Migration<%= "[#{ActiveRecord::Migration.current_version}]" if Statesman::Utils.rails_5_or_higher? %>
22
def change
3+
add_column :<%= table_name %>, :from_state, :string, null: false
34
add_column :<%= table_name %>, :to_state, :string, null: false
45
add_column :<%= table_name %>, :metadata, :text<%= ", default: #{metadata_default_value}" unless mysql? %>
56
add_column :<%= table_name %>, :sort_key, :integer, null: false

lib/statesman/adapters/active_record.rb

+9-3
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def reset
8383

8484
def create_transition(from, to, metadata)
8585
transition = transitions_for_parent.build(
86-
default_transition_attributes(to, metadata),
86+
default_transition_attributes(from, to, metadata),
8787
)
8888

8989
::ActiveRecord::Base.transaction(requires_new: true) do
@@ -118,13 +118,19 @@ def create_transition(from, to, metadata)
118118
transition
119119
end
120120

121-
def default_transition_attributes(to, metadata)
122-
{
121+
def default_transition_attributes(from, to, metadata)
122+
attributes = {
123123
to_state: to,
124124
sort_key: next_sort_key,
125125
metadata: metadata,
126126
most_recent: not_most_recent_value(db_cast: false),
127127
}
128+
129+
if @transition_class.has_attribute?(:from_state)
130+
attributes[:from_state] = from
131+
end
132+
133+
attributes
128134
end
129135

130136
def add_after_commit_callback(from, to, transition)

lib/statesman/adapters/active_record_transition.rb

+8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ module ActiveRecordTransition
1515
class_attribute :updated_timestamp_column
1616
self.updated_timestamp_column = DEFAULT_UPDATED_TIMESTAMP_COLUMN
1717
end
18+
19+
def from_state
20+
if has_attribute?(:from_state)
21+
self[:from_state]
22+
else
23+
nil
24+
end
25+
end
1826
end
1927
end
2028
end

lib/statesman/adapters/memory.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def initialize(transition_class, parent_model, observer, _opts = {})
2020
def create(from, to, metadata = {})
2121
from = from.to_s
2222
to = to.to_s
23-
transition = transition_class.new(to, next_sort_key, metadata)
23+
transition = transition_class.new(from, to, next_sort_key, metadata)
2424

2525
@observer.execute(:before, from, to, transition)
2626
@history << transition

lib/statesman/adapters/memory_transition.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ module Adapters
55
class MemoryTransition
66
attr_accessor :created_at
77
attr_accessor :updated_at
8+
attr_accessor :from_state
89
attr_accessor :to_state
910
attr_accessor :sort_key
1011
attr_accessor :metadata
1112

12-
def initialize(to, sort_key, metadata = {})
13+
def initialize(from, to, sort_key, metadata = {})
1314
@created_at = Time.now
1415
@updated_at = Time.now
16+
@from_state = from
1517
@to_state = to
1618
@sort_key = sort_key
1719
@metadata = metadata

spec/statesman/adapters/memory_transition_spec.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,20 @@
55

66
describe Statesman::Adapters::MemoryTransition do
77
describe "#initialize" do
8+
let(:from) { :n }
89
let(:to) { :y }
910
let(:sort_key) { 0 }
10-
let(:create) { described_class.new(to, sort_key) }
11+
let(:create) { described_class.new(from, to, sort_key) }
1112

13+
specify { expect(create.from_state).to equal(from) }
1214
specify { expect(create.to_state).to equal(to) }
1315
specify { expect(create.created_at).to be_a(Time) }
1416
specify { expect(create.updated_at).to be_a(Time) }
1517
specify { expect(create.sort_key).to be(sort_key) }
1618

1719
context "with metadata passed" do
1820
let(:metadata) { { some: :hash } }
19-
let(:create) { described_class.new(to, sort_key, metadata) }
21+
let(:create) { described_class.new(from, to, sort_key, metadata) }
2022

2123
specify { expect(create.metadata).to eq(metadata) }
2224
end

spec/support/active_record.rb

+7
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def change
6767
class CreateMyActiveRecordModelTransitionMigration < MIGRATION_CLASS
6868
def change
6969
create_table :my_active_record_model_transitions do |t|
70+
t.string :from_state
7071
t.string :to_state
7172
t.integer :my_active_record_model_id
7273
t.integer :sort_key
@@ -142,9 +143,11 @@ def change
142143
end
143144
end
144145

146+
# rubocop:disable MethodLength, Metrics/AbcSize
145147
class CreateOtherActiveRecordModelTransitionMigration < MIGRATION_CLASS
146148
def change
147149
create_table :other_active_record_model_transitions do |t|
150+
t.string :from_state
148151
t.string :to_state
149152
t.integer :other_active_record_model_id
150153
t.integer :sort_key
@@ -185,6 +188,7 @@ def change
185188
end
186189
end
187190
end
191+
# rubocop:enable MethodLength, Metrics/AbcSize
188192

189193
class DropMostRecentColumn < MIGRATION_CLASS
190194
def change
@@ -238,9 +242,11 @@ def change
238242
end
239243
end
240244

245+
# rubocop:disable MethodLength, Metrics/AbcSize
241246
class CreateNamespacedARModelTransitionMigration < MIGRATION_CLASS
242247
def change
243248
create_table :my_namespace_my_active_record_model_transitions do |t|
249+
t.string :from_state
244250
t.string :to_state
245251
t.integer :my_active_record_model_id
246252
t.integer :sort_key
@@ -277,4 +283,5 @@ def change
277283
name: "index_namespace_model_transitions_parent_latest"
278284
end
279285
end
286+
# rubocop:enable MethodLength, Metrics/AbcSize
280287
end

0 commit comments

Comments
 (0)