Class: FolderItemsActionsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/folder_items_actions_controller.rb

Overview

use to share folder item actions (email, cite, delete) between folders and bookmarks

Instance Method Summary collapse

Instance Method Details

#folder_item_actionsObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/folder_items_actions_controller.rb', line 5

def folder_item_actions
  @folder = Bpluser::Folder.find(params[:id]) if params[:origin] == 'folders'
  @user = current_or_guest_user

  unless params[:selected]
    flash[:error] = t('blacklight.folders.update_items.remove.no_items')
    redirect_back(fallback_location: root_path)
  end

  items = params[:selected]
  case params[:commit]
  # email
  when t('blacklight.tools.email')
    redirect_to email_solr_document_path(id: items)
  # cite
  when t('blacklight.tools.citation')
    redirect_to citation_solr_document_path(id: items)
  # remove
  when t('blacklight.tools.remove')
    if params[:origin] == 'folders'
      if @folder.folder_items.destroy_by(document_id: items)
        flash[:notice] = t('blacklight.folders.update_items.remove.success')
      else
        flash[:error] = t('blacklight.folders.update_items.remove.failure')
      end
      redirect_to folder_path(@folder, view_params)
    else
      if current_or_guest_user.bookmarks.destroy_by(document_id: items)
        flash[:notice] = t('blacklight.folders.update_items.remove.success')
      else
        flash[:error] = t('blacklight.folders.update_items.remove.failure')
      end
      redirect_to bookmarks_path(view_params)
    end
  # copy
  when /#{t('blacklight.tools.copy_to')}/
    destination = params[:commit].split("#{t('blacklight.tools.copy_to')} ")[1]
    if destination == t('blacklight.bookmarks.title')
      success = items.all? do |item_id|
        next true if current_or_guest_user.bookmarks.exists?(document_id: item_id)

        current_or_guest_user.bookmarks.create(document_id: item_id)
      end
    else
      folder_to_update = current_user.folders.find(destination)
      success = items.all? do |item_id|
        next true if folder_to_update.folder_item?(item_id)

        begin
          folder_to_update.folder_items.create!(document_id: item_id)
          next true
        rescue ActiveRecord::RecordInvalid
          break false
        end
      end
    end

    if success
      folder_display_name = destination == t('blacklight.bookmarks.title') ? t('blacklight.bookmarks.title') : folder_to_update.title
      flash[:notice] = t('blacklight.folders.update_items.copy.success', folder_name: folder_display_name)
    else
      flash[:error] = t('blacklight.folders.update_items.copy.failure')
    end
    redirect_back(fallback_location: root_path)
  end
end