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/#update to /bookmarks/$docuemnt_id instead.
-
#destroy ⇒ Object
Beware, :id is the Solr document_id, not the actual Bookmark id.
- #index ⇒ Object
- #permit_bookmarks ⇒ Object protected
-
#search_action_url(*args) ⇒ Object
Blacklight uses #search_action_url to figure out the right URL for the global search box.
- #start_new_search_session? ⇒ Boolean protected
- #update ⇒ Object
- #verify_user ⇒ Object protected
Instance Method Details
#action_documents ⇒ Object
25 26 27 28 29 |
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 25 def action_documents bookmarks = token_or_current_or_guest_user.bookmarks bookmark_ids = bookmarks.collect { |b| b.document_id.to_s } fetch(bookmark_ids) end |
#action_success_redirect_path ⇒ Object
31 32 33 |
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 31 def action_success_redirect_path bookmarks_path end |
#clear ⇒ Object
137 138 139 140 141 142 143 144 |
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 137 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/#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[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 90 91 92 93 94 95 96 97 98 99 100 101 |
# 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? 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(plain: "", status: "500") 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 if respond_to? :redirect_back redirect_back fallback_location: bookmarks_path else # Deprecated in Rails 5.0 redirect_to :back end end end |
#destroy ⇒ Object
Beware, :id is the Solr document_id, not the actual Bookmark id. idempotent, as DELETE is supposed to be.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 105 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 && bookmark.delete && bookmark.destroyed? end if success if request.xhr? render(json: { bookmarks: { count: current_or_guest_user.bookmarks.count }}) elsif respond_to? :redirect_back redirect_back fallback_location: bookmarks_path, notice: I18n.t('blacklight.bookmarks.remove.success') else # Deprecated in Rails 5.0 redirect_to :back, notice: I18n.t('blacklight.bookmarks.remove.success') end elsif request.xhr? head 500 # ajaxy request needs no redirect and should not have flash set elsif respond_to? :redirect_back redirect_back fallback_location: bookmarks_path, flash: { error: I18n.t('blacklight.bookmarks.remove.failure') } else # Deprecated in Rails 5.0 redirect_to :back, flash: { error: I18n.t('blacklight.bookmarks.remove.failure') } end end |
#index ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 41 def index @bookmarks = token_or_current_or_guest_user.bookmarks bookmark_ids = @bookmarks.collect { |b| b.document_id.to_s } @response, @document_list = fetch(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 |
#permit_bookmarks ⇒ Object (protected)
158 159 160 |
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 158 def permit_bookmarks params.permit(bookmarks: [:document_id, :document_type]) end |
#search_action_url(*args) ⇒ Object
Blacklight uses #search_action_url to figure out the right URL for the global search box
37 38 39 |
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 37 def search_action_url *args search_catalog_url(*args) end |
#start_new_search_session? ⇒ Boolean (protected)
154 155 156 |
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 154 def start_new_search_session? action_name == "index" end |
#update ⇒ Object
60 61 62 |
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 60 def update create end |
#verify_user ⇒ Object (protected)
148 149 150 151 152 |
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 148 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 |