Module: Blacklight::Bookmarks
- Extended by:
- ActiveSupport::Concern
- Included in:
- BookmarksController
- Defined in:
- app/controllers/concerns/blacklight/bookmarks.rb
Overview
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.
Instance Method Summary collapse
- #action_documents ⇒ Object
- #action_success_redirect_path ⇒ Object
- #clear ⇒ Object
-
#create ⇒ Object
For adding a single bookmark, suggest use PUT to /bookmarks/:document_id instead (triggering the #update method).
-
#create_response(success) ⇒ Object
Override the create_response method to handle the response appropriately.
-
#destroy ⇒ Object
Beware, :id is the Solr document_id, not the actual Bookmark id.
-
#destroy_response(success) ⇒ Object
Override the destroy_response method to handle the response appropriately.
- #index ⇒ Object
-
#search_action_url ⇒ Object
Blacklight uses #search_action_url to figure out the right URL for the global search box.
-
#search_service_context ⇒ Hash
A hash of context information to pass through to the search service.
- #update ⇒ Object
Instance Method Details
#action_documents ⇒ Object
27 28 29 30 31 |
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 27 def action_documents bookmarks = token_or_current_or_guest_user.bookmarks bookmark_ids = bookmarks.collect { |b| b.document_id.to_s } search_service.fetch(bookmark_ids, rows: bookmark_ids.count) end |
#action_success_redirect_path ⇒ Object
33 34 35 |
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 33 def action_success_redirect_path bookmarks_path end |
#clear ⇒ Object
139 140 141 142 143 144 145 146 |
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 139 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 |
#create ⇒ Object
For adding a single bookmark, suggest use PUT to /bookmarks/:document_id instead (triggering the #update method). This method, accessed via POST to /bookmarks, can be used for creating multiple bookmarks at once, by posting with keys such as bookmarks[document_id], bookmarks[title]. It can also be used for creating a single bookmark by including keys bookmark and bookmark, but in that case #update is simpler.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 72 def create @bookmarks = if params[:bookmarks] permit_bookmarks[:bookmarks] else [{ document_id: params[:id], document_type: blacklight_config.document_model.to_s }] end current_or_guest_user.save! unless current_or_guest_user.persisted? bookmarks_to_add = @bookmarks.reject { |bookmark| current_or_guest_user.bookmarks.where(bookmark).exists? } success = ActiveRecord::Base.transaction do current_or_guest_user.bookmarks.create!(bookmarks_to_add) rescue ActiveRecord::RecordInvalid false end create_response(success) end |
#create_response(success) ⇒ Object
Override the create_response method to handle the response appropriately.
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 92 def create_response(success) if request.xhr? success ? render(json: { bookmarks: { count: current_or_guest_user.bookmarks.count } }) : render(json: current_or_guest_user.errors., status: :internal_server_error) else if @bookmarks.any? && success flash[:notice] = I18n.t('blacklight.bookmarks.add.success', count: @bookmarks.length) elsif @bookmarks.any? flash[:error] = I18n.t('blacklight.bookmarks.add.failure', count: @bookmarks.length) end redirect_back fallback_location: bookmarks_path end end |
#destroy ⇒ Object
Beware, :id is the Solr document_id, not the actual Bookmark id. idempotent, as DELETE is supposed to be.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 108 def destroy @bookmarks = if params[:bookmarks] permit_bookmarks[:bookmarks] else [{ document_id: params[:id], document_type: blacklight_config.document_model.to_s }] end success = @bookmarks.all? do |bookmark| bookmark = current_or_guest_user.bookmarks.find_by(bookmark) bookmark&.delete && bookmark.destroyed? end destroy_response(success) end |
#destroy_response(success) ⇒ Object
Override the destroy_response method to handle the response appropriately.
125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 125 def destroy_response(success) if success if request.xhr? render(json: { bookmarks: { count: current_or_guest_user.bookmarks.count } }) else redirect_back fallback_location: bookmarks_path, notice: I18n.t('blacklight.bookmarks.remove.success') end elsif request.xhr? head :internal_server_error # ajaxy request needs no redirect and should not have flash set else redirect_back fallback_location: bookmarks_path, flash: { error: I18n.t('blacklight.bookmarks.remove.failure') } end end |
#index ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 48 def index @bookmarks = token_or_current_or_guest_user.bookmarks @response = search_service.search_results respond_to do |format| format.html {} format.rss { render layout: false } format.atom { render layout: false } additional_response_formats(format) document_export_formats(format) end end |
#search_action_url ⇒ Object
Blacklight uses #search_action_url to figure out the right URL for the global search box
39 40 41 |
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 39 def search_action_url(*) search_catalog_url(*) end |
#search_service_context ⇒ Hash
Returns a hash of context information to pass through to the search service.
44 45 46 |
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 44 def search_service_context { bookmarks: @bookmarks } end |
#update ⇒ Object
62 63 64 |
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 62 def update create end |