Module: Queryable

Defined in:
lib/queryable.rb,
lib/queryable/default_query.rb,
lib/queryable/default_scope.rb

Overview

Public: Allows to define default scopes in query objects, and inherit them in query object subclasses.

Defined Under Namespace

Modules: ClassMethods, DefaultQuery, DefaultScope

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_delegated_methodsObject

Internal: Default methods to be delegated to the internal query.

Returns an Array with the name of the methods to delegate.



115
116
117
118
119
# File 'lib/queryable.rb', line 115

def self.default_delegated_methods
  Array.instance_methods - Object.instance_methods +
  [:all, :where, :distinct, :group, :having, :includes, :joins, :limit, :offset, :order, :reverse_order] +
  [:==, :as_json, :cache_key, :decorate]
end

.included(base) ⇒ Object

Internal: Adds class methods, a query accessor, and method delegation.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/queryable.rb', line 16

def self.included(base)
  base.extend Forwardable
  base.extend ClassMethods
  base.class_eval do
    # Public: Gets/Sets the internal query.
    attr_accessor :query

    # Internal: Delegates Array and Criteria methods to the internal query.
    delegate *Queryable.default_delegated_methods
  end
end

.scope_method(name) ⇒ Object

Internal: Generates the scope interceptor method.

name - Name of the method to convert to a scope.

Returns a String with the code of the scope method.



103
104
105
106
107
108
109
110
# File 'lib/queryable.rb', line 103

def self.scope_method(name)
  "    def \#{name}(*args)\n      @query = super\n      self\n    end\n  SCOPE\nend\n"

Instance Method Details

#initialize(query) ⇒ Object

Public: Initialize a Queryable with a query.

query - The internal query to build upon.



31
32
33
# File 'lib/queryable.rb', line 31

def initialize(query)
  @query = query.all
end