Class: Panda::Core::Admin::FilesController
Instance Method Summary
collapse
#add_breadcrumb, #authenticate_admin_user!, #authenticate_user!, #breadcrumbs, #current_user, #set_current_request_details, #user_signed_in?
#authorize!, #authorized_for?, #authorized_for_admin_access?, #can?
Instance Method Details
#create ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'app/controllers/panda/core/admin/files_controller.rb', line 55
def create
if params[:image].present?
return create_from_editor
end
if params[:file].present?
return create_file_from_editor
end
file = params.dig(:file_upload, :file)
category_id = params.dig(:file_upload, :file_category_id)
if file.blank?
redirect_to admin_files_path, alert: "Please select a file to upload."
return
end
if category_id.blank?
redirect_to admin_files_path, alert: "Please select a category."
return
end
unless (category = Panda::Core::FileCategory.find_by(id: category_id))
redirect_to admin_files_path, alert: "Selected category not found."
return
end
blob = find_existing_blob(file) || ActiveStorage::Blob.create_and_upload!(
io: file,
filename: file.original_filename,
content_type: file.content_type
)
stamp_uploader(blob)
Panda::Core::FileCategorization.find_or_create_by!(file_category: category, blob: blob)
redirect_to admin_files_path, notice: "File uploaded successfully."
end
|
#destroy ⇒ Object
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
# File 'app/controllers/panda/core/admin/files_controller.rb', line 133
def destroy
@blob.variant_records.includes(image_attachment: :blob).find_each do |vr|
vr.image.purge_later if vr.image.attached?
vr.destroy
end
ActiveStorage::Attachment.where(blob_id: @blob.id).delete_all
Panda::Core::FileCategorization.where(blob_id: @blob.id).delete_all
@blob.purge_later
redirect_to admin_files_path, notice: "File was successfully deleted.", status: :see_other
end
|
#index ⇒ Object
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
|
# File 'app/controllers/panda/core/admin/files_controller.rb', line 10
def index
variant_blob_ids = ActiveStorage::Attachment
.where(record_type: "ActiveStorage::VariantRecord")
.select(:blob_id)
@files = ActiveStorage::Blob
.where.not(id: variant_blob_ids)
.includes(:variant_records)
.order(created_at: :desc)
@file_categories = Panda::Core::FileCategory.ordered
@active_category = params[:category]
if @active_category == "uncategorized"
categorized_blob_ids = Panda::Core::FileCategorization.select(:blob_id)
@files = @files.where.not(id: categorized_blob_ids)
elsif @active_category.present?
category = Panda::Core::FileCategory.find_by(slug: @active_category)
if category
blob_ids = Panda::Core::FileCategorization.where(file_category: category).select(:blob_id)
@files = @files.where(id: blob_ids)
end
end
categorizations = Panda::Core::FileCategorization
.where(blob_id: @files.select(:id))
.includes(:file_category)
@blob_categories = categorizations.each_with_object({}) do |cat, hash|
hash[cat.blob_id] = cat.file_category
end
end
|
#show ⇒ Object
45
46
47
48
49
50
51
52
53
|
# File 'app/controllers/panda/core/admin/files_controller.rb', line 45
def show
@file_categories = Panda::Core::FileCategory.ordered
if request.xhr? || request.["Turbo-Frame"].present?
render partial: "file_details", locals: {file: @blob, file_categories: @file_categories}
else
redirect_to admin_files_path
end
end
|
#update ⇒ Object
98
99
100
101
102
103
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
131
|
# File 'app/controllers/panda/core/admin/files_controller.rb', line 98
def update
blob_params = params.require(:blob).permit(:filename, :description, :file_category_id)
new_filename = blob_params[:filename].to_s.strip
if new_filename.present?
original_ext = File.extname(@blob.filename.to_s)
new_filename += original_ext if File.extname(new_filename).blank? && original_ext.present?
@blob.filename = new_filename
end
metadata = @blob.metadata || {}
metadata["description"] = blob_params[:description].to_s.strip
@blob.metadata = metadata
@blob.save!
update_file_category(blob_params[:file_category_id])
@file_categories = Panda::Core::FileCategory.ordered
locals = {file: @blob, file_categories: @file_categories, notice: "File updated successfully."}
respond_to do |format|
format.turbo_stream do
render turbo_stream: turbo_stream.update(
"file-gallery-slideover-content",
partial: "file_details",
locals: locals
)
end
format.html do
redirect_to admin_files_path, notice: "File updated successfully.", status: :see_other
end
end
end
|