Module: AttachableHelper

Defined in:
app/helpers/attachable_helper.rb

Instance Method Summary collapse

Instance Method Details

#attachable_content(context_object, options = nil) ⇒ Object



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

def attachable_content(context_object, options = nil)
  # Use explicit context_class from options if provided, otherwise use the object's class
  context_class = options&.dig(:context_class) || context_object.class
  controller_path = options&.dig(:controller_path) || controller_name
  disable_delete = options&.dig(:disable_delete) || false

  turbo_frame_tag "attachments-frame-#{context_object.try(:id).to_i}" do
    (:div, id: "attachments-#{context_object.try(:id).to_i}", class: 'row row-cols-1 row-cols-sm-2 row-cols-md-3 row-cols-lg-4 row-cols-xl-6 g-4 attachments', data: { context_object_id: context_object.try(:id).to_i }) do
      context_object.uploads.each do |upload|
        render Crm::UploadWrapperComponent.new(
          upload: upload,
          context_object: context_object,
          context_class: context_class,
          controller_path: controller_path,
          disable_delete: disable_delete
        )
      end
    end
  end
end

#attachable_form(context_object, options = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/helpers/attachable_helper.rb', line 53

def attachable_form(context_object, options = {})
  # Use explicit context_class from options if provided, otherwise use the object's class
  context_class = options[:context_class] || context_object.class
  # Use explicit controller_path from options if provided, otherwise use current controller_name
  # This is needed for irregular controllers (e.g., article_trainings vs Article model)
  controller_path = options[:controller_path] || controller_name

  # Generate attach URL - prefer explicit URL from options, otherwise use url_for with controller_path
  attach_url = options[:attach_url].presence || url_for(controller: controller_path, action: :attach, only_path: true)

  # Use the new component system instead of legacy combo_form partial
  render Crm::AttachmentsComponent.new(
    context_object: context_object,
    attach_url: attach_url,
    context_class_name: context_class.name, # Used for hidden field (load_context)
    controller_path: controller_path, # Used by components for route generation
    wrapped: options[:wrapped] != false, # Default to wrapped (true), unless explicitly false
    skip_publications: options[:skip_publications] || false,
    multiple_files_allowed: options[:mutiple_files_allowed] != false, # Handle typo in legacy code
    template_options_for_select: options[:template_options_for_select],
    category_options_for_select: options[:category_options_for_select],
    parent_form_id: options[:parent_form_id]
  )
end

#attachable_panel(context_object, options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'app/helpers/attachable_helper.rb', line 22

def attachable_panel(context_object, options = {})
  if options[:wrapped] == false
    attachable_content(context_object, options)
  else
    simple_panel('Attachments') do
      attachable_content(context_object, options)
    end
  end
end


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/helpers/attachable_helper.rb', line 2

def removable_link(record_type, upload, record = nil)
  button_to(
        url_for(action: :remove_attachment,
                upload_id: upload.id,
                context_object_id: record.try(:id),
                context_class: record_type || record.class.name),
        method: :delete,
        class: 'btn btn-sm btn-outline-danger w-100',
        form: {
          data: {
            turbo_stream: true,
            turbo_confirm: 'Are you sure you want to delete this attachment?'
          },
          style: 'display: inline;'
        }
      ) do
    fa_icon('trash', text: 'Remove')
  end
end