Skip to content

Commit

Permalink
Search Bar Display Logic (#730)
Browse files Browse the repository at this point in the history
* hides search bar when no attributes are searchable

* adds tests for search bar display logic
* extracts capture_view_locals method to ControllerHelpers

* no need for page argument to show_search_bar? method

* streamlines show_search_bar? method
  • Loading branch information
akestner authored and BenMorganIO committed Jan 17, 2017
1 parent 4ecced1 commit b28f125
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 14 deletions.
7 changes: 7 additions & 0 deletions app/controllers/administrate/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def index
resources: resources,
search_term: search_term,
page: page,
show_search_bar: show_search_bar?
}
end

Expand Down Expand Up @@ -122,5 +123,11 @@ def translate_with_resource(key)
resource: resource_resolver.resource_title,
)
end

def show_search_bar?
dashboard.attribute_types_for(
dashboard.collection_attributes
).any? { |_name, attribute| attribute.searchable? }
end
end
end
6 changes: 5 additions & 1 deletion app/views/administrate/application/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ It renders the `_table` partial to display details about the resources.
By default, these resources are passed to the table partial to be displayed.
- `search_term`:
A string containing the term the user has searched for, if any.
- `show_search_bar`:
A boolean that determines if the search bar should be shown.
[1]: http://www.rubydoc.info/gems/administrate/Administrate/Page/Collection
%>
Expand All @@ -26,7 +28,9 @@ It renders the `_table` partial to display details about the resources.
<% end %>
<% content_for(:search) do %>
<%= render "search", search_term: search_term %>
<% if show_search_bar %>
<%= render "search", search_term: search_term %>
<% end %>
<% end %>

<header class="header">
Expand Down
20 changes: 7 additions & 13 deletions spec/controllers/admin/customers_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@

expect(locals[:page]).to be_instance_of(Administrate::Page::Collection)
end

it "shows the search bar" do
customer = create(:customer)

locals = capture_view_locals { get :index }
expect(locals[:show_search_bar]).to be_truthy
end
end

describe "GET show" do
Expand Down Expand Up @@ -163,17 +170,4 @@
expect(response).to redirect_to(admin_customers_url)
end
end

def capture_view_locals
allow(@controller).to receive(:render)
yield

locals = nil
expect(@controller).to have_received(:render).at_least(1).times do |*args|
args.each do |arg|
locals ||= arg.try(:fetch, :locals, nil)
end
end
locals
end
end
13 changes: 13 additions & 0 deletions spec/controllers/admin/line_item_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require "rails_helper"

describe Admin::LineItemsController, type: :controller do
describe "GET index" do
it "hides the search bar" do
line_item = create(:line_item)

locals = capture_view_locals { get :index }
expect(locals[:show_search_bar]).to be_falsey
end
end
end

1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module Features
RSpec.configure do |config|
config.include Features, type: :feature
config.include DashboardHelpers
config.include ControllerHelpers
config.infer_base_class_for_anonymous_controllers = false
config.infer_spec_type_from_file_location!
config.use_transactional_fixtures = false
Expand Down
14 changes: 14 additions & 0 deletions spec/support/controller_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module ControllerHelpers
def capture_view_locals
allow(@controller).to receive(:render)
yield

locals = nil
expect(@controller).to have_received(:render).at_least(1).times do |*args|
args.each do |arg|
locals ||= arg.try(:fetch, :locals, nil)
end
end
locals
end
end

0 comments on commit b28f125

Please sign in to comment.