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

Add possibility to skip constant creation for each state #773

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: 8 additions & 3 deletions lib/aasm/base.rb
Expand Up @@ -54,6 +54,9 @@ def initialize(klass, name, state_machine, options={}, &block)
# Configure a logger, with default being a Logger to STDERR
configure :logger, Logger.new(STDERR)

# Add possibility to skip state's constant creation
configure :create_constants, true

# setup timestamp-setting callback if enabled
setup_timestamps(@name)

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

const_name = namespace? ? "STATE_#{namespace.upcase}_#{name.upcase}" : "STATE_#{name.upcase}"
unless klass.const_defined?(const_name)
klass.const_set(const_name, name)
if @state_machine.config.create_constants
const_name = namespace? ? "STATE_#{namespace.upcase}_#{name.upcase}" : "STATE_#{name.upcase}"
unless klass.const_defined?(const_name)
klass.const_set(const_name, name)
end
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/aasm/configuration.rb
Expand Up @@ -41,6 +41,9 @@ class Configuration
# Configure a logger, with default being a Logger to STDERR
attr_accessor :logger

# Add possibility to skip state's constant creation
attr_accessor :create_constants

class << self
attr_accessor :hide_warnings
end
Expand Down
11 changes: 11 additions & 0 deletions spec/models/no_constant.rb
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class NoConstant
include AASM

aasm create_constants: false do
state :initialised, initial: true
state :filled_out
state :authorised
end
end
13 changes: 13 additions & 0 deletions spec/unit/no_constants_spec.rb
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'No Constants' do
let(:simple) { NoConstant.new }

it 'defines constants for each state name' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it 'defines constants for each state name' do
it 'does not define constants for each state name' do

expect(NoConstant.const_defined?('STATE_INITIALISED')).to be_falsey
expect(NoConstant.const_defined?('STATE_FILLED_OUT')).to be_falsey
expect(NoConstant.const_defined?('STATE_AUTHORISED')).to be_falsey
end
end