Skip to content

Commit

Permalink
Add from state method to transition history
Browse files Browse the repository at this point in the history
  • Loading branch information
marclerodrigues committed May 27, 2019
1 parent fdab116 commit dfb54f6
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class <%= klass %> < <%= Statesman::Utils.rails_5_or_higher? ? 'ApplicationRecor
# self.updated_timestamp_column = nil

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

<%- end -%>
belongs_to :<%= parent_name %><%= class_name_option %>, inverse_of: :<%= table_name %>
Expand Down
1 change: 1 addition & 0 deletions lib/generators/statesman/templates/create_migration.rb.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Create<%= migration_class_name %> < ActiveRecord::Migration<%= "[#{ActiveRecord::Migration.current_version}]" if Statesman::Utils.rails_5_or_higher? %>
def change
create_table :<%= table_name %> do |t|
t.string :from_state, null: false
t.string :to_state, null: false
t.text :metadata<%= ", default: #{metadata_default_value}" unless mysql? %>
t.integer :sort_key, null: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class <%= klass %>
include Mongoid::Document
include Mongoid::Timestamps

field :from_state, type: String
field :to_state, type: String
field :sort_key, type: Integer
field :statesman_metadata, type: Hash
Expand Down
1 change: 1 addition & 0 deletions lib/generators/statesman/templates/update_migration.rb.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class AddStatesmanTo<%= migration_class_name %> < ActiveRecord::Migration<%= "[#{ActiveRecord::Migration.current_version}]" if Statesman::Utils.rails_5_or_higher? %>
def change
add_column :<%= table_name %>, :from_state, :string, null: false
add_column :<%= table_name %>, :to_state, :string, null: false
add_column :<%= table_name %>, :metadata, :text<%= ", default: #{metadata_default_value}" unless mysql? %>
add_column :<%= table_name %>, :sort_key, :integer, null: false
Expand Down
9 changes: 6 additions & 3 deletions lib/statesman/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ def last(force_reload: false)
private

def create_transition(from, to, metadata)
transition_attributes = { to_state: to,
sort_key: next_sort_key,
metadata: metadata }
transition_attributes = {
from_state: from,
to_state: to,
sort_key: next_sort_key,
metadata: metadata
}

transition_attributes[:most_recent] = true

Expand Down
2 changes: 1 addition & 1 deletion lib/statesman/adapters/memory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def initialize(transition_class, parent_model, observer, _opts = {})
def create(from, to, metadata = {})
from = from.to_s
to = to.to_s
transition = transition_class.new(to, next_sort_key, metadata)
transition = transition_class.new(from, to, next_sort_key, metadata)

@observer.execute(:before, from, to, transition)
@history << transition
Expand Down
4 changes: 3 additions & 1 deletion lib/statesman/adapters/memory_transition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ module Adapters
class MemoryTransition
attr_accessor :created_at
attr_accessor :updated_at
attr_accessor :from_state
attr_accessor :to_state
attr_accessor :sort_key
attr_accessor :metadata

def initialize(to, sort_key, metadata = {})
def initialize(from, to, sort_key, metadata = {})
@created_at = Time.now
@updated_at = Time.now
@from_state = from
@to_state = to
@sort_key = sort_key
@metadata = metadata
Expand Down
3 changes: 2 additions & 1 deletion lib/statesman/adapters/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def initialize(transition_class, parent_model, observer, _opts = {})
def create(from, to, metadata = {})
from = from.to_s
to = to.to_s
transition = transitions_for_parent.build(to_state: to,
transition = transitions_for_parent.build(from_state: from,
to_state: to,
sort_key: next_sort_key,
statesman_metadata: metadata)

Expand Down
6 changes: 4 additions & 2 deletions spec/statesman/adapters/memory_transition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@

describe Statesman::Adapters::MemoryTransition do
describe "#initialize" do
let(:from) { :n }
let(:to) { :y }
let(:sort_key) { 0 }
let(:create) { described_class.new(to, sort_key) }
let(:create) { described_class.new(from, to, sort_key) }

specify { expect(create.from_state).to equal(from) }
specify { expect(create.to_state).to equal(to) }
specify { expect(create.created_at).to be_a(Time) }
specify { expect(create.updated_at).to be_a(Time) }
specify { expect(create.sort_key).to be(sort_key) }

context "with metadata passed" do
let(:metadata) { { some: :hash } }
let(:create) { described_class.new(to, sort_key, metadata) }
let(:create) { described_class.new(from, to, sort_key, metadata) }

specify { expect(create.metadata).to eq(metadata) }
end
Expand Down
3 changes: 3 additions & 0 deletions spec/support/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def change
class CreateMyActiveRecordModelTransitionMigration < MIGRATION_CLASS
def change
create_table :my_active_record_model_transitions do |t|
t.string :from_state
t.string :to_state
t.integer :my_active_record_model_id
t.integer :sort_key
Expand Down Expand Up @@ -141,6 +142,7 @@ def change
class CreateOtherActiveRecordModelTransitionMigration < MIGRATION_CLASS
def change
create_table :other_active_record_model_transitions do |t|
t.string :from_state
t.string :to_state
t.integer :other_active_record_model_id
t.integer :sort_key
Expand Down Expand Up @@ -239,6 +241,7 @@ def change
class CreateNamespacedARModelTransitionMigration < MIGRATION_CLASS
def change
create_table :my_namespace_my_active_record_model_transitions do |t|
t.string :from_state
t.string :to_state
t.integer :my_active_record_model_id
t.integer :sort_key
Expand Down
1 change: 1 addition & 0 deletions spec/support/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class MyMongoidModelTransition
include Mongoid::Document
include Mongoid::Timestamps

field :from_state, type: String
field :to_state, type: String
field :sort_key, type: Integer
field :statesman_metadata, type: Hash
Expand Down

0 comments on commit dfb54f6

Please sign in to comment.