Class: FortyFacets::Order
- Inherits:
-
Struct
- Object
- Struct
- FortyFacets::Order
- Defined in:
- lib/forty_facets/order.rb
Overview
Represents the ordering for a specific search
Instance Attribute Summary collapse
-
#active ⇒ Object
Returns the value of attribute active.
-
#definition ⇒ Object
Returns the value of attribute definition.
-
#search ⇒ Object
Returns the value of attribute search.
Instance Method Summary collapse
- #apply(query) ⇒ Object
-
#by ⇒ Object
Returns a search with the same filter ordered by this criteria.
- #title ⇒ Object
Instance Attribute Details
#active ⇒ Object
Returns the value of attribute active
3 4 5 |
# File 'lib/forty_facets/order.rb', line 3 def active @active end |
#definition ⇒ Object
Returns the value of attribute definition
3 4 5 |
# File 'lib/forty_facets/order.rb', line 3 def definition @definition end |
#search ⇒ Object
Returns the value of attribute 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 |
#by ⇒ Object
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 |
#title ⇒ Object
4 5 6 |
# File 'lib/forty_facets/order.rb', line 4 def title definition.title end |