Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
131 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |