Module: ResourceFull::Query::ClassMethods

Defined in:
lib/resource_full/query.rb

Instance Method Summary collapse

Instance Method Details

#clear_queryable_params!Object

:nodoc:



296
297
298
# File 'lib/resource_full/query.rb', line 296

def clear_queryable_params!
  @queryable_params = []
end

#filter_with_scope(scope = nil, &block_scope) ⇒ Object

Raises:

  • (ArgumentError)


289
290
291
292
293
# File 'lib/resource_full/query.rb', line 289

def filter_with_scope(scope=nil, &block_scope)
  raise ArgumentError, "must provide a scope name, standard scope definition, or block scope" unless (scope || block_scope)
  # TODO These should not require the use of dummy parameter names.
  queryable_with "__unused__", :default => true, :scope => scope || block_scope
end

#nests_within(*resources) ⇒ Object



321
322
323
324
325
326
# File 'lib/resource_full/query.rb', line 321

def nests_within(*resources)
  resources.each do |resource|
    expected_nest_id = "#{resource.to_s.singularize}_id"
    queryable_with expected_nest_id, :from => resource.to_sym, :resource_identifier => true
  end
end

#orderable_by(*params) ⇒ Object



328
329
330
331
332
333
# File 'lib/resource_full/query.rb', line 328

def orderable_by(*params)
  opts = params.extract_options!
  params.each do |param|
    orderables[param] = opts
  end
end

#orderablesObject



341
342
343
# File 'lib/resource_full/query.rb', line 341

def orderables
  read_inheritable_attribute(:orderables) || write_inheritable_hash(:orderables, {})
end

#queryable_paramsObject

All queryable parameters. Objects are of type ResourceFull::Query::Parameter or one of its subclasses.



301
302
303
304
305
306
307
308
309
# File 'lib/resource_full/query.rb', line 301

def queryable_params
  unless defined?(@queryable_params) && !@queryable_params.nil?
    @queryable_params = []
    if superclass.respond_to?(:queryable_params)
      @queryable_params += superclass.queryable_params.collect {|param| param.subclass(self)}
    end
  end
  @queryable_params
end

#queryable_params=(params) ⇒ Object

:nodoc:



317
318
319
# File 'lib/resource_full/query.rb', line 317

def queryable_params=(params)
  @queryable_params = params
end

#queryable_with(*args) ⇒ Object

Indicates that the resource should be queryable with the given parameters, which will be pulled from the params hash on an index or count call. Accepts the following options:

* :scope => (scope) : Use a scope. The value of this parameter may be a symbol (named scope), lambda, or hash.
  Most other parameter options build scopes internally.
* :fuzzy => true : Use a LIKE query instead of =.
* :columns / :column => ... : Override the default column, or provide a list of columns to query for this value.
* :from => :join_name : Indicate that this value should be queried by joining on another model.  Should use
  a valid relationship from this controller's exposed model (e.g., :account if belongs_to :account is specified.)
* :resource_identifier => true : Try to look up the resource controller for this value and honor its
  specified resource identifier.  Useful for nesting relationships.
* :allow_nil => true : Indicates that a nil value for a parameter should be taken to literally indicate
  that null values should be returned.  This may be changed in the future to expect the literal string 'null'
  or some other reasonable standin.
* :default => (value) : Default to this value if the parameter is not physically present in the request.

Examples:

queryable_with :user_id
queryable_with :description, :fuzzy => true
queryable_with :name, :columns => [:first_name, :last_name]
queryable_with :street_address, :from => :address, :column => :street

TODO No full-text search support.



277
278
279
280
281
282
283
284
285
286
287
# File 'lib/resource_full/query.rb', line 277

def queryable_with(*args)
  opts = args.extract_options!
  opts.assert_valid_keys :default, :fuzzy, :column, :columns, :from, :table, :resource_identifier, :allow_nil, :negated, :scope
  args.each do |param|
    self.queryable_params << if opts.has_key?(:scope)
      ResourceFull::Query::ScopedParameter.new(param, self, opts)
    else
      ResourceFull::Query::CustomParameter.new(param, self, opts)
    end
  end
end

#queryable_with?(*params) ⇒ Boolean

Returns true if the controller is queryable with all of the named parameters.

Returns:

  • (Boolean)


312
313
314
# File 'lib/resource_full/query.rb', line 312

def queryable_with?(*params)
  (queryable_params.collect(&:name) & params.collect(&:to_sym)).size == params.size
end

#queryable_with_orderObject



335
336
337
338
339
# File 'lib/resource_full/query.rb', line 335

def queryable_with_order
  unless queryable_with?(:order_by)
    queryable_params << ResourceFull::Query::OrderParameter.new(:order_by, self)
  end
end