mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
commit
9a7a933ee2
4 changed files with 35 additions and 31 deletions
|
@ -750,6 +750,11 @@ Job.aasm.states_for_select
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### RubyMotion support
|
||||||
|
|
||||||
|
To use AASM with a RubyMotion project, use it with the [motion-bundler](https://github.com/archan937/motion-bundler) gem.
|
||||||
|
|
||||||
|
|
||||||
### Testing
|
### Testing
|
||||||
|
|
||||||
AASM provides some matchers for [RSpec](http://rspec.info): `transition_from`, `have_state`, `allow_event` and `allow_transition_to`. Add `require 'aasm/rspec'` to your `spec_helper.rb` file and use them like this
|
AASM provides some matchers for [RSpec](http://rspec.info): `transition_from`, `have_state`, `allow_event` and `allow_transition_to`. Add `require 'aasm/rspec'` to your `spec_helper.rb` file and use them like this
|
||||||
|
|
|
@ -12,4 +12,5 @@ require 'aasm/core/state'
|
||||||
require 'aasm/localizer'
|
require 'aasm/localizer'
|
||||||
require 'aasm/state_machine'
|
require 'aasm/state_machine'
|
||||||
require 'aasm/persistence'
|
require 'aasm/persistence'
|
||||||
|
require 'aasm/persistence/plain_persistence' # RubyMotion support
|
||||||
require 'aasm/aasm'
|
require 'aasm/aasm'
|
||||||
|
|
|
@ -31,17 +31,16 @@ module AASM
|
||||||
|
|
||||||
# make sure to raise an error if no_direct_assignment is enabled
|
# make sure to raise an error if no_direct_assignment is enabled
|
||||||
# and attribute is directly assigned though
|
# and attribute is directly assigned though
|
||||||
@klass.class_eval %Q(
|
aasm_name = @name
|
||||||
def #{@state_machine.config.column}=(state_name)
|
@klass.send :define_method, "#{@state_machine.config.column}=", ->(state_name) do
|
||||||
if self.class.aasm(:#{@name}).state_machine.config.no_direct_assignment
|
if self.class.aasm(:"#{aasm_name}").state_machine.config.no_direct_assignment
|
||||||
raise AASM::NoDirectAssignmentError.new(
|
raise AASM::NoDirectAssignmentError.new(
|
||||||
'direct assignment of AASM column has been disabled (see AASM configuration for this class)'
|
'direct assignment of AASM column has been disabled (see AASM configuration for this class)'
|
||||||
)
|
)
|
||||||
else
|
else
|
||||||
super
|
super(state_name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# This method is both a getter and a setter
|
# This method is both a getter and a setter
|
||||||
|
@ -70,11 +69,10 @@ module AASM
|
||||||
warn "#{@klass.name}: The aasm state name #{name} is already used!"
|
warn "#{@klass.name}: The aasm state name #{name} is already used!"
|
||||||
end
|
end
|
||||||
|
|
||||||
@klass.class_eval <<-EORUBY, __FILE__, __LINE__ + 1
|
aasm_name = @name
|
||||||
def #{name}?
|
@klass.send :define_method, "#{name}?", ->() do
|
||||||
aasm(:#{@name}).current_state == :#{name}
|
aasm(:"#{aasm_name}").current_state == :"#{name}"
|
||||||
end
|
end
|
||||||
EORUBY
|
|
||||||
|
|
||||||
unless @klass.const_defined?("STATE_#{name.upcase}")
|
unless @klass.const_defined?("STATE_#{name.upcase}")
|
||||||
@klass.const_set("STATE_#{name.upcase}", name)
|
@klass.const_set("STATE_#{name.upcase}", name)
|
||||||
|
@ -92,21 +90,21 @@ module AASM
|
||||||
# an addition over standard aasm so that, before firing an event, you can ask
|
# an addition over standard aasm so that, before firing an event, you can ask
|
||||||
# may_event? and get back a boolean that tells you whether the guard method
|
# may_event? and get back a boolean that tells you whether the guard method
|
||||||
# on the transition will let this happen.
|
# on the transition will let this happen.
|
||||||
@klass.class_eval <<-EORUBY, __FILE__, __LINE__ + 1
|
aasm_name = @name
|
||||||
def may_#{name}?(*args)
|
|
||||||
aasm(:#{@name}).may_fire_event?(:#{name}, *args)
|
@klass.send :define_method, "may_#{name}?", ->(*args) do
|
||||||
|
aasm(:"#{aasm_name}").may_fire_event?(:"#{name}", *args)
|
||||||
end
|
end
|
||||||
|
|
||||||
def #{name}!(*args, &block)
|
@klass.send :define_method, "#{name}!", ->(*args, &block) do
|
||||||
aasm(:#{@name}).current_event = :#{name}!
|
aasm(:"#{aasm_name}").current_event = :"#{name}!"
|
||||||
aasm_fire_event(:#{@name}, :#{name}, {:persist => true}, *args, &block)
|
aasm_fire_event(:"#{aasm_name}", :"#{name}", {:persist => true}, *args, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def #{name}(*args, &block)
|
@klass.send :define_method, "#{name}", ->(*args, &block) do
|
||||||
aasm(:#{@name}).current_event = :#{name}
|
aasm(:"#{aasm_name}").current_event = :"#{name}"
|
||||||
aasm_fire_event(:#{@name}, :#{name}, {:persist => false}, *args, &block)
|
aasm_fire_event(:"#{aasm_name}", :"#{name}", {:persist => false}, *args, &block)
|
||||||
end
|
end
|
||||||
EORUBY
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def after_all_transitions(*callbacks, &block)
|
def after_all_transitions(*callbacks, &block)
|
||||||
|
|
|
@ -23,7 +23,7 @@ module AASM
|
||||||
|
|
||||||
def include_persistence(base, type)
|
def include_persistence(base, type)
|
||||||
require File.join(File.dirname(__FILE__), 'persistence', "#{type}_persistence")
|
require File.join(File.dirname(__FILE__), 'persistence', "#{type}_persistence")
|
||||||
base.send(:include, constantize("AASM::Persistence::#{capitalize(type)}Persistence"))
|
base.send(:include, constantize("#{capitalize(type)}Persistence"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def capitalize(string_or_symbol)
|
def capitalize(string_or_symbol)
|
||||||
|
@ -31,7 +31,7 @@ module AASM
|
||||||
end
|
end
|
||||||
|
|
||||||
def constantize(string)
|
def constantize(string)
|
||||||
instance_eval(string)
|
AASM::Persistence.const_get(string)
|
||||||
end
|
end
|
||||||
|
|
||||||
end # class << self
|
end # class << self
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue