Module: ActiveData::Model::Scopes::ScopeProxy

Extended by:
ActiveSupport::Concern
Defined in:
lib/active_data/model/scopes.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

up to 2.6.x



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/active_data/model/scopes.rb', line 66

def method_missing(method, *args, **kwargs, &block)
  with_scope do
    model = self.class._scope_model
    if model.respond_to?(method)
      result = model.public_send(method, *args, **kwargs, &block)
      result.is_a?(ActiveData::Model::Scopes) ? result : model.scope_class.new(result)
    else
      super
    end
  end
end

Class Method Details

.for(model) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/active_data/model/scopes.rb', line 14

def self.for(model)
  klass = Class.new(model._scope_base) do
    include ActiveData::Model::Scopes::ScopeProxy
  end
  klass.define_singleton_method(:_scope_model) { model }
  model.const_set('ScopeProxy', klass)
end

Instance Method Details

#respond_to_missing?(method, _) ⇒ Boolean

Returns:



36
37
38
# File 'lib/active_data/model/scopes.rb', line 36

def respond_to_missing?(method, _)
  super || self.class._scope_model.respond_to?(method)
end

#with_scopeObject

rubocop:enable Style/MethodMissing



79
80
81
82
83
84
85
# File 'lib/active_data/model/scopes.rb', line 79

def with_scope
  previous_scope = self.class._scope_model.current_scope
  self.class._scope_model.current_scope = self
  result = yield
  self.class._scope_model.current_scope = previous_scope
  result
end