Skip to content

Commit

Permalink
re-unify bookmark#index and bookmark#export (and, while we're at it, …
Browse files Browse the repository at this point in the history
…add the other catalog#index response formats
  • Loading branch information
cbeer committed Mar 31, 2014
1 parent 1043a52 commit 9432568
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
48 changes: 27 additions & 21 deletions app/controllers/bookmarks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,31 @@ def search_action_url *args
catalog_index_url *args
end

before_filter :verify_user, :except => :export
before_filter :verify_user

def index
@bookmarks = current_or_guest_user.bookmarks
@bookmarks = current_guest_or_token_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)

format.endnote do
# Just concatenate individual endnote exports with blank lines. Not
# every record can be exported as endnote -- only include those that
# can.
render :text => @document_list.collect {|d| d.export_as(:endnote) if d.export_formats.keys.include? :endnote}.join("\n"), :layout => false
end
end
end

# Much like #index, but does NOT require authentication, instead
# gets an _encrypted and signed_ user_id in params, and it delivers
# that users bookmarks, in some export format.
#
# Used for third party services requiring callback urls, such as refworks,
# that need to export a certain users bookmarks without being auth'd as
# that user.
def export
user_id = decrypt_user_id params[:encrypted_user_id]

@bookmarks = User.find(user_id).bookmarks
bookmark_ids = @bookmarks.collect { |b| b.document_id.to_s }

@response, @document_list = get_solr_response_for_field_values(SolrDocument.unique_key, bookmark_ids)

respond_to do |format|
format.refworks_marc_txt do
# Just concatenate individual refworks_marc_txt exports with blank lines. Not
# every record can be exported as refworks_marc_txt -- only include those that
Expand Down Expand Up @@ -134,7 +124,9 @@ def clear

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

def start_new_search_session?
Expand Down Expand Up @@ -171,4 +163,18 @@ def message_encryptor
derived_secret = bookmarks_export_secret_token("bookmarks session key")
ActiveSupport::MessageEncryptor.new(derived_secret)
end

def current_guest_or_token_user
current_user || token_user || current_or_guest_user
end

def token_user
if params[:encrypted_user_id]
user_id = decrypt_user_id params[:encrypted_user_id]

@token_user ||= User.find(user_id)
else
nil
end
end
end
7 changes: 2 additions & 5 deletions app/helpers/bookmarks_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ module BookmarksHelper
# user's bookmarks in 'refworks marc txt' format -- we tell refworks
# to expect that format.
def bookmarks_refworks_export_url(user_id = current_or_guest_user.id)
callback_url = export_bookmarks_callback_url(
encrypt_user_id(current_or_guest_user.id),
:refworks_marc_txt,
params_for_search )
callback_url = bookmarks_url(params_for_search.merge(format: :refworks_marc_txt, encrypted_user_id: encrypt_user_id(current_or_guest_user.id) ))

"http://www.refworks.com/express/expressimport.asp?vendor=#{CGI.escape(application_name)}&filter=MARC%20Format&encoding=65001&url=#{CGI.escape(callback_url)}"
end
end
end
1 change: 0 additions & 1 deletion lib/blacklight/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ module RouteSets
def bookmarks(_)
add_routes do |options|
delete "bookmarks/clear", :to => "bookmarks#clear", :as => "clear_bookmarks"
get "bookmarks/export/:encrypted_user_id", :to => "bookmarks#export", :as => "export_bookmarks_callback"
resources :bookmarks
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/bookmarks_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
end
end

describe "export" do
describe ".refworks format" do
render_views

before(:all) do
Expand All @@ -52,7 +52,7 @@

User.should_receive(:find).with(user_id).and_return(@user_with_3)

get :export, :format => :refworks_marc_txt, :encrypted_user_id => encrypted_user_id
get :index, :format => :refworks_marc_txt, :encrypted_user_id => encrypted_user_id

expect(response.code).to eq "200"
# For some reason having trouble getting actual doc.export_as(:refworks_marc_txt)
Expand Down

0 comments on commit 9432568

Please sign in to comment.