Class: Admin::AssetsController

Inherits:
AdminController show all
Defined in:
app/controllers/admin/assets_controller.rb

Instance Method Summary collapse

Instance Method Details

#attach_filesObject

POST /assets/ingest

Receives JSON hashes for direct uploaded files in params, and id in params (friendlier_id). Creates filesets for them and attaches. Redirects to the assets list with a success notice.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/controllers/admin/assets_controller.rb', line 104

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 = 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_assets_url, notice: "Files attached successfully."
end

#destroyObject

DELETE /assets/1 or /assets/1.json

Deletes an asset and redirects to the assets list with a success notice.



84
85
86
87
88
89
90
91
# File 'app/controllers/admin/assets_controller.rb', line 84

def destroy
  @asset.destroy

  respond_to do |format|
    format.html { redirect_to admin_assets_url, notice: "Asset was successfully destroyed." }
    format.json { head :no_content }
  end
end

#display_attach_formObject

/assets/display_attach_form

Displays a form to attach files to an asset.



96
97
# File 'app/controllers/admin/assets_controller.rb', line 96

def display_attach_form
end

#editObject

GET /assets/1/edit

Provides a form to edit an asset.



62
63
# File 'app/controllers/admin/assets_controller.rb', line 62

def edit
end

#indexObject

GET /admin/asset_files

Lists all assets. Supports searching by ID, friendlier_id, title, or parent_id. If a date is provided as a search query, it filters assets created on that date.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/controllers/admin/assets_controller.rb', line 31

def index
  scope = Asset
  search_query = params[:q].strip if params[:q].present?

  # Basic search functionality
  if search_query.present?
    scope = if date_check?(search_query)
      Asset.where("created_at BETWEEN ? AND ?", search_query.to_date.beginning_of_day, search_query.to_date.end_of_day)
    else
      scope.where(id: search_query).or(
        Asset.where(friendlier_id: search_query)
      ).or(
        Asset.where("title like ?", "%" + Asset.sanitize_sql_like(search_query) + "%")
      ).or(
        Asset.where(parent_id: search_query)
      )
    end
  end

  @pagy, @assets = pagy(scope, items: 20)
end

#showObject

GET /assets/1 or /assets/1.json

Displays a specific asset.



56
57
# File 'app/controllers/admin/assets_controller.rb', line 56

def show
end

#updateObject

PATCH/PUT /assets/1 or /assets/1.json

Updates an asset with new data. If successful, redirects to the asset’s page. Otherwise, re-renders the edit form with errors.



69
70
71
72
73
74
75
76
77
78
79
# File 'app/controllers/admin/assets_controller.rb', line 69

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