Module: Heimdallr::Model::ClassMethods

Defined in:
lib/heimdallr/model.rb

Overview

Class methods for Heimdallr::Model. See also ActiveSupport::Concern.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#heimdallr_relationsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

An internal attribute to store the list of user-defined relation-like methods which return ActiveRecord family objects and can be automatically restricted.



68
69
70
# File 'lib/heimdallr/model.rb', line 68

def heimdallr_relations
  @heimdallr_relations
end

#heimdallr_scopesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

An internal attribute to store the list of user-defined name scopes. It is required because ActiveRecord does not provide any introspection for named scopes.



54
55
56
# File 'lib/heimdallr/model.rb', line 54

def heimdallr_scopes
  @heimdallr_scopes
end

Instance Method Details

#heimdallr_relation(*methods) ⇒ Object

A DSL method for defining relation-like methods.



71
72
73
74
# File 'lib/heimdallr/model.rb', line 71

def heimdallr_relation(*methods)
  self.heimdallr_relations ||= []
  self.heimdallr_relations  += methods.map(&:to_sym)
end

#restrict { ... } ⇒ Object #restrict(context, action = :view) ⇒ Proxy::Collection

Overloads:

  • #restrict { ... } ⇒ Object

    Define restrictions for a model with a DSL. See Heimdallr::Model overview for DSL documentation.

    Yields:

    • A passed block is executed in the context of a new Evaluator.

  • #restrict(context, action = :view) ⇒ Proxy::Collection

    Return a secure collection object for the current scope.

    Parameters:

    • context (Object)

      security context

    • action (Symbol) (defaults to: :view)

      kind of actions which will be performed

    Returns:



34
35
36
37
38
39
40
# File 'lib/heimdallr/model.rb', line 34

def restrict(context=nil, options={}, &block)
  if block
    @restrictions = Evaluator.new(self, block)
  else
    Proxy::Collection.new(context, restrictions(context).request_scope(:fetch, self), options)
  end
end

#restrictions(context, record = nil) ⇒ Evaluator

Evaluate the restrictions for a given context and record.

Returns:



45
46
47
# File 'lib/heimdallr/model.rb', line 45

def restrictions(context, record=nil)
  @restrictions.evaluate(context, record)
end

#scope(name, *args) ⇒ Object

An interceptor for named scopes which adds them to #heimdallr_scopes list.



57
58
59
60
61
62
# File 'lib/heimdallr/model.rb', line 57

def scope(name, *args)
  self.heimdallr_scopes ||= []
  self.heimdallr_scopes.push name

  super
end