Permalink
Browse files

MongoMapper >= 0.9.0 support

  • Loading branch information...
1 parent f5760f5 commit 12c52e66bba5a894f114627672b0e4f91a2b4c6f @bmarini bmarini committed Jun 4, 2011
View
@@ -11,10 +11,7 @@ group :default do
gem "rails", "~> 3.0.7"
gem "webrat", "~> 0.7.2"
- gem "bcrypt-ruby", :require => "bcrypt"
- gem "oauth2"
- gem "warden", ">= 0.10.7"
- gem 'mongo_mapper', '>= 0.8.4'
+ gem 'mongo_mapper', '0.9.0'
gem 'sugar-high', '~> 0.3.4'
gem 'devise', '>= 1.1.1'
gem 'rails3-generators', '>= 0.13.0'
@@ -1,26 +1,37 @@
-require 'mongo_mapper'
-require 'devise/orm/mongo_mapper/compatibility'
-require 'devise/orm/mongo_mapper/schema'
-require 'devise/orm/mongo_mapper/date_time'
-require 'devise/orm/mongo_mapper/mm-validations'
+require 'orm_adapter/adapters/mongo_mapper'
module Devise
module Orm
module MongoMapper
+ extend ActiveSupport::Concern
+
+ included do
+ extend Devise::Models
+ extend Devise::Orm::MongoMapper::Hook
+ end
+
module Hook
def devise_modules_hook!
extend Schema
- include Compatibility
- include Validatable
yield
return unless Devise.apply_schema
devise_modules.each { |m| send(m) if respond_to?(m, true) }
- end
+ end
+
end
+
+ module Schema
+ include Devise::Schema
+
+ # Tell how to apply schema methods
+ def apply_devise_schema(name, type, options={})
+ type = Time if type == DateTime
+ key(name, type, options)
+ end
+ end
+
end
end
end
-MongoMapper::Document.append_extensions(Devise::Models)
-MongoMapper::Document.append_extensions(Devise::Orm::MongoMapper::Hook)
-
+MongoMapper::Document.plugin Devise::Orm::MongoMapper
@@ -1,89 +0,0 @@
-module Devise
- module Orm
- module MongoMapper
- module Compatibility
- extend ActiveSupport::Concern
-
- extend ActiveSupport::Concern
- extend ActiveModel::Naming
-
- include ActiveModel::Serializers::Xml
- include ActiveModel::Serializers::JSON
-
- def to_xml(options = {}, &block)
- options[:except] ||= []
- options[:except] << :_id
-
- super options do |b|
- b.id self.id
- block.call(b) if block_given?
- end
- end
-
-
- module ClassMethods
- # Hooks for confirmable
- def before_create(*args)
- wrap_hook(:before, :create, *args)
- end
-
- def after_create(*args)
- wrap_hook(:after, :create, *args)
- end
-
- def before_save(*args)
- wrap_hook(:before, :save, *args)
- end
-
- def wrap_hook(action, method, *args)
- options = args.extract_options!
-
- args.each do |callback|
- callback_method = :"#{callback}_callback_wrap"
- send action, method, callback_method
- class_eval <<-METHOD, __FILE__, __LINE__ + 1
- def #{callback_method}
- #{callback} if #{options[:if] || true}
- end
- METHOD
- end
- end
-
- # Add ActiveRecord like finder
- def find(*args)
- case args.first
- when :first, :all
- send(args.shift, *args)
- else
- where(:'_id' => args.first).first
- end
- end
- end
-
- # def changed?
- # dirty?
- # end
-=begin
- def save(options=nil)
- if options.is_a?(Hash) && options[:validate] == false
- save!
- else
- super()
- end
- end
-
- def update_attribute(name, value)
- update_attributes(name => value)
- end
- #
- # def update_attributes(*args)
- # update(*args)
- # end
-=end
- def invalid?
- !valid?
- end
- end
- end
- end
-end
@@ -1,14 +0,0 @@
-class Date
- unless method_defined?(:gmtime)
- delegate :gmtime, :to => :to_time
- end
-
- def utc
- Time.zone.parse("#{hour.to_i - zone.to_i}:#{min}:#{sec}")
- # self.to_datetime.new_offset(0)
- end
-
- def to_i
- to_time.utc.to_i
- end
-end
@@ -1,91 +0,0 @@
-require 'validatable'
-
-module Validatable
- # Monkey-patch Validatable::Errors to support generation of error message from
- # a Symbol.
- class Errors
- @@default_error_messages = {}
-
- # Holds a hash with all the default error messages that can be replaced by your own copy or localizations.
- def self.default_error_messages=(default_error_messages)
- @@default_error_messages = default_error_messages
- end
-
- def self.default_error_message(key, field, *values)
- field = field.to_s.humanize
- @@default_error_messages[key] % [field, *values].flatten
- end
-
- # original add before monkey-patch
- # def add(attribute, message) #:nodoc:
- # errors[attribute.to_sym] = [] if errors[attribute.to_sym].nil?
- # errors[attribute.to_sym] << message
- # end
-
- alias_method :original_add, :add
-
- # If the message is a Symbol, allow +default_error_message+ to generate
- # the message, including translation.
- def add(field_name, message)
- if message.kind_of?(Symbol)
- message = self.class.default_error_message(message, field_name)
- end
- fld_name = field_name.to_sym
- errors[fld_name] = [] if errors[fld_name].nil?
- original_add(fld_name, message) unless errors[fld_name].include?(message)
- end
- end
-end
-
-module Validatable
- class ValidatesPresenceOf < ValidationBase #:nodoc:
- def message(instance)
- super || "can't be blank"
- end
- end
-end
-
-module Validatable
- class ValidatesLengthOf < ValidationBase #:nodoc:
- def message(instance)
- min = !within.nil? ? within.first : minimum
- max = !within.nil? ? within.last : maximum
- super || "must be between #{min} and #{max} characters long"
- end
- end
-end
-
-
-# Default error messages consistent with ActiveModel messages and devise
-# expectations.
-Validatable::Errors.default_error_messages = {
- :absent => 'must be absent',
- :inclusion => 'is not included in the list',
- :exclusion => 'is reserved',
- :invalid => 'is invalid',
- :confirmation => " doesn't match confirmation",
- :accepted => 'must be accepted',
- :nil => 'must not be nil',
- :empty => "can't be empty",
- :blank => "can't be blank",
- :length_between => 'must be between %s and %s characters long',
- :too_long => 'is too long (maximum is %s characters)',
- :too_short => 'is too short (minimum is %s characters)',
- :wrong_length => 'is the wrong length (should be %s characters)',
- :taken => 'has already been taken',
- :not_a_number => 'is not a number',
- :not_an_integer => 'must be an integer',
- :greater_than => 'must be greater than %s',
- :greater_than_or_equal_to => 'must be greater than or equal to ',
- :equal_to => 'must be equal to %s',
- :not_equal_to => 'must not be equal to %s',
- :less_than => 'must be less than %s',
- :less_than_or_equal_to => 'must be less than or equal to %s',
- :value_between => 'must be between %s and %s',
- :odd => 'must be odd',
- :even => 'must be even',
- :primitive => 'must be of type %s',
- :not_found => 'not found',
- :already_confirmed => 'was already confirmed',
- :not_locked => 'was not locked'
-}
@@ -1,86 +0,0 @@
-module MongoMapper
- class ValidationUtil
- class << self
- attr_accessor :counter
-
- def inc_counter
- @counter ||= 0
- @counter += 1
- end
- end
- end
-end
-
-module Devise
- module Orm
- module MongoMapper
- module Schema
- include Devise::Schema
-
- SCHEMA_OPTIONS = {
- :null => :required,
- :limit => :length
- }
-
- # Tell how to apply schema methods. This automatically maps :limit to
- # :length and :null to :required.
- def apply_devise_schema(name, type, options={})
- SCHEMA_OPTIONS.each do |old_key, new_key|
- next unless options.key?(old_key)
- if :null == old_key
- # :required is opposite of :null
- options[new_key] = !options.delete(old_key)
- else
- options[new_key] = options.delete(old_key)
- end
- end
-
- options.delete(:default) if options[:default].nil?
- required_option = options.delete(:required)
- length_option = options.delete(:length)
-
- type = Time if type == DateTime
-
- key name, type, options
-
- counter = ::MongoMapper::ValidationUtil.inc_counter
-
- handle_email(name, counter) if name == :email
- handle_length(name, counter, length_option) if length_option
- handle_required(name, counter) if required_option
- end
-
- protected
-
- def make_key prefix, name, counter
- "#{prefix}_#{name}_#{counter}"
- end
-
- def handle_required name, counter
- key = make_key :presence, name, counter
- validates_presence_of name.to_sym, :key => key
- end
-
- def handle_email name, counter
- key = make_key :unique, name, counter
- validates_uniqueness_of name.to_sym, :key => key
- end
-
- def handle_length name, counter, length_option
- key = make_key :length, name, counter
- options = case length_option
- when Fixnum
- {:maximum => length_option}
- when Hash
- length_option
- else
- raise ArgumentError, "length validation option must be either a Fixnum or Hash"
- end
- options[:key] = key
-
- validates_length_of name.to_sym, options
- end
- end
- end
- end
-end
Oops, something went wrong.

0 comments on commit 12c52e6

Please sign in to comment.