Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add from state method to transition history #493

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
12 changes: 9 additions & 3 deletions lib/statesman/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def reset

def create_transition(from, to, metadata)
transition = transitions_for_parent.build(
default_transition_attributes(to, metadata),
default_transition_attributes(from, to, metadata),
)

transition_class.transaction(requires_new: true) do
Expand Down Expand Up @@ -116,13 +116,19 @@ def create_transition(from, to, metadata)
transition
end

def default_transition_attributes(to, metadata)
{
def default_transition_attributes(from, to, metadata)
attributes = {
to_state: to,
sort_key: next_sort_key,
metadata: metadata,
most_recent: not_most_recent_value(db_cast: false),
}

if @transition_class.has_attribute?(:from_state)
attributes[:from_state] = from
end

attributes
end

def add_after_commit_callback(from, to, transition)
Expand Down
6 changes: 6 additions & 0 deletions lib/statesman/adapters/active_record_transition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ module ActiveRecordTransition
class_attribute :updated_timestamp_column
self.updated_timestamp_column = DEFAULT_UPDATED_TIMESTAMP_COLUMN
end

def from_state
if has_attribute?(:from_state)
self[:from_state]
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/statesman/adapters/memory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,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 @@ -5,13 +5,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
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 @@ -4,18 +4,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 @@ -70,6 +70,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 @@ -189,6 +190,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 @@ -284,6 +286,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
Loading