Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #822 - Namespaced scopes perform incorrect queries #823

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/aasm/persistence/active_record_persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ def self.included(base)
end

module ClassMethods
def aasm_create_scope(state_machine_name, scope_name)
def aasm_create_scope(state_machine_name, scope_name, state_name)
if ActiveRecord::VERSION::MAJOR >= 3
conditions = { aasm(state_machine_name).attribute_name => scope_name.to_s }
conditions = { aasm(state_machine_name).attribute_name => state_name.to_s }
class_eval do
scope scope_name, lambda { where(table_name => conditions) }
end
else
conditions = {
table_name => { aasm(state_machine_name).attribute_name => scope_name.to_s }
table_name => { aasm(state_machine_name).attribute_name => state_name.to_s }
}
class_eval do
named_scope scope_name, :conditions => conditions
Expand Down
16 changes: 5 additions & 11 deletions lib/aasm/persistence/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ class Base
# make sure to create a (named) scope for each state
def state_with_scope(*args)
names = state_without_scope(*args)
names.each do |name|
create_scopes(name)
end
names.each { |name| create_scopes(name) }
end
alias_method :state_without_scope, :state
alias_method :state, :state_with_scope
Expand All @@ -72,17 +70,13 @@ def create_scope?(name)
@state_machine.config.create_scopes && !@klass.respond_to?(name) && @klass.respond_to?(:aasm_create_scope)
end

def create_scope(name)
@klass.aasm_create_scope(@name, name) if create_scope?(name)
def create_scope(scope_name, state_name)
@klass.aasm_create_scope(@name, scope_name, state_name) if create_scope?(scope_name)
end

def create_scopes(name)
if namespace?
# Create default scopes even when namespace? for backward compatibility
namepaced_name = "#{namespace}_#{name}"
create_scope(namepaced_name)
end
create_scope(name)
create_scope("#{namespace}_#{name}", name) if namespace?
create_scope(name, name) # Create default scopes even when namespace? for backward compatibility
end
end # Base

Expand Down
4 changes: 2 additions & 2 deletions lib/aasm/persistence/core_data_query_persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def self.included(base)
end

module ClassMethods
def aasm_create_scope(state_machine_name, scope_name)
scope(scope_name.to_sym, lambda { where(aasm(state_machine_name).attribute_name.to_sym).eq(scope_name.to_s) })
def aasm_create_scope(state_machine_name, scope_name, state_name)
scope(scope_name.to_sym, lambda { where(aasm(state_machine_name).attribute_name.to_sym).eq(state_name.to_s) })
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/aasm/persistence/mongoid_persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ def self.included(base)
end

module ClassMethods
def aasm_create_scope(state_machine_name, scope_name)
def aasm_create_scope(state_machine_name, scope_name, state_name)
scope_options = lambda {
send(
:where,
{ aasm(state_machine_name).attribute_name.to_sym => scope_name.to_s }
{ aasm(state_machine_name).attribute_name.to_sym => state_name.to_s }
)
}
send(:scope, scope_name, scope_options)
Expand Down
4 changes: 2 additions & 2 deletions lib/aasm/persistence/no_brainer_persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ def self.included(base)
end

module ClassMethods
def aasm_create_scope(state_machine_name, scope_name)
def aasm_create_scope(state_machine_name, scope_name, state_name)
scope_options = lambda {
where(aasm(state_machine_name).attribute_name.to_sym => scope_name.to_s)
where(aasm(state_machine_name).attribute_name.to_sym => state_name.to_s)
}
send(:scope, scope_name, scope_options)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@
end
end

context "when namespeced" do
context "when namespaced" do
it "add namespaced scopes" do
expect(MultipleNamespaced).to respond_to(:car_unsold)
expect(MultipleNamespaced).to respond_to(:car_sold)
Expand All @@ -347,6 +347,10 @@
expect(MultipleNamespaced.unsold.is_a?(ActiveRecord::Relation)).to be_truthy
expect(MultipleNamespaced.sold.is_a?(ActiveRecord::Relation)).to be_truthy
end
it "creates identical queries" do
expect(MultipleNamespaced.car_unsold.all.to_sql == MultipleNamespaced.unsold.all.to_sql).to be_truthy
expect(MultipleNamespaced.car_sold.all.to_sql == MultipleNamespaced.sold.all.to_sql).to be_truthy
end
end
end # scopes

Expand Down