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_proxy_class ⇒ Object
Builds the Proxy class that should be used to wrap this model.
-
#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.
73 74 75 |
# File 'lib/heimdallr/model.rb', line 73 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.
59 60 61 |
# File 'lib/heimdallr/model.rb', line 59 def heimdallr_scopes @heimdallr_scopes end |
Instance Method Details
#heimdallr_proxy_class ⇒ Object
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
39 40 41 42 43 44 45 |
# File 'lib/heimdallr/model.rb', line 39 def restrict(context=nil, ={}, &block) if block self.heimdallr_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
.
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 |