Skip to content

Commit

Permalink
Use Rails resourceful routing concerns to inject blacklight-required …
Browse files Browse the repository at this point in the history
…model and controller routing
  • Loading branch information
cbeer committed Nov 4, 2015
1 parent b492a82 commit 31ae8f5
Show file tree
Hide file tree
Showing 36 changed files with 183 additions and 278 deletions.
2 changes: 1 addition & 1 deletion app/controllers/concerns/blacklight/bookmarks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def action_success_redirect_path
# Blacklight uses #search_action_url to figure out the right URL for
# the global search box
def search_action_url *args
catalog_index_url *args
search_catalog_url *args
end

def index
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/concerns/blacklight/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def blacklight_path
# which action the search form should use
def search_action_url options = {}
# Rails 4.2 deprecated url helpers accepting string keys for 'controller' or 'action'
catalog_index_url(options.except(:controller, :action))
search_catalog_url(options.except(:controller, :action))
end

def search_action_path *args
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/saved_searches_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def clear
else
flash[:error] = I18n.t('blacklight.saved_searches.clear.failure')
end
redirect_to :action => "index"
redirect_to blacklight.saved_searches_url
end


Expand Down
9 changes: 8 additions & 1 deletion app/helpers/blacklight/component_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ def document_action_label action, opts
end

def document_action_path action_opts, url_opts = nil
self.send(action_opts.path ||"#{action_opts.key}_#{controller_name}_path", url_opts)
case
when action_opts.path
self.send(action_opts.path, url_opts)
when url_opts[:id].class.respond_to?(:model_name)
url_for([action_opts.key, url_opts[:id]])
else
self.send("#{action_opts.key}_#{controller_name}_path", url_opts)
end
end

##
Expand Down
8 changes: 7 additions & 1 deletion app/helpers/blacklight/url_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ def session_tracking_params document, counter
##
# Get the URL for tracking search sessions across pages using polymorphic routing
def session_tracking_path document, params = {}
polymorphic_path([:track, document], params)
controller_tracking_method = "track_#{controller_name}_path"

if respond_to? controller_tracking_method
send(controller_tracking_method, params.merge(id: document.id))
else
polymorphic_path(document, params)
end
end

#
Expand Down
2 changes: 1 addition & 1 deletion app/views/blacklight/nav/_saved_searches.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<%= link_to t('blacklight.header_links.saved_searches'), saved_searches_path %>
<%= link_to t('blacklight.header_links.saved_searches'), blacklight.saved_searches_path %>
2 changes: 1 addition & 1 deletion app/views/blacklight/nav/_search_history.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<%= link_to t('blacklight.header_links.search_history'), search_history_path %>
<%= link_to t('blacklight.header_links.search_history'), blacklight.search_history_path %>
4 changes: 2 additions & 2 deletions app/views/saved_searches/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@

<%- else -%>
<p>
<%= link_to t('blacklight.saved_searches.clear.action_title'), clear_saved_searches_path, :method => :delete, :data => { :confirm => t('blacklight.saved_searches.clear.action_confirm') } %>
<%= link_to t('blacklight.saved_searches.clear.action_title'), blacklight.clear_saved_searches_path, :method => :delete, :data => { :confirm => t('blacklight.saved_searches.clear.action_confirm') } %>
</p>

<h3 class='section-heading'><%= t('blacklight.saved_searches.list_title') %></h3>
<table class="table table-striped">
<%- @searches.each do |search| -%>
<tr>
<td><%= link_to_previous_search(search.query_params) %></td>
<td><%= button_to t('blacklight.saved_searches.delete'), forget_search_path(search.id) %></td>
<td><%= button_to t('blacklight.saved_searches.delete'), blacklight.forget_search_path(search.id) %></td>
</tr>
<%- end -%>
</table>
Expand Down
6 changes: 3 additions & 3 deletions app/views/search_history/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<%- if @searches.blank? -%>
<h3 class='section-heading'><%=t('blacklight.search_history.no_history')%></h3>
<%- else -%>
<%= link_to t('blacklight.search_history.clear.action_title'), clear_search_history_path, :method => :delete, :data => { :confirm => t('blacklight.search_history.clear.action_confirm') }, :class => 'btn btn-danger pull-right' %>
<%= link_to t('blacklight.search_history.clear.action_title'), blacklight.clear_search_history_path, :method => :delete, :data => { :confirm => t('blacklight.search_history.clear.action_confirm') }, :class => 'btn btn-danger pull-right' %>
<h3 class='section-heading'><%=t('blacklight.search_history.recent')%></h3>
<table class="table table-striped search_history">
<%- @searches.each_with_index do |search,index| -%>
Expand All @@ -14,9 +14,9 @@
<%- if has_user_authentication_provider? -%>
<td class="actions">
<%- if current_or_guest_user && search.saved? -%>
<%= button_to t('blacklight.search_history.forget'), forget_search_path(search.id), :class => 'btn btn-default' %>
<%= button_to t('blacklight.search_history.forget'), blacklight.forget_search_path(search.id), :class => 'btn btn-default' %>
<%- else -%>
<%= button_to t('blacklight.search_history.save'), save_search_path(search.id), :method => :put, :class => 'btn btn-default' %>
<%= button_to t('blacklight.search_history.save'), blacklight.save_search_path(search.id), :method => :put, :class => 'btn btn-default' %>
<%- end -%>
</td>
<%- end -%>
Expand Down
9 changes: 9 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Blacklight::Engine.routes.draw do
get "search_history", :to => "search_history#index", :as => "search_history"
delete "search_history/clear", :to => "search_history#clear", :as => "clear_search_history"
delete "saved_searches/clear", :to => "saved_searches#clear", :as => "clear_saved_searches"
get "saved_searches", :to => "saved_searches#index", :as => "saved_searches"
put "saved_searches/save/:id", :to => "saved_searches#save", :as => "save_search"
delete "saved_searches/forget/:id", :to => "saved_searches#forget", :as => "forget_search"
post "saved_searches/forget/:id", :to => "saved_searches#forget"
end
1 change: 0 additions & 1 deletion lib/blacklight/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ class Engine < Rails::Engine
engine_name "blacklight"

require 'bootstrap-sass'
require 'blacklight/rails/routes'

# BlacklightHelper is needed by all helpers, so we inject it
# into action view base here.
Expand Down
29 changes: 0 additions & 29 deletions lib/blacklight/rails/routes.rb

This file was deleted.

129 changes: 3 additions & 126 deletions lib/blacklight/routes.rb
Original file line number Diff line number Diff line change
@@ -1,130 +1,7 @@
# -*- encoding : utf-8 -*-
module Blacklight
class Routes
attr_reader :resources

# adds as class and instance level accessors, default_route_sets
# returns an array of symbols for method names that define routes.
# Order is important:. (e.g. /catalog/email precedes /catalog/:id)
#
# Add-ons that want to add routes into default routing might
# monkey-patch Blacklight::Routes, say:
#
# module MyWidget::Routes
# extend ActiveSupport::Concern
# included do |klass|
# klass.default_route_sets += [:widget_routing]
# end
# def widget_routing(primary_resource)
# add_routes do |options|
# get "#{primary_resource}/widget", "#{primary_resource}#widget"
# end
# end
# end
# Blacklight::Routes.send(:include, MyWidget::Routes)
class_attribute :default_route_sets
self.default_route_sets = [:bookmarks, :search_history, :saved_searches, :export, :solr_document]

def initialize(router, options)
@router = router
@options = options
@resources = options.fetch(:resources, [:catalog])
end

def draw
route_sets.each do |r|
self.send(r, primary_resource)
end
resources.each do |r|
self.map_resource(r)
end
end

protected

def primary_resource
resources.first
end

def add_routes &blk
@router.instance_exec(@options, &blk)
end

def route_sets
(@options[:only] || default_route_sets) - (@options[:except] || [])
end



module RouteSets
def bookmarks(_)
add_routes do |options|
delete "bookmarks/clear", :to => "bookmarks#clear", :as => "clear_bookmarks"
get "bookmarks/email", :as => "email_bookmarks"
post "bookmarks/email"
get "bookmarks/sms", :as => "sms_bookmarks"
post "bookmarks/sms"
get "bookmarks/citation", :as => "citation_bookmarks"

args = {}
args[:constraints] = options[:constraints] if options[:constraints]
resources :bookmarks, args
end
end

def search_history(_)
add_routes do |options|
get "search_history", :to => "search_history#index", :as => "search_history"
delete "search_history/clear", :to => "search_history#clear", :as => "clear_search_history"
end
end


def saved_searches(_)
add_routes do |options|
delete "saved_searches/clear", :to => "saved_searches#clear", :as => "clear_saved_searches"
get "saved_searches", :to => "saved_searches#index", :as => "saved_searches"
put "saved_searches/save/:id", :to => "saved_searches#save", :as => "save_search"
delete "saved_searches/forget/:id", :to => "saved_searches#forget", :as => "forget_search"
post "saved_searches/forget/:id", :to => "saved_searches#forget"
end
end

def map_resource(key)
add_routes do |options|
get "#{key}/facet/:id", :to => "#{key}#facet", :as => "#{key}_facet"
get "#{key}", :to => "#{key}#index", :as => "#{key}_index"
end
end

def export(primary_resource)
add_routes do |options|
get "#{primary_resource}/opensearch", :as => "opensearch_#{primary_resource}"
get "#{primary_resource}/citation", :as => "citation_#{primary_resource}"
get "#{primary_resource}/email", :as => "email_#{primary_resource}"
post "#{primary_resource}/email"
get "#{primary_resource}/sms", :as => "sms_#{primary_resource}"
post "#{primary_resource}/sms"
end
end

def solr_document(primary_resource)
add_routes do |options|

args = {only: [:show]}
args[:constraints] = options[:constraints] if options[:constraints]

resources :solr_document, args.merge(path: primary_resource, controller: primary_resource) do
member do
post "track"
end
end

# :show and :update are for backwards-compatibility with catalog_url named routes
resources primary_resource, args
end
end
end
include RouteSets
module Routes
require 'blacklight/routes/exportable'
require 'blacklight/routes/searchable'
end
end
25 changes: 25 additions & 0 deletions lib/blacklight/routes/exportable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Blacklight
module Routes
class Exportable
def initialize(defaults = {})
@defaults = defaults
end

def call(mapper, options = {})
options = @defaults.merge(options)

mapper.member do
mapper.match 'email', via: [:get, :post]
mapper.match 'sms', via: [:get, :post]
mapper.get 'citation'
end

mapper.collection do
mapper.match 'email', via: [:get, :post]
mapper.match 'sms', via: [:get, :post]
mapper.get 'citation'
end
end
end
end
end
20 changes: 20 additions & 0 deletions lib/blacklight/routes/searchable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Blacklight
module Routes
class Searchable
def initialize(defaults = {})
@defaults = defaults
end

def call(mapper, options = {})
options = @defaults.merge(options)

mapper.match '/', action: 'index', as: 'search', via: [:get, :post]

mapper.post ":id/track", action: 'track', as: 'track'

mapper.get "opensearch"
mapper.get "facet/:id", action: 'facet', as: 'facet'
end
end
end
end
11 changes: 7 additions & 4 deletions lib/generators/blacklight/controller_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ def create_blacklight_catalog
end

def inject_blacklight_routes
# These will end up in routes.rb file in reverse order
# we add em, since each is added at the top of file.
# we want "root" to be FIRST for optimal url generation.
route("blacklight_for :#{controller_name}")
route <<-EOF
concern :searchable, Blacklight::Routes::Searchable.new
resource :catalog, only: [:index], as: 'catalog', path: '/catalog', controller: 'catalog' do
concerns :searchable
end
EOF
end
end
end
6 changes: 6 additions & 0 deletions lib/generators/blacklight/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,11 @@ def generate_blacklight_marc_demo
generate 'blacklight:marc:install'
end
end

def add_routes
route <<-EOF
mount Blacklight::Engine => '/'
EOF
end
end
end
18 changes: 18 additions & 0 deletions lib/generators/blacklight/models_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ def copy_migrations
rake "blacklight:install:migrations"
end

def add_routes
route <<-EOF
concern :exportable, Blacklight::Routes::Exportable.new
resources :solr_documents, only: [:show], path: '/catalog', controller: 'catalog' do
concerns :exportable
end
resources :bookmarks do
concerns :exportable
collection do
delete 'clear'
end
end
EOF
end


end
end
Loading

0 comments on commit 31ae8f5

Please sign in to comment.