Class Criteria::Query
In: lib/query.rb
Parent: TableAlias

Repesents a query object. Queries are created by calling the query method on an Activerecord object: pq = Person.query

Methods

count   find   find_one   find_options   includes   join_alias   new   paginate  

Included Modules

Filterable

Public Class methods

[Source]

    # File lib/query.rb, line 13
13:     def initialize( model_class, scope = nil )
14:       @model_class = model_class
15:       @scope = scope
16:       @query = self
17:       @table_alias = self
18:       @join_aliases = {model_class.table_name=>[model_class.table_name]}
19:       @alias = model_class.table_name
20:     end

Public Instance methods

[Source]

    # File lib/query.rb, line 65
65:     def count options={}
66:       if @scope
67:         model_class.send(:with_scope,@scope) { model_class.count find_options(options) }
68:       else
69:         model_class.count find_options(options)
70:       end
71:     end

execute the query. Pass in additional ActiveRecord find options, for example Person.query.find(:all, :limit=>20, :order=>’name’) method can be :all or :first, if omitted will default to :all.

[Source]

    # File lib/query.rb, line 40
40:     def find method=:all, options={}
41:       if method.is_a? Hash
42:         options = method
43:         method= :all
44:       end
45:       if @scope
46:         model_class.send(:with_scope, @scope) {model_class.find method, find_options(options)}
47:       else
48:         model_class.find method, find_options(options)
49:       end
50:     end

shortcut for find(:first)

[Source]

    # File lib/query.rb, line 74
74:     def find_one options={}
75:       find :first, options
76:     end

constructs an options hash that can be passed to a ActiveRecord find method

[Source]

    # File lib/query.rb, line 23
23:     def find_options options={}
24:       con = conditions
25:       par = parameters
26:       inc = includes
27:       cond_ary = nil
28:       if conditions
29:         cond_ary=[con]
30:         cond_ary += par if par
31:       end
32:       options[:conditions] = cond_ary if cond_ary
33:       options[:include] = includes if includes
34:       options      
35:     end

returns the hash to be used for the :include option passed to the ActiveRecord find method.

[Source]

     # File lib/query.rb, line 99
 99:     def includes
100:       return nil unless @joins and not @joins.empty?
101:       includes = @joins.collect{|j| j.includes}
102:     end

builds the correct join alias for association includes the table name is determined by reflecting on the parent model classes’ association attribute if this is the first time this table is used in the query, user the table name as the alias for subsequent uses, uses <pluralized_attribute_name>_<table_name>[_index] where index is only used if the same association is used more than once (see ActiveRecord::Associations::Base)

[Source]

    # File lib/query.rb, line 82
82:     def join_alias(join)
83:       table_name = join.model_class.table_name
84:       new_alias = table_name
85:       if @join_aliases[table_name]
86:         new_alias = "#{join.reflection.pluralize}_#{join.parent.model_class.table_name}"
87:         if @join_aliases[table_name].include? new_alias
88:           new_alias += '1'
89:           while @join_aliases[table_name].include? new_alias
90:             new_alias = new_alias.succ
91:           end
92:         end
93:       end
94:       (@join_aliases[table_name] ||= []) << new_alias
95:       return new_alias
96:     end

adds compatibility with the will_paginate plugin

[Source]

    # File lib/query.rb, line 53
53:     def paginate method=:all, options={}
54:       if method.is_a? Hash
55:         options = method
56:         method= :all
57:       end
58:       if @scope
59:         model_class.send(:with_scope, @scope) { model_class.paginate method, find_options(options) }
60:       else
61:         model_class.paginate method, find_options(options)
62:       end
63:     end

[Validate]