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.



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

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.



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

def heimdallr_scopes
  @heimdallr_scopes
end

Instance Method Details

#heimdallr_proxy_classObject

Builds the Proxy class that should be used to wrap this model



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/heimdallr/model.rb', line 82

def heimdallr_proxy_class
  unless @heimdallr_proxy_class
    record = self

    @heimdallr_proxy_class = Class.new(Proxy::Record) do
      define_singleton_method :model_name do
        record.model_name
      end
    end
  end

  @heimdallr_proxy_class
end

#heimdallr_relation(*methods) ⇒ Object

A DSL method for defining relation-like methods.



76
77
78
79
# File 'lib/heimdallr/model.rb', line 76

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:



39
40
41
42
43
44
45
# File 'lib/heimdallr/model.rb', line 39

def restrict(context=nil, options={}, &block)
  if block
    self.heimdallr_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:



50
51
52
# File 'lib/heimdallr/model.rb', line 50

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

#scope(name, *args) ⇒ Object

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



62
63
64
65
66
67
# File 'lib/heimdallr/model.rb', line 62

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

  super
end