Skip to content

Commit

Permalink
aasm_column is deprecated. Use aasm.attribute_name instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
alto committed Nov 24, 2014
1 parent 13d78f7 commit 6fbdefd
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 70 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Expand Up @@ -2,7 +2,12 @@

## 4.1.0 (not yet released)

* `aasm_human_event_name` is deprecated, use `aasm.human_event_name` instead
* `aasm_column` has been removed. Use `aasm.attribute_name` instead
* `aasm_human_event_name` has been removed. Use `aasm.human_event_name` instead

## 4.0.x (not yet released)

* `aasm_column` is deprecated. Use `aasm.attribute_name` instead

## 4.0.2

Expand All @@ -11,6 +16,7 @@
## 4.0.1

* fire guards only once per transition (see [issue #184](https://github.com/aasm/aasm/issues/184) for details)
* `aasm_human_event_name` is deprecated, use `aasm.human_event_name` instead

## 4.0.0

Expand Down
10 changes: 10 additions & 0 deletions lib/aasm/base.rb
Expand Up @@ -34,6 +34,16 @@ def initialize(klass, options={}, &block)
end
end

# This method is both a getter and a setter
def attribute_name(column_name=nil)
if column_name
@state_machine.config.column = column_name.to_sym
else
@state_machine.config.column ||= :aasm_state
end
@state_machine.config.column
end

def initial_state(new_initial_state=nil)
if new_initial_state
@state_machine.initial_state = new_initial_state
Expand Down
2 changes: 1 addition & 1 deletion lib/aasm/localizer.rb
Expand Up @@ -21,7 +21,7 @@ def human_state_name(klass, state)

def item_for(klass, state, ancestor, options={})
separator = options[:old_style] ? '.' : '/'
:"#{i18n_scope(klass)}.attributes.#{i18n_klass(ancestor)}.#{klass.aasm_column}#{separator}#{state}"
:"#{i18n_scope(klass)}.attributes.#{i18n_klass(ancestor)}.#{klass.aasm.attribute_name}#{separator}#{state}"
end

def translate_queue(checklist)
Expand Down
14 changes: 7 additions & 7 deletions lib/aasm/persistence/active_record_persistence.rb
Expand Up @@ -84,17 +84,17 @@ module InstanceMethods
#
# NOTE: intended to be called from an event
def aasm_write_state(state)
old_value = read_attribute(self.class.aasm_column)
old_value = read_attribute(self.class.aasm.attribute_name)
aasm_write_attribute state

success = if aasm_skipping_validations
value = aasm_raw_attribute_value state
self.class.where(self.class.primary_key => self.id).update_all(self.class.aasm_column => value) == 1
self.class.where(self.class.primary_key => self.id).update_all(self.class.aasm.attribute_name => value) == 1
else
self.save
end
unless success
write_attribute(self.class.aasm_column, old_value)
write_attribute(self.class.aasm.attribute_name, old_value)
return false
end

Expand Down Expand Up @@ -128,19 +128,19 @@ def aasm_enum
end

def aasm_column_looks_like_enum
self.class.columns_hash[self.class.aasm_column.to_s].type == :integer
self.class.columns_hash[self.class.aasm.attribute_name.to_s].type == :integer
end

def aasm_guess_enum_method
self.class.aasm_column.to_s.pluralize.to_sym
self.class.aasm.attribute_name.to_s.pluralize.to_sym
end

def aasm_skipping_validations
AASM::StateMachine[self.class].config.skip_validation_on_save
end

def aasm_write_attribute(state)
write_attribute self.class.aasm_column, aasm_raw_attribute_value(state)
write_attribute self.class.aasm.attribute_name, aasm_raw_attribute_value(state)
end

def aasm_raw_attribute_value(state)
Expand All @@ -167,7 +167,7 @@ def aasm_raw_attribute_value(state)
# foo.aasm_state # => nil
#
def aasm_ensure_initial_state
aasm.enter_initial_state if send(self.class.aasm_column).blank?
aasm.enter_initial_state if send(self.class.aasm.attribute_name).blank?
end

def aasm_fire_event(name, options, *args, &block)
Expand Down
46 changes: 7 additions & 39 deletions lib/aasm/persistence/base.rb
Expand Up @@ -6,7 +6,7 @@ def self.included(base) #:nodoc:
base.extend ClassMethods
end

# Returns the value of the aasm_column - called from <tt>aasm.current_state</tt>
# Returns the value of the aasm.attribute_name - called from <tt>aasm.current_state</tt>
#
# If it's a new record, and the aasm state column is blank it returns the initial state
# (example provided here for ActiveRecord, but it's true for Mongoid as well):
Expand All @@ -33,7 +33,7 @@ def self.included(base) #:nodoc:
#
# This allows for nil aasm states - be sure to add validation to your model
def aasm_read_state
state = send(self.class.aasm_column)
state = send(self.class.aasm.attribute_name)
if new_record?
state.blank? ? aasm.determine_state_name(self.class.aasm.initial_state) : state.to_sym
else
Expand All @@ -42,41 +42,9 @@ def aasm_read_state
end

module ClassMethods
# Maps to the aasm_column in the database. Defaults to "aasm_state". You can write
# (example provided here for ActiveRecord, but it's true for Mongoid as well):
#
# create_table :foos do |t|
# t.string :name
# t.string :aasm_state
# end
#
# class Foo < ActiveRecord::Base
# include AASM
# end
#
# OR:
#
# create_table :foos do |t|
# t.string :name
# t.string :status
# end
#
# class Foo < ActiveRecord::Base
# include AASM
# aasm_column :status
# end
#
# This method is both a getter and a setter
def aasm_column(column_name=nil)
if column_name
AASM::StateMachine[self].config.column = column_name.to_sym
# @aasm_column = column_name.to_sym
else
AASM::StateMachine[self].config.column ||= :aasm_state
# @aasm_column ||= :aasm_state
end
# @aasm_column
AASM::StateMachine[self].config.column
def aasm_column(attribute_name=nil)
warn "[DEPRECATION] aasm_column is deprecated. Use aasm.attribute_name instead"
aasm.attribute_name(attribute_name)
end
end # ClassMethods

Expand All @@ -90,7 +58,7 @@ def state_with_scope(name, *args)
if AASM::StateMachine[@klass].config.create_scopes && !@klass.respond_to?(name)
if @klass.ancestors.map {|klass| klass.to_s}.include?("ActiveRecord::Base")

conditions = {"#{@klass.table_name}.#{@klass.aasm_column}" => name.to_s}
conditions = {"#{@klass.table_name}.#{@klass.aasm.attribute_name}" => name.to_s}
if ActiveRecord::VERSION::MAJOR >= 3
@klass.class_eval do
scope name, lambda { where(conditions) }
Expand All @@ -101,7 +69,7 @@ def state_with_scope(name, *args)
end
end
elsif @klass.ancestors.map {|klass| klass.to_s}.include?("Mongoid::Document")
scope_options = lambda { @klass.send(:where, {@klass.aasm_column.to_sym => name.to_s}) }
scope_options = lambda { @klass.send(:where, {@klass.aasm.attribute_name.to_sym => name.to_s}) }
@klass.send(:scope, name, scope_options)
end
end
Expand Down
14 changes: 7 additions & 7 deletions lib/aasm/persistence/mongoid_persistence.rb
Expand Up @@ -55,7 +55,7 @@ def count_in_state(state, *args)
end

def with_state_scope(state)
with_scope where(aasm_column.to_sym => state.to_s) do
with_scope where(aasm.attribute_name.to_sym => state.to_s) do
yield if block_given?
end
end
Expand All @@ -75,11 +75,11 @@ module InstanceMethods
#
# NOTE: intended to be called from an event
def aasm_write_state(state)
old_value = read_attribute(self.class.aasm_column)
write_attribute(self.class.aasm_column, state.to_s)
old_value = read_attribute(self.class.aasm.attribute_name)
write_attribute(self.class.aasm.attribute_name, state.to_s)

unless self.save(:validate => false)
write_attribute(self.class.aasm_column, old_value)
write_attribute(self.class.aasm.attribute_name, old_value)
return false
end

Expand All @@ -99,7 +99,7 @@ def aasm_write_state(state)
#
# NOTE: intended to be called from an event
def aasm_write_state_without_persistence(state)
write_attribute(self.class.aasm_column, state.to_s)
write_attribute(self.class.aasm.attribute_name, state.to_s)
end

private
Expand All @@ -120,14 +120,14 @@ def aasm_write_state_without_persistence(state)
# foo.aasm_state # => nil
#
def aasm_ensure_initial_state
send("#{self.class.aasm_column}=", aasm.enter_initial_state.to_s) if send(self.class.aasm_column).blank?
send("#{self.class.aasm.attribute_name}=", aasm.enter_initial_state.to_s) if send(self.class.aasm.attribute_name).blank?
end
end # InstanceMethods

module NamedScopeMethods
def aasm_state_with_named_scope name, options = {}
aasm_state_without_named_scope name, options
self.named_scope name, :conditions => { "#{table_name}.#{self.aasm_column}" => name.to_s} unless self.respond_to?(name)
self.named_scope name, :conditions => { "#{table_name}.#{self.aasm.attribute_name}" => name.to_s} unless self.respond_to?(name)
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions lib/aasm/persistence/sequel_persistence.rb
Expand Up @@ -17,7 +17,7 @@ def before_create
super
end

# Returns the value of the aasm_column - called from <tt>aasm.current_state</tt>
# Returns the value of the aasm.attribute_name - called from <tt>aasm.current_state</tt>
#
# If it's a new record, and the aasm state column is blank it returns the initial state
#
Expand All @@ -43,7 +43,7 @@ def before_create
#
# This allows for nil aasm states - be sure to add validation to your model
def aasm_read_state
state = send(self.class.aasm_column)
state = send(self.class.aasm.attribute_name)
if new? && state.to_s.strip.empty?
aasm.determine_state_name(self.class.aasm.initial_state)
elsif state.nil?
Expand All @@ -70,7 +70,7 @@ def aasm_read_state
#
def aasm_ensure_initial_state
aasm.enter_initial_state if
send(self.class.aasm_column).to_s.strip.empty?
send(self.class.aasm.attribute_name).to_s.strip.empty?
end

# Writes <tt>state</tt> to the state column and persists it to the database
Expand All @@ -83,7 +83,7 @@ def aasm_ensure_initial_state
#
# NOTE: intended to be called from an event
def aasm_write_state state
aasm_column = self.class.aasm_column
aasm_column = self.class.aasm.attribute_name
update_only({aasm_column => state.to_s}, aasm_column)
end

Expand All @@ -100,7 +100,7 @@ def aasm_write_state state
#
# NOTE: intended to be called from an event
def aasm_write_state_without_persistence state
send("#{self.class.aasm_column}=", state.to_s)
send("#{self.class.aasm.attribute_name}=", state.to_s)
end
end
end
Expand Down
16 changes: 8 additions & 8 deletions spec/unit/persistence/active_record_persistence_spec.rb
Expand Up @@ -30,7 +30,7 @@
let(:columns_hash) { Hash[column_name, column] }

before :each do
gate.class.stub(:aasm_column).and_return(column_name.to_sym)
gate.class.aasm.stub(:attribute_name).and_return(column_name.to_sym)
gate.class.stub(:columns_hash).and_return(columns_hash)
end

Expand All @@ -55,7 +55,7 @@
subject { lambda{ gate.send(:aasm_guess_enum_method) } }

before :each do
gate.class.stub(:aasm_column).and_return(:value)
gate.class.aasm.stub(:attribute_name).and_return(:value)
end

it "pluralizes AASM column name" do
Expand All @@ -81,7 +81,7 @@
context "when AASM enum setting is simply set to true" do
before :each do
AASM::StateMachine[Gate].config.stub(:enum).and_return(true)
Gate.stub(:aasm_column).and_return(:value)
Gate.aasm.stub(:attribute_name).and_return(:value)
gate.stub(:aasm_guess_enum_method).and_return(:values)
end

Expand All @@ -104,7 +104,7 @@
context "when AASM enum setting is not enabled" do
before :each do
AASM::StateMachine[Gate].config.stub(:enum).and_return(nil)
Gate.stub(:aasm_column).and_return(:value)
Gate.aasm.stub(:attribute_name).and_return(:value)
end

context "when AASM column looks like enum" do
Expand Down Expand Up @@ -172,7 +172,7 @@
gate.aasm_write_state state_sym

expect(obj).to have_received(:update_all)
.with(Hash[gate.class.aasm_column, state_code])
.with(Hash[gate.class.aasm.attribute_name, state_code])
end
end

Expand Down Expand Up @@ -253,7 +253,7 @@
expect(gate.aasm.current_state).to eq(:closed)
end

it "should return the aasm column when not new and the aasm_column is not nil" do
it "should return the aasm column when not new and the aasm.attribute_name is not nil" do
allow(gate).to receive(:new_record?).and_return(false)
gate.aasm_state = "state"
expect(gate.aasm.current_state).to eq(:state)
Expand Down Expand Up @@ -293,8 +293,8 @@
end

it "should have the same column as its parent even for the new dsl" do
expect(SimpleNewDsl.aasm_column).to eq(:status)
expect(DerivateNewDsl.aasm_column).to eq(:status)
expect(SimpleNewDsl.aasm.attribute_name).to eq(:status)
expect(DerivateNewDsl.aasm.attribute_name).to eq(:status)
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/unit/persistence/sequel_persistence_spec.rb
Expand Up @@ -81,8 +81,8 @@
end

it "should have the same column as its parent even for the new dsl" do
expect(@model.aasm_column).to eq(:status)
expect(Class.new(@model).aasm_column).to eq(:status)
expect(@model.aasm.attribute_name).to eq(:status)
expect(Class.new(@model).aasm.attribute_name).to eq(:status)
end
end

Expand Down

0 comments on commit 6fbdefd

Please sign in to comment.