Class: Workarea::Search::FacetSorting

Inherits:
Object
  • Object
show all
Defined in:
app/queries/workarea/search/facet_sorting.rb,
app/queries/workarea/search/facet_sorting/size.rb

Defined Under Namespace

Classes: Size

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ FacetSorting

Returns a new instance of FacetSorting.



6
7
8
# File 'app/queries/workarea/search/facet_sorting.rb', line 6

def initialize(name)
  @name = name.to_sym
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'app/queries/workarea/search/facet_sorting.rb', line 4

def name
  @name
end

Instance Method Details

#apply(results, size = full_result_size) ⇒ Object



17
18
19
20
# File 'app/queries/workarea/search/facet_sorting.rb', line 17

def apply(results, size = full_result_size)
  return results unless dynamic?
  Hash[dynamic_option.call(name, results).first(size)]
end

#dynamic?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'app/queries/workarea/search/facet_sorting.rb', line 39

def dynamic?
  dynamic_option.present?
end

#dynamic_optionObject



28
29
30
31
32
33
34
35
36
37
# File 'app/queries/workarea/search/facet_sorting.rb', line 28

def dynamic_option
  @dynamic_option ||=
    if option.respond_to?(:call)
      option
    elsif option.try(:constantize).respond_to?(:call)
      option.constantize
    end
rescue NameError
  @dynamic_option = nil
end

#optionObject



22
23
24
25
26
# File 'app/queries/workarea/search/facet_sorting.rb', line 22

def option
  @option ||=
    Workarea.config.search_facet_sorts[name].presence ||
    default_option
end

#to_hObject



10
11
12
13
14
15
# File 'app/queries/workarea/search/facet_sorting.rb', line 10

def to_h
  return dynamic_sorting if dynamic?
  return {} unless query_values[option].present?

  { order: query_values[option] }
end