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

aasm_column is deprecated. Use aasm.attribute_name instead.

This commit is contained in:
Thorsten Böttger 2014-11-24 23:50:04 +01:00
parent 13d78f7876
commit 6fbdefd620
9 changed files with 54 additions and 70 deletions

View file

@ -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
@ -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

View file

@ -34,6 +34,16 @@ module AASM
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

View file

@ -21,7 +21,7 @@ module AASM
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)

View file

@ -84,17 +84,17 @@ module AASM
#
# 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
@ -128,11 +128,11 @@ module AASM
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
@ -140,7 +140,7 @@ module AASM
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)
@ -167,7 +167,7 @@ module AASM
# 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)

View file

@ -6,7 +6,7 @@ module AASM
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):
@ -33,7 +33,7 @@ module AASM
#
# 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
@ -42,41 +42,9 @@ module AASM
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
@ -90,7 +58,7 @@ module AASM
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) }
@ -101,7 +69,7 @@ module AASM
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

View file

@ -55,7 +55,7 @@ module AASM
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
@ -75,11 +75,11 @@ module AASM
#
# 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
@ -99,7 +99,7 @@ module AASM
#
# 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
@ -120,14 +120,14 @@ module AASM
# 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

View file

@ -17,7 +17,7 @@ module AASM
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
#
@ -43,7 +43,7 @@ module AASM
#
# 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?
@ -70,7 +70,7 @@ module AASM
#
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
@ -83,7 +83,7 @@ module AASM
#
# 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
@ -100,7 +100,7 @@ module AASM
#
# 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

View file

@ -30,7 +30,7 @@ describe "instance methods" do
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
@ -55,7 +55,7 @@ describe "instance methods" do
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
@ -81,7 +81,7 @@ describe "instance methods" do
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
@ -104,7 +104,7 @@ describe "instance methods" do
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
@ -172,7 +172,7 @@ describe "instance methods" do
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
@ -253,7 +253,7 @@ describe "instance methods" do
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)
@ -293,8 +293,8 @@ describe 'subclasses' do
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

View file

@ -81,8 +81,8 @@ describe 'sequel' do
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