Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toggle 'STATE_' prefix on autogenerated constants #811

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/aasm/base.rb
Expand Up @@ -51,6 +51,9 @@ def initialize(klass, name, state_machine, options={}, &block)
# Set to true to namespace reader methods and constants
configure :namespace, false

# Set to true to prefix reader constants
configure :prefix_constants, true

# Configure a logger, with default being a Logger to STDERR
configure :logger, Logger.new(STDERR)

Expand Down Expand Up @@ -100,7 +103,9 @@ def state(*args)
aasm(aasm_name).current_state == state
end

const_name = namespace? ? "STATE_#{namespace.upcase}_#{name.upcase}" : "STATE_#{name.upcase}"
const_name = namespace? ? "#{namespace.upcase}_#{name.upcase}" : "#{name.upcase}"
const_name = prefix_constants? ? "STATE_#{const_name}" : const_name

unless klass.const_defined?(const_name)
klass.const_set(const_name, name)
end
Expand Down Expand Up @@ -239,6 +244,10 @@ def apply_ruby2_keyword(klass, sym)
end
end

def prefix_constants?
!!@state_machine.config.prefix_constants
end

def namespace?
!!@state_machine.config.namespace
end
Expand Down
3 changes: 3 additions & 0 deletions lib/aasm/configuration.rb
Expand Up @@ -38,6 +38,9 @@ class Configuration
# namespace reader methods and constants
attr_accessor :namespace

# prefix_constants reader
attr_accessor :prefix_constants

# Configure a logger, with default being a Logger to STDERR
attr_accessor :logger

Expand Down
21 changes: 21 additions & 0 deletions spec/models/simple_example_without_prefix_constants.rb
@@ -0,0 +1,21 @@
class SimpleWithoutPrefixConstantsExample
include AASM
aasm prefix_constants: false do
state :initialised, :initial => true
state :filled_out
state :denied
state :authorised

event :fill_out do
transitions :from => :initialised, :to => :filled_out
end

event :deny do
transitions from: :initialised, to: :denied
end

event :authorise do
transitions :from => :filled_out, :to => :authorised
end
end
end
57 changes: 57 additions & 0 deletions spec/unit/simple_example_without_prefix_constants_spec.rb
@@ -0,0 +1,57 @@
require 'spec_helper'

describe 'state machine' do
let(:simple) { SimpleWithoutPrefixConstantsExample.new }

it 'starts with an initial state' do
expect(simple.aasm.current_state).to eq(:initialised)
expect(simple).to respond_to(:initialised?)
expect(simple).to be_initialised
end

it 'allows transitions to other states' do
expect(simple).to respond_to(:fill_out)
expect(simple).to respond_to(:fill_out!)
simple.fill_out!
expect(simple).to respond_to(:filled_out?)
expect(simple).to be_filled_out

expect(simple).to respond_to(:authorise)
expect(simple).to respond_to(:authorise!)
simple.authorise
expect(simple).to respond_to(:authorised?)
expect(simple).to be_authorised
end

it 'shows the permitted transitions' do
expect(simple.aasm.permitted_transitions).to eq(
[
{ event: :fill_out, state: :filled_out },
{ event: :deny, state: :denied }
]
)

simple.fill_out!
expect(simple.aasm.permitted_transitions).to eq([{ event: :authorise, state: :authorised }])

simple.authorise
expect(simple.aasm.permitted_transitions).to eq([])
end

it 'denies transitions to other states' do
expect {simple.authorise}.to raise_error(AASM::InvalidTransition)
expect {simple.authorise!}.to raise_error(AASM::InvalidTransition)
simple.fill_out
expect {simple.fill_out}.to raise_error(AASM::InvalidTransition)
expect {simple.fill_out!}.to raise_error(AASM::InvalidTransition)
simple.authorise
expect {simple.fill_out}.to raise_error(AASM::InvalidTransition)
expect {simple.fill_out!}.to raise_error(AASM::InvalidTransition)
end

it 'defines constants for each state name' do
expect(SimpleWithoutPrefixConstantsExample::INITIALISED).to eq(:initialised)
expect(SimpleWithoutPrefixConstantsExample::FILLED_OUT).to eq(:filled_out)
expect(SimpleWithoutPrefixConstantsExample::AUTHORISED).to eq(:authorised)
end
end