Class: Crm::PublicationPickerComponent

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

Overview

Publication picker as an offcanvas side panel with infinite scroll.

Instance Attribute Summary collapse

Delegated Instance Attributes collapse

Instance Method Summary collapse

Constructor Details

#initialize(offcanvas_id:, context_object:, context_object_id: nil, context_class_name: nil, controller_path: nil, publications: nil, existing_upload_ids: nil, search_term: nil, cursor: nil, has_more: false, body_only: false, auto_show: false) ⇒ PublicationPickerComponent

Returns a new instance of PublicationPickerComponent.

Parameters:

  • offcanvas_id (String)

    DOM id of the offcanvas panel

  • context_object (Object, nil)

    the record publications are attached to

  • context_object_id (Integer, nil) (defaults to: nil)

    explicit id of the context record;
    falls back to +context_object.id+

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

    explicit class name of the context
    record; falls back to the record's class or the controller name

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

    controller path used to build the
    search/attach URLs; falls back to the current controller

  • publications (Array<Publication>, nil) (defaults to: nil)

    publications for the current page

  • existing_upload_ids (Array<Integer>, nil) (defaults to: nil)

    upload ids already attached;
    falls back to +context_object.upload_ids+

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

    explicit search term; falls back to
    the +term+ request param

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

    pagination cursor for the next page

  • has_more (Boolean) (defaults to: false)

    whether more results are available

  • body_only (Boolean) (defaults to: false)

    render only the offcanvas body (for turbo appends)

  • auto_show (Boolean) (defaults to: false)

    open the offcanvas automatically on render



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/components/crm/publication_picker_component.rb', line 36

def initialize(offcanvas_id:,
               context_object:,
               context_object_id: nil,
               context_class_name: nil,
               controller_path: nil,
               publications: nil,
               existing_upload_ids: nil,
               search_term: nil,
               cursor: nil,
               has_more: false,
               body_only: false,
               auto_show: false)
  super()
  @offcanvas_id = offcanvas_id
  @context_object = context_object
  @context_object_id = context_object_id || context_object.try(:id).to_i
  @explicit_context_class_name = context_class_name
  @controller_path = controller_path
  @publications = publications || []
  @existing_upload_ids = existing_upload_ids || context_object.try(:upload_ids) || []
  @explicit_search_term = search_term
  @cursor = cursor
  @has_more = has_more
  @body_only = body_only
  @auto_show = auto_show
end

Instance Attribute Details

#context_objectObject? (readonly)

Returns the record publications are attached to.

Returns:

  • (Object, nil)

    the record publications are attached to



17
# File 'app/components/crm/publication_picker_component.rb', line 17

attr_reader :offcanvas_id, :context_object, :context_object_id, :publications, :existing_upload_ids, :cursor

#context_object_idInteger (readonly)

Returns id of the context record (+0+ when none).

Returns:

  • (Integer)

    id of the context record (+0+ when none)



17
# File 'app/components/crm/publication_picker_component.rb', line 17

attr_reader :offcanvas_id, :context_object, :context_object_id, :publications, :existing_upload_ids, :cursor

#cursorObject (readonly)

Returns the value of attribute cursor.



17
# File 'app/components/crm/publication_picker_component.rb', line 17

attr_reader :offcanvas_id, :context_object, :context_object_id, :publications, :existing_upload_ids, :cursor

#existing_upload_idsArray<Integer> (readonly)

Returns upload ids already attached to the context.

Returns:

  • (Array<Integer>)

    upload ids already attached to the context



17
# File 'app/components/crm/publication_picker_component.rb', line 17

attr_reader :offcanvas_id, :context_object, :context_object_id, :publications, :existing_upload_ids, :cursor

#offcanvas_idString (readonly)

Returns DOM id of the offcanvas panel.

Returns:

  • (String)

    DOM id of the offcanvas panel



17
18
19
# File 'app/components/crm/publication_picker_component.rb', line 17

def offcanvas_id
  @offcanvas_id
end

#publicationsArray<Publication> (readonly)

Returns publications for the current page.

Returns:

  • (Array<Publication>)

    publications for the current page



17
# File 'app/components/crm/publication_picker_component.rb', line 17

attr_reader :offcanvas_id, :context_object, :context_object_id, :publications, :existing_upload_ids, :cursor

Instance Method Details

#attach_urlString

Returns URL of the endpoint attaching a publication to the context.

Returns:

  • (String)

    URL of the endpoint attaching a publication to the context



116
117
118
# File 'app/components/crm/publication_picker_component.rb', line 116

def attach_url
  helpers.url_for(controller: controller_path, action: :attach)
end

#auto_show?Boolean

Returns whether the offcanvas opens automatically on render.

Returns:

  • (Boolean)

    whether the offcanvas opens automatically on render



91
92
93
# File 'app/components/crm/publication_picker_component.rb', line 91

def auto_show?
  @auto_show
end

#body_only?Boolean

Returns whether only the offcanvas body is rendered.

Returns:

  • (Boolean)

    whether only the offcanvas body is rendered



86
87
88
# File 'app/components/crm/publication_picker_component.rb', line 86

def body_only?
  @body_only
end

#context_class_nameString

Class name of the context record, used in search/attach requests.

Returns:

  • (String)


65
66
67
68
69
70
71
72
73
# File 'app/components/crm/publication_picker_component.rb', line 65

def context_class_name
  @context_class_name ||= if @explicit_context_class_name.present?
                            @explicit_context_class_name
                          elsif @context_object.present?
                            @context_object.class.name
                          else
                            helpers.controller.controller_name.classify
                          end
end

#controller_pathString

Returns controller path used to build the search/attach URLs.

Returns:

  • (String)

    controller path used to build the search/attach URLs



106
107
108
# File 'app/components/crm/publication_picker_component.rb', line 106

def controller_path
  @controller_path || helpers.controller.controller_name
end

#display_empty_state_messageString

Returns message shown when the results list is empty.

Returns:

  • (String)

    message shown when the results list is empty



141
142
143
# File 'app/components/crm/publication_picker_component.rb', line 141

def display_empty_state_message
  search_term.present? ? "No publications found. Try a different search term." : "Enter a search term to find publications."
end

#frame_idString

Returns DOM id of the picker's turbo frame.

Returns:

  • (String)

    DOM id of the picker's turbo frame



76
77
78
# File 'app/components/crm/publication_picker_component.rb', line 76

def frame_id
  "publication-picker-frame-#{context_object_id}"
end

#has_more?Boolean

Returns whether more results are available.

Returns:

  • (Boolean)

    whether more results are available



101
102
103
# File 'app/components/crm/publication_picker_component.rb', line 101

def has_more?
  @has_more
end

#next_page_urlString?

Returns URL for the next page of results, or nil when there
are no more pages.

Returns:

  • (String, nil)

    URL for the next page of results, or nil when there
    are no more pages



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/components/crm/publication_picker_component.rb', line 122

def next_page_url
  return nil unless has_more? && cursor.present?

  helpers.url_for(
    controller: controller_path,
    action: "search_library",
    term: search_term,
    cursor: cursor,
    context_class: context_class_name,
    context_object_id: context_object_id,
    append: true
  )
end

#publications_present?Boolean

Returns whether any publications were loaded for the current page.

Returns:

  • (Boolean)

    whether any publications were loaded for the current page



138
# File 'app/components/crm/publication_picker_component.rb', line 138

delegate :present?, to: :publications, prefix: true

#results_container_idString

Returns DOM id of the results container appended during infinite scroll.

Returns:

  • (String)

    DOM id of the results container appended during infinite scroll



81
82
83
# File 'app/components/crm/publication_picker_component.rb', line 81

def results_container_id
  "publication-picker-results-#{context_object_id}"
end

#search_termString?

Returns the active library search term.

Returns:

  • (String, nil)

    the active library search term



96
97
98
# File 'app/components/crm/publication_picker_component.rb', line 96

def search_term
  @explicit_search_term.presence || helpers.params[:term]
end

#search_urlString

Returns URL of the publication library search endpoint.

Returns:

  • (String)

    URL of the publication library search endpoint



111
112
113
# File 'app/components/crm/publication_picker_component.rb', line 111

def search_url
  helpers.url_for(controller: controller_path, action: "search_library")
end