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

Create namespaced scopes #735

Merged
merged 3 commits into from Mar 25, 2021
Merged
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
15 changes: 13 additions & 2 deletions lib/aasm/persistence/base.rb
Expand Up @@ -59,7 +59,9 @@ class Base
# make sure to create a (named) scope for each state
def state_with_scope(*args)
names = state_without_scope(*args)
names.each { |name| create_scope(name) if create_scope?(name) }
names.each do |name|
create_scopes(name)
end
end
alias_method :state_without_scope, :state
alias_method :state, :state_with_scope
Expand All @@ -71,7 +73,16 @@ def create_scope?(name)
end

def create_scope(name)
@klass.aasm_create_scope(@name, name)
@klass.aasm_create_scope(@name, name) if create_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)
end
end # Base

Expand Down
15 changes: 4 additions & 11 deletions spec/database.rb
Expand Up @@ -5,17 +5,10 @@
end
end

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 "implemented_abstract_class_dsls", :force => true do |t|
t.string "status"
end
ActiveRecord::Migration.create_table "users", :force => true do |t|
t.string "status"
%w(simple_new_dsls multiple_simple_new_dsls implemented_abstract_class_dsls users multiple_namespaceds).each do |table_name|
ActiveRecord::Migration.create_table table_name, :force => true do |t|
t.string "status"
end
end

ActiveRecord::Migration.create_table "complex_active_record_examples", :force => true do |t|
Expand Down
16 changes: 16 additions & 0 deletions spec/models/active_record/namespaced.rb
@@ -0,0 +1,16 @@
class MultipleNamespaced < ActiveRecord::Base
include AASM

aasm(:status, namespace: :car) do
state :unsold, initial: true
state :sold

event :sell do
transitions from: :unsold, to: :sold
end

event :return do
transitions from: :sold, to: :unsold
end
end
end
17 changes: 17 additions & 0 deletions spec/unit/persistence/active_record_persistence_multiple_spec.rb
Expand Up @@ -331,6 +331,23 @@
expect(MultipleSimpleNewDsl.unknown_scope).to contain_exactly(dsl2)
end
end

context "when namespeced" do
it "add namespaced scopes" do
expect(MultipleNamespaced).to respond_to(:car_unsold)
expect(MultipleNamespaced).to respond_to(:car_sold)

expect(MultipleNamespaced.car_unsold.is_a?(ActiveRecord::Relation)).to be_truthy
expect(MultipleNamespaced.car_sold.is_a?(ActiveRecord::Relation)).to be_truthy
end
it "add unnamespaced scopes" do
expect(MultipleNamespaced).to respond_to(:unsold)
expect(MultipleNamespaced).to respond_to(:sold)

expect(MultipleNamespaced.unsold.is_a?(ActiveRecord::Relation)).to be_truthy
expect(MultipleNamespaced.sold.is_a?(ActiveRecord::Relation)).to be_truthy
end
end
end # scopes

describe "direct assignment" do
Expand Down