Skip to content

Commit

Permalink
Allow custom scope for dashboard resource (thoughtbot#910)
Browse files Browse the repository at this point in the history
* Allow custom scope for dashboard resource

* HoundCI fixes
  • Loading branch information
bobf authored and nickcharlton committed Jun 20, 2017
1 parent c96c6ea commit c660ecd
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 13 deletions.
5 changes: 3 additions & 2 deletions app/controllers/administrate/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def requested_resource
end

def find_resource(param)
resource_class.find(param)
resource_scope.find(param)
end

def resource_includes
Expand All @@ -117,7 +117,8 @@ def resource_params
permit(dashboard.permitted_attributes)
end

delegate :resource_class, :resource_name, :namespace, to: :resource_resolver
delegate :resource_class, :resource_name, :namespace, :resource_scope,
to: :resource_resolver
helper_method :namespace
helper_method :resource_name

Expand Down
4 changes: 4 additions & 0 deletions lib/administrate/resource_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def resource_class
Object.const_get(resource_class_name)
end

def resource_scope
dashboard_class.new.try(:resource_scope) || resource_class.default_scoped
end

def resource_name
model_path_parts.map(&:underscore).join("__").to_sym
end
Expand Down
6 changes: 3 additions & 3 deletions lib/administrate/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ def initialize(resolver, term)

def run
if @term.blank?
resource_class.all
resource_scope.all
else
resource_class.where(query, *search_terms)
resource_scope.where(query, *search_terms)
end
end

private

delegate :resource_class, to: :resolver
delegate :resource_class, :resource_scope, to: :resolver

def query
search_attributes.map do |attr|
Expand Down
32 changes: 32 additions & 0 deletions spec/lib/administrate/resource_resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,38 @@ module Library; class Book; end; end
end
end

describe "#resource_scope" do
it "defaults to model default scope when no override defined" do
begin
class Post
def self.default_scoped
"default scope"
end
end
class PostDashboard; end
resolver = Administrate::ResourceResolver.new("admin/posts")
expect(resolver.resource_scope).to eq("default scope")
ensure
remove_constants :PostDashboard, :Post
end
end

it "uses defined scope when present" do
begin
class Post; end
class PostDashboard
def resource_scope
"a resource scope"
end
end
resolver = Administrate::ResourceResolver.new("admin/posts")
expect(resolver.resource_scope).to eq("a resource scope")
ensure
remove_constants :PostDashboard, :Post
end
end
end

describe "#resource_title" do
it "handles global-namepsace models" do
resolver = Administrate::ResourceResolver.new("admin/users")
Expand Down
45 changes: 37 additions & 8 deletions spec/lib/administrate/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ class MockDashboard
it "returns all records when no search term" do
begin
class User; end
resolver = double(resource_class: User, dashboard_class: MockDashboard)
scope = double(all: nil)
resolver = double(resource_class: User, dashboard_class: MockDashboard,
resource_scope: scope)
search = Administrate::Search.new(resolver, nil)
expect(User).to receive(:all)
expect(scope).to receive(:all)

search.run
ensure
Expand All @@ -31,9 +33,11 @@ class User; end
it "returns all records when search is empty" do
begin
class User; end
resolver = double(resource_class: User, dashboard_class: MockDashboard)
scope = double(all: nil)
resolver = double(resource_class: User, dashboard_class: MockDashboard,
resource_scope: scope)
search = Administrate::Search.new(resolver, " ")
expect(User).to receive(:all)
expect(scope).to receive(:all)

search.run
ensure
Expand All @@ -44,15 +48,17 @@ class User; end
it "searches using lower() + LIKE for all searchable fields" do
begin
class User < ActiveRecord::Base; end
resolver = double(resource_class: User, dashboard_class: MockDashboard)
scope = double(where: nil)
resolver = double(resource_class: User, dashboard_class: MockDashboard,
resource_scope: scope)
search = Administrate::Search.new(resolver, "test")
expected_query = [
"lower(\"users\".\"name\") LIKE ?"\
" OR lower(\"users\".\"email\") LIKE ?",
"%test%",
"%test%",
]
expect(User).to receive(:where).with(*expected_query)
expect(scope).to receive(:where).with(*expected_query)

search.run
ensure
Expand All @@ -63,20 +69,43 @@ class User < ActiveRecord::Base; end
it "converts search term lower case for latin and cyrillic strings" do
begin
class User < ActiveRecord::Base; end
resolver = double(resource_class: User, dashboard_class: MockDashboard)
scope = double(where: nil)
resolver = double(resource_class: User, dashboard_class: MockDashboard,
resource_scope: scope)
search = Administrate::Search.new(resolver, "Тест Test")
expected_query = [
"lower(\"users\".\"name\") LIKE ?"\
" OR lower(\"users\".\"email\") LIKE ?",
"%тест test%",
"%тест test%",
]
expect(User).to receive(:where).with(*expected_query)
expect(scope).to receive(:where).with(*expected_query)

search.run
ensure
remove_constants :User
end
end

it "respects Dashboard#resource_scope when defined" do
begin
class MockScope
end
class User < ActiveRecord::Base
scope :my_scope, -> { MockScope }
end
class UserDashboard < Administrate::BaseDashboard
def resource_scope
User.my_scope
end
end
resolver = Administrate::ResourceResolver.new("admin/users")
search = Administrate::Search.new(resolver, nil)
expect(MockScope).to receive(:all)
search.run
ensure
remove_constants :User, :UserDashboard, :MockScope
end
end
end
end

0 comments on commit c660ecd

Please sign in to comment.