Module: Granite::Form::Model::Scopes::ScopeProxy

Extended by:
ActiveSupport::Concern
Defined in:
lib/granite/form/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



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/granite/form/model/scopes.rb', line 70

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?(Granite::Form::Model::Scopes) ? result : model.scope_class.new(result)
    else
      super
    end
  end
end

Class Method Details

.for(model) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/granite/form/model/scopes.rb', line 15

def self.for(model)
  klass = Class.new(model._scope_base) do
    include Granite::Form::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:



40
41
42
# File 'lib/granite/form/model/scopes.rb', line 40

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

#with_scopeObject



81
82
83
84
85
86
87
# File 'lib/granite/form/model/scopes.rb', line 81

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