Class: FortyFacets::Order

Inherits:
Struct
  • Object
show all
Defined in:
lib/forty_facets/order.rb

Overview

Represents the ordering for a specific search

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#activeObject

Returns the value of attribute active

Returns:

  • (Object)

    the current value of active



3
4
5
# File 'lib/forty_facets/order.rb', line 3

def active
  @active
end

#definitionObject

Returns the value of attribute definition

Returns:

  • (Object)

    the current value of definition



3
4
5
# File 'lib/forty_facets/order.rb', line 3

def definition
  @definition
end

#searchObject

Returns the value of attribute search

Returns:

  • (Object)

    the current value of search



3
4
5
# File 'lib/forty_facets/order.rb', line 3

def search
  @search
end

Instance Method Details

#apply(query) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/forty_facets/order.rb', line 15

def apply(query)
  if [Symbol, String, Hash].include? definition.clause.class
    query.order(definition.clause)
  elsif definition.clause.is_a? Array # [:person, :first_name]
    # new and unelegant handling of ordering by columns from joined tables
    root_table = search.class.root_class.table_name
    primary_key = search.class.root_class.primary_key
    just_ids = query.select("#{root_table}.#{primary_key}")
    path_to_order_property = definition.clause
    order_class = path_to_order_property
                  .take(path_to_order_property.length - 1)
                  .inject(search.class.root_class) do |prev_class, assoc_name|
                    prev_class.reflect_on_association(assoc_name).klass
                  end
    joins = path_to_order_property
              .reverse
              .drop(1)
              .inject(nil) do |sum, elem|
                if sum
                  {elem => sum}
                else
                  elem
                end
              end
    search.root
      .joins(joins)
      .where(search.class.root_class.primary_key => just_ids)
      .order("#{order_class.table_name}.#{path_to_order_property.last}")
  end

end

#byObject

Returns a search with the same filter ordered by this criteria



9
10
11
12
13
# File 'lib/forty_facets/order.rb', line 9

def by
  new_params = search.params || {}
  new_params[:order] = definition.request_value
  search.class.new_unwrapped(new_params, search.root)
end

#titleObject



4
5
6
# File 'lib/forty_facets/order.rb', line 4

def title
  definition.title
end