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

[View source]

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_pathObject

[View source]

33
34
35
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 33

def action_success_redirect_path
  bookmarks_path
end

#clearObject

[View source]

129
130
131
132
133
134
135
136
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 129

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.

[View source]

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

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

#destroyObject

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

[View source]

103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 103

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 :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

#indexObject

[View source]

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(*args) ⇒ Object

Blacklight uses #search_action_url to figure out the right URL for the global search box

[View source]

39
40
41
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 39

def search_action_url *args
  search_catalog_url(*args)
end

#search_service_contextHash

Returns a hash of context information to pass through to the search service.

Returns:

  • (Hash)

    a hash of context information to pass through to the search service

[View source]

44
45
46
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 44

def search_service_context
  { bookmarks: @bookmarks }
end

#updateObject

[View source]

62
63
64
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 62

def update
  create
end