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

Fix AR persistence so new records have their state set. [Joao Paulo Lins]

Ensure that a state is written for a new record even if aasm_current_state or
{state}= are never called.
This commit is contained in:
Scott Barron 2008-02-27 15:32:52 -05:00
parent d9300f5abc
commit 05249c09a3

View file

@ -5,6 +5,10 @@ module AASM
base.extend AASM::Persistence::ActiveRecordPersistence::ClassMethods
base.send(:include, AASM::Persistence::ActiveRecordPersistence::WriteState) unless base.method_defined?(:aasm_write_state)
base.send(:include, AASM::Persistence::ActiveRecordPersistence::ReadState) unless base.method_defined?(:aasm_read_state)
base.before_save do |record|
record.send("#{record.class.aasm_column}=", record.aasm_current_state.to_s)
end
end
module ClassMethods
@ -12,23 +16,20 @@ module AASM
if column_name
@aasm_column = column_name.to_sym
else
@aasm_column
@aasm_column ||= :aasm_state
end
end
end
module WriteState
def aasm_write_state(state)
@aasm_column ||= :aasm_state
update_attribute(@aasm_column, state.to_s)
update_attribute(self.class.aasm_column, state.to_s)
end
end
module ReadState
def aasm_read_state
@aasm_column ||= :aasm_state
send(@aasm_column).to_sym
new_record? ? self.class.aasm_initial_state : send(self.class.aasm_column).to_sym
end
end
end