Module: Sufia::SufiaHelperBehavior

Included in:
SufiaHelper
Defined in:
app/helpers/sufia/sufia_helper_behavior.rb

Instance Method Summary collapse

Instance Method Details

#current_search_parametersObject

Only display the current search parameters if the user is not in the dashboard. If they are in the dashboard, then the search defaults to the user’s files and not all of Sufia.



159
160
161
162
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 159

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

#display_user_name(recent_document) ⇒ Object



84
85
86
87
88
89
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 84

def display_user_name(recent_document)
  return "no display name" unless recent_document.depositor
  ::User.find_by_user_key(recent_document.depositor).name
rescue
  recent_document.depositor
end

#error_messages_for(object) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 16

def error_messages_for(object)
  if 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
  else
    '' # return empty string
  end
end

#has_collection_search_parameters?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 80

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


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

def iconify_auto_link(text, show_link = true)
  # this block is only executed when a link is inserted;
  # if we pass text containing no links, it just returns text.
  auto_link(text) do |value|
    "<i class='glyphicon glyphicon-new-window'></i>#{('&nbsp;' + value) if show_link}<br />"
  end
end


124
125
126
127
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 124

def iconify_link(text)
  # different from above in that if text is not a url, return nil.
  iconify_auto_link(text, false) if is_url?(text)
end

#is_url?(str) ⇒ Boolean

Returns:

  • (Boolean)


196
197
198
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 196

def is_url?(str)
  str =~ /\A#{URI.regexp(['http', 'https'])}\z/
end

Create a link back to the dashboard screen, keeping the user’s facet, query and paging choices intact by using session.



63
64
65
66
67
68
69
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 63

def link_back_to_dashboard(opts = { label: 'Back to Search' })
  query_params = session[:search] ? session[:search].dup : {}
  query_params.delete :counter
  query_params.delete :total
  link_url = dashboard_index_path + "?" + query_params.to_query
  link_to opts[:label], link_url
end


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

def link_to_dashboard_query(query)
  p = params.dup
  p.delete :page
  p.delete :action
  p[:q] = query
  link_url = dashboard_index_path(p)
  link_to(query, link_url)
end


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

def link_to_facet(field, field_string)
  link_to(field, add_facet_params(field_string, field).merge!(controller: "catalog", action: "index"))
end

Parameters:

  • values (Array)

    The values to display

  • solr_field (String)

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

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

    (‘No value entered’) The message to display if no values are passed in.

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

    (‘, ’) The value to join with.



103
104
105
106
107
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 103

def link_to_facet_list(values, solr_field, empty_message = "No value entered", 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


109
110
111
112
113
114
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 109

def link_to_field(fieldname, fieldvalue, displayvalue = nil)
  p = { search_field: 'advanced', fieldname => '"' + fieldvalue + '"' }
  link_url = catalog_index_path(p)
  display = displayvalue.blank? ? fieldvalue : displayvalue
  link_to(display, link_url)
end


129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 129

def link_to_profile()
  user = ::User.find_by_user_key()
  return  if user.nil?

  text =
    if user.respond_to? :name
      user.name
    else
      
    end

  link_to text, Sufia::Engine.routes.url_helpers.profile_path(user)
end


151
152
153
154
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 151

def link_to_telephone(user = nil)
  @user ||= user
  link_to @user.telephone, "wtai://wp/mc;#{@user.telephone}" if @user.telephone
end

#linkify_chat_id(chat_id) ⇒ Object



143
144
145
146
147
148
149
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 143

def linkify_chat_id(chat_id)
  if chat_id.end_with? '@chat.psu.edu'
    "<a href=\"xmpp:#{chat_id}\">#{chat_id}</a>"
  else
    chat_id
  end
end

#number_of_deposits(user) ⇒ Object



91
92
93
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 91

def number_of_deposits(user)
  ActiveFedora::Base.where(Solrizer.solr_name('depositor', :symbol) => user.user_key).count
end

#orcid_label(style_class = '') ⇒ Object



3
4
5
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 3

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

#render_visibility_label(document) ⇒ Object



179
180
181
182
183
184
185
186
187
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 179

def render_visibility_label(document)
  if document.registered?
     :span, t('sufia.institution_name'), class: "label label-info", title: t('sufia.institution_name')
  elsif document.public?
     :span, t('sufia.visibility.open'), class: "label label-success", title: t('sufia.visibility.open_title_attr')
  else
     :span, t('sufia.visibility.private'), class: "label label-danger", title: t('sufia.visibility.private_title_attr')
  end
end


174
175
176
177
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 174

def render_visibility_link(document)
  link_to render_visibility_label(document), sufia.edit_generic_file_path(document, anchor: "permissions_display"),
          id: "permission_" + document.id, class: "visibility-link"
end

#search_form_actionObject

Depending on which page we’re landed on, we’ll need to set the appropriate action url for our search form.



166
167
168
169
170
171
172
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 166

def search_form_action
  if on_the_dashboard?
    search_action_for_dashboard
  else
    catalog_index_path
  end
end

#show_transfer_request_title(req) ⇒ Object



31
32
33
34
35
36
37
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 31

def show_transfer_request_title(req)
  if req.deleted_file?
    req.title
  else
    link_to(req.title, sufia.generic_file_path(req.generic_file_id))
  end
end

#sufia_thumbnail_tag(document, options) ⇒ Object

You can configure blacklight to use this as the thumbnail example:

config.index.thumbnail_method = :sufia_thumbnail_tag


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 42

def sufia_thumbnail_tag(document, options)
  # collection
  if document.collection?
    (:span, "", class: "glyphicon glyphicon-th collection-icon-search")

  # file
  else
    path =
      if document.image? || document.pdf? || document.video? || document.office_document?
        sufia.download_path document, file: 'thumbnail'
      elsif document.audio?
        "audio.png"
      else
        "default.png"
      end
    options[:alt] = ""
    image_tag path, options
  end
end

#user_display_name_and_key(user_key) ⇒ Object



189
190
191
192
193
194
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 189

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



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

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



12
13
14
# File 'app/helpers/sufia/sufia_helper_behavior.rb', line 12

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