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
# and attribute is directly assigned though
aasm_name = @name
if !(klass.superclass.methods.include?(:abstract_class) &&
klass.superclass.abstract_class)
klass.send :define_method, "#{@state_machine.config.column}=", ->(state_name) do
if self.class.aasm(:"#{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)'
)
else
super(state_name)
end
return true if should_not_define_method(klass)
klass.send :define_method, "#{@state_machine.config.column}=", ->(state_name) do
if self.class.aasm(:"#{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)'
)
else
super(state_name)
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
def attribute_name(column_name=nil)
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|
t.string "status"
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|
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