Skip to content

Commit

Permalink
No lazy loading of transition class
Browse files Browse the repository at this point in the history
  • Loading branch information
wvanbergen committed Jan 31, 2014
1 parent 46d4984 commit a15e70b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
14 changes: 10 additions & 4 deletions lib/state_machine/audit_trail/transition_auditing.rb
Expand Up @@ -12,22 +12,28 @@ module StateMachine::AuditTrail::TransitionAuditing
def store_audit_trail(options = {})
state_machine = self
state_machine.transition_class_name = (options[:to] || default_transition_class_name).to_s
state_machine.setup_backend(options[:context_to_log])

state_machine.after_transition do |object, transition|
state_machine.audit_trail(options[:context_to_log]).log(object, transition.event, transition.from, transition.to)
state_machine.backend.log(object, transition.event, transition.from, transition.to)
end

unless state_machine.action == nil
state_machine.owner_class.after_create do |object|
if !object.send(state_machine.attribute).nil?
state_machine.audit_trail(options[:context_to_log]).log(object, nil, nil, object.send(state_machine.attribute))
state_machine.backend.log(object, nil, nil, object.send(state_machine.attribute))
end
end
end
end

def setup_backend(context_to_log = nil)
@backend = StateMachine::AuditTrail::Backend.create_for_transition_class(transition_class, self.owner_class, context_to_log)
end

# Public returns an instance of the class which does the actual audit trail logging
def audit_trail(context_to_log = nil)
@transition_auditor ||= StateMachine::AuditTrail::Backend.create_for_transition_class(transition_class, self.owner_class, context_to_log)
def backend
@backend
end

private
Expand Down
11 changes: 4 additions & 7 deletions spec/helpers/active_record.rb
Expand Up @@ -153,6 +153,10 @@ class ActiveRecordTestModelWithMultipleStateMachines < ActiveRecord::Base
end

module SomeNamespace
class ActiveRecordTestModelStateTransition < ActiveRecord::Base
belongs_to :test_model
end

class ActiveRecordTestModel < ActiveRecord::Base

state_machine :state, :initial => :waiting do # log initial state?
Expand All @@ -169,13 +173,6 @@ class ActiveRecordTestModel < ActiveRecord::Base
end
end

module SomeNamespace
class ActiveRecordTestModelStateTransition < ActiveRecord::Base
belongs_to :test_model
end
end


def create_transition_table(owner_class, state, add_context = false)
class_name = "#{owner_class.name}#{state.to_s.camelize}Transition"
ActiveRecord::Base.connection.create_table(class_name.tableize) do |t|
Expand Down

0 comments on commit a15e70b

Please sign in to comment.