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

Instance Method Details

#action_documentsObject



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 }
  search_service.fetch(bookmark_ids, rows: bookmark_ids.count)
end

#action_success_redirect_pathObject



31
32
33
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 31

def action_success_redirect_path
  bookmarks_path
end

#clearObject



124
125
126
127
128
129
130
131
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 124

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

#createObject

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.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 67

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

  if request.xhr?
    success ? render(json: { bookmarks: { count: current_or_guest_user.bookmarks.count } }) : render(json: current_or_guest_user.errors.full_messages, 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

    redirect_back fallback_location: bookmarks_path
  end
end

#destroyObject

Beware, :id is the Solr document_id, not the actual Bookmark id. idempotent, as DELETE is supposed to be.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 98

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 } })
    else
      redirect_back fallback_location: bookmarks_path, notice: I18n.t('blacklight.bookmarks.remove.success')
    end
  elsif request.xhr?
    head 500 # 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

#indexObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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, deprecated_document_list = search_service.fetch(bookmark_ids)
  @document_list = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(deprecated_document_list, "The @document_list instance variable is now deprecated and will be removed in Blacklight 8.0")

  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(*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

#updateObject



57
58
59
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 57

def update
  create
end