Module: Mongoid::Mirrored::HelperMethods

Included in:
ClassMethods
Defined in:
lib/mongoid-mirrored/helper_methods.rb

Instance Method Summary collapse

Instance Method Details

#extract_options(*args) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/mongoid-mirrored/helper_methods.rb', line 4

def extract_options(*args)
  options = args.extract_options!
  self.embedding_models = {:all => args, :current => nil}
  self.embedding_options = options
  
  # set defaults
  self.embedding_options[:sync_events] ||= :all
  self.embedding_options[:sync_events] = [self.embedding_options[:sync_events]] unless embedding_options[:sync_events].is_a? Array
  self.embedding_options[:sync_direction] ||= :both
  self.embedding_options[:replicate_to_siblings] = true if self.embedding_options[:replicate_to_siblings].nil?
  self.embedding_options[:inverse_of] ||= :many
  self.embedding_options[:index] = false if self.embedding_options[:index].nil?
  self.embedding_options[:background_index] = false if self.embedding_options[:background_index].nil?
end

#root_associationObject

name of the association used by the embedding class eg: comments



21
22
23
24
25
# File 'lib/mongoid-mirrored/helper_methods.rb', line 21

def root_association
  inverse_of = @root_klass.name.underscore
  inverse_of = inverse_of.pluralize if embedding_options[:inverse_of] == :many
  inverse_of
end

#symbol_to_class(symbol) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/mongoid-mirrored/helper_methods.rb', line 27

def symbol_to_class(symbol)
  begin
    symbol.to_s.classify.constantize 
  rescue
    Object.const_set symbol.to_s.classify, Class.new 
  ensure
    symbol.to_s.classify.constantize 
  end
end