Sha256: 36dd7c60d99ae4b4f4119bd2e2d00d17bed59592f10735f712876b52b3e5e9bb

Contents?: true

Size: 1.6 KB

Versions: 3

Compression:

Stored size: 1.6 KB

Contents

require 'mongoid'

File.open(File.join(Rails.root, 'config/database.yml'), 'r') do |f|
  @settings = YAML.load(f)[Rails.env]
end

Mongoid::Config.instance.from_hash(@settings)

## various patches
module Mongoid #:nodoc:
  
  # Enabling scope in validates_uniqueness_of validation
  module Validations #:nodoc:
    class UniquenessValidator < ActiveModel::EachValidator
      def validate_each(document, attribute, value)
        conditions = { attribute => value, :_id.ne => document._id }
        
        if options.has_key?(:scope) && !options[:scope].nil?
          [*options[:scope]].each do |scoped_attr|
            conditions[scoped_attr] = document.attributes[scoped_attr]
          end
        end
      
        # Rails.logger.debug "conditions = #{conditions.inspect} / #{options[:scope].inspect}"
        
        return if document.class.where(conditions).empty?
      
        # if document.new_record? || key_changed?(document)
          document.errors.add(attribute, :taken, :default => options[:message], :value => value)
        # end
      end

      # protected
      # def key_changed?(document)
      #   (document.primary_key || {}).each do |key|
      #     return true if document.send("#{key}_changed?")
      #   end; false
      # end
    end
  end
  
  # FIX BUG #71 http://github.com/durran/mongoid/commit/47a97094b32448aa09965c854a24c78803c7f42e
  module Associations
    module InstanceMethods      
      def update_embedded(name)
        association = send(name)
        association.to_a.each { |doc| doc.save if doc.changed? || doc.new_record? } unless association.blank?
      end      
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
locomotive_cms-0.0.1.3 config/initializers/mongoid.rb
locomotive_cms-0.0.1.2 config/initializers/mongoid.rb
locomotive_cms-0.0.1.1 config/initializers/mongoid.rb