Module: ConvenientScopes

Includes:
ScopeDefinitions
Defined in:
lib/convenient_scopes.rb

Defined Under Namespace

Modules: ScopeDefinitions Classes: InvalidScopes

Constant Summary

Constants included from ScopeDefinitions

ScopeDefinitions::ORDERING_SCOPE_DEFINITIONS, ScopeDefinitions::SCOPE_DEFINITIONS, ScopeDefinitions::SCOPE_WITHOUT_VALUE_DEFINITIONS

Instance Method Summary collapse

Methods included from ScopeDefinitions

#boolean_column_scopes, #equals_scope, #ordering_scopes, #scopes_with_value, #scopes_without_value

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/convenient_scopes.rb', line 3

def method_missing(name, *args, &block)
  if scope_data = (define_scope name)
    scope name, convert_to_scope_arg(scope_data)
    send name, *args, &block
  else
    super
  end
end

Instance Method Details

#ar_relation_methods?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def ar_relation_methods? name
  (ActiveRecord::Relation::MULTI_VALUE_METHODS + ActiveRecord::Relation::SINGLE_VALUE_METHODS +
  ActiveRecord::Relation::ASSOCIATION_METHODS).include? name
end

#define_scope(name) ⇒ Object



12
13
14
15
16
# File 'lib/convenient_scopes.rb', line 12

def define_scope name
  (ScopeDefinitions.instance_methods.inject nil do |memo, scope_type|
    memo ||= send scope_type.to_sym, name
  end) || (association_scope name)
end

#determine_order_scope_data(name, direction) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/convenient_scopes.rb', line 35

def determine_order_scope_data name, direction
  if column_names.include? name.to_s
    unscoped.order("#{quoted_table_name}.#{name} #{direction}")
  elsif assoc = (possible_association_for_scope name)
    next_scope = extract_next_scope name, assoc
    scope_arg = assoc.klass.determine_order_scope_data next_scope, direction
    scope_arg.is_a?(Array) ? [assoc.name] + scope_arg : [assoc.name, scope_arg] if scope_arg
  end
end

#search(search_scopes) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/convenient_scopes.rb', line 18

def search search_scopes
  res = scoped
  search_scopes.each do |name, args|
    if scopes.keys.include?(name.to_sym) || ar_relation_methods?(name) || !respond_to?(name)
      res = res.send name, args unless args == false
    else
      raise InvalidScopes
    end
  end
  res
end