forked from wvanbergen/state_machine-audit_trail
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move code around to allow for better separation of backends.
- Loading branch information
1 parent
46ee885
commit 0e5cf64
Showing
11 changed files
with
236 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,25 @@ | ||
class StateMachine::AuditTrail::Backend < Struct.new(:transition_class) | ||
|
||
autoload :Mongoid, 'state_machine/audit_trail/backend/mongoid' | ||
autoload :ActiveRecord, 'state_machine/audit_trail/backend/active_record' | ||
|
||
def log(object, event, from, to, timestamp = Time.now) | ||
raise NotImplemented, "Implement in a subclass." | ||
end | ||
|
||
# Public creates an instance of the class which does the actual logging | ||
# | ||
# transition_class: the Class which holds the audit trail | ||
# | ||
# in order to adda new ORM here, copy audit_trail/mongoid.rb to whatever you want to call the new file and implement the #log function there | ||
# then, return from here the appropriate object based on which ORM the transition_class is using | ||
def self.create_for_transition_class(transition_class) | ||
if Object.const_defined?('ActiveRecord') && transition_class.ancestors.include?(::ActiveRecord::Base) | ||
return StateMachine::AuditTrail::Backend::ActiveRecord.new(transition_class) | ||
elsif Object.const_defined?('Mongoid') && transition_class.ancestors.include?(::Mongoid::Document) | ||
return StateMachine::AuditTrail::Backend::Mongoid.new(transition_class) | ||
else | ||
raise NotImplemented, "Only support for ActiveRecord and Mongoid is included at this time" | ||
end | ||
end | ||
end | ||
|
||
require 'state_machine/audit_trail/active_record' | ||
require 'state_machine/audit_trail/mongoid' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
lib/state_machine/audit_trail/mongoid.rb → ...te_machine/audit_trail/backend/mongoid.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
require 'active_record' | ||
|
||
### Setup test database | ||
|
||
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:') | ||
|
||
ActiveRecord::Base.connection.create_table(:active_record_test_models) do |t| | ||
t.string :state | ||
t.string :type | ||
t.timestamps | ||
end | ||
|
||
ActiveRecord::Base.connection.create_table(:active_record_test_model_with_multiple_state_machines) do |t| | ||
t.string :first | ||
t.string :second | ||
t.timestamps | ||
end | ||
|
||
# We probably want to provide a generator for this model and the accompanying migration. | ||
class ActiveRecordTestModelStateTransition < ActiveRecord::Base | ||
belongs_to :test_model | ||
end | ||
|
||
class ActiveRecordTestModelWithMultipleStateMachinesFirstTransition < ActiveRecord::Base | ||
belongs_to :test_model | ||
end | ||
|
||
class ActiveRecordTestModelWithMultipleStateMachinesSecondTransition < ActiveRecord::Base | ||
belongs_to :test_model | ||
end | ||
|
||
class ActiveRecordTestModel < ActiveRecord::Base | ||
|
||
state_machine :state, :initial => :waiting do # log initial state? | ||
store_audit_trail | ||
|
||
event :start do | ||
transition [:waiting, :stopped] => :started | ||
end | ||
|
||
event :stop do | ||
transition :started => :stopped | ||
end | ||
end | ||
end | ||
|
||
class ActiveRecordTestModelDescendant < ActiveRecordTestModel | ||
end | ||
|
||
class ActiveRecordTestModelWithMultipleStateMachines < ActiveRecord::Base | ||
|
||
state_machine :first, :initial => :beginning do | ||
store_audit_trail | ||
|
||
event :begin_first do | ||
transition :beginning => :end | ||
end | ||
end | ||
|
||
state_machine :second do | ||
store_audit_trail | ||
|
||
event :begin_second do | ||
transition nil => :beginning_second | ||
end | ||
end | ||
end | ||
|
||
def create_transition_table(owner_class, state) | ||
class_name = "#{owner_class.name}#{state.to_s.camelize}Transition" | ||
|
||
ActiveRecord::Base.connection.create_table(class_name.tableize) do |t| | ||
t.integer owner_class.name.foreign_key | ||
t.string :event | ||
t.string :from | ||
t.string :to | ||
t.datetime :created_at | ||
end | ||
end | ||
|
||
create_transition_table(ActiveRecordTestModel, :state) | ||
create_transition_table(ActiveRecordTestModelWithMultipleStateMachines, :first) | ||
create_transition_table(ActiveRecordTestModelWithMultipleStateMachines, :second) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.