Skip to content

Commit

Permalink
first set of ActiveRecord tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alto committed Jul 1, 2015
1 parent a5fe043 commit 5c94b62
Show file tree
Hide file tree
Showing 19 changed files with 766 additions and 16 deletions.
1 change: 1 addition & 0 deletions PLANNED_CHANGES.md
Expand Up @@ -4,6 +4,7 @@

* add support for multiple state machines per class
* check all tests
* _ActiveRecord_ when :right state machine is defined as well
* what happen's if someone accesses `aasm`, but has defined a
state machine for `aasm(:my_name)`?
* persistence
Expand Down
2 changes: 1 addition & 1 deletion lib/aasm/base.rb
Expand Up @@ -33,7 +33,7 @@ def initialize(klass, name, state_machine, options={}, &block)
# and attribute is directly assigned though
@klass.class_eval %Q(
def #{@state_machine.config.column}=(state_name)
if self.class.aasm.state_machine.config.no_direct_assignment
if self.class.aasm(:#{@name}).state_machine.config.no_direct_assignment
raise AASM::NoDirectAssignmentError.new(
'direct assignment of AASM column has been disabled (see AASM configuration for this class)'
)
Expand Down
12 changes: 6 additions & 6 deletions lib/aasm/persistence/active_record_persistence.rb
Expand Up @@ -118,18 +118,18 @@ def aasm_write_state_without_persistence(state, name=:default)
def aasm_enum(name=:default)
case AASM::StateMachine[self.class][name].config.enum
when false then nil
when true then aasm_guess_enum_method
when nil then aasm_guess_enum_method if aasm_column_looks_like_enum
when true then aasm_guess_enum_method(name)
when nil then aasm_guess_enum_method(name) if aasm_column_looks_like_enum(name)
else AASM::StateMachine[self.class][name].config.enum
end
end

def aasm_column_looks_like_enum
self.class.columns_hash[self.class.aasm.attribute_name.to_s].type == :integer
def aasm_column_looks_like_enum(name=:default)
self.class.columns_hash[self.class.aasm(name).attribute_name.to_s].type == :integer
end

def aasm_guess_enum_method
self.class.aasm.attribute_name.to_s.pluralize.to_sym
def aasm_guess_enum_method(name=:default)
self.class.aasm(name).attribute_name.to_s.pluralize.to_sym
end

def aasm_skipping_validations(state_machine_name)
Expand Down
18 changes: 17 additions & 1 deletion spec/database.rb
@@ -1,5 +1,5 @@
ActiveRecord::Migration.suppress_messages do
%w{gates readers writers transients simples no_scopes no_direct_assignments thieves localizer_test_models persisted_states provided_and_persisted_states with_enums with_true_enums with_false_enums false_states}.each do |table_name|
%w{gates multiple_gates readers writers transients simples no_scopes multiple_no_scopes no_direct_assignments multiple_no_direct_assignments thieves multiple_thieves localizer_test_models persisted_states provided_and_persisted_states with_enums with_true_enums with_false_enums false_states multiple_with_enums multiple_with_true_enums multiple_with_false_enums multiple_false_states}.each do |table_name|
ActiveRecord::Migration.create_table table_name, :force => true do |t|
t.string "aasm_state"
end
Expand All @@ -8,17 +8,29 @@
ActiveRecord::Migration.create_table "simple_new_dsls", :force => true do |t|
t.string "status"
end
ActiveRecord::Migration.create_table "multiple_simple_new_dsls", :force => true do |t|
t.string "status"
end

ActiveRecord::Migration.create_table "validators", :force => true do |t|
t.string "name"
t.string "status"
end
ActiveRecord::Migration.create_table "multiple_validators", :force => true do |t|
t.string "name"
t.string "status"
end

ActiveRecord::Migration.create_table "transactors", :force => true do |t|
t.string "name"
t.string "status"
t.integer "worker_id"
end
ActiveRecord::Migration.create_table "multiple_transactors", :force => true do |t|
t.string "name"
t.string "status"
t.integer "worker_id"
end

ActiveRecord::Migration.create_table "workers", :force => true do |t|
t.string "name"
Expand All @@ -29,6 +41,10 @@
t.string "name"
t.string "status"
end
ActiveRecord::Migration.create_table "multiple_invalid_persistors", :force => true do |t|
t.string "name"
t.string "status"
end

ActiveRecord::Migration.create_table "fathers", :force => true do |t|
t.string "aasm_state"
Expand Down
4 changes: 4 additions & 0 deletions spec/models/active_record/derivate_new_dsl.rb
@@ -1,3 +1,7 @@
require_relative 'simple_new_dsl'

class DerivateNewDsl < SimpleNewDsl
end

class MultipleDerivateNewDsl < MultipleSimpleNewDsl
end
18 changes: 18 additions & 0 deletions spec/models/active_record/false_state.rb
Expand Up @@ -15,3 +15,21 @@ def initialize(*args)
end
end
end

class MultipleFalseState < ActiveRecord::Base
include AASM

def initialize(*args)
super
self.aasm_state = false
end

aasm :left do
state :opened
state :closed

event :view do
transitions :to => :read, :from => [:needs_attention]
end
end
end
20 changes: 20 additions & 0 deletions spec/models/active_record/gate.rb
Expand Up @@ -17,3 +17,23 @@ def value
end
end
end

class MultipleGate < ActiveRecord::Base
include AASM

# Fake this column for testing purposes
# attr_accessor :aasm_state

def value
'value'
end

aasm :left do
state :opened
state :closed

event :view do
transitions :to => :read, :from => [:needs_attention]
end
end
end
11 changes: 11 additions & 0 deletions spec/models/active_record/no_direct_assignment.rb
Expand Up @@ -8,3 +8,14 @@ class NoDirectAssignment < ActiveRecord::Base
end
end
end

class MultipleNoDirectAssignment < ActiveRecord::Base
include AASM
aasm :left, :no_direct_assignment => true do
state :pending, :initial => true
state :running
event :run do
transitions :from => :pending, :to => :running
end
end
end
11 changes: 11 additions & 0 deletions spec/models/active_record/no_scope.rb
Expand Up @@ -8,3 +8,14 @@ class NoScope < ActiveRecord::Base
end
end
end

class MultipleNoScope < ActiveRecord::Base
include AASM
aasm :left, :create_scopes => false do
state :pending, :initial => true
state :running
event :run do
transitions :from => :pending, :to => :running
end
end
end
9 changes: 9 additions & 0 deletions spec/models/active_record/simple_new_dsl.rb
Expand Up @@ -6,3 +6,12 @@ class SimpleNewDsl < ActiveRecord::Base
state :new
end
end

class MultipleSimpleNewDsl < ActiveRecord::Base
include AASM
aasm :left, :column => :status
aasm :left do
state :unknown_scope
state :new
end
end
15 changes: 15 additions & 0 deletions spec/models/active_record/thief.rb
Expand Up @@ -12,3 +12,18 @@ class Thief < ActiveRecord::Base
end
attr_accessor :skilled, :aasm_state
end

class MultipleThief < ActiveRecord::Base
if ActiveRecord::VERSION::MAJOR >= 3
self.table_name = 'multiple_thieves'
else
set_table_name "multiple_thieves"
end
include AASM
aasm :left do
state :rich
state :jailed
initial_state Proc.new {|thief| thief.skilled ? :rich : :jailed }
end
attr_accessor :skilled, :aasm_state
end
20 changes: 20 additions & 0 deletions spec/models/active_record/with_enum.rb
Expand Up @@ -17,3 +17,23 @@ def self.test
end
end
end

class MultipleWithEnum < ActiveRecord::Base
include AASM

# Fake this column for testing purposes
attr_accessor :aasm_state

def self.test
{}
end

aasm :left, :enum => :test do
state :opened
state :closed

event :view do
transitions :to => :read, :from => [:needs_attention]
end
end
end
16 changes: 16 additions & 0 deletions spec/models/active_record/with_false_enum.rb
Expand Up @@ -13,3 +13,19 @@ class WithFalseEnum < ActiveRecord::Base
end
end
end

class MultipleWithFalseEnum < ActiveRecord::Base
include AASM

# Fake this column for testing purposes
attr_accessor :aasm_state

aasm :left, :enum => false do
state :opened
state :closed

event :view do
transitions :to => :read, :from => [:needs_attention]
end
end
end
20 changes: 20 additions & 0 deletions spec/models/active_record/with_true_enum.rb
Expand Up @@ -17,3 +17,23 @@ def value
end
end
end

class MultipleWithTrueEnum < ActiveRecord::Base
include AASM

# Fake this column for testing purposes
attr_accessor :aasm_state

def value
'value'
end

aasm :left, :enum => true do
state :opened
state :closed

event :view do
transitions :to => :read, :from => [:needs_attention]
end
end
end
15 changes: 15 additions & 0 deletions spec/models/invalid_persistor.rb
Expand Up @@ -14,3 +14,18 @@ class InvalidPersistor < ActiveRecord::Base
end
validates_presence_of :name
end

class MultipleInvalidPersistor < ActiveRecord::Base
include AASM
aasm :left, :column => :status, :skip_validation_on_save => true do
state :sleeping, :initial => true
state :running
event :run do
transitions :to => :running, :from => :sleeping
end
event :sleep do
transitions :to => :sleeping, :from => :running
end
end
validates_presence_of :name
end
27 changes: 27 additions & 0 deletions spec/models/transactor.rb
@@ -1,4 +1,5 @@
require 'active_record'

class Transactor < ActiveRecord::Base

belongs_to :worker
Expand All @@ -24,3 +25,29 @@ def fail
end

end

class MultipleTransactor < ActiveRecord::Base

belongs_to :worker

include AASM
aasm :left, :column => :status do
state :sleeping, :initial => true
state :running, :before_enter => :start_worker, :after_enter => :fail

event :run do
transitions :to => :running, :from => :sleeping
end
end

private

def start_worker
worker.update_attribute(:status, 'running')
end

def fail
raise StandardError.new('failed on purpose')
end

end
39 changes: 39 additions & 0 deletions spec/models/validator.rb
Expand Up @@ -38,3 +38,42 @@ def fail
raise StandardError.new('failed on purpose')
end
end

class MultipleValidator < ActiveRecord::Base

include AASM
aasm :left, :column => :status do
state :sleeping, :initial => true
state :running
state :failed, :after_enter => :fail

event :run, :after_commit => :change_name! do
transitions :to => :running, :from => :sleeping
end
event :sleep do
after_commit do |name|
change_name_on_sleep name
end
transitions :to => :sleeping, :from => :running
end
event :fail do
transitions :to => :failed, :from => [:sleeping, :running]
end
end

validates_presence_of :name

def change_name!
self.name = "name changed"
save!
end

def change_name_on_sleep name
self.name = name
save!
end

def fail
raise StandardError.new('failed on purpose')
end
end

0 comments on commit 5c94b62

Please sign in to comment.