Skip to content

Commit

Permalink
Feed link tag helpers should accept a route_set
Browse files Browse the repository at this point in the history
For use in Rails engine gems.
  • Loading branch information
jcoyne committed Aug 16, 2016
1 parent 846317f commit 1fe22ff
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
20 changes: 14 additions & 6 deletions app/helpers/blacklight/catalog_helper_behavior.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# frozen_string_literal: true
module Blacklight::CatalogHelperBehavior

def rss_feed_link_tag
auto_discovery_link_tag(:rss, feed_link_url('rss'), title: t('blacklight.search.rss_feed'))
# @param [Hash] options
# @option options :route_set the route scope to use when constructing the link
def rss_feed_link_tag(options = {})
auto_discovery_link_tag(:rss, feed_link_url('rss', options), title: t('blacklight.search.rss_feed'))
end

def atom_feed_link_tag
auto_discovery_link_tag(:atom, feed_link_url('atom'), title: t('blacklight.search.atom_feed'))
# @param [Hash] options
# @option options :route_set the route scope to use when constructing the link
def atom_feed_link_tag(options = {})
auto_discovery_link_tag(:atom, feed_link_url('atom', options), title: t('blacklight.search.atom_feed'))
end

##
Expand Down Expand Up @@ -285,7 +289,11 @@ def render_search_to_page_title(params)

private

def feed_link_url(format)
url_for search_state.to_h.merge(format: format)
# @param [String] format
# @param [Hash] options
# @option options :route_set the route scope to use when constructing the link
def feed_link_url(format, options = {})
scope = options.delete(:route_set) || self
scope.url_for search_state.to_h.merge(format: format)
end
end
36 changes: 36 additions & 0 deletions spec/helpers/catalog_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,42 @@ def render_grouped_response?
end
end

describe "rss_feed_link_tag" do
context "when an alternate scope is passed in" do
let(:my_engine) { double("Engine") }
let(:query_params) { { controller: 'catalog', action: 'index' } }
let(:config) { Blacklight::Configuration.new }
let(:search_state) { Blacklight::SearchState.new(query_params, config) }

it "calls url_for on the engine scope" do
allow(helper).to receive(:search_state).and_return search_state
expect(my_engine).to receive(:url_for).and_return(url_for(query_params))
tag = helper.rss_feed_link_tag(route_set: my_engine)
expect(tag).to match /title="RSS for results"/
expect(tag).to match /rel="alternate"/
expect(tag).to match %r{type="application/rss\+xml"}
end
end
end

describe "atom_feed_link_tag" do
context "when an alternate scope is passed in" do
let(:my_engine) { double("Engine") }
let(:query_params) { { controller: 'catalog', action: 'index' } }
let(:config) { Blacklight::Configuration.new }
let(:search_state) { Blacklight::SearchState.new(query_params, config) }

it "calls url_for on the engine scope" do
allow(helper).to receive(:search_state).and_return search_state
expect(my_engine).to receive(:url_for).and_return(url_for(query_params))
tag = helper.atom_feed_link_tag(route_set: my_engine)
expect(tag).to match /title="Atom for results"/
expect(tag).to match /rel="alternate"/
expect(tag).to match %r{type="application/atom\+xml"}
end
end
end

describe "should_autofocus_on_search_box?" do
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)
Expand Down

0 comments on commit 1fe22ff

Please sign in to comment.