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

Add specs for abstract super class

This commit is contained in:
Anil Maurya 2019-04-21 15:13:51 +05:30 committed by Anil Kumar Maurya
parent 23f98a100d
commit 66ec7a6152
4 changed files with 68 additions and 10 deletions

View file

@ -54,20 +54,25 @@ module AASM
# make sure to raise an error if no_direct_assignment is enabled # make sure to raise an error if no_direct_assignment is enabled
# and attribute is directly assigned though # and attribute is directly assigned though
aasm_name = @name aasm_name = @name
if !(klass.superclass.methods.include?(:abstract_class) && return true if should_not_define_method(klass)
klass.superclass.abstract_class) klass.send :define_method, "#{@state_machine.config.column}=", ->(state_name) do
klass.send :define_method, "#{@state_machine.config.column}=", ->(state_name) do if self.class.aasm(:"#{aasm_name}").state_machine.config.no_direct_assignment
if self.class.aasm(:"#{aasm_name}").state_machine.config.no_direct_assignment raise AASM::NoDirectAssignmentError.new(
raise AASM::NoDirectAssignmentError.new( 'direct assignment of AASM column has been disabled (see AASM configuration for this class)'
'direct assignment of AASM column has been disabled (see AASM configuration for this class)' )
) else
else super(state_name)
super(state_name)
end
end end
end end
end end
def should_not_define_method(klass)
(klass.methods.include?(:abstract_class) &&
klass.abstract_class) ||
(klass.superclass.methods.include?(:abstract_class) &&
klass.superclass.abstract_class)
end
# This method is both a getter and a setter # This method is both a getter and a setter
def attribute_name(column_name=nil) def attribute_name(column_name=nil)
if column_name if column_name

View file

@ -14,6 +14,9 @@ ActiveRecord::Migration.suppress_messages do
ActiveRecord::Migration.create_table "implemented_abstract_class_dsls", :force => true do |t| ActiveRecord::Migration.create_table "implemented_abstract_class_dsls", :force => true do |t|
t.string "status" t.string "status"
end end
ActiveRecord::Migration.create_table "users", :force => true do |t|
t.string "status"
end
ActiveRecord::Migration.create_table "complex_active_record_examples", :force => true do |t| ActiveRecord::Migration.create_table "complex_active_record_examples", :force => true do |t|
t.string "left" t.string "left"

View file

@ -0,0 +1,23 @@
class User < ActiveRecord::Base
self.abstract_class = true
self.table_name = 'users'
include AASM
aasm column: 'status' do
state :inactive, initial: true
state :active
event :activate do
transitions from: :inactive, to: :active
end
event :deactivate do
transitions from: :active, to: :inactive
end
end
end
class Person < User
end

View file

@ -0,0 +1,27 @@
require 'spec_helper'
if defined?(ActiveRecord)
require 'models/active_record/person'
load_schema
describe 'Abstract subclassing' do
it 'should have the parent states' do
Person.aasm.states.each do |state|
expect(User.aasm.states).to include(state)
end
expect(Person.aasm.states).to eq(User.aasm.states)
end
it 'should have the same events as its parent' do
expect(User.aasm.events).to eq(Person.aasm.events)
end
it 'should not break aasm methods when super class is abstract_class' do
person = Person.new
person.status = 'active'
person.deactivate!
expect(person.aasm.current_state).to eq(:inactive)
end
end
end