From cbb47965992065a74d341c9c81f840011d25ee60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 20 Oct 2009 12:49:49 -0200 Subject: [PATCH] Revert "KISS." Simpler doesn't work here. This reverts commit e144ce29c9480430759691f9307fef799c514fdf. --- lib/devise.rb | 20 ++++++++++++++++++++ lib/devise/active_record.rb | 12 +++++++++++- lib/devise/models/authenticable.rb | 7 +++---- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/lib/devise.rb b/lib/devise.rb index 3b3f5685..83fcacef 100644 --- a/lib/devise.rb +++ b/lib/devise.rb @@ -16,6 +16,26 @@ module Devise }.freeze TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE'].freeze + + MODEL_CONFIG = [] + + def self.model_config(klass, accessor, default=nil) + # Create Devise accessor + mattr_accessor accessor + + # Set default value + send(:"#{accessor}=", default) + + # Store configuration method + MODEL_CONFIG << accessor + + # Set default value + klass.class_eval <<-METHOD + def #{accessor} + Devise.#{accessor} + end + METHOD + end end require 'devise/warden' diff --git a/lib/devise/active_record.rb b/lib/devise/active_record.rb index 92ba5e4e..f6d2cd7a 100644 --- a/lib/devise/active_record.rb +++ b/lib/devise/active_record.rb @@ -47,6 +47,7 @@ module Devise # def devise(*modules) options = modules.extract_options! + options.assert_valid_keys(:except, *Devise::MODEL_CONFIG) modules = Devise::ALL if modules.include?(:all) modules -= Array(options.delete(:except)) if options.key?(:except) @@ -57,8 +58,17 @@ module Devise include Devise::Models.const_get(m.to_s.classify) end + # Convert new keys to methods which overwrites Devise defaults options.each do |key, value| - self.send(:"#{key}=", value) + if value.is_a?(Proc) + define_method key, &value + else + class_eval <<-END_EVAL, __FILE__, __LINE__ + def #{key} + #{value.inspect} + end + END_EVAL + end end end diff --git a/lib/devise/models/authenticable.rb b/lib/devise/models/authenticable.rb index df2ab96b..feb95a8a 100644 --- a/lib/devise/models/authenticable.rb +++ b/lib/devise/models/authenticable.rb @@ -24,14 +24,13 @@ module Devise # User.find(1).valid_password?('password123') # returns true/false # module Authenticable + Devise.model_config(self, :pepper) + Devise.model_config(self, :stretches, 10) + def self.included(base) base.class_eval do extend ClassMethods - cattr_accessor :pepper, :stretches, :instance_writer => false - protected :pepper, :stretches - self.pepper, self.stretches = nil, 10 - attr_reader :password attr_accessor :password_confirmation attr_accessible :email, :password, :password_confirmation