Module: Mongoid::Lookup::Model::ClassMethods

Defined in:
lib/mongoid_lookup/model.rb

Instance Method Summary collapse

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, options
  return if options[: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.

Parameters:

  • name (Symbol)

    name of lookup

  • options (Hash)

Options Hash (options):

  • :collection (Mongoid::Document)


56
57
58
59
60
# File 'lib/mongoid_lookup/model.rb', line 56

def build_lookup name, options, █
  define_lookup_reference(name, options, &block)
  relate_lookup_reference(name, options)
  attach_lookup_reference_callback(name, options)
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, options, █
  const_set("#{name.to_s.classify}Reference", Class.new(lookup_reference_parent(name, options)))
  lookup(name).send(:include, Reference) unless included_modules.include?(Reference)
  lookup(name).configure_lookup_reference(options)
  if block_given?
    lookup(name).class_eval(&block)
  end
end

#has_lookup?(name) ⇒ Boolean

whether or not the given lookup is defined

Parameters:

  • name (String, Symbol)

Returns:

  • (Boolean)


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

Parameters:

  • name (String, Symbol)

Returns:



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

Parameters:

  • name (Symbol)

Returns:

  • (Array<String>)


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

Parameters:

  • name (String, Symbol)
  • options (Array)

Returns:

Raises:

  • KeyError if not inheriting and collection is not given



108
109
110
# File 'lib/mongoid_lookup/model.rb', line 108

def lookup_reference_parent name, options
  options[:inherit] ? nearest_lookup_reference(name) : options.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.

Parameters:

  • name (String, Symbol)

Returns:

Raises:



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, options
  has_one "#{name}_reference".to_sym, :as => :referenced, :class_name => lookup(name).name, :dependent => :destroy
end