Module: Blacklight::Catalog

Extended by:
ActiveSupport::Concern
Includes:
Configurable, SolrHelper
Included in:
CatalogController
Defined in:
lib/blacklight/catalog.rb

Constant Summary

SearchHistoryWindow =

how many searches to save in session history

12

Instance Attribute Summary

Attributes included from Configurable

#blacklight_config

Instance Method Summary (collapse)

Methods included from SolrHelper

#add_facet_fq_to_solr, #add_facetting_to_solr, #add_paging_to_solr, #add_query_to_solr, #add_solr_fields_to_query, #add_sorting_to_solr, #default_solr_parameters, #facet_limit_for, #facet_value_to_fq_string, #find, #force_to_utf8, #get_facet_pagination, #get_opensearch_response, #get_search_results, #get_single_doc_via_search, #get_solr_response_for_doc_id, #get_solr_response_for_field_values, #query_solr, #solr_doc_params, #solr_facet_params, #solr_opensearch_params, #solr_param_quote, #solr_search_params

Methods included from SearchFields

#default_search_field, #label_for_search_field, #search_field_def_for_key, #search_field_list, #search_field_options_for_select

Methods included from Configurable

default_configuration, default_configuration=

Instance Method Details

- (Object) adjust_for_results_view (protected)

we need to know if we are viewing the item as part of search results so we know whether to include certain partials or not



268
269
270
271
272
273
274
# File 'lib/blacklight/catalog.rb', line 268

def adjust_for_results_view
  if params[:results_view] == "false"
    session[:search][:results_view] = false
  else
    session[:search][:results_view] = true
  end
end

- (Object) blacklight_solr (protected)



312
313
314
# File 'lib/blacklight/catalog.rb', line 312

def blacklight_solr
  @solr ||=  RSolr.connect(blacklight_solr_config)
end

- (Object) blacklight_solr_config (protected)



316
317
318
# File 'lib/blacklight/catalog.rb', line 316

def blacklight_solr_config
  Blacklight.solr_config
end

- (Object) citation

citation action



96
97
98
99
100
101
102
# File 'lib/blacklight/catalog.rb', line 96

def citation
  @response, @documents = get_solr_response_for_field_values(SolrDocument.unique_key,params[:id])
  respond_to do |format|
    format.html
    format.js { render :layout => false }
  end
end

- (Object) delete_or_assign_search_session_params (protected)

This method copies request params to session, omitting certain known blacklisted params not part of search, omitting keys with blank values. All keys in session are as symbols rather than strings.



231
232
233
234
235
236
237
# File 'lib/blacklight/catalog.rb', line 231

def delete_or_assign_search_session_params
  session[:search] = {}
  params.each_pair do |key, value|
    session[:search][key.to_sym] = value unless ["commit", "counter"].include?(key.to_s) ||
      value.blank?
  end
end

- (Object) email

Email Action (this will render the appropriate view on GET requests and process the form and send the email on POST requests)



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/blacklight/catalog.rb', line 112

def email
  @response, @documents = get_solr_response_for_field_values(SolrDocument.unique_key,params[:id])
  if request.post?
    if params[:to]
      url_gen_params = {:host => request.host_with_port, :protocol => request.protocol}
      
      if params[:to].match(/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/)
        email = RecordMailer.email_record(@documents, {:to => params[:to], :message => params[:message]}, url_gen_params)
      else
        flash[:error] = I18n.t('blacklight.email.errors.to.invalid', :to => params[:to])
      end
    else
      flash[:error] = I18n.t('blacklight.email.errors.to.blank')
    end

    unless flash[:error]
      email.deliver 
      flash[:success] = "Email sent"
      redirect_to catalog_path(params['id']) unless request.xhr?
    end
  end

  unless !request.xhr? && flash[:success]
    respond_to do |format|
      format.js { render :layout => false }
      format.html
    end
  end
end

- (Object) endnote

grabs a bunch of documents to export to endnote



104
105
106
107
108
109
# File 'lib/blacklight/catalog.rb', line 104

def endnote
  @response, @documents = get_solr_response_for_field_values(SolrDocument.unique_key,params[:id])
  respond_to do |format|
    format.endnote { render :layout => false }
  end
end

- (Object) facet

displays values and pagination links for a single facet field



74
75
76
77
78
79
80
81
# File 'lib/blacklight/catalog.rb', line 74

def facet
  @pagination = get_facet_pagination(params[:id], params)

  respond_to do |format|
    format.html 
    format.js { render :layout => false }
  end
end

- (Object) history_session (protected)

sets up the session hash if it doesn't already exist. assigns all Search objects (that match the searches in session) to a variable @searches.



223
224
225
226
# File 'lib/blacklight/catalog.rb', line 223

def history_session
  session[:history] ||= []
  @searches = searches_from_history # <- in BlacklightController
end

- (Object) index

get search results from the solr index



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/blacklight/catalog.rb', line 33

def index
  
  extra_head_content << view_context.auto_discovery_link_tag(:rss, url_for(params.merge(:format => 'rss')), :title => t('blacklight.search.rss_feed') )
  extra_head_content << view_context.auto_discovery_link_tag(:atom, url_for(params.merge(:format => 'atom')), :title => t('blacklight.search.atom_feed') )
  
  (@response, @document_list) = get_search_results
  @filters = params[:f] || []
  
  respond_to do |format|
    format.html { save_current_search_params }
    format.rss  { render :layout => false }
    format.atom { render :layout => false }
  end
end

- (Object) invalid_solr_id_error (protected)

when a request for /catalog/BAD_SOLR_ID is made, this method is executed…



301
302
303
304
305
306
307
308
309
310
# File 'lib/blacklight/catalog.rb', line 301

def invalid_solr_id_error
  if Rails.env == "development"
    render # will give us the stack trace
  else
    flash[:notice] = I18n.t('blacklight.search.errors.invalid_solr_id')
    params.delete(:id)
    index
    render "index", :status => 404
  end
end

- (Object) librarian_view



179
180
181
182
183
184
185
186
# File 'lib/blacklight/catalog.rb', line 179

def librarian_view
  @response, @document = get_solr_response_for_doc_id

  respond_to do |format|
    format.html
    format.js { render :layout => false }
  end
end

- (Object) opensearch

method to serve up XML OpenSearch description and JSON autocomplete response



84
85
86
87
88
89
90
91
92
93
# File 'lib/blacklight/catalog.rb', line 84

def opensearch
  respond_to do |format|
    format.xml do
      render :layout => false
    end
    format.json do
      render :json => get_opensearch_response
    end
  end
end

- (Object) rsolr_request_error(exception) (protected)

when solr (RSolr) throws an error (RSolr::RequestError), this method is executed.



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/blacklight/catalog.rb', line 278

def rsolr_request_error(exception)
  
  if Rails.env.development?
    raise exception # Rails own code will catch and give usual Rails error page with stack trace
  else

    flash_notice = I18n.t('blacklight.search.errors.request_error')

    # If there are errors coming from the index page, we want to trap those sensibly

    if flash[:notice] == flash_notice
      logger.error "Cowardly aborting rsolr_request_error exception handling, because we redirected to a page that raises another exception"
      raise exception
    end

    logger.error exception

    flash[:notice] = flash_notice 
    redirect_to root_path
  end
end

- (Object) save_current_search_params (protected)

Saves the current search (if it does not already exist) as a models/search object then adds the id of the serach object to session



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/blacklight/catalog.rb', line 241

def save_current_search_params    
  # If it's got anything other than controller, action, total, we
  # consider it an actual search to be saved. Can't predict exactly
  # what the keys for a search will be, due to possible extra plugins.
  return if (search_session.keys - [:controller, :action, :total, :counter, :commit ]) == [] 
  params_copy = search_session.clone # don't think we need a deep copy for this
  params_copy.delete(:page)
  
  unless @searches.collect { |search| search.query_params }.include?(params_copy)
    
    new_search = Search.create(:query_params => params_copy)
    session[:history].unshift(new_search.id)
    # Only keep most recent X searches in history, for performance. 
    # both database (fetching em all), and cookies (session is in cookie)
    session[:history] = session[:history].slice(0, Blacklight::Catalog::SearchHistoryWindow )
  end
end

- (Object) search_action_url



28
29
30
# File 'lib/blacklight/catalog.rb', line 28

def search_action_url
  url_for(:action => 'index', :only_path => true)
end

- (Object) search_session (protected)

sets up the session hash if it doesn't already exist



217
218
219
# File 'lib/blacklight/catalog.rb', line 217

def search_session
  session[:search] ||= {}
end

- (Object) set_additional_search_session_values (protected)

sets some additional search metadata so that the show view can display it.



260
261
262
263
264
# File 'lib/blacklight/catalog.rb', line 260

def set_additional_search_session_values
  unless @response.nil?
    search_session[:total] = @response.total
  end
end

- (Object) setup_document_by_counter(counter) (protected)

gets a document based on its position within a resultset



202
203
204
205
206
# File 'lib/blacklight/catalog.rb', line 202

def setup_document_by_counter(counter)
  return if counter < 1 || session[:search].blank?
  search = session[:search] || {}
  get_single_doc_via_search(counter, search)
end

- (Object) setup_next_and_previous_documents (protected)

calls setup_previous_document then setup_next_document. used in the show action for single view pagination.



196
197
198
199
# File 'lib/blacklight/catalog.rb', line 196

def setup_next_and_previous_documents
  setup_previous_document
  setup_next_document
end

- (Object) setup_next_document (protected)



212
213
214
# File 'lib/blacklight/catalog.rb', line 212

def setup_next_document
  @next_document = session[:search][:counter] ? setup_document_by_counter(session[:search][:counter].to_i + 1) : nil
end

- (Object) setup_previous_document (protected)



208
209
210
# File 'lib/blacklight/catalog.rb', line 208

def setup_previous_document
  @previous_document = session[:search][:counter] ? setup_document_by_counter(session[:search][:counter].to_i - 1) : nil
end

- (Object) show

get single document from the solr index



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/blacklight/catalog.rb', line 49

def show
  @response, @document = get_solr_response_for_doc_id    

  respond_to do |format|
    format.html {setup_next_and_previous_documents}

    # Add all dynamically added (such as by document extensions)
    # export formats.
    @document.export_formats.each_key do | format_name |
      # It's important that the argument to send be a symbol;
      # if it's a string, it makes Rails unhappy for unclear reasons. 
      format.send(format_name.to_sym) { render :text => @document.export_as(format_name), :layout => false }
    end
    
  end
end

- (Object) sms

SMS action (this will render the appropriate view on GET requests and process the form and send the email on POST requests)



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/blacklight/catalog.rb', line 143

def sms 
  @response, @documents = get_solr_response_for_field_values(SolrDocument.unique_key,params[:id])
  if request.post?
    url_gen_params = {:host => request.host_with_port, :protocol => request.protocol}
    
    if params[:to]
      phone_num = params[:to].gsub(/[^\d]/, '')
      unless params[:carrier].blank?
        if phone_num.length != 10
          flash[:error] = I18n.t('blacklight.sms.errors.to.invalid', :to => params[:to])
        else
          email = RecordMailer.sms_record(@documents, {:to => phone_num, :carrier => params[:carrier]}, url_gen_params)
        end

      else
        flash[:error] = I18n.t('blacklight.sms.errors.carrier.blank')
      end
    else
      flash[:error] = I18n.t('blacklight.sms.errors.to.blank')
    end

    unless flash[:error]
      email.deliver 
      flash[:success] = "SMS sent"
      redirect_to catalog_path(params['id']) unless request.xhr?
    end
  end
    
  unless !request.xhr? && flash[:success]
    respond_to do |format|
      format.js { render :layout => false }
      format.html
    end
  end
end

- (Object) update

updates the search counter (allows the show view to paginate)



67
68
69
70
71
# File 'lib/blacklight/catalog.rb', line 67

def update
  adjust_for_results_view
  session[:search][:counter] = params[:counter]
  redirect_to :action => "show"
end