Module: Blacklight::Controller
- Extended by:
- ActiveSupport::Concern, Deprecation
- Defined in:
- app/controllers/concerns/blacklight/controller.rb
Overview
Filters added to this controller apply to all controllers in the hosting application as this module is mixed-in to the application controller in the hosting app on installation.
Instance Method Summary collapse
-
#access_denied ⇒ Object
protected
To handle failed authorization attempts, redirect the user to the login form and persist the current request uri as a parameter.
-
#blacklight_configuration_context ⇒ Object
protected
Context in which to evaluate blacklight configuration conditionals.
-
#current_or_guest_user ⇒ Object
(also: #blacklight_current_or_guest_user)
protected
Here's a stub implementation we'll add if it isn't provided for us.
- #default_catalog_controller ⇒ Object
-
#discard_flash_if_xhr ⇒ Object
protected
We discard flash messages generated by the xhr requests to avoid confusing UX.
- #has_user_authentication_provider? ⇒ Boolean protected
-
#render_bookmarks_control? ⇒ Boolean
protected
Determine whether to render the bookmarks control (Needs to be available globally, as it is used in the navbar).
-
#render_saved_searches? ⇒ Boolean
protected
Determine whether to render the saved searches link (Needs to be available globally, as it is used in the navbar).
- #require_user_authentication_provider ⇒ Object protected
- #search_action_path(*args) ⇒ Object protected
-
#search_action_url(options = {}) ⇒ Object
protected
Default route to the search action (used e.g. in global partials).
- #search_facet_path(options = {}) ⇒ Object protected
- #search_facet_url(options = {}) ⇒ Object protected
-
#search_state ⇒ Blacklight::SearchState
protected
A memoized instance of the parameter state.
-
#searches_from_history ⇒ Object
protected
Returns a list of Searches from the ids in the user's history.
-
#transfer_guest_user_actions_to_current_user ⇒ Object
protected
When a user logs in, transfer any saved searches or bookmarks to the current_user.
Instance Method Details
#access_denied ⇒ Object (protected)
To handle failed authorization attempts, redirect the user to the login form and persist the current request uri as a parameter
179 180 181 182 183 184 185 186 187 188 |
# File 'app/controllers/concerns/blacklight/controller.rb', line 179 def access_denied # send the user home if the access was previously denied by the same # request to avoid sending the user back to the login page # (e.g. protected page -> logout -> returned to protected page -> home) redirect_to root_url and flash.discard and return if request.referer and request.referer.ends_with? request.fullpath redirect_to root_url and return unless has_user_authentication_provider? redirect_to new_user_session_url(:referer => request.fullpath) end |
#blacklight_configuration_context ⇒ Object (protected)
Context in which to evaluate blacklight configuration conditionals
53 54 55 |
# File 'app/controllers/concerns/blacklight/controller.rb', line 53 def blacklight_configuration_context @blacklight_configuration_context ||= Blacklight::Configuration::Context.new(self) end |
#current_or_guest_user ⇒ Object (protected) Also known as: blacklight_current_or_guest_user
Here's a stub implementation we'll add if it isn't provided for us
124 125 126 127 128 129 130 |
# File 'app/controllers/concerns/blacklight/controller.rb', line 124 def current_or_guest_user if defined? super super elsif has_user_authentication_provider? current_user end end |
#default_catalog_controller ⇒ Object
43 44 45 |
# File 'app/controllers/concerns/blacklight/controller.rb', line 43 def default_catalog_controller CatalogController end |
#discard_flash_if_xhr ⇒ Object (protected)
We discard flash messages generated by the xhr requests to avoid confusing UX.
136 137 138 |
# File 'app/controllers/concerns/blacklight/controller.rb', line 136 def discard_flash_if_xhr flash.discard if request.xhr? end |
#has_user_authentication_provider? ⇒ Boolean (protected)
147 148 149 |
# File 'app/controllers/concerns/blacklight/controller.rb', line 147 def has_user_authentication_provider? respond_to? :current_user end |
#render_bookmarks_control? ⇒ Boolean (protected)
Determine whether to render the bookmarks control (Needs to be available globally, as it is used in the navbar)
60 61 62 |
# File 'app/controllers/concerns/blacklight/controller.rb', line 60 def render_bookmarks_control? has_user_authentication_provider? and current_or_guest_user.present? end |
#render_saved_searches? ⇒ Boolean (protected)
Determine whether to render the saved searches link (Needs to be available globally, as it is used in the navbar)
67 68 69 |
# File 'app/controllers/concerns/blacklight/controller.rb', line 67 def render_saved_searches? has_user_authentication_provider? and current_user end |
#require_user_authentication_provider ⇒ Object (protected)
151 152 153 |
# File 'app/controllers/concerns/blacklight/controller.rb', line 151 def require_user_authentication_provider raise ActionController::RoutingError, 'Not Found' unless has_user_authentication_provider? end |
#search_action_path(*args) ⇒ Object (protected)
92 93 94 95 96 97 98 |
# File 'app/controllers/concerns/blacklight/controller.rb', line 92 def search_action_path *args if args.first.is_a? Hash args.first[:only_path] = true end search_action_url(*args) end |
#search_action_url(options = {}) ⇒ Object (protected)
Default route to the search action (used e.g. in global partials). Override this method in a controller or in your ApplicationController to introduce custom logic for choosing which action the search form should use
87 88 89 90 |
# File 'app/controllers/concerns/blacklight/controller.rb', line 87 def search_action_url = {} # Rails 4.2 deprecated url helpers accepting string keys for 'controller' or 'action' search_catalog_url(.except(:controller, :action)) end |
#search_facet_path(options = {}) ⇒ Object (protected)
106 107 108 109 110 |
# File 'app/controllers/concerns/blacklight/controller.rb', line 106 def search_facet_path( = {}) Deprecation.silence(Blacklight::Controller) do search_facet_url(.merge(only_path: true)) end end |
#search_facet_url(options = {}) ⇒ Object (protected)
100 101 102 103 |
# File 'app/controllers/concerns/blacklight/controller.rb', line 100 def search_facet_url = {} opts = search_state.to_h.merge(action: "facet").merge().except(:page) url_for opts end |
#search_state ⇒ Blacklight::SearchState (protected)
Returns a memoized instance of the parameter state.
72 73 74 75 76 77 78 79 80 81 82 |
# File 'app/controllers/concerns/blacklight/controller.rb', line 72 def search_state @search_state ||= begin if search_state_class.instance_method(:initialize).arity == -3 search_state_class.new(params, blacklight_config, self) else Deprecation.warn(search_state_class, "The constructor for #{search_state_class} now requires a third argument. " \ "Invoking it will 2 arguments is deprecated and will be removed in Blacklight 7.") search_state_class.new(params, blacklight_config) end end end |
#searches_from_history ⇒ Object (protected)
Returns a list of Searches from the ids in the user's history.
113 114 115 |
# File 'app/controllers/concerns/blacklight/controller.rb', line 113 def searches_from_history session[:history].blank? ? Search.none : Search.where(:id => session[:history]).order("updated_at desc") end |
#transfer_guest_user_actions_to_current_user ⇒ Object (protected)
When a user logs in, transfer any saved searches or bookmarks to the current_user
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'app/controllers/concerns/blacklight/controller.rb', line 157 def transfer_guest_user_actions_to_current_user return unless respond_to? :current_user and respond_to? :guest_user and current_user and guest_user current_user_searches = current_user.searches.pluck(:query_params) current_user_bookmarks = current_user.bookmarks.pluck(:document_id) guest_user.searches.reject { |s| current_user_searches.include?(s.query_params)}.each do |s| current_user.searches << s s.save! end guest_user.bookmarks.reject { |b| current_user_bookmarks.include?(b.document_id)}.each do |b| current_user.bookmarks << b b.save! end # let guest_user know we've moved some bookmarks from under it guest_user.reload if guest_user.persisted? end |