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

Search Bar Display Logic #730

Merged
merged 4 commits into from Jan 17, 2017
Merged
Changes from 3 commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -13,6 +13,7 @@ def index
resources: resources,
search_term: search_term,
page: page,
show_search_bar: show_search_bar?
}
end

@@ -122,5 +123,19 @@ def translate_with_resource(key)
resource: resource_resolver.resource_title,
)
end

def show_search_bar?
show_search_bar = false
dashboard.attribute_types_for(
dashboard.collection_attributes
).each do |name, attribute|
if attribute.searchable?
show_search_bar = true
break
end
end

show_search_bar
end

This comment has been minimized.

Copy link
@BenMorganIO

BenMorganIO Jan 13, 2017

Collaborator

Refactor suggestion:

# Checks if any of the dashboard's collection attributes are searchable. If none
# of them are, then we know that there isn't any need to load the search bar. In
# which case, we hide it.
#
# @return [Boolean]
def show_search_bar?
  dashboard.attribute_types_for(
    dashboard.collection_attributes
  ).any? do |_name, attribute|
    attribute.searchable?
  end
end

This comment has been minimized.

Copy link
@akestner

akestner Jan 13, 2017

Author Contributor

Nice, I'll update.

end
end
@@ -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
%>
@@ -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">
@@ -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
@@ -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
@@ -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

@@ -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
@@ -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
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.