Skip to content

Commit

Permalink
More docs and more tests for helper
Browse files Browse the repository at this point in the history
  • Loading branch information
atz committed Jul 8, 2016
1 parent a094c8f commit d31923c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 23 deletions.
36 changes: 22 additions & 14 deletions app/helpers/sufia/sufia_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ def browser_supports_directory_upload?
user_agent.include? 'Chrome'
end

# A Blacklight helper_method
# @param [Hash] options from blacklight invocation of helper_method
# @return [Date]
def human_readable_date(options)
Date.parse(options[:value]).to_formatted_s(:standard)
end

def error_messages_for(object)
return '' unless object.try(:errors) && object.errors.full_messages.any?
content_tag(:div, class: 'alert alert-block alert-error validation-errors') do
Expand Down Expand Up @@ -104,10 +97,22 @@ def link_to_field(name, value, label = nil, facet_hash = {})
link_to(label, main_app.search_catalog_path(state))
end

## GROUP: helper_methods

# A Blacklight helper_method
# @param [Hash] options from blacklight invocation of helper_method
# @see #index_field_link params
# @return [Date]
def human_readable_date(options)
Date.parse(options[:value]).to_formatted_s(:standard)
end

# A Blacklight helper_method
# @param options [Hash] Blacklight sends :document, :field, :config, :value and whatever else was in options
# @option options [Hash] :config including {:field_name => "my_name"}
# @param options [Hash{Symbol=>Object}] Blacklight sends :document, :field, :config, :value and whatever else was in options
# @option options [Array{String}] :value
# @option text [Hash] :config including {:field_name => "my_name"}
# @option text [Hash] :document
# @option text [Array{String}] :value the strings you might otherwise have passed to this method singly
# @return [ActiveSupport::SafeBuffer] the html_safe link
def index_field_link(options)
raise ArgumentError unless options[:config] && options[:config][:field_name]
Expand All @@ -116,18 +121,21 @@ def index_field_link(options)
safe_join(values.map { |item| link_to_field(name, item, item) }, ", ".html_safe)
end

# *Sometimes* a Blacklight helper_method
# @param text [String,Hash] string to escape or a hash containing that string under the :value key.
# @param showLink [Boolean]
def iconify_auto_link(text, showLink = true)
# @param show_link [Boolean]
# @return [ActiveSupport::SafeBuffer]
def iconify_auto_link(text, show_link = true)
text = presenter(text[:document]).field_value(Array.wrap(text[:value]).first, text[:config]) if text.is_a? Hash
# this block is only executed when a link is inserted;
# if we pass text containing no links, it just returns text.
auto_link(html_escape(text)) do |value|
"<span class='glyphicon glyphicon-new-window'></span>#{('&nbsp;' + value) if showLink}"
"<span class='glyphicon glyphicon-new-window'></span>#{('&nbsp;' + value) if show_link}"
end
end

# @param [String,User,Hash] args if a hash, the user_key must be under :value
# *Sometimes* a Blacklight helper_method
# @param [String,User,Hash{Symbol=>Array}] args if a hash, the user_key must be under :value
# @return [ActiveSupport::SafeBuffer] the html_safe link
def link_to_profile(args)
user_or_key = args.is_a?(Hash) ? args[:value].first : args
Expand All @@ -145,7 +153,7 @@ def link_to_profile(args)
# A Blacklight helper_method
# @param [Hash] options from blacklight helper_method invocation. Maps rights URIs to links with labels.
# @return [ActiveSupport::SafeBuffer] rights statement links, html_safe
def rights_statement_links(options = {})
def rights_statement_links(options)
options[:value].map { |right| link_to RightsService.label(right), right }.to_sentence.html_safe
end

Expand Down
5 changes: 4 additions & 1 deletion spec/helpers/blacklight_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ def search_action_path(stuff)

context "description_tesim" do
let(:field_name) { 'description_tesim' }
it { pending 'need a different way to process description?'; is_expected.to eq '<span itemprop="description">This links to <a href="http://example.com/"><span class="glyphicon glyphicon-new-window"></span> http://example.com/</a> What about that?</span>' }
it do
pending 'need a different way to process description?'
is_expected.to eq '<span itemprop="description">This links to <a href="http://example.com/"><span class="glyphicon glyphicon-new-window"></span> http://example.com/</a> What about that?</span>'
end
end

context "rights_tesim" do
Expand Down
36 changes: 28 additions & 8 deletions spec/helpers/sufia_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,36 @@ def create_models(model, user1, user2)
end

describe "#iconify_auto_link" do
subject { helper.iconify_auto_link('Foo < http://www.example.com. & More text') }
it "escapes input" do
expect(subject).to start_with('Foo &lt;')
expect(subject).to end_with('. &amp; More text')
let(:text) { 'Foo < http://www.example.com. & More text' }
let(:document) { SolrDocument.new(has_model_ssim: ['Collection'], id: 512, title_tesim: 'Test Document') }
let(:blacklight_config) { CatalogController.blacklight_config }
before do
allow(controller).to receive(:action_name).and_return('index')
allow(helper).to receive(:blacklight_config).and_return(blacklight_config)
end
it "adds links" do
expect(subject).to include('<a href="http://www.example.com">')
context "String argument" do
subject { helper.iconify_auto_link(text) }
it "escapes input" do
expect(subject).to start_with('Foo &lt;').and end_with('. &amp; More text')
end
it "adds links" do
expect(subject).to include('<a href="http://www.example.com">')
end
it "adds icons" do
expect(subject).to include('class="glyphicon glyphicon-new-window"')
end
end
it "adds icons" do
expect(subject).to include('class="glyphicon glyphicon-new-window"')
context "Hash argument" do
subject { helper.iconify_auto_link(document: document, value: text, config: blacklight_config.search_fields['title']) }
it "escapes input" do
expect(subject).to start_with('Foo &lt;').and end_with('. &amp; More text')
end
it "adds links" do
expect(subject).to include('<a href="http://www.example.com">')
end
it "adds icons" do
expect(subject).to include('class="glyphicon glyphicon-new-window"')
end
end
end

Expand Down

0 comments on commit d31923c

Please sign in to comment.