Class: HasHelpers::SearchPresenter

Inherits:
BasePresenter show all
Defined in:
app/presenters/has_helpers/search_presenter.rb

Defined Under Namespace

Classes: DetailListPresenter, ResultPresenter

Instance Attribute Summary collapse

Attributes inherited from BasePresenter

#errors, #wrapped_object

Instance Method Summary collapse

Methods inherited from BasePresenter

#==, #error_on, #initialize, wrap, wrap_with_options

Methods included from Attributes

included

Constructor Details

This class inherits a constructor from HasHelpers::BasePresenter

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class HasHelpers::BasePresenter

Instance Attribute Details

#exact_submitObject

Returns the value of attribute exact_submit.



153
154
155
# File 'app/presenters/has_helpers/search_presenter.rb', line 153

def exact_submit
  @exact_submit
end

#inline_addObject

Returns the value of attribute inline_add.



153
154
155
# File 'app/presenters/has_helpers/search_presenter.rb', line 153

def inline_add
  @inline_add
end

#multiscreenObject

Returns the value of attribute multiscreen.



153
154
155
# File 'app/presenters/has_helpers/search_presenter.rb', line 153

def multiscreen
  @multiscreen
end

#pageObject

Returns the value of attribute page.



153
154
155
# File 'app/presenters/has_helpers/search_presenter.rb', line 153

def page
  @page
end

#per_pageObject

Returns the value of attribute per_page.



153
154
155
# File 'app/presenters/has_helpers/search_presenter.rb', line 153

def per_page
  @per_page
end

#results_id_field=(value) ⇒ Object

Sets the attribute results_id_field

Parameters:

  • value

    the value to set the attribute results_id_field to.



152
153
154
# File 'app/presenters/has_helpers/search_presenter.rb', line 152

def results_id_field=(value)
  @results_id_field = value
end

#screenObject

Returns the value of attribute screen.



153
154
155
# File 'app/presenters/has_helpers/search_presenter.rb', line 153

def screen
  @screen
end

#search_submitObject



201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'app/presenters/has_helpers/search_presenter.rb', line 201

def search_submit
  @search_submit_memoized ||= if @multiscreen # rubocop:disable Naming/MemoizedInstanceVariableName
    if @exact_submit
      @exact_submit
    elsif @search_submit.is_a?(Proc)
      -> result { "#{ @search_submit[result] }[#{ @screen.singularize.foreign_key }]" }
    else
      "#{ @search_submit }[#{ @screen.singularize.foreign_key }]"
    end
  else
    @search_submit
  end
end

#show_typeObject

Returns the value of attribute show_type.



153
154
155
# File 'app/presenters/has_helpers/search_presenter.rb', line 153

def show_type
  @show_type
end

Instance Method Details

#iconObject



215
216
217
# File 'app/presenters/has_helpers/search_presenter.rb', line 215

def icon
  @icon && @icon.to_icon("icon-item")
end

#raw_resultsObject



197
198
199
# File 'app/presenters/has_helpers/search_presenter.rb', line 197

def raw_results
  wrapped_object
end

#results(format_ungrouped: false) ⇒ Object

Returns an associative Array, where the keys are screen names and values are collections of results.



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'app/presenters/has_helpers/search_presenter.rb', line 156

def results(
  format_ungrouped: false # When set to true the results will NOT be grouped.
)
  intermediate_results = raw_results || {}

  intermediate_results = intermediate_results.map do |result|
    case result
    when ::ActiveRecord::Base
      # `result` should be an owner detail object, e.g. an OwnerAdvisor rather than an Advisor.
      ResultPresenter.new(
        name: result.try(:name),
        type: result.try(:type) || result.class.name,
        klass: result.class,
        service: -> { result.to_service }, # Service may not be needed, so delay its evaluation by wrapping it in a proc.
        id: result.try(results_id_field),
        details: result.try(:detail) || {},
        sub_item: result.try(:sub_item),
        sub_item_type: result.try(:sub_item_type),
        photo_info: result.try(:photo_info),
        icon: result.try(:icon) || :circle,
        abbreviation: result.try(:abbreviation),
        search_submit: search_submit.is_a?(Proc) ? search_submit.call(result) : search_submit,
        screen: screen,
        show_type: show_type
      )
    when ::Hash
      ResultPresenter.new(result)
    else  # Handles String, which is displayed as a heading
      ResultPresenter.new(name: result.to_s.titleize, is_heading: true)
    end
  end

  if format_ungrouped
    intermediate_results.map(&:to_h)
  else
    # Wrap the results in a hash if they are not already.
    intermediate_results = { nil => intermediate_results } unless intermediate_results.respond_to?(:keys) || intermediate_results.try(:blank?)
    intermediate_results
  end
end

#to_hObject



219
220
221
222
223
# File 'app/presenters/has_helpers/search_presenter.rb', line 219

def to_h
  results.map do |group, results|
    { name: group, results: results.map(&:to_h) }
  end
end