forked from activeadmin/activeadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes activeadmin#77. Fix performance issue with scope :all.
Scope :all does not call :all on the relation anymore but returns the ActiveRecord class instead. Moved the logic scoping the scope into a helper called ScopeChain. I wanted to implement this method on the Scope class itself but that runs the scoping block in the Scope instance context instead of the ResourceController one. This method is both used in the ResourceController and the ViewHelper Scopes. @gregbell: Could we share this method without creating a new helper?
- Loading branch information
Showing
7 changed files
with
96 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module ActiveAdmin | ||
module ScopeChain | ||
# Scope an ActiveRecord::Relation chain | ||
# | ||
# Example: | ||
# scope_chain(Scope.new(:published), Article) | ||
# # => Article.published | ||
# | ||
# @param scope The <ActiveAdmin::Scope> we want to scope on | ||
# @param chain The ActiveRecord::Relation chain or ActiveRecord::Base class to scope | ||
# @return <ActiveRecord::Relation or ActiveRecord::Base> The scoped relation chain | ||
# | ||
def scope_chain(scope, chain) | ||
if scope.scope_method | ||
chain.send(scope.scope_method) | ||
elsif scope.scope_block | ||
instance_exec chain, &scope.scope_block | ||
else | ||
chain | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require 'spec_helper' | ||
|
||
describe ActiveAdmin::ScopeChain do | ||
|
||
include ActiveAdmin::ScopeChain | ||
|
||
describe "#scope_chain" do | ||
let(:relation) { mock } | ||
|
||
context "when Scope has a scope method" do | ||
let(:scope) { ActiveAdmin::Scope.new :published } | ||
|
||
it "should call the method on the relation and return it" do | ||
relation.should_receive(:published).and_return(:scoped_relation) | ||
scope_chain(scope, relation).should == :scoped_relation | ||
end | ||
end | ||
|
||
context "when Scope has the scope method method ':all'" do | ||
let(:scope) { ActiveAdmin::Scope.new :all } | ||
|
||
it "should return the relation" do | ||
scope_chain(scope, relation).should == relation | ||
end | ||
end | ||
|
||
context "when Scope has a name and a scope block" do | ||
let(:scope) { ActiveAdmin::Scope.new("My Scope"){|s| :scoped_relation } } | ||
|
||
it "should instance_exec the block and return it" do | ||
scope_chain(scope, relation).should == :scoped_relation | ||
end | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters