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



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

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)
end

#action_success_redirect_pathObject



35
36
37
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 35

def action_success_redirect_path
  bookmarks_path
end

#clearObject



133
134
135
136
137
138
139
140
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 133

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.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 66

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.



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 86

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.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_or_to(bookmarks_path)
  end
end

#destroyObject

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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 102

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.



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

def destroy_response(success)
  if success
    if request.xhr?
      render(json: { bookmarks: { count: current_or_guest_user.bookmarks.count } })
    else
      redirect_back_or_to(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_or_to(bookmarks_path, flash: { error: I18n.t('blacklight.bookmarks.remove.failure') })
  end
end

#indexObject



50
51
52
53
54
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 50

def index
  @bookmarks = token_or_current_or_guest_user.bookmarks

  super
end

#search_action_urlObject

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



41
42
43
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 41

def search_action_url(*)
  search_catalog_url(*)
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



46
47
48
# File 'app/controllers/concerns/blacklight/bookmarks.rb', line 46

def search_service_context
  { bookmarks: @bookmarks }
end

#updateObject



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

def update
  create
end