Skip to content

Commit

Permalink
Extract SearchBarPresenter
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Aug 27, 2017
1 parent 1f93ec4 commit 66ab7a6
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 3 deletions.
10 changes: 9 additions & 1 deletion app/helpers/blacklight/blacklight_helper_behavior.rb
Expand Up @@ -65,7 +65,11 @@ def extra_body_classes
# Render the search navbar
# @return [String]
def render_search_bar
render :partial => 'catalog/search_form'
search_bar_presenter.render
end

def search_bar_presenter
@search_bar ||= search_bar_presenter_class.new(controller, blacklight_config)
end

##
Expand Down Expand Up @@ -263,6 +267,10 @@ def index_presenter_class(_document)
blacklight_config.index.document_presenter_class
end

def search_bar_presenter_class
blacklight_config.index.search_bar_presenter_class
end

##
# Open Search discovery tag for HTML <head> links
def opensearch_description_tag title, href
Expand Down
1 change: 1 addition & 0 deletions app/helpers/blacklight/catalog_helper_behavior.rb
Expand Up @@ -196,6 +196,7 @@ def should_autofocus_on_search_box?
action_name == "index" &&
!has_search_parameters?
end
deprecation_deprecate should_autofocus_on_search_box?: "use SearchBarPresenter#autofocus?"

##
# Does the document have a thumbnail to render?
Expand Down
1 change: 1 addition & 0 deletions app/helpers/blacklight/suggest_helper_behavior.rb
Expand Up @@ -7,5 +7,6 @@ def autocomplete_enabled?
blacklight_config.autocomplete_enabled.present? &&
blacklight_config.autocomplete_path.present?
end
deprecation_deprecate autocomplete_enabled?: "use SearchBarPresenter#autocomplete_enabled?"
end
end
37 changes: 37 additions & 0 deletions app/presenters/blacklight/search_bar_presenter.rb
@@ -0,0 +1,37 @@
module Blacklight
class SearchBarPresenter
attr_reader :configuration, :view_context, :controller

# Set the partial this presenter draws
class_attribute :partial
self.partial = 'catalog/search_form'

def initialize(controller, configuration = view_context.blacklight_config)
@controller = controller
@view_context = controller.view_context
@configuration = configuration
end

def render
view_context.render partial, presenter: self
end

##
# @return [Boolean] should autocomplete be enabled in the UI
def autocomplete_enabled?
configuration.autocomplete_enabled.present? &&
configuration.autocomplete_path.present?
end

##
# If no search parameters have been given, we should
# auto-focus the user's cursor into the searchbox
#
# @return [Boolean]
def autofocus?
controller.is_a?(Blacklight::Catalog) &&
controller.action_name == "index" &&
!controller.has_search_parameters?
end
end
end
2 changes: 1 addition & 1 deletion app/views/catalog/_search_form.html.erb
Expand Up @@ -15,7 +15,7 @@
<% end %>

<label for="q" class="sr-only"><%= t('blacklight.search.form.search.label') %></label>
<%= text_field_tag :q, params[:q], placeholder: t('blacklight.search.form.search.placeholder'), class: "search_q q form-control", id: "q", autofocus: should_autofocus_on_search_box?, data: { autocomplete_enabled: autocomplete_enabled?, autocomplete_path: search_action_path(action: :suggest) } %>
<%= text_field_tag :q, params[:q], placeholder: t('blacklight.search.form.search.placeholder'), class: "search_q q form-control", id: "q", autofocus: presenter.autofocus?, data: { autocomplete_enabled: presenter.autocomplete_enabled?, autocomplete_path: search_action_path(action: :suggest) } %>

<span class="input-group-btn">
<button type="submit" class="btn btn-primary search-btn" id="search">
Expand Down
4 changes: 4 additions & 0 deletions lib/blacklight/configuration/view_config.rb
@@ -1,6 +1,10 @@
# frozen_string_literal: true
class Blacklight::Configuration
class ViewConfig < Blacklight::OpenStructWithHashAccess
def search_bar_presenter_class
super || Blacklight::SearchBarPresenter
end

class Show < ViewConfig
def document_presenter_class
super || Blacklight::ShowPresenter
Expand Down
4 changes: 3 additions & 1 deletion spec/helpers/blacklight/suggest_helper_behavior_spec.rb
Expand Up @@ -3,7 +3,9 @@
RSpec.describe Blacklight::SuggestHelperBehavior do
before do
allow(helper).to receive(:blacklight_config).and_return(blacklight_config)
expect(Deprecation).to receive(:warn)
end

describe '#autocomplete_enabled?' do
describe 'with autocomplete config' do
let(:blacklight_config) do
Expand Down Expand Up @@ -38,4 +40,4 @@
end
end
end
end
end
3 changes: 3 additions & 0 deletions spec/helpers/catalog_helper_spec.rb
Expand Up @@ -174,6 +174,9 @@ def render_grouped_response?
end

describe "should_autofocus_on_search_box?" do
before do
expect(Deprecation).to receive(:warn)
end
it "is focused if we're on a catalog-like index page without query or facet parameters" do
allow(helper).to receive_messages(controller: CatalogController.new, action_name: "index", has_search_parameters?: false)
expect(helper.should_autofocus_on_search_box?).to be true
Expand Down
72 changes: 72 additions & 0 deletions spec/presenters/blacklight/search_bar_presenter_spec.rb
@@ -0,0 +1,72 @@
# frozen_string_literal: true
require 'spec_helper'

RSpec.describe Blacklight::SearchBarPresenter do
let(:controller) { CatalogController.new }
let(:presenter) { described_class.new(controller, blacklight_config) }
let(:blacklight_config) { Blacklight::Configuration.new }

describe '#autocomplete_enabled?' do
subject { presenter.autocomplete_enabled? }

describe 'with autocomplete config' do
let(:blacklight_config) do
Blacklight::Configuration.new.configure do |config|
config.autocomplete_enabled = true
config.autocomplete_path = 'suggest'
end
end
it { is_expected.to be true }
end

describe 'without disabled config' do
let(:blacklight_config) do
Blacklight::Configuration.new.configure do |config|
config.autocomplete_enabled = false
config.autocomplete_path = 'suggest'
end
end
it { is_expected.to be false }
end

describe 'without path config' do
let(:blacklight_config) do
Blacklight::Configuration.new.configure do |config|
config.autocomplete_enabled = true
end
end

it { is_expected.to be false }
end
end

describe "#autofocus?" do
subject { presenter.autofocus? }
context "on a catalog-like index page without query or facet parameters" do
before do
allow(controller).to receive(:action_name).and_return('index')
allow(controller).to receive(:has_search_parameters?).and_return(false)
end
it { is_expected.to be true }
end

context "when not the catalog controller" do
let(:controller) { ApplicationController.new }
it { is_expected.to be false }
end

context "when on the catalog controller show page" do
before do
allow(controller).to receive(:action_name).and_return('show')
end
it { is_expected.to be false }
end

context "when search parameters are provided" do
before do
allow(controller).to receive(:has_search_parameters?).and_return(true)
end
it { is_expected.to be false }
end
end
end

0 comments on commit 66ab7a6

Please sign in to comment.