Class: Crm::AttachmentsComponent

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/crm/attachments_component.rb

Overview

ViewComponent: renders the attachments block — a card (or unwrapped action
row) with upload/publication buttons and the Turbo Frame listing a context
object's uploads, driven by the crm-attachments Stimulus controller.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context_object:, attach_url:, wrapped: true, skip_publications: false, multiple_files_allowed: true, template_options_for_select: nil, category_options_for_select: nil, disable_delete: false, context_class_name: nil, controller_path: nil, parent_form_id: nil, small_buttons: true) ⇒ AttachmentsComponent

Returns a new instance of AttachmentsComponent.

Parameters:

  • context_object (ActiveRecord::Base)

    record whose uploads are listed and attached to

  • attach_url (String)

    endpoint URL the Stimulus controller POSTs uploads to

  • wrapped (Boolean) (defaults to: true)

    whether to render the full card chrome (default true)

  • skip_publications (Boolean) (defaults to: false)

    hide the "Publication" picker button (default false)

  • multiple_files_allowed (Boolean) (defaults to: true)

    allow multi-select in the file input (default true)

  • template_options_for_select (Array<Array(String, String)>, nil) (defaults to: nil)

    label/value pairs for the template dropdown

  • category_options_for_select (Array<Array(String, String)>, nil) (defaults to: nil)

    label/value pairs for the category dropdown

  • disable_delete (Boolean) (defaults to: false)

    disable delete controls on each upload (default false)

  • context_class_name (String, nil) (defaults to: nil)

    override for the context object's class name sent to the server

  • controller_path (String, nil) (defaults to: nil)

    override for the controller path used in publication links

  • parent_form_id (String, nil) (defaults to: nil)

    DOM id of a parent form uploads should be associated with

  • small_buttons (Boolean) (defaults to: true)

    render header action buttons as btn-sm (default true)



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/components/crm/attachments_component.rb', line 43

def initialize(context_object:,
               attach_url:,
               wrapped: true,
               skip_publications: false,
               multiple_files_allowed: true,
               template_options_for_select: nil,
               category_options_for_select: nil,
               disable_delete: false,
               context_class_name: nil,
               controller_path: nil,
               parent_form_id: nil,
               small_buttons: true)
  super()
  @context_object = context_object
  @attach_url = attach_url
  @wrapped = wrapped
  @skip_publications = skip_publications
  @multiple_files_allowed = multiple_files_allowed
  @template_options_for_select = template_options_for_select
  @category_options_for_select = category_options_for_select
  @disable_delete = disable_delete
  @explicit_context_class_name = context_class_name || context_object.class.name
  @controller_path = controller_path
  @parent_form_id = parent_form_id
  @small_buttons = small_buttons
end

Instance Attribute Details

#attach_urlString (readonly)

Returns endpoint URL the Stimulus controller POSTs uploads to.

Returns:

  • (String)

    endpoint URL the Stimulus controller POSTs uploads to



27
28
29
# File 'app/components/crm/attachments_component.rb', line 27

attr_reader :context_object, :wrapped, :skip_publications, :multiple_files_allowed,
:template_options_for_select, :category_options_for_select, :disable_delete, :attach_url, :parent_form_id,
:small_buttons

#category_options_for_selectArray<Array(String, String)>? (readonly)

Returns label/value pairs for the category dropdown.

Returns:

  • (Array<Array(String, String)>, nil)

    label/value pairs for the category dropdown



27
28
29
# File 'app/components/crm/attachments_component.rb', line 27

attr_reader :context_object, :wrapped, :skip_publications, :multiple_files_allowed,
:template_options_for_select, :category_options_for_select, :disable_delete, :attach_url, :parent_form_id,
:small_buttons

#context_objectActiveRecord::Base (readonly)

Returns record whose uploads are listed and attached to.

Returns:

  • (ActiveRecord::Base)

    record whose uploads are listed and attached to



27
28
29
# File 'app/components/crm/attachments_component.rb', line 27

def context_object
  @context_object
end

#disable_deleteBoolean (readonly)

Returns whether delete controls are disabled on each upload.

Returns:

  • (Boolean)

    whether delete controls are disabled on each upload



27
28
29
# File 'app/components/crm/attachments_component.rb', line 27

attr_reader :context_object, :wrapped, :skip_publications, :multiple_files_allowed,
:template_options_for_select, :category_options_for_select, :disable_delete, :attach_url, :parent_form_id,
:small_buttons

#multiple_files_allowedBoolean (readonly)

Returns whether the file input accepts multiple files.

Returns:

  • (Boolean)

    whether the file input accepts multiple files



27
28
29
# File 'app/components/crm/attachments_component.rb', line 27

attr_reader :context_object, :wrapped, :skip_publications, :multiple_files_allowed,
:template_options_for_select, :category_options_for_select, :disable_delete, :attach_url, :parent_form_id,
:small_buttons

#parent_form_idString? (readonly)

Returns DOM id of a parent form uploads should be associated with.

Returns:

  • (String, nil)

    DOM id of a parent form uploads should be associated with



27
28
29
# File 'app/components/crm/attachments_component.rb', line 27

attr_reader :context_object, :wrapped, :skip_publications, :multiple_files_allowed,
:template_options_for_select, :category_options_for_select, :disable_delete, :attach_url, :parent_form_id,
:small_buttons

#skip_publicationsBoolean (readonly)

Returns whether to hide the "Publication" picker button.

Returns:

  • (Boolean)

    whether to hide the "Publication" picker button



27
28
29
# File 'app/components/crm/attachments_component.rb', line 27

attr_reader :context_object, :wrapped, :skip_publications, :multiple_files_allowed,
:template_options_for_select, :category_options_for_select, :disable_delete, :attach_url, :parent_form_id,
:small_buttons

#small_buttonsObject (readonly)

Returns the value of attribute small_buttons.



27
28
29
# File 'app/components/crm/attachments_component.rb', line 27

attr_reader :context_object, :wrapped, :skip_publications, :multiple_files_allowed,
:template_options_for_select, :category_options_for_select, :disable_delete, :attach_url, :parent_form_id,
:small_buttons

#template_options_for_selectArray<Array(String, String)>? (readonly)

Returns label/value pairs for the template dropdown.

Returns:

  • (Array<Array(String, String)>, nil)

    label/value pairs for the template dropdown



27
28
29
# File 'app/components/crm/attachments_component.rb', line 27

attr_reader :context_object, :wrapped, :skip_publications, :multiple_files_allowed,
:template_options_for_select, :category_options_for_select, :disable_delete, :attach_url, :parent_form_id,
:small_buttons

#wrappedBoolean (readonly)

Returns whether to render the full card chrome (header + body).

Returns:

  • (Boolean)

    whether to render the full card chrome (header + body)



27
28
29
# File 'app/components/crm/attachments_component.rb', line 27

attr_reader :context_object, :wrapped, :skip_publications, :multiple_files_allowed,
:template_options_for_select, :category_options_for_select, :disable_delete, :attach_url, :parent_form_id,
:small_buttons

Instance Method Details

#action_button_class(variant = 'danger') ⇒ String

Header actions default to btn-sm; the activity form opts out so these and
its result submit buttons share one default-sized row.

Parameters:

  • variant (String) (defaults to: 'danger')

    Bootstrap outline variant for the button (default 'danger')

Returns:

  • (String)

    space-separated CSS classes for an action button



74
75
76
# File 'app/components/crm/attachments_component.rb', line 74

def action_button_class(variant = 'danger')
  ['btn', ('btn-sm' if small_buttons), "btn-outline-#{variant}"].compact.join(' ')
end

#attachments_container_idString

Returns DOM id of the container div holding the upload cards.

Returns:

  • (String)

    DOM id of the container div holding the upload cards



105
106
107
# File 'app/components/crm/attachments_component.rb', line 105

def attachments_container_id
  "attachments-#{context_object_id}"
end

#context_class_nameString

Returns class name of the context object, or the explicit override.

Returns:

  • (String)

    class name of the context object, or the explicit override



84
85
86
# File 'app/components/crm/attachments_component.rb', line 84

def context_class_name
  @explicit_context_class_name
end

#context_object_idInteger

Returns id of the context object (0 when unset, e.g. unpersisted records).

Returns:

  • (Integer)

    id of the context object (0 when unset, e.g. unpersisted records)



79
80
81
# File 'app/components/crm/attachments_component.rb', line 79

def context_object_id
  context_object.try(:id).to_i
end

#controller_pathString

Returns controller path for publication links, derived from the
context object's class when not given explicitly.

Returns:

  • (String)

    controller path for publication links, derived from the
    context object's class when not given explicitly



90
91
92
# File 'app/components/crm/attachments_component.rb', line 90

def controller_path
  @controller_path || context_object.class.name.tableize
end

#frame_idString

Returns DOM id of the Turbo Frame wrapping the uploads list.

Returns:

  • (String)

    DOM id of the Turbo Frame wrapping the uploads list



100
101
102
# File 'app/components/crm/attachments_component.rb', line 100

def frame_id
  "attachments-frame-#{context_object_id}"
end

Returns DOM id of the publication-picker modal.

Returns:

  • (String)

    DOM id of the publication-picker modal



95
96
97
# File 'app/components/crm/attachments_component.rb', line 95

def modal_id
  "publication-picker-modal-#{context_object_id}"
end