Module: Sufia::SufiaHelperBehavior

Includes:
ERB::Util, CitationsBehavior
Included in:
SufiaHelper
Defined in:
app/helpers/sufia/sufia_helper_behavior.rb

Constant Summary

Constants included from CitationsBehaviors::TitleBehavior

CitationsBehaviors::TitleBehavior::EXPANDED_NOCAPS, CitationsBehaviors::TitleBehavior::TITLE_NOCAPS

Instance Method Summary collapse

Methods included from CitationsBehavior

#export_as_apa_citation, #export_as_chicago_citation, #export_as_endnote, #export_as_mla_citation, #export_as_openurl_ctx_kev

Methods included from CitationsBehaviors::TitleBehavior

#chicago_citation_title, #mla_citation_title, #process_title_parts, #setup_title_info

Methods included from CitationsBehaviors::CommonBehavior

#clean_end_punctuation, #persistent_url

Methods included from CitationsBehaviors::NameBehavior

#abbreviate_name, #all_authors, #author_list, #given_name_first, #surname_first

Methods included from CitationsBehaviors::PublicationBehavior

#setup_pub_date, #setup_pub_info, #setup_pub_place, #setup_pub_publisher

Instance Method Details

#application_nameObject



6
7
8
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 6

def application_name
  t('sufia.product_name', default: super)
end

#browser_supports_directory_upload?Boolean

TODO:

Replace uses with more granular client-side detection (as jQuery-fileuploader already does)

Only Chrome supports this

Returns:

  • (Boolean)


34
35
36
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 34

def browser_supports_directory_upload?
  user_agent.include? 'Chrome'
end

#collection_thumbnail(_document, _image_options = {}, _url_options = {}) ⇒ Object



206
207
208
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 206

def collection_thumbnail(_document, _image_options = {}, _url_options = {})
  (:span, "", class: ["fa", "fa-cubes", "collection-icon-search"])
end

#current_search_parametersObject

Only display the current search parameters if the user is not in the dashboard. Otherwise, search defaults to the user’s works and not all of Sufia.



178
179
180
181
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 178

def current_search_parameters
  return if on_the_dashboard?
  params[:q]
end

#error_messages_for(object) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 38

def error_messages_for(object)
  return '' unless object.try(:errors) && object.errors.full_messages.any?
  (:div, class: 'alert alert-block alert-error validation-errors') do
    (:h4, I18n.t('sufia.errors.header', model: object.class.model_name.human.downcase), class: 'alert-heading') +
      (:ul) do
        object.errors.full_messages.map do |message|
          (:li, message)
        end.join('').html_safe
      end
  end
end

#has_collection_search_parameters?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 60

def has_collection_search_parameters?
  !params[:cq].blank?
end

#human_readable_date(options) ⇒ Date

A Blacklight helper_method

Parameters:

  • options (Hash)

    from blacklight invocation of helper_method

Returns:

  • (Date)

See Also:



106
107
108
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 106

def human_readable_date(options)
  Date.parse(options[:value]).to_formatted_s(:standard)
end
TODO:

stop being a helper_method, start being part of the Blacklight render stack?

Uses Rails auto_link to add links to fields

Parameters:

  • field (String, Hash)

    string to format and escape, or a hash as per helper_method

  • show_link (Boolean) (defaults to: true)

Options Hash (field):

  • :document (SolrDocument)
  • :field (String)

    name of the solr field

  • :config (Blacklight::Configuration::IndexField, Blacklight::Configuration::ShowField)
  • :value (Array)

    array of values for the field

Returns:

  • (ActiveSupport::SafeBuffer)


134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 134

def iconify_auto_link(field, show_link = true)
  if field.is_a? Hash
    options = field[:config].separator_options || {}
    text = field[:value].to_sentence(options)
  else
    text = field
  end
  # 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 show_link}"
  end
end

A Blacklight helper_method

Parameters:

  • options (Hash{Symbol=>Object})

    Blacklight sends :document, :field, :config, :value and whatever else was in options

Options Hash (options):

  • :value (Array{String})
  • :config (Hash)

    including => “my_name”

  • :document (Hash)
  • :value (Array{String})

    the strings you might otherwise have passed to this method singly

Returns:

  • (ActiveSupport::SafeBuffer)

    the html_safe link

Raises:

  • (ArgumentError)


117
118
119
120
121
122
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 117

def index_field_link(options)
  raise ArgumentError unless options[:config] && options[:config][:field_name]
  name = options[:config][:field_name]
  values = options[:value]
  safe_join(values.map { |item| link_to_field(name, item, item) }, ", ".html_safe)
end

#institution_nameObject



10
11
12
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 10

def institution_name
  t('sufia.institution_name')
end

#institution_name_fullObject



14
15
16
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 14

def institution_name_full
  t('sufia.institution_name_full', default: institution_name)
end

Returns the html_safe link.

Parameters:

  • item (Object)
  • field (String)

Returns:

  • (ActiveSupport::SafeBuffer)

    the html_safe link



71
72
73
74
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 71

def link_to_facet(item, field)
  path = search_action_path(search_state.add_facet_params_and_redirect(field, item))
  link_to(item, path)
end

Returns the html_safe link.

Parameters:

  • values (Array{String})

    strings to display

  • solr_field (String)

    name of the solr field to link to, without its suffix (:facetable)

  • empty_message (String) (defaults to: "No value entered".html_safe)

    message to display if no values are passed in

  • separator (String) (defaults to: ", ")

    value to join with

Returns:

  • (ActiveSupport::SafeBuffer)

    the html_safe link



81
82
83
84
85
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 81

def link_to_facet_list(values, solr_field, empty_message = "No value entered".html_safe, separator = ", ")
  return empty_message if values.blank?
  facet_field = Solrizer.solr_name(solr_field, :facetable)
  safe_join(values.map { |item| link_to_facet(item, facet_field) }, separator)
end

Returns the html_safe link.

Parameters:

  • name (String)

    field name

  • value (String)

    field value

  • label (String) (defaults to: nil)

    defaults to value

  • facet_hash (Hash) (defaults to: {})

    first argument to Blacklight::SearchState.new

Returns:

  • (ActiveSupport::SafeBuffer)

    the html_safe link

See Also:

  • Blacklight::SearchState#initialize


93
94
95
96
97
98
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 93

def link_to_field(name, value, label = nil, facet_hash = {})
  label ||= value
  params = { search_field: 'advanced', name => "\"#{value}\"" }
  state = search_state_with_facets(params, facet_hash)
  link_to(label, main_app.search_catalog_path(state))
end

Sometimes a Blacklight helper_method

Parameters:

  • args (String, User, Hash{Symbol=>Array})

    if a hash, the user_key must be under :value

Returns:

  • (ActiveSupport::SafeBuffer)

    the html_safe link



151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 151

def link_to_profile(args)
  user_or_key = args.is_a?(Hash) ? args[:value].first : args
  user = case user_or_key
         when User
           user_or_key
         when String
           ::User.find_by_user_key(user_or_key)
         end
  return user_or_key if user.nil?
  text = user.respond_to?(:name) ? user.name : user_or_key
  link_to text, Sufia::Engine.routes.url_helpers.profile_path(user)
end


171
172
173
174
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 171

def link_to_telephone(user)
  return unless user
  link_to user.telephone, "wtai://wp/mc;#{user.telephone}" if user.telephone
end

#number_of_deposits(user) ⇒ Object



64
65
66
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 64

def number_of_deposits(user)
  ActiveFedora::Base.where(DepositSearchBuilder.depositor_field => user.user_key).count
end

#orcid_label(style_class = '') ⇒ Object



18
19
20
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 18

def orcid_label(style_class = '')
  "#{image_tag 'orcid.png', alt: t('sufia.user_profile.orcid.alt'), class: style_class} #{t('sufia.user_profile.orcid.label')}".html_safe
end


192
193
194
195
196
197
198
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 192

def render_visibility_link(document)
  # Anchor must match with a tab in
  # https://github.com/projecthydra/sufia/blob/master/app/views/curation_concerns/base/_guts4form.html.erb#L2
  link_to render_visibility_label(document),
          edit_polymorphic_path([main_app, document], anchor: "share"),
          id: "permission_" + document.id, class: "visibility-link"
end

A Blacklight helper_method

Parameters:

  • options (Hash)

    from blacklight helper_method invocation. Maps rights URIs to links with labels.

Returns:

  • (ActiveSupport::SafeBuffer)

    rights statement links, html_safe



167
168
169
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 167

def rights_statement_links(options)
  options[:value].map { |right| link_to RightsService.label(right), right }.to_sentence.html_safe
end

#search_form_actionString

Returns the appropriate action url for our search form (depending on our current page).

Returns:

  • (String)

    the appropriate action url for our search form (depending on our current page)



184
185
186
187
188
189
190
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 184

def search_form_action
  if on_the_dashboard?
    search_action_for_dashboard
  else
    main_app.search_catalog_path
  end
end

#show_transfer_request_title(req) ⇒ Object

Parameters:



51
52
53
54
55
56
57
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 51

def show_transfer_request_title(req)
  if req.deleted_work? || req.canceled?
    req.to_s
  else
    link_to(req.to_s, req.work)
  end
end

#user_display_name_and_key(user_key) ⇒ Object



200
201
202
203
204
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 200

def user_display_name_and_key(user_key)
  user = ::User.find_by_user_key(user_key)
  return user_key if user.nil?
  user.respond_to?(:name) ? "#{user.name} (#{user_key})" : user_key
end

#zotero_label(opts = {}) ⇒ Object



22
23
24
25
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 22

def zotero_label(opts = {})
  html_class = opts[:html_class] || ''
  "#{image_tag 'zotero.png', alt: t('sufia.user_profile.zotero.alt'), class: html_class} #{t('sufia.user_profile.zotero.label')}".html_safe
end

#zotero_profile_url(zotero_user_id) ⇒ Object



27
28
29
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 27

def zotero_profile_url(zotero_user_id)
  "https://www.zotero.org/users/#{zotero_user_id}"
end