Class: Morpheus::Mixins::Associations::Association
- Inherits:
- ActiveSupport::BasicObject
- Defined in:
- lib/morpheus/mixins/associations/association.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#id ⇒ Object
The proxy implements a few methods that need to be delegated to the target so that they will work as expected.
- #includes(*associations) ⇒ Object
-
#initialize(owner, association, settings = {}) ⇒ Association
constructor
Associations can be loaded with several options.
-
#load_target! ⇒ Object
This is left to be implemented by the subclasses as it will operate differently in each case.
- #nil? ⇒ Boolean
- #to_param ⇒ Object
- #try(method, *args, &block) ⇒ Object
Constructor Details
#initialize(owner, association, settings = {}) ⇒ Association
Associations can be loaded with several options.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/morpheus/mixins/associations/association.rb', line 22 def initialize(owner, association, settings = {}) # @owner stores the class that the association exists on. @owner = owner # @association stores the associated class, as named in the association # method (i.e. :automobile, :car, :car_club) @association = association # @target stores the loaded object. It is not typically accessed directly, # but instead should be accessed through the loaded_target method. @target = settings[:target] @filters = settings[:filters] || [] @includes = [] # @options holds the chosen options for the association. Several of these # options are set in the subclass' initializer. @options = settings[:options] || {} # In some cases, the association name will not match that of the class # that should be instantiated when it is invoked. Here, we can specify # that this association uses a specified class as its target. When the # request is made for the association, this class will be used to # instantiate this object or collection. @options[:class_name] = settings[:options][:class_name] || @association.to_s.classify end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object (private)
The method_missing hook will be called when methods that do not exist on the proxy object are invoked. This is the point at which the proxied object is loaded, if it has not been loaded already.
101 102 103 104 105 106 107 |
# File 'lib/morpheus/mixins/associations/association.rb', line 101 def method_missing(m, *args, &block) if filter = @association_class.find_filter(m) with_filter(filter) else loaded_target.send(m, *args, &block) end end |
Instance Method Details
#id ⇒ Object
The proxy implements a few methods that need to be delegated to the target so that they will work as expected.
52 53 54 |
# File 'lib/morpheus/mixins/associations/association.rb', line 52 def id loaded_target.id end |
#includes(*associations) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/morpheus/mixins/associations/association.rb', line 68 def includes(*associations) associations.each do |association| @includes << association unless @includes.include?(association) end self end |
#load_target! ⇒ Object
This is left to be implemented by the subclasses as it will operate differently in each case.
77 78 |
# File 'lib/morpheus/mixins/associations/association.rb', line 77 def load_target! end |
#nil? ⇒ Boolean
56 57 58 |
# File 'lib/morpheus/mixins/associations/association.rb', line 56 def nil? loaded_target.nil? end |
#to_param ⇒ Object
60 61 62 |
# File 'lib/morpheus/mixins/associations/association.rb', line 60 def to_param loaded_target.to_param end |
#try(method, *args, &block) ⇒ Object
64 65 66 |
# File 'lib/morpheus/mixins/associations/association.rb', line 64 def try(method, *args, &block) loaded_target.try(method, *args, &block) end |