Sha256: eaa23631f2c9818173e1f72ab2ea94bb6d0b460eb1028af56e882204811db9d1

Contents?: true

Size: 1.71 KB

Versions: 5

Compression:

Stored size: 1.71 KB

Contents

require 'dm-devise'
require 'devise/orm/data_mapper/compatibility'
require 'devise/orm/data_mapper/schema'
require 'devise/orm/data_mapper/date_time'

module Devise
  module Orm
    module DataMapper
      module Hook
        def devise_modules_hook!
          extend Schema
          include ActiveModel::Validations
          include ActiveModelCompatibility
          include Compatibility

          def self.validates_uniqueness_of(*fields)
            validates_with UniquenessValidator, _merge_attributes(fields)
          end
          
          yield
          return unless Devise.apply_schema
          devise_modules.each { |m| send(m) if respond_to?(m, true) }
        end
      end

      class UniquenessValidator < ActiveModel::EachValidator
        def validate_each(target, attribute, value)
          resource = ::DataMapper.repository(target.repository.name) { target.model.first(attribute => value) }
          if resource.nil? || (target.saved? && resource.key == target.key)
            return true
          else
            target.errors.add(attribute, :taken)
            return false
          end
        end
      end

      module ActiveModelCompatibility
        # include ActiveModel::Validations does not make save check valid?.
        # This may not be the best solution, but it seems to work. Note that
        # Compatibility is included after this module; its #save method handles
        # the :validate => false option.
        def save(*args)
          retval = valid? && super(*args)
          assert_save_successful(:save, retval)
          retval
        end
      end
    end
  end
end

DataMapper::Model.append_extensions(Devise::Models)
DataMapper::Model.append_extensions(Devise::Orm::DataMapper::Hook)

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dm-devise-1.1.8 lib/devise/orm/data_mapper_active_model.rb
dm-devise-1.1.6 lib/devise/orm/data_mapper_active_model.rb
dm-devise-1.1.5 lib/devise/orm/data_mapper_active_model.rb
dm-devise-1.1.4 lib/devise/orm/data_mapper_active_model.rb
dm-devise-1.1.3 lib/devise/orm/data_mapper_active_model.rb