Class: Deli::Adapters::ActiveRecord::Query

Inherits:
Query
  • Object
show all
Defined in:
lib/deli/adapters/active_record.rb

Instance Attribute Summary

Attributes inherited from Query

#controller

Instance Method Summary collapse

Methods inherited from Query

#append_join, build_query, #conditions, #conditions?, #default_sort, #find, #initialize, #joins, #matching, #parse, parse_query, #parse_query, parse_string, #parse_string, #to_conditions, unescape

Constructor Details

This class inherits a constructor from Deli::Query

Instance Method Details

#render(params) ⇒ Hash

Converts request parameters into an options Hash for ActiveRecord finder methods.# Outputs a query hash for Mongoid.

Examples:

Declare the queries you want to make in your controller.


queries :order => "created_at desc" do
  match :full_name
  match :created, :field => :created_at, :strict => true
  match :status, :operators => [:in] # should ultimately have full-on validations
  match :product, :type => :string, :scope => :with_bookmarked_products
end

Request with all possible parameters


"/users?full_name=^lance&created=2011-08-01..t&status=active&product=laptop"

Parameters:

  • params (Hash)

    ({}) The query params hash from the controller.

Options Hash (params):

  • :type (::Symbol)

    Defaults to the database field type. If you use named scopes to reference assiated models, as of now you must specify the type manually. Haven’t looked through the Rails 3 scoping code to figure out how use reflection to get join keys.

  • :field (::String, ::Symbol)

    If a ::Symbol, the table/collection attribute to query, if a ::String, the fully qualified table/collection + attribute string.

  • :operators (::Array)

    Optional, refines the allowable query value formats.

  • :strict (::Boolean) — default: +false

    If true, it will raise an error if the URL parameter is not properly formatted (e.g. “2011..” vs. “2011..t”)

  • :scope (::Symbol)

    Optional, a class method on the model for the controller, used as a named scope. In here place your joins(:x).where(:y) code.

Returns:

  • (Hash)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/deli/adapters/active_record.rb', line 64

def render(params)
  hash                = super
  
  hash["sort"]        = default_sort if hash["sort"].blank? && default_sort.present?
  
  sort                = hash.delete("sort")
  limit               = hash.delete("limit") || 20
  page                = hash.delete("page") || 1
  
  offset              = limit.present? && page.present? ? ((page - 1) * limit).to_i : 0
            
  keys                = []
  values              = []
  
  hash.values.collect do |array|
    keys << array[0]
    values << array[1..-1]
  end
  
  keys.map! {|i| "(#{i})"} if keys.length > 1
  keys                = keys.join(" AND ")
  values.flatten!
  conditions          = [keys, *values]
  
  result              = {}
  result[:conditions] = conditions if conditions.present?
  result[:limit]      = limit if limit.present? && limit > 0
  result[:offset]     = offset if offset.present? && offset > 0
  result[:order]      = sort if sort.present?
  
  result
end