Class: Avo::MediaLibraryController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- Avo::MediaLibraryController
- Includes:
- Pagy::Method
- Defined in:
- app/controllers/avo/media_library_controller.rb
Instance Method Summary collapse
- #attach ⇒ Object
- #destroy ⇒ Object
- #edit ⇒ Object
- #extract_initials(filename) ⇒ Object
- #index ⇒ Object
- #show ⇒ Object
- #update ⇒ Object
Instance Method Details
#attach ⇒ Object
64 65 66 67 68 |
# File 'app/controllers/avo/media_library_controller.rb', line 64 def attach @attaching = true render :index end |
#destroy ⇒ Object
35 36 37 38 39 40 |
# File 'app/controllers/avo/media_library_controller.rb', line 35 def destroy @blob = ActiveStorage::Blob.find(params[:id]) @blob.destroy! redirect_to avo.media_library_index_path end |
#edit ⇒ Object
17 18 19 20 21 22 |
# File 'app/controllers/avo/media_library_controller.rb', line 17 def edit @blob = ActiveStorage::Blob.find(params[:id]) title: "Media Library", path: avo.media_library_index_path, initials: "ML" title: @blob.filename.to_s, path: nil, initials: extract_initials(@blob.filename.to_s) end |
#extract_initials(filename) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'app/controllers/avo/media_library_controller.rb', line 24 def extract_initials(filename) # Remove file extension name_without_ext = File.basename(filename, File.extname(filename)) # Split by spaces and take first 2 words words = name_without_ext.split(" ").first(2) # Extract first character of each word and join words.map { |word| word[0] }.join("").upcase end |
#index ⇒ Object
8 9 10 11 |
# File 'app/controllers/avo/media_library_controller.rb', line 8 def index @attaching = false title: "Media Library", initials: "ML" end |
#show ⇒ Object
13 14 15 |
# File 'app/controllers/avo/media_library_controller.rb', line 13 def show redirect_to avo.edit_media_library_path(params[:id]) end |
#update ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'app/controllers/avo/media_library_controller.rb', line 42 def update @blob = ActiveStorage::Blob.find(params[:id]) # A blank filename makes the blob unroutable (Active Storage URLs need a # :filename segment) — refuse it rather than mangling the blob. Omitted # filename (metadata-only PATCH) is fine; leave the existing name alone. if blob_params.key?(:filename) && blob_params[:filename].blank? flash[:error] = "Filename can't be blank." return redirect_to avo.edit_media_library_path(@blob) end attributes = { # Merge, don't replace: keep system metadata (width/height/analyzed/…). metadata: @blob..merge(blob_params[:metadata]&.to_h || {}) } attributes[:filename] = blob_params[:filename] if blob_params.key?(:filename) @blob.update!(attributes) redirect_to avo.edit_media_library_path(@blob) end |