Sha256: f00662602668876a9d4e197f2a1bddb592d19f2a79d180d4b431e6706b712a58

Contents?: true

Size: 1.5 KB

Versions: 6

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

class BookmarksController < CatalogController
  include Blacklight::Bookmarks

  # LOCAL OVERRIDE to render update.js.erb partial when bookmark created
  def create
    @bookmarks = params[:bookmarks].present? ? bookmark_params : default_bookmark_params

    current_or_guest_user.save! unless current_or_guest_user.persisted?

    success = @bookmarks.all? do |bookmark|
      next true if current_or_guest_user.bookmarks.exists?(bookmark)

      begin
        current_or_guest_user.bookmarks.create!(bookmark)
        next true
      rescue ActiveRecord::RecordInvalid
        break false
      end
    end

    if request.xhr?
      # success ? render(json: { bookmarks: { count: current_or_guest_user.bookmarks.count }}) : render(:text => "", :status => "500")
      success ? render(:update) : render(plain: '', status: '500')
    else
      if @bookmarks.any? && success
        flash[:notice] = I18n.t('blacklight.bookmarks.add.success', count: @bookmarks.count)
      elsif @bookmarks.any?
        flash[:error] = I18n.t('blacklight.bookmarks.add.failure', count: @bookmarks.count)
      end

      redirect_back fallback_location: bookmarks_path
    end
  end

  def folder_item_actions
    redirect_to action: 'index'
  end

  private

  def default_bookmark_params
    [{ document_id: params[:id], document_type: blacklight_config.document_model.to_s }]
  end

  def bookmark_params
    params.require(:bookmarks).map { |item_params| item_params.permit(:document_id, :document_type) }
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
bpluser-0.3.0 app/controllers/bookmarks_controller.rb
bpluser-0.2.0.4 app/controllers/bookmarks_controller.rb
bpluser-0.2.0.3 app/controllers/bookmarks_controller.rb
bpluser-0.2.0.2 app/controllers/bookmarks_controller.rb
bpluser-0.2.0.1 app/controllers/bookmarks_controller.rb
bpluser-0.2.0 app/controllers/bookmarks_controller.rb