Class: QuickSearch::PinPresenter

Inherits:
BasePresenter
  • Object
show all
Defined in:
app/presenters/quick_search/pin_presenter.rb

Overview

Presenter for a record pinned in the CRM quick search / pinned-items UI.

Resolves the concrete presenter class for any record via PinPresenter.pin_class,
builds display attributes (title, sub-header, link, badges) in
#set_attributes, and serializes the card with #as_json.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, template, options = {}) ⇒ QuickSearch::PinPresenter

Parameters:

  • object (Object)

    the pinned record

  • template (Object)

    the view context

  • options (Hash) (defaults to: {})

    presenter options, stored for subclass use
    (no keys are currently read)

Options Hash (options):

  • :any (Object)

    any subclass-specific option



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/presenters/quick_search/pin_presenter.rb', line 53

def initialize(object, template, options = {})
  super(object, template)
  @options = options
  set_attributes
  set_result_class(object)
  @title ||= object.to_s
  if @link.blank?
    @link = if object.respond_to? :crm_link
              object.crm_link
            else
              begin
                template.polymorphic_path(object)
              rescue StandardError
                nil
              end
            end
  end
  @reference_number ||= object.try(:reference_number)
end

Instance Attribute Details

#invalidObject (readonly)

Returns the value of attribute invalid.



14
15
16
# File 'app/presenters/quick_search/pin_presenter.rb', line 14

def invalid
  @invalid
end

Returns the value of attribute link.



14
15
16
# File 'app/presenters/quick_search/pin_presenter.rb', line 14

def link
  @link
end

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'app/presenters/quick_search/pin_presenter.rb', line 14

def options
  @options
end

#reference_numberObject (readonly)

Returns the value of attribute reference_number.



14
15
16
# File 'app/presenters/quick_search/pin_presenter.rb', line 14

def reference_number
  @reference_number
end

#result_classObject (readonly)

Returns the value of attribute result_class.



14
15
16
# File 'app/presenters/quick_search/pin_presenter.rb', line 14

def result_class
  @result_class
end

#significant_dateObject (readonly)

Returns the value of attribute significant_date.



14
15
16
# File 'app/presenters/quick_search/pin_presenter.rb', line 14

def significant_date
  @significant_date
end

#sub_headerObject (readonly)

Returns the value of attribute sub_header.



14
15
16
# File 'app/presenters/quick_search/pin_presenter.rb', line 14

def sub_header
  @sub_header
end

#titleObject (readonly)

Returns the value of attribute title.



14
15
16
# File 'app/presenters/quick_search/pin_presenter.rb', line 14

def title
  @title
end

Class Method Details

.build(object, template) ⇒ QuickSearch::PinPresenter

Builds a presenter for +object+ using the class resolved by pin_class.

Parameters:

  • object (Object)

    the record to present

  • template (Object)

    the view context used for helpers and URL generation

Returns:

  • (QuickSearch::PinPresenter)

    the presenter instance



43
44
45
# File 'app/presenters/quick_search/pin_presenter.rb', line 43

def self.build(object, template)
  pin_class(object).new(object, template)
end

.pin_class(r) ⇒ Class

Instantiate the appropriate search class

Parameters:

  • r (Object)

    the record to find a presenter class for

Returns:

  • (Class)

    the matching presenter class, falling back to the record's
    superclass presenter and finally UnknownPresenter



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/presenters/quick_search/pin_presenter.rb', line 21

def self.pin_class(r)
  klass = begin
    "QuickSearch::#{r.class.name}Presenter".constantize
  rescue StandardError
    nil
  end
  # Try a parent (e.g. Party if you are a Customer, Supplier, etc.)
  klass ||= begin
    "QuickSearch::#{r.superclass.name}Presenter".constantize
  rescue StandardError
    nil
  end
  # Default to unknownpresenter
  klass ||= "QuickSearch::UnknownPresenter".constantize
  klass
end

Instance Method Details

#as_json(_options = {}) ⇒ Hash

JSON payload for a result card in quick search responses.

Parameters:

  • _options (Hash) (defaults to: {})

    unused serialization options

Returns:

  • (Hash)

    the card attributes



94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/presenters/quick_search/pin_presenter.rb', line 94

def as_json(_options = {})
  {
    title: title,
    sub_header: sub_header,
    text_sub_header: text_sub_header,
    url: link,
    ref: try(:reference_number),
    result_class: result_class,
    type_label: type_label,
    profile_image_url: profile_image_url
  }
end

#presenting_context?(context_type, context_id) ⇒ Boolean

Whether this pin represents the record currently being viewed.

Parameters:

  • context_type (String)

    the class name of the viewed record

  • context_id (Integer, String)

    the id of the viewed record

Returns:

  • (Boolean)

    true when the result's resource matches the context



141
142
143
144
145
# File 'app/presenters/quick_search/pin_presenter.rb', line 141

def presenting_context?(context_type, context_id)
  return false unless context_type.present? && context_id.present?

  result.resource_type == context_type.to_s and result.resource_id == context_id.to_i
end

#profile_image_urlString?

Override in subclasses that support profile images (Customer, Contact, Supplier)

Returns:

  • (String, nil)

    the image URL, or nil when there is no image



124
125
126
# File 'app/presenters/quick_search/pin_presenter.rb', line 124

def profile_image_url
  nil
end

#set_attributesvoid

This method returns an undefined value.

Populates +@title+, +@sub_header+, +@link+, and friends from the record.

Raises:

  • (RuntimeError)

    always, unless implemented in a subclass



132
133
134
# File 'app/presenters/quick_search/pin_presenter.rb', line 132

def set_attributes
  raise "Not Implemented, you must implement this method in your subclass"
end

#set_result_class(object) ⇒ Symbol

Sets +result_class+ from the record's class name unless already assigned.

Parameters:

  • object (Object)

    the record being presented

Returns:

  • (Symbol)

    the tableized, singularized class name



77
78
79
80
81
# File 'app/presenters/quick_search/pin_presenter.rb', line 77

def set_result_class(object)
  # rubocop:disable Naming/MemoizedInstanceVariableName -- @result_class is the public attr_reader, not memoizing this method
  @result_class ||= object.class.name.tableize.singularize.to_sym
  # rubocop:enable Naming/MemoizedInstanceVariableName
end

#text_sub_headerString?

Override in subclasses that have both badges AND text info

Returns:

  • (String, nil)

    plain-text sub-header, or nil when unused



110
111
112
# File 'app/presenters/quick_search/pin_presenter.rb', line 110

def text_sub_header
  nil
end

#to_sString

One-line label for the pin, e.g. in the pinned-items list.

Returns:

  • (String)

    reference number and title joined with a bullet



86
87
88
# File 'app/presenters/quick_search/pin_presenter.rb', line 86

def to_s
  [reference_number, title].compact_blank.join(" \u2022 ")
end

#type_labelString?

Override in subclasses to customize the type label shown on cards

Returns:

  • (String, nil)

    the type label, or nil when unused



117
118
119
# File 'app/presenters/quick_search/pin_presenter.rb', line 117

def type_label
  nil
end