Skip to content

Commit

Permalink
Refactor email/sms/cite into actions within extendable action framework
Browse files Browse the repository at this point in the history
  • Loading branch information
cjcolvar authored and cbeer committed Nov 10, 2014
1 parent 9207d38 commit b979bce
Show file tree
Hide file tree
Showing 18 changed files with 307 additions and 260 deletions.
166 changes: 3 additions & 163 deletions app/controllers/bookmarks_controller.rb
@@ -1,166 +1,6 @@
# -*- encoding : utf-8 -*-
# note that while this is mostly restful routing, the #update and #destroy actions
# take the Solr document ID as the :id, NOT the id of the actual Bookmark action.
class BookmarksController < CatalogController
class BookmarksController < CatalogController

##
# Give Bookmarks access to the CatalogController configuration
include Blacklight::Configurable
include Blacklight::SolrHelper
include Blacklight::Bookmarks

copy_blacklight_config_from(CatalogController)

rescue_from Blacklight::Exceptions::ExpiredSessionToken do
head :unauthorized
end

# 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
end

before_filter :verify_user

def index
@bookmarks = token_or_current_or_guest_user.bookmarks
bookmark_ids = @bookmarks.collect { |b| b.document_id.to_s }

@response, @document_list = get_solr_response_for_document_ids(bookmark_ids)

respond_to do |format|
format.html { }
format.rss { render :layout => false }
format.atom { render :layout => false }
format.json do
render json: render_search_results_as_json
end

additional_response_formats(format)
document_export_formats(format)
end
end


def update
create
end

# For adding a single bookmark, suggest use PUT/#update to
# /bookmarks/$docuemnt_id instead.
# But this method, accessed via POST to /bookmarks, can be used for
# creating multiple bookmarks at once, by posting with keys
# such as bookmarks[n][document_id], bookmarks[n][title].
# It can also be used for creating a single bookmark by including keys
# bookmark[title] and bookmark[document_id], but in that case #update
# is simpler.
def create
if params[:bookmarks]
@bookmarks = params[:bookmarks]
else
@bookmarks = [{ document_id: params[:id], document_type: blacklight_config.solr_document_model.to_s }]
end

current_or_guest_user.save! unless current_or_guest_user.persisted?

success = @bookmarks.all? do |bookmark|
current_or_guest_user.bookmarks.where(bookmark).exists? || current_or_guest_user.bookmarks.create(bookmark)
end

if request.xhr?
success ? render(json: { bookmarks: { count: current_or_guest_user.bookmarks.count }}) : render(:text => "", :status => "500")
else
if @bookmarks.length > 0 && success
flash[:notice] = I18n.t('blacklight.bookmarks.add.success', :count => @bookmarks.length)
elsif @bookmarks.length > 0
flash[:error] = I18n.t('blacklight.bookmarks.add.failure', :count => @bookmarks.length)
end

redirect_to :back
end
end

# Beware, :id is the Solr document_id, not the actual Bookmark id.
# idempotent, as DELETE is supposed to be.
def destroy
bookmark = current_or_guest_user.bookmarks.where(document_id: params[:id], document_type: blacklight_config.solr_document_model).first

success = bookmark && bookmark.delete && bookmark.destroyed?

unless request.xhr?
if success
flash[:notice] = I18n.t('blacklight.bookmarks.remove.success')
else
flash[:error] = I18n.t('blacklight.bookmarks.remove.failure')
end
redirect_to :back
else
# ajaxy request needs no redirect and should not have flash set
success ? render(json: { bookmarks: { count: current_or_guest_user.bookmarks.count }}) : render(:text => "", :status => "500")
end
end

def clear
if current_or_guest_user.bookmarks.clear
flash[:notice] = I18n.t('blacklight.bookmarks.clear.success')
else
flash[:error] = I18n.t('blacklight.bookmarks.clear.failure')
end
redirect_to :action => "index"
end

protected
def verify_user
unless current_or_guest_user or (action == "index" and token_or_current_or_guest_user)
flash[:notice] = I18n.t('blacklight.bookmarks.need_login') and raise Blacklight::Exceptions::AccessDenied
end
end

def start_new_search_session?
action_name == "index"
end

# Used for #export action, with encrypted user_id.
def decrypt_user_id(encrypted_user_id)
user_id, timestamp = message_encryptor.decrypt_and_verify(encrypted_user_id)

if timestamp < 1.hour.ago
raise Blacklight::Exceptions::ExpiredSessionToken.new
end

user_id
end

# Used for #export action with encrypted user_id, available
# as a helper method for views.
def encrypt_user_id(user_id)
message_encryptor.encrypt_and_sign([user_id, Time.now])
end
helper_method :encrypt_user_id

##
# This method provides Rails 3 compatibility to our message encryptor.
# When we drop support for Rails 3, we can just use the AS::KeyGenerator
# directly instead of this helper.
def bookmarks_export_secret_token salt
OpenSSL::PKCS5.pbkdf2_hmac_sha1(Blacklight.secret_key, salt, 1000, 64)
end

def message_encryptor
derived_secret = bookmarks_export_secret_token("bookmarks session key")
ActiveSupport::MessageEncryptor.new(derived_secret)
end

def token_or_current_or_guest_user
token_user || current_or_guest_user
end

def token_user
@token_user ||= if params[:encrypted_user_id]
user_id = decrypt_user_id params[:encrypted_user_id]
User.find(user_id)
else
nil
end
end
end
end
14 changes: 6 additions & 8 deletions app/views/bookmarks/_tools.html.erb
@@ -1,11 +1,9 @@
<ul class="bookmarkTools nav nav-pills">
<li class="cite">
<%= link_to t('blacklight.tools.cite'), citation_catalog_path(:sort=>params[:sort], :per_page=>params[:per_page], :id => @response.documents.map {|doc| doc.id}), {:id => 'citeLink', :name => 'citation', :class => 'btn btn-default', :data => {:ajax_modal => "trigger"}} %>
</li>

<li class="email">
<%= link_to t('blacklight.tools.email'), email_catalog_path(:sort=>params[:sort], :per_page=>params[:per_page], :id => @response.documents.map {|doc| doc.id}), :class=>"btn btn-default", :id => "emailLink", :data => {:ajax_modal => "trigger"} %>
</li>
<ul class="<%= controller_name %>Tools nav nav-pills">
<% document_actions.each do |action| %>
<li class="<%= action %>">
<%= link_to t("blacklight.tools.#{action}"), self.send("#{action}_#{controller_name}_path"), {:id => "#{action}Link", :name => action, :class => 'btn btn-default', :data => {:ajax_modal => "trigger"}} %>
</li>
<% end %>
<%= render_marc_tools %>
</ul>
2 changes: 1 addition & 1 deletion app/views/catalog/_email_form.html.erb
@@ -1,4 +1,4 @@
<%= form_tag url_for(:controller => "catalog", :action => "email"), :id => 'email_form', :class => "form-horizontal ajax_form", :method => :post do %>
<%= form_tag url_for(:controller => controller_name, :action => "email"), :id => 'email_form', :class => "form-horizontal ajax_form", :method => :post do %>

<div class="modal-body">
<%= render :partial=>'/flash_msg' %>
Expand Down
18 changes: 4 additions & 14 deletions app/views/catalog/_show_tools.html.erb
Expand Up @@ -17,11 +17,6 @@
<li>
<%= render_show_doc_actions @document %>
</li>
<% if (@document.respond_to?(:export_as_mla_citation_txt) || @document.respond_to?(:export_as_apa_citation_txt)) %>
<li class="cite">
<%= link_to t('blacklight.tools.cite'), citation_catalog_path(:id => @document), {:id => 'citeLink', :data => {:ajax_modal => "trigger"}} %>
</li>
<% end %>
<% if @document.export_formats.keys.include?( :refworks_marc_txt ) %>
<li class="refworks">
<%= link_to t('blacklight.tools.refworks'), refworks_export_url(url: polymorphic_url(url_for_document(@document), format: :refworks_marc_txt, only_path: false)) %>
Expand All @@ -32,16 +27,11 @@
<%= link_to t('blacklight.tools.endnote'), catalog_path(@document, :format => 'endnote') %>
</li>
<% end %>
<% if @document.respond_to?( :to_email_text ) %>
<li class="email">
<%= link_to t('blacklight.tools.email'), email_catalog_path(:id => @document), {:id => 'emailLink', :data => {:ajax_modal => "trigger"}} %>
</li>
<%- end -%>
<%- if @document.respond_to?( :to_sms_text ) -%>
<li class="sms">
<%= link_to t('blacklight.tools.sms'), sms_catalog_path(:id => @document), {:id => 'smsLink', :data => {:ajax_modal => "trigger"}} %>
<% document_actions.each do |action| %>
<li class="#{action}">
<%= link_to t("blacklight.tools.#{action}"), self.send("#{action}_#{controller_name}_path", :id => @document), {:id => "#{action}Link", :data => {:ajax_modal => "trigger"}} %>
</li>
<%- end -%>
<% end %>
<% if respond_to? :librarian_view_catalog_path and @document.respond_to?(:to_marc) %>
<li class="librarian_view">
Expand Down
2 changes: 1 addition & 1 deletion app/views/catalog/_sms_form.html.erb
@@ -1,4 +1,4 @@
<%= form_tag url_for(:controller => "catalog", :action => "sms"), :id => 'sms_form', :class => "form-horizontal ajax_form", :method => :post do %>
<%= form_tag url_for(:controller => controller_name, :action => "sms"), :id => 'sms_form', :class => "form-horizontal ajax_form", :method => :post do %>
<div class="modal-body">
<%= render :partial=>'/flash_msg' %>
<div class="form-group">
Expand Down
2 changes: 1 addition & 1 deletion app/views/catalog/citation.js.erb
@@ -1,6 +1,6 @@
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3><%= t('blacklight.tools.cite') %></h3>
<h3><%= t('blacklight.tools.citation') %></h3>
</div>
<div class="modal-body">
<%= render :partial => 'citation' -%>
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion config/locales/blacklight.en.yml
Expand Up @@ -80,7 +80,7 @@ en:

tools:
title: 'Tools'
cite: 'Cite'
citation: 'Cite'
endnote: 'Export to EndNote'
refworks: 'Export to Refworks'
email: 'Email'
Expand Down
2 changes: 1 addition & 1 deletion config/locales/blacklight.es.yml
Expand Up @@ -80,7 +80,7 @@ es:

tools:
title: 'Herramientas'
cite: 'Cité'
citation: 'Cité'
endnote: 'Exportar a EndNote'
refworks: 'Exportar a Refworks'
email: 'Correo electrónico'
Expand Down
2 changes: 1 addition & 1 deletion config/locales/blacklight.fr.yml
Expand Up @@ -90,7 +90,7 @@ fr:

tools:
title: 'Outils'
cite: 'Citer'
citation: 'Citer'
endnote: 'Exporter vers EndNote ou Zotero'
refworks: 'Exporter vers Refworks'
email: 'Envoyer'
Expand Down
2 changes: 1 addition & 1 deletion config/locales/blacklight.pt-BR.yml
Expand Up @@ -101,7 +101,7 @@ pt-BR:

tools:
title: 'Ferramentas'
cite: 'Citar'
citation: 'Citar'
endnote: 'Exportar para EndNote'
refworks: 'Exportar para Refworks'
email: 'Email'
Expand Down
1 change: 1 addition & 0 deletions lib/blacklight.rb
Expand Up @@ -19,6 +19,7 @@ module Blacklight
autoload :Controller, 'blacklight/controller'
autoload :Base, 'blacklight/base'
autoload :Catalog, 'blacklight/catalog'
autoload :Bookmarks, 'blacklight/bookmarks'
autoload :DocumentPresenter, 'blacklight/document_presenter'

autoload :Routes, 'blacklight/routes'
Expand Down

0 comments on commit b979bce

Please sign in to comment.