Class: Chillout::Integrations::StateMachine

Inherits:
Object
  • Object
show all
Defined in:
lib/chillout/integrations/state_machine.rb

Instance Method Summary collapse

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
55
# File 'lib/chillout/integrations/state_machine.rb', line 45

def available?
  if defined?(::StateMachines::Integrations::ActiveRecord::VERSION) && defined?(::ActiveRecord)
    return Gem::Version.new(::StateMachines::Integrations::ActiveRecord::VERSION) >= Gem::Version.new('0.3.0')
  end
  if defined?(::StateMachine) && defined?(::ActiveRecord)
    require 'state_machine/version'
    return Gem::Version.new(::StateMachine::VERSION) >= Gem::Version.new('1.1.2')
  end
rescue
  false
end

#enable(client) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/chillout/integrations/state_machine.rb', line 57

def enable(client)
  callback_class = defined?(::StateMachine::Callback) ? ::StateMachine::Callback : ::StateMachines::Callback
  ActiveRecord::Base.subclasses.select do |klass|
    klass.respond_to?(:state_machines)
  end.each do |klass|
    klass.state_machines.each_value do |state_machine|
      state_machine.callbacks[:before].unshift(
        callback_class.new(:around, &callback(client))
      )
    end
  end
end