1
0
Fork 0
mirror of https://github.com/aasm/aasm synced 2023-03-27 23:22:41 -04:00

Merge branch 'namespaces' of https://github.com/allspiritseve/aasm into allspiritseve-namespaces

Conflicts:
	lib/aasm/base.rb
This commit is contained in:
Thorsten Böttger 2016-03-19 18:13:39 +13:00
commit 092ca0e420
4 changed files with 113 additions and 5 deletions

View file

@ -1,8 +1,7 @@
module AASM
class Base
attr_reader :klass,
:state_machine
attr_reader :klass, :state_machine
def initialize(klass, name, state_machine, options={}, &block)
@klass = klass
@ -38,6 +37,9 @@ module AASM
configure :enum, nil
# Set to true to namespace reader methods and constants
configure :namespace, false
# make sure to raise an error if no_direct_assignment is enabled
# and attribute is directly assigned though
aasm_name = @name
@ -76,12 +78,23 @@ module AASM
aasm_name = @name.to_sym
state = name.to_sym
safely_define_method klass, "#{name}?", -> do
if namespace?
method_name = "#{namespace}_#{name}"
else
method_name = name
end
safely_define_method klass, "#{method_name}?", -> do
aasm(aasm_name).current_state == state
end
unless klass.const_defined?("STATE_#{name.upcase}")
klass.const_set("STATE_#{name.upcase}", name)
if namespace?
const_name = "STATE_#{namespace.upcase}_#{name.upcase}"
else
const_name = "STATE_#{name.upcase}"
end
unless klass.const_defined?(const_name)
klass.const_set(const_name, name)
end
end
@ -185,5 +198,16 @@ module AASM
klass.send(:define_method, method_name, method_definition)
end
def namespace?
!!@state_machine.config.namespace
end
def namespace
if @state_machine.config.namespace == true
@name
else
@state_machine.config.namespace
end
end
end
end

View file

@ -25,5 +25,8 @@ module AASM
attr_accessor :with_klass
attr_accessor :enum
# namespace reader methods and constants
attr_accessor :namespace
end
end

View file

@ -0,0 +1,28 @@
class NamespacedMultipleExample
include AASM
aasm(:status) do
state :unapproved, :initial => true
state :approved
event :approve do
transitions :from => :unapproved, :to => :approved
end
event :unapprove do
transitions :from => :approved, :to => :unapproved
end
end
aasm(:review_status, namespace: :review) do
state :unapproved, :initial => true
state :approved
event :approve_review do
transitions :from => :unapproved, :to => :approved
end
event :unapprove_review do
transitions :from => :approved, :to => :unapproved
end
end
end

View file

@ -0,0 +1,53 @@
require 'spec_helper'
describe 'state machine' do
let(:namespaced) { NamespacedMultipleExample.new }
it 'starts with an initial state' do
expect(namespaced.aasm(:status).current_state).to eq(:unapproved)
expect(namespaced).to respond_to(:unapproved?)
expect(namespaced).to be_unapproved
expect(namespaced.aasm(:review_status).current_state).to eq(:unapproved)
expect(namespaced).to respond_to(:review_unapproved?)
expect(namespaced).to be_review_unapproved
end
it 'allows transitions to other states' do
expect(namespaced).to respond_to(:approve)
expect(namespaced).to respond_to(:approve!)
namespaced.approve!
expect(namespaced).to respond_to(:approved?)
expect(namespaced).to be_approved
expect(namespaced).to respond_to(:approve_review)
expect(namespaced).to respond_to(:approve_review!)
namespaced.approve_review!
expect(namespaced).to respond_to(:review_approved?)
expect(namespaced).to be_review_approved
end
it 'denies transitions to other states' do
expect {namespaced.unapprove}.to raise_error(AASM::InvalidTransition)
expect {namespaced.unapprove!}.to raise_error(AASM::InvalidTransition)
namespaced.approve
expect {namespaced.approve}.to raise_error(AASM::InvalidTransition)
expect {namespaced.approve!}.to raise_error(AASM::InvalidTransition)
namespaced.unapprove
expect {namespaced.unapprove_review}.to raise_error(AASM::InvalidTransition)
expect {namespaced.unapprove_review!}.to raise_error(AASM::InvalidTransition)
namespaced.approve_review
expect {namespaced.approve_review}.to raise_error(AASM::InvalidTransition)
expect {namespaced.approve_review!}.to raise_error(AASM::InvalidTransition)
namespaced.unapprove_review
end
it 'defines constants for each state name' do
expect(NamespacedMultipleExample::STATE_UNAPPROVED).to eq(:unapproved)
expect(NamespacedMultipleExample::STATE_APPROVED).to eq(:approved)
expect(NamespacedMultipleExample::STATE_REVIEW_UNAPPROVED).to eq(:unapproved)
expect(NamespacedMultipleExample::STATE_REVIEW_APPROVED).to eq(:approved)
end
end