Class: QueryableWith::QuerySet

Inherits:
Object
  • Object
show all
Defined in:
lib/queryable_with.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ QuerySet

:nodoc:



45
46
47
48
49
50
51
# File 'lib/queryable_with.rb', line 45

def initialize(options={}) # :nodoc:
  @queryables = []
  
  if options.has_key?(:parent)
    @queryables << QueryableWith::ImplicitScopeParameter.new(options[:parent])
  end
end

Instance Method Details

#add_scope(scope) ⇒ Object

Add a scope that is always applied to any calls to #query. This may be a symbol (the name of the scope to add), a Hash (scope conditions) or a lambda. Useful when, for example, you only ever want to see records with an active flag set to true.

add_scope :active
add_scope :conditions => { :active => true }


89
90
91
# File 'lib/queryable_with.rb', line 89

def add_scope(scope)
  @queryables << QueryableWith::ImplicitScopeParameter.new(scope)
end

#query(base_scope, params = {}) ⇒ Object

Applies all of the defined queryable and added scopes in this query set to the given base scope (usually an ActiveRecord class, but can also be a pre-existing NamedScope object) based on the query parameters and returns a new scope, ready to be queried.



96
97
98
99
100
# File 'lib/queryable_with.rb', line 96

def query(base_scope, params={}) # :nodoc:
  @queryables.inject(base_scope) do |scope, queryer|
    queryer.query(scope, params)
  end.scoped({})
end

#queryable_with(*expected_parameters, &block) ⇒ Object

Make this QuerySet queryable with the named parameter(s). If no other options are given, this will result in a query on either the column or (if defined) scope of the same name of the base scope and values passed to #query. It also accepts the following options:

  • scope - Map the incoming parameter to this scope. The argument by be a symbol (name of the scope), a Hash (scope conditions) or a lambda.

  • column - Map the incoming parameter to this column.

  • default - Default the incoming parameter to this value even if it isn’t provided.

  • wildcard - If true, generate a SQL LIKE query with the incoming parameter. Used only if the scope option is absent or a block is not provided.

  • allow_blank - If true, treat incoming parameters mapped to nil or a blank string as IS NULL for the purposes of SQL query generation. Used only if the scope option is absent.

If a block is provided, incoming parameters to the query will be passed through that function first. For example,

queryable_with(:company_name, :scope => :by_company) do |name| 
  Company.find_by_name(name)
end

will attempt to look up a company name first, then pass it to a pre-defined scope called by_company.



75
76
77
78
79
80
81
# File 'lib/queryable_with.rb', line 75

def queryable_with(*expected_parameters, &block)
  options = expected_parameters.extract_options!
  
  @queryables += expected_parameters.map do |parameter|
    QueryableWith::QueryableParameter.new(parameter, options, &block)
  end
end