Class: Admin::DocumentAssetsController

Inherits:
AdminController
  • Object
show all
Defined in:
app/controllers/admin/document_assets_controller.rb

Instance Method Summary collapse

Instance Method Details

#asset_is_collection_thumbnail?Boolean

Returns:

  • (Boolean)


168
169
170
# File 'app/controllers/admin/document_assets_controller.rb', line 168

def asset_is_collection_thumbnail?
  @asset.parent.is_a? Collection
end

#attach_filesObject

Receives json hashes for direct uploaded files in params, and id in params (friendlier_id) creates filesets for them and attach.

POST /document/:id/ingest



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/admin/document_assets_controller.rb', line 88

def attach_files
  @parent = Document.find_by_friendlier_id!(params[:id])

  current_position = @parent.members.maximum(:position) || 0

  files_params = (params[:cached_files] || [])
    .collect { |s| JSON.parse(s) }
    .sort_by { |h| h&.dig("metadata", "filename") }

  files_params.each do |file_data|
    asset = Kithe::Asset.new

    # if derivative_storage_type = params.dig(:storage_type_for, file_data["id"])
    #  asset.derivative_storage_type = derivative_storage_type
    # end

    asset.position = (current_position += 1)
    asset.parent_id = @parent.id
    asset.file = file_data
    asset.title = (asset.file&.original_filename || "Untitled")
    asset.save!
  end

  @parent.update(representative: @parent.members.order(:position).first) if @parent.representative_id.nil?

  redirect_to admin_document_path(@parent.friendlier_id, anchor: "nav-members")
end

#check_fixityObject



69
70
71
72
73
# File 'app/controllers/admin/document_assets_controller.rb', line 69

def check_fixity
  @asset = Kithe::Asset.find_by_friendlier_id!(params[:asset_id])
  SingleAssetCheckerJob.perform_later(@asset)
  redirect_to admin_asset_url(@asset), notice: "This file will be checked shortly."
end

#convert_to_child_workObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/controllers/admin/document_assets_controller.rb', line 116

def convert_to_child_work
  @asset = Kithe::Asset.find_by_friendlier_id!(params[:id])

  parent = @asset.parent

  new_child = Work.new(title: @asset.title)

  # Asking for permission to create a new Work,
  # which is arguably the main thing going on in this method.
  # authorize! :create, Work as the first line of the method
  # would be better, but we currently aren't allowed to do that
  # see (https://github.com/chaps-io/access-granted/pull/56).
  authorize! :create, new_child

  new_child.parent = parent
  # collections
  new_child.contained_by = parent.contained_by
  new_child.position = @asset.position
  new_child.representative = @asset
  # we can copy _all_ the non-title metadata like this...
  new_child.json_attributes = parent.json_attributes

  @asset.parent = new_child

  Kithe::Model.transaction do
    new_child.save!
    @asset.save! # to get new parent

    if parent.representative_id == @asset.id
      parent.representative = new_child
      parent.save!
    end
  end

  redirect_to edit_admin_work_path(new_child), notice: "Asset promoted to child work #{new_child.title}"
end

#destroyObject



56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/controllers/admin/document_assets_controller.rb', line 56

def destroy
  @asset = Kithe::Asset.find_by_friendlier_id!(params[:id])
  @asset.destroy

  respond_to do |format|
    format.html do
      redirect_to document_document_assets_path(@document),
        notice: "Asset '#{@asset.title}' was successfully destroyed."
    end
    format.json { head :no_content }
  end
end

#display_attach_formObject



79
80
81
# File 'app/controllers/admin/document_assets_controller.rb', line 79

def display_attach_form
  @document = Document.find_by_friendlier_id!(params[:document_id])
end

#editObject



34
35
36
37
# File 'app/controllers/admin/document_assets_controller.rb', line 34

def edit
  @asset = Kithe::Asset.find_by_friendlier_id!(params[:id])
  authorize! :update, @asset
end

#edit_path(asset) ⇒ Object



173
174
175
# File 'app/controllers/admin/document_assets_controller.rb', line 173

def edit_path(asset)
  asset.parent.is_a? Collection ? edit_admin_collection_path(asset.parent) : edit_admin_asset_path(asset)
end

#fixity_reportObject



75
76
77
# File 'app/controllers/admin/document_assets_controller.rb', line 75

def fixity_report
  @fixity_report = FixityReport.new
end

#indexObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/controllers/admin/document_assets_controller.rb', line 8

def index
  scope = Kithe::Asset

  # simple simple search on a few simple attributes with OR combo.
  if params[:document_id].present?
    document = Document.find_by_friendlier_id(params[:document_id])
    scope = scope.where(parent_id: document.id)
  end

  # scope = scope.page(params[:page]).per(20).order(created_at: :desc)
  scope = scope.includes(:parent)

  @document_assets = scope
end

#parent_path(asset) ⇒ Object



178
179
180
181
182
# File 'app/controllers/admin/document_assets_controller.rb', line 178

def parent_path(asset)
  return nil if asset.parent.nil?

  asset.parent.is_a? Collection ? collection_path(asset.parent) : admin_work_path(asset.parent)
end

#refresh_active_encode_statusObject

requires params



154
155
156
157
158
159
160
161
# File 'app/controllers/admin/document_assets_controller.rb', line 154

def refresh_active_encode_status
  status = ActiveEncodeStatus.find(params[:active_encode_status_id])

  RefreshActiveEncodeStatusJob.perform_later(status)

  redirect_to admin_asset_url(status.asset),
    notice: "Started refresh for ActiveEncode job #{status.active_encode_id}"
end

#showObject



23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/admin/document_assets_controller.rb', line 23

def show
  @asset = Kithe::Asset.find_by_friendlier_id!(params[:id])
  authorize! :read, @asset

  return unless @asset.stored?

  @checks = @asset.fixity_checks.order("created_at asc")
  @latest_check = @checks.last
  @earliest_check = @checks.first
end

#updateObject

PATCH/PUT /works/1 PATCH/PUT /works/1.json



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/admin/document_assets_controller.rb', line 41

def update
  @asset = Kithe::Asset.find_by_friendlier_id!(params[:id])
  authorize! :update, @asset

  respond_to do |format|
    if @asset.update(asset_params)
      format.html { redirect_to admin_asset_url(@asset), notice: "Asset was successfully updated." }
      format.json { render :show, status: :ok, location: @asset }
    else
      format.html { render :edit }
      format.json { render json: @asset.errors, status: :unprocessable_entity }
    end
  end
end

#work_is_oral_history?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'app/controllers/admin/document_assets_controller.rb', line 163

def work_is_oral_history?
  (@asset.parent.is_a? Work) && @asset.parent.genre && @asset.parent.genre.include?("Oral histories")
end