Module: Heimdallr::Model::ClassMethods
- Defined in:
- lib/heimdallr/model.rb
Overview
Class methods for Heimdallr::Model. See also ActiveSupport::Concern
.
Instance Attribute Summary collapse
-
#heimdallr_relations ⇒ Object
private
An internal attribute to store the list of user-defined relation-like methods which return ActiveRecord family objects and can be automatically restricted.
-
#heimdallr_scopes ⇒ Object
private
An internal attribute to store the list of user-defined name scopes.
Instance Method Summary collapse
-
#heimdallr_relation(*methods) ⇒ Object
A DSL method for defining relation-like methods.
- #restrict(context = nil, options = {}, &block) ⇒ Object
-
#restrictions(context, record = nil) ⇒ Evaluator
Evaluate the restrictions for a given
context
andrecord
. -
#scope(name, *args) ⇒ Object
An interceptor for named scopes which adds them to #heimdallr_scopes list.
Instance Attribute Details
#heimdallr_relations ⇒ Object
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_scopes ⇒ Object
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
34 35 36 37 38 39 40 |
# File 'lib/heimdallr/model.rb', line 34 def restrict(context=nil, ={}, &block) if block @restrictions = Evaluator.new(self, block) else Proxy::Collection.new(context, restrictions(context).request_scope(:fetch, self), ) end end |
#restrictions(context, record = nil) ⇒ Evaluator
Evaluate the restrictions for a given context
and record
.
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 |