Class: ResourceFull::Query::OrderParameter

Inherits:
Parameter
  • Object
show all
Defined in:
lib/resource_full/query.rb

Instance Attribute Summary

Attributes inherited from Parameter

#name, #resource

Instance Method Summary collapse

Methods inherited from Parameter

#allow_nil?, #fuzzy?, #initialize, #to_xml

Constructor Details

This class inherits a constructor from ResourceFull::Query::Parameter

Instance Method Details

#applicable_to?(request_params) ⇒ Boolean

Returns:

  • (Boolean)


213
214
215
# File 'lib/resource_full/query.rb', line 213

def applicable_to?(request_params)
  request_params.has_key?(:order_by)
end

#find(finder, request_params) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/resource_full/query.rb', line 225

def find(finder, request_params)
  return finder unless applicable_to?(request_params)
    
  order_by = request_params[:order_by]
  order_direction = request_params[:order_direction] || "asc"
  sort_params = resource.orderables[order_by.to_sym] || {}
  table = table_for(sort_params)
  column = sort_params[:column] || order_by
    
  order_params = returning({}) do |hash|
    hash[:include] = sort_params[:from]
  end
    
  if natural_sort_for(sort_params)
    # to use this natural sort you must follow these instructions: http://www.ciarpame.com/2008/06/28/true-mysql-natural-order-by-trick/
    finder.scoped order_params.merge( :order => "natsort_canon(#{table}.#{column}, 'natural') #{order_direction}" )
  else
    finder.scoped order_params.merge( :order => "#{table}.#{column} #{order_direction}" )
  end
end

#natural_sort_for(opts) ⇒ Object



217
218
219
220
221
222
223
# File 'lib/resource_full/query.rb', line 217

def natural_sort_for(opts)
  if opts.has_key?(:natural_sort)
    opts[:natural_sort]
  else
    false
  end
end

#subclass(new_resource) ⇒ Object



246
247
248
# File 'lib/resource_full/query.rb', line 246

def subclass(new_resource)
  self.class.new(@name, new_resource)
end