Module: Mongoid::Denormalize::ClassMethods

Defined in:
lib/mongoid_denormalize.rb

Instance Method Summary collapse

Instance Method Details

#denormalize(*args) ⇒ Object

Set a field or a number of fields to denormalize. Specify the associated object using the :from or :to options.

def Post
  include Mongoid::Document
  include Mongoid::Denormalize

  referenced_in :user
  references_many :comments

  denormalize :name, :avatar, :from => :user
  denormalize :created_at, :to => :comments
end


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mongoid_denormalize.rb', line 30

def denormalize(*args)
  *fields, options = args
  
  (self.denormalize_definitions ||= []) << { :fields => fields, :options => options }

  # Define schema
  unless options[:to]
    prefix = options[:as] || options[:from]
    fields.each do |name|
      field "#{prefix}_#{name}", :type => options[:type] || String
    end
  end
end

#is_denormalized?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/mongoid_denormalize.rb', line 44

def is_denormalized?
  true
end