From 8f3539c14fbd5e292f4d54ad2fca906b8870a81b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 16 Apr 2011 13:15:31 +0200 Subject: [PATCH] Don't include the same module several times, closes #765. --- lib/devise/models.rb | 7 ++++--- test/models_test.rb | 22 +++++++++++++++++----- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/lib/devise/models.rb b/lib/devise/models.rb index 143d92e5..435b4b33 100644 --- a/lib/devise/models.rb +++ b/lib/devise/models.rb @@ -51,12 +51,12 @@ module Devise include Devise::Models::Authenticatable options = modules.extract_options!.dup - self.devise_modules += modules.map(&:to_sym).uniq.sort_by { |s| + selected_modules = modules.map(&:to_sym).uniq.sort_by do |s| Devise::ALL.index(s) || -1 # follow Devise::ALL order - } + end devise_modules_hook! do - devise_modules.each do |m| + selected_modules.each do |m| mod = Devise::Models.const_get(m.to_s.classify) if mod.const_defined?("ClassMethods") @@ -75,6 +75,7 @@ module Devise include mod end + self.devise_modules |= selected_modules options.each { |key, value| send(:"#{key}=", value) } end end diff --git a/test/models_test.rb b/test/models_test.rb index eed5e6ba..ad1d8e7d 100644 --- a/test/models_test.rb +++ b/test/models_test.rb @@ -10,6 +10,11 @@ class WithValidation < Admin devise :database_authenticatable, :validatable, :password_length => 2..6 end +class Several < Admin + devise :validatable + devise :lockable +end + class Inheritable < Admin end @@ -33,11 +38,18 @@ class ActiveRecordTest < ActiveSupport::TestCase assert_include_modules Admin, :database_authenticatable, :registerable, :timeoutable, :recoverable, :lockable, :rememberable, :encryptable end - test 'validations options are not applied to late' do - validators = WithValidation.validators_on :password - length = validators.find { |v| v.kind == :length } - assert_equal 2, length.options[:minimum] - assert_equal 6, length.options[:maximum] + if DEVISE_ORM == :active_record + test 'validations options are not applied to late' do + validators = WithValidation.validators_on :password + length = validators.find { |v| v.kind == :length } + assert_equal 2, length.options[:minimum] + assert_equal 6, length.options[:maximum] + end + + test 'validations are applied just once' do + validators = Several.validators_on :password + assert_equal 1, validators.select{ |v| v.kind == :length }.length + end end test 'chosen modules are inheritable' do