Skip to content

Commit

Permalink
Enable AASM scopes to be defined on abstract classes (#560)
Browse files Browse the repository at this point in the history
Currently, if aasm is called on an abstract class, the scopes are not
properly defined because it infers the table_name at time of creation.
This change pushes the inference of table_name to when the scope is
called which enables scopes to be dynamically defined in descendant
models.
  • Loading branch information
RickCSong authored and Anil Kumar Maurya committed Aug 10, 2018
1 parent a4aaa51 commit d6f992b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# CHANGELOG

## unreleased
* Enable AASM scopes to be defined on abstract classes.

## 5.0.0

* Chore(invokers): Refactor callback invokers, add class-callbacks support [#541](https://github.com/aasm/aasm/pull/541), thanks to [pandomic](https://github.com/pandomic)
* Add docker setup to readme
* Add support for Nobrainer (RethinkDB) [#522](https://github.com/aasm/aasm/pull/522), thanks to [zedtux](https://github.com/zedtux)
* Patch `allow_event` to accept event with custom arguments [#419](https://github.com/aasm/aasm/pull/419), thanks to [czhc](https://github.com/czhc)


## 4.12.3

* Add to AASM fire(event) and fire!(event) methods [#494](https://github.com/aasm/aasm/pull/494), thanks to [slayer](https://github.com/slayer)
Expand Down
9 changes: 5 additions & 4 deletions lib/aasm/persistence/active_record_persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ def self.included(base)

module ClassMethods
def aasm_create_scope(state_machine_name, scope_name)
conditions = {
table_name => { aasm(state_machine_name).attribute_name => scope_name.to_s }
}
if ActiveRecord::VERSION::MAJOR >= 3
conditions = { aasm(state_machine_name).attribute_name => scope_name.to_s }
class_eval do
scope scope_name, lambda { where(conditions) }
scope scope_name, lambda { where(table_name => conditions) }
end
else
conditions = {
table_name => { aasm(state_machine_name).attribute_name => scope_name.to_s }
}
class_eval do
named_scope scope_name, :conditions => conditions
end
Expand Down
3 changes: 3 additions & 0 deletions spec/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
ActiveRecord::Migration.create_table "multiple_simple_new_dsls", :force => true do |t|
t.string "status"
end
ActiveRecord::Migration.create_table "implemented_abstract_class_dsls", :force => true do |t|
t.string "status"
end

ActiveRecord::Migration.create_table "complex_active_record_examples", :force => true do |t|
t.string "left"
Expand Down
15 changes: 15 additions & 0 deletions spec/models/active_record/simple_new_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,18 @@ class MultipleSimpleNewDsl < ActiveRecord::Base
state :new
end
end

class AbstractClassDsl < ActiveRecord::Base
include AASM

self.abstract_class = true

aasm :column => :status
aasm do
state :unknown_scope, :another_unknown_scope
state :new
end
end

class ImplementedAbstractClassDsl < AbstractClassDsl
end
16 changes: 16 additions & 0 deletions spec/unit/persistence/active_record_persistence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,22 @@
end
end

# Scopes on abstract classes didn't work until Rails 5.
#
# Reference:
# https://github.com/rails/rails/issues/10658
if ActiveRecord::VERSION::MAJOR >= 5
context "For a descendant of an abstract model" do
it "should add the scope without the table_name" do
expect(ImplementedAbstractClassDsl).to respond_to(:unknown_scope)
expect(ImplementedAbstractClassDsl).to respond_to(:another_unknown_scope)

expect(ImplementedAbstractClassDsl.unknown_scope.is_a?(ActiveRecord::Relation)).to be_truthy
expect(ImplementedAbstractClassDsl.another_unknown_scope.is_a?(ActiveRecord::Relation)).to be_truthy
end
end
end

it "does not create scopes if requested" do
expect(NoScope).not_to respond_to(:pending)
end
Expand Down

0 comments on commit d6f992b

Please sign in to comment.