Module: Mongoid::Lookup::Model::ClassMethods
- Defined in:
- lib/mongoid_lookup/model.rb
Instance Method Summary collapse
-
#attach_lookup_reference_callback(name, options) ⇒ Object
add a save hook for the given reference unless already defined (inheriting).
-
#build_lookup(name, options, &block) ⇒ Object
Create a lookup of the given name on the host model.
-
#define_lookup_reference(name, options, &block) ⇒ Object
Create a class to serve as the model for the lookup reference.
-
#has_lookup?(name) ⇒ Boolean
whether or not the given lookup is defined.
-
#lookup(name) ⇒ Mongoid::Lookup::Reference
lookup reference class for the given lookup name.
-
#lookup_fields(name) ⇒ Array<String>
the fields on the Model which map to fields on the lookup reference.
-
#lookup_reference_parent(name, options) ⇒ Mongoid::Lookup::Collection, Mongoid::Lookup::Reference
returns the appropriate lookup reference parent for the given options.
-
#nearest_lookup_reference(name) ⇒ Mongoid::Lookup::Reference
returns the lookup reference for the given name belonging to the nearest ancestor of the calling class.
-
#relate_lookup_reference(name, options) ⇒ Object
relate the Model to the created lookup Reference.
Instance Method Details
#attach_lookup_reference_callback(name, options) ⇒ Object
add a save hook for the given reference unless already defined (inheriting)
141 142 143 144 145 146 147 148 |
# File 'lib/mongoid_lookup/model.rb', line 141 def attach_lookup_reference_callback name, return if [:inherit] set_callback :save, :before do maintain_lookup_reference(name) true end end |
#build_lookup(name, options, &block) ⇒ Object
Create a lookup of the given name on the host model.
56 57 58 59 60 |
# File 'lib/mongoid_lookup/model.rb', line 56 def build_lookup name, , █ define_lookup_reference(name, , &block) relate_lookup_reference(name, ) attach_lookup_reference_callback(name, ) end |
#define_lookup_reference(name, options, &block) ⇒ Object
Create a class to serve as the model for the lookup reference. Const is set within the current model.
66 67 68 69 70 71 72 73 |
# File 'lib/mongoid_lookup/model.rb', line 66 def define_lookup_reference name, , █ const_set("#{name.to_s.classify}Reference", Class.new(lookup_reference_parent(name, ))) lookup(name).send(:include, Reference) unless included_modules.include?(Reference) lookup(name).configure_lookup_reference() if block_given? lookup(name).class_eval(&block) end end |
#has_lookup?(name) ⇒ Boolean
whether or not the given lookup is defined
96 97 98 |
# File 'lib/mongoid_lookup/model.rb', line 96 def has_lookup? name const_defined?("#{name.to_s.classify}Reference") end |
#lookup(name) ⇒ Mongoid::Lookup::Reference
lookup reference class for the given lookup name
79 80 81 |
# File 'lib/mongoid_lookup/model.rb', line 79 def lookup name const_get("#{name.to_s.classify}Reference") end |
#lookup_fields(name) ⇒ Array<String>
the fields on the Model which map to fields on the lookup reference
88 89 90 |
# File 'lib/mongoid_lookup/model.rb', line 88 def lookup_fields(name) lookup(name).lookup_field_map.values.collect{ |v| v.to_s } end |
#lookup_reference_parent(name, options) ⇒ Mongoid::Lookup::Collection, Mongoid::Lookup::Reference
returns the appropriate lookup reference parent for the given options
108 109 110 |
# File 'lib/mongoid_lookup/model.rb', line 108 def lookup_reference_parent name, [:inherit] ? nearest_lookup_reference(name) : .fetch(:collection) end |
#nearest_lookup_reference(name) ⇒ Mongoid::Lookup::Reference
returns the lookup reference for the given name belonging to the nearest ancestor of the calling class.
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/mongoid_lookup/model.rb', line 118 def nearest_lookup_reference name (ancestors - included_modules).each do |klass| if klass.respond_to?(:has_lookup?) if klass.has_lookup?(name) return klass.lookup(name) end end end raise InheritError, "no ancestor of #{self.name} has a lookup named '#{name}'" end |
#relate_lookup_reference(name, options) ⇒ Object
relate the Model to the created lookup Reference
133 134 135 |
# File 'lib/mongoid_lookup/model.rb', line 133 def relate_lookup_reference name, has_one "#{name}_reference".to_sym, :as => :referenced, :class_name => lookup(name).name, :dependent => :destroy end |