Skip to content

Commit

Permalink
Support Rails 5
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Dec 14, 2018
1 parent e7764c1 commit 60ec511
Show file tree
Hide file tree
Showing 21 changed files with 184 additions and 125 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ notifications:
email: false

rvm:
- 2.3.1
- 2.3.8

matrix:
include:
- rvm: 2.5.3
env: "RAILS_VERSION=5.2.2"
- rvm: 2.2.5
env: "RAILS_VERSION=4.1.13"
- rvm: 2.1.5
Expand Down
6 changes: 5 additions & 1 deletion app/controllers/concerns/blacklight/bookmarks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ module Blacklight::Bookmarks

copy_blacklight_config_from(CatalogController)

before_filter :verify_user
if Rails.version < '5'
before_filter :verify_user
else
before_action :verify_user
end

blacklight_config.add_results_collection_tool(:clear_bookmarks_widget)

Expand Down
28 changes: 17 additions & 11 deletions app/controllers/concerns/blacklight/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,34 @@
#
# 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.
module Blacklight::Controller
module Blacklight::Controller

extend ActiveSupport::Concern
extend Deprecation

self.deprecation_horizon = 'blacklight 6.0'

included do
include Blacklight::SearchFields
helper Blacklight::SearchFields

include ActiveSupport::Callbacks

# now in application.rb file under config.filter_parameters
# filter_parameter_logging :password, :password_confirmation
# filter_parameter_logging :password, :password_confirmation
helper_method :current_user_session, :current_user, :current_or_guest_user
after_filter :discard_flash_if_xhr

if Rails.version < '5'
after_filter :discard_flash_if_xhr
else
after_action :discard_flash_if_xhr
end

# handle basic authorization exception with #access_denied
rescue_from Blacklight::Exceptions::AccessDenied, :with => :access_denied

helper_method :request_is_for_user_resource?

# extra head content
helper_method :has_user_authentication_provider?
helper_method :blacklight_config
Expand Down Expand Up @@ -66,14 +71,15 @@ def search_action_path *args
end

def search_facet_url options = {}
url_for params.merge(action: "facet").merge(options).except(:page)
new_params = params.respond_to?(:to_unsafe_h) ? params.to_unsafe_h : params
url_for new_params.merge(action: "facet").merge(options).except(:page)
end

# Returns a list of Searches from the ids in the user's history.
def searches_from_history
session[:history].blank? ? Search.none : Search.where(:id => session[:history]).order("updated_at desc")
end

#
# Controller and view helper for determining if the current url is a request for a user resource
#
Expand Down Expand Up @@ -112,7 +118,7 @@ def discard_flash_if_xhr
#
def has_user_authentication_provider?
respond_to? :current_user
end
end

def require_user_authentication_provider
raise ActionController::RoutingError.new('Not Found') unless has_user_authentication_provider?
Expand Down Expand Up @@ -140,7 +146,7 @@ def transfer_guest_user_actions_to_current_user
end

##
# To handle failed authorization attempts, redirect the user to the
# To handle failed authorization attempts, redirect the user to the
# login form and persist the current request uri as a parameter
def access_denied
# send the user home if the access was previously denied by the same
Expand All @@ -152,5 +158,5 @@ def access_denied

redirect_to new_user_session_url(:referer => request.fullpath)
end

end
20 changes: 12 additions & 8 deletions app/controllers/concerns/blacklight/search_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,30 @@ module Blacklight::SearchContext

# The following code is executed when someone includes blacklight::catalog::search_session in their
# own controller.
included do
included do
helper_method :current_search_session, :search_session

end

module ClassMethods
# Save the submitted search parameters in the search session
def record_search_parameters opts = { only: :index}
before_filter :current_search_session, opts
if Rails.version < '5'
before_filter :current_search_session, opts
else
before_action :current_search_session, opts
end
end
end

protected

# sets up the session[:search] hash if it doesn't already exist
def search_session
session[:search] ||= {}
end
# The current search session

# The current search session
def current_search_session

@current_search_session ||= if start_new_search_session?
Expand All @@ -31,7 +35,7 @@ def current_search_session
find_or_initialize_search_session_from_params JSON.load(params[:search_context])
elsif params[:search_id].present?
begin
# TODO : check the search id signature.
# TODO : check the search id signature.
searches_from_history.find(params[:search_id])
rescue ActiveRecord::RecordNotFound
nil
Expand Down Expand Up @@ -79,11 +83,11 @@ def add_to_search_history search
if session[:history].length > blacklight_config.search_history_window

session[:history] = session[:history].slice(0, blacklight_config.search_history_window )

end
end

# A list of query parameters that should not be persisted for a search
# A list of query parameters that should not be persisted for a search
def blacklisted_search_session_params
[:commit, :counter, :total, :search_id, :page, :per_page]
end
Expand Down
21 changes: 13 additions & 8 deletions app/controllers/saved_searches_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ class SavedSearchesController < ApplicationController
include Blacklight::Configurable

copy_blacklight_config_from(CatalogController)
before_filter :require_user_authentication_provider
before_filter :verify_user

if Rails.version < '5'
before_filter :require_user_authentication_provider
before_filter :verify_user
else
before_action :require_user_authentication_provider
before_action :verify_user
end

def index
@searches = current_user.searches
end
def save

def save
current_user.searches << searches_from_history.find(params[:id])
if current_user.save
flash[:notice] = I18n.t('blacklight.saved_searches.add.success')
Expand All @@ -33,14 +38,14 @@ def forget
end
redirect_to :back
end

# Only dereferences the user rather than removing the items in case they
# are in the session[:history]
def clear
def clear
if current_user.searches.update_all("user_id = NULL")
flash[:notice] = I18n.t('blacklight.saved_searches.clear.success')
else
flash[:error] = I18n.t('blacklight.saved_searches.clear.failure')
flash[:error] = I18n.t('blacklight.saved_searches.clear.failure')
end
redirect_to :action => "index"
end
Expand Down
49 changes: 33 additions & 16 deletions app/helpers/blacklight/catalog_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ module Blacklight::CatalogHelperBehavior
extend Deprecation
self.deprecation_horizon = "Blacklight 6.x"

def rss_feed_link_tag
auto_discovery_link_tag(:rss, feed_link_url('rss'), title: t('blacklight.search.rss_feed'))
end

def atom_feed_link_tag
auto_discovery_link_tag(:atom, feed_link_url('atom'), title: t('blacklight.search.atom_feed'))
end

##
# Override the Kaminari page_entries_info helper with our own, blacklight-aware
# implementation.
Expand Down Expand Up @@ -37,7 +45,7 @@ def page_entries_info(collection, options = {})
else
collection.total_count
end

case collection.total_count
when 0; t('blacklight.search.pagination_info.no_items_found', :entry_name => entry_name ).html_safe
when 1; t('blacklight.search.pagination_info.single_item_found', :entry_name => entry_name).html_safe
Expand All @@ -54,15 +62,15 @@ def document_counter_with_offset idx, offset = nil
offset ||= @response.start if @response
offset ||= 0

unless render_grouped_response?
unless render_grouped_response?
idx + 1 + offset
end
end

##
# Like #page_entries_info above, but for an individual
# item show page. Displays "showing X of Y items" message.
#
#
# @see #page_entries_info
# @return [String]
def item_page_entry_info
Expand All @@ -86,15 +94,15 @@ def current_sort_field

##
# Look up the current per page value, or the default if none if set
#
#
# @return [Integer]
def current_per_page
(@response.rows if @response and @response.rows > 0) || params.fetch(:per_page, default_per_page).to_i
end

##
# Get the classes to add to a document's div
#
#
# @return [String]
def render_document_class(document = @document)
types = document[blacklight_config.view_config(document_index_view_type).display_type_field]
Expand Down Expand Up @@ -130,7 +138,7 @@ def render_document_main_content_partial(document = @document)

##
# Should we display the sort and per page widget?
#
#
# @param [Blacklight::Solr::Response]
# @return [Boolean]
def show_sort_and_per_page? response = nil
Expand All @@ -151,7 +159,7 @@ def show_pagination? response = nil
##
# If no search parameters have been given, we should
# auto-focus the user's cursor into the searchbox
#
#
# @return [Boolean]
def should_autofocus_on_search_box?
controller.is_a? Blacklight::Catalog and
Expand All @@ -161,7 +169,7 @@ def should_autofocus_on_search_box?

##
# Does the document have a thumbnail to render?
#
#
# @param [SolrDocument]
# @return [Boolean]
def has_thumbnail? document
Expand All @@ -172,7 +180,7 @@ def has_thumbnail? document
##
# Render the thumbnail, if available, for a document and
# link it to the document record.
#
#
# @param [SolrDocument]
# @param [Hash] options to pass to the image tag
# @param [Hash] url options to pass to #link_to_document
Expand All @@ -197,7 +205,7 @@ def render_thumbnail_tag document, image_options = {}, url_options = {}

##
# Get the URL to a document's thumbnail image
#
#
# @param [SolrDocument]
# @return [String]
def thumbnail_url document
Expand All @@ -208,7 +216,7 @@ def thumbnail_url document

##
# Get url parameters to a search within a grouped result set
#
#
# @param [Blacklight::Solr::Response::Group]
# @return [Hash]
def add_group_facet_params_and_redirect group
Expand All @@ -217,7 +225,7 @@ def add_group_facet_params_and_redirect group

##
# Render the view type icon for the results view picker
#
#
# @param [String]
# @return [String]
def render_view_type_group_icon view
Expand All @@ -232,7 +240,7 @@ def render_view_type_group_icon view
def default_view_type_group_icon_classes view
"glyphicon-#{view.to_s.parameterize } view-icon-#{view.to_s.parameterize}"
end

def current_bookmarks response = nil
response ||= @response
@current_bookmarks ||= current_or_guest_user.bookmarks_for_documents(response.documents).to_a
Expand All @@ -246,10 +254,10 @@ def bookmarked? document

alias_method :is_bookmarked?, :bookmarked?
deprecation_deprecate :is_bookmarked?

def render_marc_tools
return unless defined? Blacklight::Marc

begin
# blacklight-marc 5.4+
render 'marc_tools'
Expand Down Expand Up @@ -280,7 +288,7 @@ def render_search_to_page_title_filter(facet, values)
filter_label = facet_field_label(facet_config.key)
filter_value = if values.size < 3
values.map {|value| facet_display_value(facet, value)}.to_sentence
else
else
t('blacklight.search.page_title.many_constraint_values', values: values.size)
end
t('blacklight.search.page_title.constraint', label: filter_label, value: filter_value)
Expand All @@ -306,4 +314,13 @@ def render_search_to_page_title(params)
constraints.join(' / ')
end

private

# @param [String] format
def feed_link_url(format)
new_params = params.respond_to?(:to_unsafe_h) ? params.to_unsafe_h : params

url_for new_params.merge(format: format)
end

end
Loading

0 comments on commit 60ec511

Please sign in to comment.