Module: Queryable::ClassMethods

Defined in:
lib/queryable.rb

Overview

Internal: Contains the Queryable class methods.

Instance Method Summary collapse

Instance Method Details

#delegate(*methods) ⇒ Object

Public: Delegates the specified methods to the internal query.



40
41
42
# File 'lib/queryable.rb', line 40

def delegate(*methods)
  methods.last.is_a?(Hash) ? super : def_delegators(:queryable, *methods)
end

#scope(name, proc = nil, &block) ⇒ Object

Public: Defines a new scope method, or makes an existing method chainable.

name - Name of the scope to define for this Queryable.

proc - An optional proc or lambda to be executed in the context of the

the current query.

block - An optional block to be executed in the context of the current

query.

Yields the arguments given to the scope when invoked, generally none.

Examples

scope :active, ->{ where(status: 'active') }

scope(:recent) { desc(:created_at) }

scope :of_brand do |brand|
  where(_type: "#{brand}ExtremelyFastRacingCar")
end

scope def search(field_values)
  field_values.inject(query) { |query, (field, value)|
    query.where(field => /#{value}/i)
  }
end

Returns nothing.



73
74
75
76
77
78
79
# File 'lib/queryable.rb', line 73

def scope(name, proc=nil, &block)
  if method_defined?(name)
    scope_method(name)
  else
    define_scope(name, proc || block)
  end
end