Skip to content

Commit

Permalink
Fix search bar hiding even w/searchable attributes (thoughtbot#892)
Browse files Browse the repository at this point in the history
* Also unties search scope from representation of collection
  • Loading branch information
pustomytnyk authored and nickcharlton committed Apr 27, 2018
1 parent ead3347 commit f94c260
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/controllers/administrate/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def translate_with_resource(key)

def show_search_bar?
dashboard.attribute_types_for(
dashboard.collection_attributes,
dashboard.all_attributes,
).any? { |_name, attribute| attribute.searchable? }
end

Expand Down
6 changes: 5 additions & 1 deletion lib/administrate/base_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def attribute_types_for(attribute_names)
end
end

def all_attributes
attribute_types.keys
end

def form_attributes
self.class::FORM_ATTRIBUTES
end
Expand Down Expand Up @@ -61,7 +65,7 @@ def association_includes
field = self.class::ATTRIBUTE_TYPES[key]

next key if association_classes.include?(field)
key if association_classes.include?(field.try :deferred_class)
key if association_classes.include?(field.try(:deferred_class))
end.compact
end

Expand Down
50 changes: 46 additions & 4 deletions spec/features/index_page_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require "rails_helper"

search_input_selector = ".search__input"

describe "customer index page" do
it "displays customers' name and email" do
customer = create(:customer)
Expand Down Expand Up @@ -78,6 +80,11 @@
end

describe "sorting" do
def expect_to_appear_in_order(*elements)
positions = elements.map { |e| page.body.index(e) }
expect(positions).to eq(positions.sort)
end

it "allows sorting by columns" do
create(:customer, name: "unique name two")
create(:customer, name: "unique name one")
Expand Down Expand Up @@ -114,12 +121,47 @@
visit admin_customers_path(search: query)
click_on "Name"

expect(find(".search__input").value).to eq(query)
expect(find(search_input_selector).value).to eq(query)
end
end
end

describe "search input" do
context "when resource has searchable fields" do
let(:index_with_searchable_fields) { admin_log_entries_path }

context "but none of them are displayed" do
before do
allow_any_instance_of(LogEntryDashboard).
to receive(:collection_attributes) { [] }
visit(index_with_searchable_fields)
end

it "is shown" do
expect(page).to have_selector(search_input_selector)
end
end

context "and some of them are displayed" do
before do
visit(index_with_searchable_fields)
end

it "is shown" do
expect(page).to have_selector(search_input_selector)
end
end
end

def expect_to_appear_in_order(*elements)
positions = elements.map { |e| page.body.index(e) }
expect(positions).to eq(positions.sort)
context "when resource does not have searchable fields" do
let(:index_without_searchable_fields) { admin_line_items_path }

before do
visit(index_without_searchable_fields)
end

it "is hidden" do
expect(page).to_not have_selector(search_input_selector)
end
end
end

0 comments on commit f94c260

Please sign in to comment.