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

Add Automatic Negative Scopes #795

Open
wants to merge 3 commits 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
1 change: 1 addition & 0 deletions lib/aasm/persistence/active_record_persistence.rb
Expand Up @@ -45,6 +45,7 @@ def aasm_create_scope(state_machine_name, scope_name)
conditions = { aasm(state_machine_name).attribute_name => scope_name.to_s }
class_eval do
scope scope_name, lambda { where(table_name => conditions) }
scope "not_#{scope_name}", lambda { where.not(table_name => conditions) }
end
else
conditions = {
Expand Down
23 changes: 22 additions & 1 deletion spec/unit/persistence/active_record_persistence_spec.rb
Expand Up @@ -324,6 +324,13 @@
expect(SimpleNewDsl.unknown_scope.is_a?(ActiveRecord::Relation)).to be_truthy
expect(SimpleNewDsl.another_unknown_scope.is_a?(ActiveRecord::Relation)).to be_truthy
end
it "should add negative scopes for each state" do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it "should add negative scopes for each state" do
it "adds negative scopes for each state" do

expect(SimpleNewDsl).to respond_to(:not_unknown_scope)
expect(SimpleNewDsl).to respond_to(:not_another_unknown_scope)

expect(SimpleNewDsl.not_unknown_scope.is_a?(ActiveRecord::Relation)).to be_truthy
expect(SimpleNewDsl.not_another_unknown_scope.is_a?(ActiveRecord::Relation)).to be_truthy
end
end

context "Already respond_to? the scope name" do
Expand All @@ -346,11 +353,21 @@
expect(ImplementedAbstractClassDsl.unknown_scope.is_a?(ActiveRecord::Relation)).to be_truthy
expect(ImplementedAbstractClassDsl.another_unknown_scope.is_a?(ActiveRecord::Relation)).to be_truthy
end
it "should add negative scopes without the table_name" do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it "should add negative scopes without the table_name" do
it "adds negative scopes without the table_name" do

expect(ImplementedAbstractClassDsl).to respond_to(:not_unknown_scope)
expect(ImplementedAbstractClassDsl).to respond_to(:not_another_unknown_scope)

expect(ImplementedAbstractClassDsl.not_unknown_scope.is_a?(ActiveRecord::Relation)).to be_truthy
expect(ImplementedAbstractClassDsl.not_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)
aggregate_failures do
expect(NoScope).not_to respond_to(:pending)
expect(NoScope).not_to respond_to(:not_pending)
end
end

context "result of scope" do
Expand All @@ -364,6 +381,10 @@
it "created scope works as where(name: :scope_name)" do
expect(SimpleNewDsl.unknown_scope).to contain_exactly(dsl2)
end

it "created not scope works as where.not(name: :scope_name)" do
expect(SimpleNewDsl.not_unknown_scope).to contain_exactly(dsl1)
end
end
end # scopes

Expand Down