Module: Blacklight::Catalog

Extended by:
ActiveSupport::Concern
Includes:
Base, DefaultComponentConfiguration, Facet
Included in:
CatalogController
Defined in:
app/controllers/concerns/blacklight/catalog.rb

Constant Summary

Constants included from RequestBuilders

RequestBuilders::DEFAULT_FACET_LIMIT

Instance Attribute Summary

Attributes included from Configurable

#blacklight_config

Instance Method Summary collapse

Methods included from Facet

#facet_by_field_name, #facet_field_names, #facet_paginator, #facets_from_request

Methods included from DefaultComponentConfiguration

#render_sms_action?

Methods included from Base

#handle_request_error

Methods included from SearchContext

#add_to_search_history, #agent_is_crawler?, #blacklisted_search_session_params, #current_search_session, #find_or_initialize_search_session_from_params, #find_search_session, #search_session, #set_current_search_session, #setup_next_and_previous_documents

Methods included from SearchHelper

#fetch, #get_facet_field_response, #get_opensearch_response, #get_previous_and_next_documents_for_search, #grouped_key_for_results, #repository, #search_results

Methods included from RequestBuilders

#facet_limit_for, #previous_and_next_document_params, #search_builder, #solr_opensearch_params

Methods included from Configurable

default_configuration, default_configuration=

Instance Method Details

#action_documentsObject



95
96
97
# File 'app/controllers/concerns/blacklight/catalog.rb', line 95

def action_documents
  fetch(Array(params[:id]))
end

#action_success_redirect_pathObject



99
100
101
# File 'app/controllers/concerns/blacklight/catalog.rb', line 99

def action_success_redirect_path
  search_state.url_for_document(blacklight_config.document_model.new(id: params[:id]))
end

#additional_export_formats(document, format) ⇒ Object (protected)

Render additional export formats for the show action, as provided by the document extension framework. See Blacklight::Document::Export



154
155
156
157
158
# File 'app/controllers/concerns/blacklight/catalog.rb', line 154

def additional_export_formats(document, format)
  document.export_formats.each_key do | format_name |
    format.send(format_name.to_sym) { render body: document.export_as(format_name), layout: false }
  end
end

#additional_response_formats(format) ⇒ Object (protected)

Note:

Make sure your format has a well known mime-type or is registered in config/initializers/mime_types.rb

Render additional response formats for the index action, as provided by the blacklight configuration

Examples:

config.index.respond_to.txt = Proc.new { render plain: "A list of docs." }

Parameters:

  • format (Hash)


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/controllers/concerns/blacklight/catalog.rb', line 132

def additional_response_formats format
  blacklight_config.index.respond_to.each do |key, config|
    format.send key do
      case config
      when false
        raise ActionController::RoutingError, 'Not Found'
      when Hash
        render config
      when Proc
        instance_exec(&config)
      when Symbol, String
        send config
      else
        render({})
      end
    end
  end
end

#document_export_formats(format) ⇒ Object (protected)

Try to render a response from the document export formats available



162
163
164
165
166
167
168
169
170
171
# File 'app/controllers/concerns/blacklight/catalog.rb', line 162

def document_export_formats format
  format.any do
    format_name = params.fetch(:format, '').to_sym
    if @response.export_formats.include? format_name
      render_document_export_format format_name
    else
      raise ActionController::UnknownFormat
    end
  end
end

#email_action(documents) ⇒ Object (protected)

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



191
192
193
194
195
196
197
198
# File 'app/controllers/concerns/blacklight/catalog.rb', line 191

def email_action documents
  mail = RecordMailer.email_record(documents, {:to => params[:to], :message => params[:message]}, url_options)
  if mail.respond_to? :deliver_now
    mail.deliver_now
  else
    mail.deliver
  end
end

#facetObject

displays values and pagination links for a single facet field

Raises:

  • (ActionController::RoutingError)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/controllers/concerns/blacklight/catalog.rb', line 71

def facet
  @facet = blacklight_config.facet_fields[params[:id]]
  raise ActionController::RoutingError, 'Not Found' unless @facet
  @response = get_facet_field_response(@facet.key, params)
  @display_facet = @response.aggregations[@facet.field]
  @pagination = facet_paginator(@facet, @display_facet)
  respond_to do |format|
    format.html do
      # Draw the partial for the "more" facet modal window:
      return render layout: false if request.xhr?
      # Otherwise draw the facet selector for users who have javascript disabled.
    end
    format.json
  end
end

#has_search_parameters?Boolean

Check if any search parameters have been set

Returns:

  • (Boolean)


106
107
108
# File 'app/controllers/concerns/blacklight/catalog.rb', line 106

def has_search_parameters?
  !params[:q].blank? or !params[:f].blank? or !params[:search_field].blank?
end

#indexObject

get search results from the solr index



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/concerns/blacklight/catalog.rb', line 26

def index
  (@response, @document_list) = search_results(params)

  respond_to do |format|
    format.html { store_preferred_view }
    format.rss  { render :layout => false }
    format.atom { render :layout => false }
    format.json do
      @presenter = Blacklight::JsonPresenter.new(@response,
                                                 @document_list,
                                                 facets_from_request,
                                                 blacklight_config)
    end
    additional_response_formats(format)
    document_export_formats(format)
  end
end

#invalid_document_id_error(exception) ⇒ Object (protected)

when a request for /catalog/BAD_SOLR_ID is made, this method is executed. Just returns a 404 response, but you can override locally in your own CatalogController to do something else – older BL displayed a Catalog#inde page with a flash message and a 404 status.



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'app/controllers/concerns/blacklight/catalog.rb', line 244

def invalid_document_id_error(exception)
  raise exception unless Pathname.new("#{Rails.root}/public/404.html").exist?

  error_info = {
    "status" => "404",
    "error"  => "#{exception.class}: #{exception.message}"
  }

  respond_to do |format|
    format.xml  { render :xml  => error_info, :status => 404 }
    format.json { render :json => error_info, :status => 404 }

    # default to HTML response, even for other non-HTML formats we don't
    # neccesarily know about, seems to be consistent with what Rails4 does
    # by default with uncaught ActiveRecord::RecordNotFound in production
    format.any do
      # use standard, possibly locally overridden, 404.html file. Even for
      # possibly non-html formats, this is consistent with what Rails does
      # on raising an ActiveRecord::RecordNotFound. Rails.root IS needed
      # for it to work under testing, without worrying about CWD.
      render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false, :content_type => 'text/html'
    end
  end
end

#opensearchObject

method to serve up XML OpenSearch description and JSON autocomplete response



88
89
90
91
92
93
# File 'app/controllers/concerns/blacklight/catalog.rb', line 88

def opensearch
  respond_to do |format|
    format.xml { render layout: false }
    format.json { render json: get_opensearch_response }
  end
end

#render_document_export_format(format_name) ⇒ Object (protected)

Render the document export formats for a response First, try to render an appropriate template (e.g. index.endnote.erb) If that fails, just concatenate the document export responses with a newline.



177
178
179
180
181
# File 'app/controllers/concerns/blacklight/catalog.rb', line 177

def render_document_export_format format_name
  render
rescue ActionView::MissingTemplate
  render plain: @response.documents.map { |x| x.export_as(format_name) if x.exports_as? format_name }.compact.join("\n"), layout: false
end

#search_action_url(options = {}) ⇒ Object (protected)

Overrides the Blacklight::Controller provided #search_action_url. By default, any search action from a Blacklight::Catalog controller should use the current controller when constructing the route.



186
187
188
# File 'app/controllers/concerns/blacklight/catalog.rb', line 186

def search_action_url options = {}
  url_for(options.reverse_merge(action: 'index'))
end

#showObject

get a single document from the index to add responses for formats other than html or json see Blacklight::Document::Export



46
47
48
49
50
51
52
53
# File 'app/controllers/concerns/blacklight/catalog.rb', line 46

def show
  @response, @document = fetch params[:id]
  respond_to do |format|
    format.html { setup_next_and_previous_documents }
    format.json { render json: { response: { document: @document } } }
    additional_export_formats(@document, format)
  end
end

#sms_action(documents) ⇒ Object (protected)

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



201
202
203
204
205
206
207
208
209
# File 'app/controllers/concerns/blacklight/catalog.rb', line 201

def sms_action documents
  to = "#{params[:to].gsub(/[^\d]/, '')}@#{params[:carrier]}"
  mail = RecordMailer.sms_record(documents, { :to => to }, url_options)
  if mail.respond_to? :deliver_now
    mail.deliver_now
  else
    mail.deliver
  end
end

#sms_mappingsObject (protected)



225
226
227
# File 'app/controllers/concerns/blacklight/catalog.rb', line 225

def sms_mappings
  Blacklight::Engine.config.sms_mappings
end

#start_new_search_session?Boolean (protected)

Returns:

  • (Boolean)


269
270
271
# File 'app/controllers/concerns/blacklight/catalog.rb', line 269

def start_new_search_session?
  action_name == "index"
end

#store_preferred_viewObject (protected)

If the params specify a view, then store it in the session. If the params do not specifiy the view, set the view parameter to the value stored in the session. This enables a user with a session to do subsequent searches and have them default to the last used view.



121
122
123
# File 'app/controllers/concerns/blacklight/catalog.rb', line 121

def store_preferred_view
  session[:preferred_view] = params[:view] if params[:view]
end

#trackObject

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



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/concerns/blacklight/catalog.rb', line 56

def track
  search_session['counter'] = params[:counter]
  search_session['id'] = params[:search_id]
  search_session['per_page'] = params[:per_page]

  if params[:redirect] and (params[:redirect].starts_with?('/') or params[:redirect] =~ URI::DEFAULT_PARSER.make_regexp)
    uri = URI.parse(params[:redirect])
    path = uri.query ? "#{uri.path}?#{uri.query}" : uri.path
    redirect_to path, status: 303
  else
    redirect_to blacklight_config.document_model.new(id: params[:id]), status: 303
  end
end

#validate_email_paramsObject (protected)



229
230
231
232
233
234
235
236
237
# File 'app/controllers/concerns/blacklight/catalog.rb', line 229

def validate_email_params
  if params[:to].blank?
    flash[:error] = I18n.t('blacklight.email.errors.to.blank')
  elsif !params[:to].match(Blacklight::Engine.config.email_regexp)
    flash[:error] = I18n.t('blacklight.email.errors.to.invalid', :to => params[:to])
  end

  flash[:error].blank?
end

#validate_sms_paramsObject (protected)



211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'app/controllers/concerns/blacklight/catalog.rb', line 211

def validate_sms_params
  if params[:to].blank?
    flash[:error] = I18n.t('blacklight.sms.errors.to.blank')
  elsif params[:carrier].blank?
    flash[:error] = I18n.t('blacklight.sms.errors.carrier.blank')
  elsif params[:to].gsub(/[^\d]/, '').length != 10
    flash[:error] = I18n.t('blacklight.sms.errors.to.invalid', :to => params[:to])
  elsif !sms_mappings.values.include?(params[:carrier])
    flash[:error] = I18n.t('blacklight.sms.errors.carrier.invalid')
  end

  flash[:error].blank?
end