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

No easy way to find previous state #348

Open
wants to merge 1 commit 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
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
17 changes: 11 additions & 6 deletions lib/statesman/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,7 @@ 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[:most_recent] = true

transition_attributes = transition_attributes(from, to, metadata)
transition = transitions_for_parent.build(transition_attributes)

::ActiveRecord::Base.transaction(requires_new: true) do
Expand All @@ -92,6 +87,16 @@ def add_after_commit_callback(from, to, transition)
)
end

def transition_attributes(from, to, metadata)
{
from_state: from,
to_state: to,
sort_key: next_sort_key,
metadata: metadata,
most_recent: true,
}
end

def transitions_for_parent
parent_model.send(@association_name)
end
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
11 changes: 7 additions & 4 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 @@ -137,10 +138,11 @@ def change
end
end

# rubocop:disable MethodLength
# rubocop:disable MethodLength, Metrics/AbcSize
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 @@ -181,7 +183,7 @@ def change
end
end
end
# rubocop:enable MethodLength
# rubocop:enable MethodLength, Metrics/AbcSize

class DropMostRecentColumn < MIGRATION_CLASS
def change
Expand Down Expand Up @@ -235,10 +237,11 @@ def change
end
end

# rubocop:disable MethodLength
# rubocop:disable MethodLength, Metrics/AbcSize
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 Expand Up @@ -275,5 +278,5 @@ def change
name: "index_namespace_model_transitions_parent_latest"
end
end
# rubocop:enable MethodLength
# rubocop:enable MethodLength, Metrics/AbcSize
end
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