Sha256: f47ab2383ee4354c51079a5affd58e8fe8d48a38b0f4062805ca39f8fe55563b

Contents?: true

Size: 1.78 KB

Versions: 2

Compression:

Stored size: 1.78 KB

Contents

# encoding: utf-8
module Mongoid
  module Max
    module Denormalize
      extend ActiveSupport::Concern

      included do

      end

      module ClassMethods

        def denormalize(relation, *fields)
          options = fields.extract_options!

          #TODO make all real fields if fields.empty?

#          puts "#relation : #{relation.inspect}"
#          puts "#fields   : #{fields.inspect}"
#          puts "#options  : #{options.inspect}"

          meta = self.relations[relation.to_s]
          raise ConfigError.new("Unknown relation", self, relation) if meta.nil?
#          puts "#        meta : #{meta.inspect}"

          inverse_meta = meta.klass.relations[meta.inverse.to_s]
          raise ConfigError.new("Unknown inverse relation", self, relation) if inverse_meta.nil?
#          puts "#inverse_meta : #{inverse_meta.inspect}"


          if meta.relation == Mongoid::Relations::Referenced::In && inverse_meta.relation == Mongoid::Relations::Referenced::Many
            OneToMany.new(self, meta, inverse_meta, fields, options).attach
          elsif meta.relation == Mongoid::Relations::Referenced::Many && inverse_meta.relation == Mongoid::Relations::Referenced::In
            ManyToOne.new(self, meta, inverse_meta, fields, options).attach
          else
            raise ConfigError.new("Relation not supported", self, relation)
          end

        end

        def mongoid3
          Mongoid::VERSION =~ /\A3\./
        end
        def mongoid2
          Mongoid::VERSION =~ /\A2\./
        end

        def denormalize_update_all(conditions, updates)
          if mongoid3
            self.collection.find(conditions).update_all(updates)
          else
            self.collection.update(conditions, updates, :multi => true)
          end
        end

      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongoid_max_denormalize-0.0.7 lib/mongoid/max/denormalize.rb
mongoid_max_denormalize-0.0.6 lib/mongoid/max/denormalize.rb