Skip to content

Commit

Permalink
Use appropriate parameter class for the rails version
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Jan 4, 2016
1 parent f3df8fe commit 3b4898b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
5 changes: 2 additions & 3 deletions app/helpers/blacklight/url_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,8 @@ def controller_tracking_method

# create link to query (e.g. spelling suggestion)
def link_to_query(query)
p = params.except(:page, :action)
p[:q]=query
p.permit!
p = search_state.to_h.except(:page, :action)
p[:q] = query
link_url = search_action_path(p.to_h)
link_to(query, link_url)
end
Expand Down
31 changes: 20 additions & 11 deletions spec/helpers/url_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
end
end

let(:parameter_class) do
Rails.version >= '5.0.0' ? ActionController::Parameters : HashWithIndifferentAccess
end

before do
allow(helper).to receive(:search_action_path) do |*args|
search_catalog_url *args
Expand All @@ -26,7 +30,7 @@
before do
allow(helper).to receive_messages(controller: controller_class)
allow(helper).to receive_messages(controller_name: controller_class.controller_name)
allow(helper).to receive_messages(params: ActionController::Parameters.new)
allow(helper).to receive_messages(params: parameter_class.new)
end

it "should be a polymorphic routing-ready object" do
Expand All @@ -48,12 +52,14 @@

context "within an alternative catalog controller" do
let(:controller_class) { ::AlternateController.new }

before do
helper.blacklight_config.show.route = { controller: :current }
allow(helper).to receive(:params).and_return(ActionController::Parameters.new controller: 'alternate')
allow(helper).to receive(:params).and_return(parameter_class.new controller: 'alternate')
end

it "should support the :current controller configuration" do
expect(helper.url_for_document(doc)).to eq({controller: 'alternate', action: :show, id: doc})
expect(helper.url_for_document(doc)).to eq(controller: 'alternate', action: :show, id: doc)
end
end

Expand Down Expand Up @@ -136,30 +142,33 @@
end

describe "link_to_query" do
it "should build a link tag to catalog using query string (no other params)" do
it "builds a link tag to catalog using query string (no other params)" do
query = "brilliant"
allow(helper).to receive_messages(params: ActionController::Parameters.new)
allow(helper).to receive_messages(params: parameter_class.new)
tag = helper.link_to_query(query)
expect(tag).to match /q=#{query}/
expect(tag).to match />#{query}<\/a>/
end
it "should build a link tag to catalog using query string and other existing params" do

it "builds a link tag to catalog using query string and other existing params" do
query = "wonderful"
allow(helper).to receive_messages(params: ActionController::Parameters.new(qt: "title_search", per_page: "50"))
allow(helper).to receive_messages(params: parameter_class.new(qt: "title_search", per_page: "50"))
tag = helper.link_to_query(query)
expect(tag).to match /qt=title_search/
expect(tag).to match /per_page=50/
end
it "should ignore existing :page param" do

it "ignores existing :page param" do
query = "yes"
allow(helper).to receive_messages(params: ActionController::Parameters.new(page: "2", qt: "author_search"))
allow(helper).to receive_messages(params: parameter_class.new(page: "2", qt: "author_search"))
tag = helper.link_to_query(query)
expect(tag).to match /qt=author_search/
expect(tag).to_not match /page/
end
it "should be html_safe" do

it "is html_safe" do
query = "brilliant"
allow(helper).to receive_messages(params: ActionController::Parameters.new(page: "2", qt: "author_search"))
allow(helper).to receive_messages(params: parameter_class.new(page: "2", qt: "author_search"))
tag = helper.link_to_query(query)
expect(tag).to be_html_safe
end
Expand Down
5 changes: 4 additions & 1 deletion spec/presenters/document_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

subject { presenter }
let(:presenter) { Blacklight::DocumentPresenter.new(document, request_context, config) }
let(:params) { ActionController::Parameters.new }
let(:parameter_class) do
Rails.version >= '5.0.0' ? ActionController::Parameters : HashWithIndifferentAccess
end
let(:params) { parameter_class.new }
let(:search_state) { Blacklight::SearchState.new(params, config) }

let(:document) do
Expand Down

0 comments on commit 3b4898b

Please sign in to comment.