Class: Avo::MediaLibraryController

Inherits:
ApplicationController show all
Includes:
Pagy::Method
Defined in:
app/controllers/avo/media_library_controller.rb

Constant Summary

Constants included from Concerns::FindAssociationField

Concerns::FindAssociationField::ASSOCIATIONS

Instance Method Summary collapse

Methods inherited from BaseApplicationController

#exception_logger, #turbo_frame_request?

Methods included from Concerns::FindAssociationField

#find_association_field

Methods included from Concerns::Breadcrumbs

#add_breadcrumb, #avo_breadcrumbs

Methods included from UrlHelpers

#edit_resource_path, #new_resource_path, #preview_resource_path, #related_resources_path, #resource_attach_path, #resource_detach_path, #resource_path, #resource_view_path, #resources_path

Methods included from ApplicationHelper

#a_button, #a_link, #body_classes, #button_classes, #chart_color, #container_classes, #d, #decode_filter_params, #e, #editor_file_path, #editor_url, #empty_state, #encode_filter_params, #frame_id, #get_model_class, #input_classes, #manual_frame_cookie_name, #manual_frame_remembered?, #mount_path, #number_to_social, #possibly_rails_authentication?, #render_header_menu_items, #render_license_warning, #root_path_without_url, #rtl?, #safe_blob_path, #safe_blob_representation_url, #safe_blob_url, #text_direction, #ui, #wrap_in_modal

Methods included from SummaryChartHelper

#summary_chart_params_for

Methods included from ResourcesHelper

#field_wrapper, #filter_wrapper, #index_field_wrapper, #item_selector_data_attributes, #record_path, #record_title, #resource_for_record, #resource_grid, #resource_show_path, #resource_table

Methods included from CommonController

#default_url_options, #extra_default_url_options

Methods included from InitializesAvo

#_current_user, #context, #init_app, #load_appearance_settings

Methods included from Concerns::SafeCall

#safe_call

Instance Method Details

#attachObject



64
65
66
67
68
# File 'app/controllers/avo/media_library_controller.rb', line 64

def attach
  @attaching = true

  render :index
end

#destroyObject



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

#editObject



17
18
19
20
21
22
# File 'app/controllers/avo/media_library_controller.rb', line 17

def edit
  @blob = ActiveStorage::Blob.find(params[:id])

  add_breadcrumb title: "Media Library", path: avo.media_library_index_path, initials: "ML"
  add_breadcrumb 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

#indexObject



8
9
10
11
# File 'app/controllers/avo/media_library_controller.rb', line 8

def index
  @attaching = false
  add_breadcrumb title: "Media Library", initials: "ML"
end

#showObject



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

#updateObject



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