From 7717a1b8b3ed297199cf46a5574398d188eec5a4 Mon Sep 17 00:00:00 2001 From: binarylogic Date: Wed, 28 Jan 2009 19:33:07 -0500 Subject: [PATCH] Released v1.4.0. A new I18n soluton has been added. Please see Authlogic::I18n for more info. The old solution via configuration is no longer supported. --- CHANGELOG.rdoc | 3 +- README.rdoc | 4 + Rakefile | 1 + lib/authlogic.rb | 1 + lib/authlogic/i18n.rb | 52 +++++++++++++ lib/authlogic/session/base.rb | 14 ++-- lib/authlogic/session/config.rb | 117 +++++------------------------- lib/authlogic/version.rb | 4 +- test/session_tests/config_test.rb | 84 --------------------- 9 files changed, 88 insertions(+), 192 deletions(-) create mode 100644 lib/authlogic/i18n.rb diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 13629e8e..eeac4e5d 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -1,10 +1,11 @@ -== 1.3.10 +== 1.4.0 released 2009-1-28 * Added support for cookie domain, based on your frameworks session domain configuration * Updated test helper functions to use the persistence token config value * Check for UTC times when using Time.now for current_login_at and last_request_at * Single access now looks for a single_access_allowed? method in your controllers to determine if single access should be allowed or not. Allowing you to define exactly when single access is allowed. * Finding the authenticated record uses klass.primary_key instead of assuming id. +* BREAKS BACKWARDS COMPATIBILITY: New I18n solution implemented. See Authlogic::I18n for more information. == 1.3.9 released 2009-1-9 diff --git a/README.rdoc b/README.rdoc index 485507ca..74ebaa79 100644 --- a/README.rdoc +++ b/README.rdoc @@ -477,6 +477,10 @@ Obviously there is a little more to it than this, but hopefully this clarifies a When things come together like this I think its a sign that you are doing something right. Put that in your pipe and smoke it! +== Internationalization (I18n) + +Please see Authlogic::I18n for more information. Internationalization is very easy to implement, in fact if you are using the default rails I18n library then you don't need to do anything other than defining the messages in your localization configuration files. See Authlogic::I18n for a complete list of keys you need to define. + == Testing Testing with authlogic is easy, there is a helper file that will add some convenient test helpers for you. In your test_helper.rb file do the following: diff --git a/Rakefile b/Rakefile index a0621cea..e11ddac1 100644 --- a/Rakefile +++ b/Rakefile @@ -10,4 +10,5 @@ Echoe.new 'authlogic' do |p| p.summary = "A clean, simple, and unobtrusive ruby authentication solution." p.url = "http://github.com/binarylogic/authlogic" p.dependencies = %w(activesupport echoe) + p.install_message = "BREAKS BACKWARDS COMPATIBILITY! This is only for those using I18n. If you were using the Authlogic configuration to implement I18n you need to update your configuration. A new cleaner approach has been implemented for I18n in Authlogic. See Authlogic::I18n for more details." end \ No newline at end of file diff --git a/lib/authlogic.rb b/lib/authlogic.rb index 7b0ef84a..f2a871cb 100644 --- a/lib/authlogic.rb +++ b/lib/authlogic.rb @@ -1,6 +1,7 @@ require "active_support" require File.dirname(__FILE__) + "/authlogic/version" +require File.dirname(__FILE__) + "/authlogic/i18n" require File.dirname(__FILE__) + "/authlogic/controller_adapters/abstract_adapter" require File.dirname(__FILE__) + "/authlogic/controller_adapters/rails_adapter" if defined?(Rails) diff --git a/lib/authlogic/i18n.rb b/lib/authlogic/i18n.rb new file mode 100644 index 00000000..6b464675 --- /dev/null +++ b/lib/authlogic/i18n.rb @@ -0,0 +1,52 @@ +module Authlogic + # I18n + # + # Allows any message, error message, etc to use internationalization. In earlier versions of Authlogic each message was translated via configuration. + # This cluttered up the configuration and cluttered up Authlogic. So all translation has been extracted out into this class. Now all messages pass through + # this class, making it much easier to implement in I18n library / plugin you want. Use this as a layer that sits between Authlogic and whatever I18n + # library you want to use. + # + # By default this uses the rails I18n library, if it exists. If it doesnt exist it just returns the default english message. Using the Authlogic I18n class + # works EXACTLY like the rails I18n class. Here is how all messages are translated internally with Authlogic: + # + # Authlogic::I18n.t('error_messages.password_invalid', :default => "is invalid") + # + # If you use a different I18n library or plugin just redefine the t method in the Authlogic::I18n class to do whatever you want with those options. For example: + # + # # config/initializers/authlogic.rb + # module MyAuthlogicI18nAdapter + # def t(key, options = {}) + # # you will have key which will be something like: "error_messages.password_invalid" + # # you will also have options[:default], which will be the default english version of the message + # # do whatever you want here with the arguments passed to you. + # end + # end + # + # Authlogic::I18n.extend MyAuthlogicI18nAdapter + # + # That it's! Here is a complete list of the keys that are passed. Just defined these however you wish: + # + # authlogic: + # error_messages: + # login_blank: can not be blank + # login_not_found: does not exist + # password_blank: can not be blank + # password_invlid: is not valid + # not_active: Your account is not active + # not_confirmed: Your account is not confirmed + # not_approved: Your account is not approved + # blank_record: You can not login with a blank record + # new_record: You can not login with a new record + class I18n + class << self + # All message translation is passed to this method. The first argument is the key for the message. The second is options, see the rails I18n library for a list of options used. + def t(key, options = {}) + if defined?(::I18n) + ::I18n.t(key, options.merge(:scope => :authlogic)) + else + options[:default] + end + end + end + end +end \ No newline at end of file diff --git a/lib/authlogic/session/base.rb b/lib/authlogic/session/base.rb index d760bf0f..bd057c50 100644 --- a/lib/authlogic/session/base.rb +++ b/lib/authlogic/session/base.rb @@ -412,19 +412,19 @@ def valid_credentials? case authenticating_with when :password - errors.add(login_field, login_blank_message) if send(login_field).blank? - errors.add(password_field, password_blank_message) if send("protected_#{password_field}").blank? + errors.add(login_field, I18n.t('error_messages.login_blank', :default => "can not be blank")) if send(login_field).blank? + errors.add(password_field, I18n.t('error_messages.password_blank', :default => "can not be blank")) if send("protected_#{password_field}").blank? return false if errors.count > 0 unchecked_record = search_for_record(find_by_login_method, send(login_field)) if unchecked_record.blank? - errors.add(login_field, login_not_found_message) + errors.add(login_field, I18n.t('error_messages.login_not_found', :default => "does not exist")) return false end unless unchecked_record.send(verify_password_method, send("protected_#{password_field}")) - errors.add(password_field, password_invalid_message) + errors.add(password_field, I18n.t('error_messages.password_invalid', :default => "is not valid")) return false end @@ -433,12 +433,12 @@ def valid_credentials? unchecked_record = unauthorized_record if unchecked_record.blank? - errors.add_to_base("You can not login with a blank record.") + errors.add_to_base(I18n.t('error_messages.blank_record', :default => "You can not login with a blank record")) return false end if unchecked_record.new_record? - errors.add_to_base("You can not login with a new record.") + errors.add_to_base(I18n.t('error_messages.new_record', :default => "You can not login with a new record")) return false end @@ -452,7 +452,7 @@ def valid_record? return true if disable_magic_states? [:active, :approved, :confirmed].each do |required_status| if record.respond_to?("#{required_status}?") && !record.send("#{required_status}?") - errors.add_to_base(send("not_#{required_status}_message")) + errors.add_to_base(I18n.t("errors_messages.not_#{required_status}", :default => "Your account is not #{required_status}")) return false end end diff --git a/lib/authlogic/session/config.rb b/lib/authlogic/session/config.rb index 5c1ea44f..69f560ed 100644 --- a/lib/authlogic/session/config.rb +++ b/lib/authlogic/session/config.rb @@ -134,29 +134,13 @@ def last_request_at_threshold(value = nil) end alias_method :last_request_at_threshold=, :last_request_at_threshold - # The error message used when the login is left blank. - # - # * Default: "can not be blank" - # * Accepts: String - def login_blank_message(value = nil) - if value.nil? - read_inheritable_attribute(:login_blank_message) || login_blank_message("can not be blank") - else - write_inheritable_attribute(:login_blank_message, value) - end + def login_blank_message(value = nil) # :nodoc: + new_i18n_error end alias_method :login_blank_message=, :login_blank_message - # The error message used when the login could not be found in the database. - # - # * Default: "does not exist" - # * Accepts: String - def login_not_found_message(value = nil) - if value.nil? - read_inheritable_attribute(:login_not_found_message) || login_not_found_message("does not exist") - else - write_inheritable_attribute(:login_not_found_message, value) - end + def login_not_found_message(value = nil) # :nodoc: + new_i18n_error end alias_method :login_not_found_message=, :login_not_found_message @@ -196,42 +180,18 @@ def logout_on_timeout(value = nil) end alias_method :logout_on_timeout=, :logout_on_timeout - # The error message used when the record returns false to active? - # - # * Default: "Your account is not active" - # * Accepts: String - def not_active_message(value = nil) - if value.nil? - read_inheritable_attribute(:not_active_message) || not_active_message("Your account is not active") - else - write_inheritable_attribute(:not_active_message, value) - end + def not_active_message(value = nil) # :nodoc: + new_i18n_error end alias_method :not_active_message=, :not_active_message - # The error message used when the record returns false to approved? - # - # * Default: "Your account is not approved" - # * Accepts: String - def not_approved_message(value = nil) - if value.nil? - read_inheritable_attribute(:not_approved_message) || not_approved_message("Your account is not approved") - else - write_inheritable_attribute(:not_approved_message, value) - end + def not_approved_message(value = nil) # :nodoc: + new_i18n_error end alias_method :not_approved_message=, :not_approved_message - # The error message used when the record returns false to confirmed? - # - # * Default: "Your account is not confirmed" - # * Accepts: String - def not_confirmed_message(value = nil) - if value.nil? - read_inheritable_attribute(:not_confirmed_message) || not_confirmed_message("Your account is not confirmed") - else - write_inheritable_attribute(:not_confirmed_message, value) - end + def not_confirmed_message(value = nil) # :nodoc: + new_i18n_error end alias_method :not_confirmed_message=, :not_confirmed_message @@ -253,16 +213,8 @@ def params_key(value = nil) end alias_method :params_key=, :params_key - # The error message used when the password is left blank. - # - # * Default: "can not be blank" - # * Accepts: String - def password_blank_message(value = nil) - if value.nil? - read_inheritable_attribute(:password_blank_message) || password_blank_message("can not be blank") - else - write_inheritable_attribute(:password_blank_message, value) - end + def password_blank_message(value = nil) # :nodoc: + new_i18n_error end alias_method :password_blank_message=, :password_blank_message @@ -279,16 +231,8 @@ def password_field(value = nil) end alias_method :password_field=, :password_field - # The error message used when the password is invalid. - # - # * Default: "is invalid" - # * Accepts: String - def password_invalid_message(value = nil) - if value.nil? - read_inheritable_attribute(:password_invalid_message) || password_invalid_message("is invalid") - else - write_inheritable_attribute(:password_invalid_message, value) - end + def password_invalid_message(value = nil) # :nodoc: + new_i18n_error end alias_method :password_invalid_message=, :password_invalid_message @@ -357,6 +301,11 @@ def verify_password_method(value = nil) end end alias_method :verify_password_method=, :verify_password_method + + private + def new_i18n_error + raise NotImplementedError.new("As of v 1.4.0 Authlogic implements a new I18n solution that is much cleaner and easier. Please see Authlogic::I18n for more information on how to provide internationalization in Authlogic.") + end end module InstanceMethods # :nodoc: @@ -383,14 +332,6 @@ def find_with def last_request_at_threshold self.class.last_request_at_threshold end - - def login_blank_message - self.class.login_blank_message - end - - def login_not_found_message - self.class.login_not_found_message - end def login_field self.class.login_field @@ -400,18 +341,6 @@ def logout_on_timeout? self.class.logout_on_timeout == true end - def not_active_message - self.class.not_active_message - end - - def not_approved_message - self.class.not_approved_message - end - - def not_confirmed_message - self.class.not_confirmed_message - end - def params_allowed_request_types build_key(self.class.params_allowed_request_types) end @@ -419,19 +348,11 @@ def params_allowed_request_types def params_key build_key(self.class.params_key) end - - def password_blank_message - self.class.password_blank_message - end def password_field self.class.password_field end - def password_invalid_message - self.class.password_invalid_message - end - def perishable_token_field klass.acts_as_authentic_config[:perishable_token_field] end diff --git a/lib/authlogic/version.rb b/lib/authlogic/version.rb index 0f73d0b3..1df59f81 100644 --- a/lib/authlogic/version.rb +++ b/lib/authlogic/version.rb @@ -43,8 +43,8 @@ def to_a end MAJOR = 1 - MINOR = 3 - TINY = 9 + MINOR = 4 + TINY = 0 # The current version as a Version instance CURRENT = new(MAJOR, MINOR, TINY) diff --git a/test/session_tests/config_test.rb b/test/session_tests/config_test.rb index d22be152..57470848 100644 --- a/test/session_tests/config_test.rb +++ b/test/session_tests/config_test.rb @@ -76,30 +76,6 @@ def test_last_request_at_threshold session = UserSession.new assert_equal 0, session.last_request_at_threshold end - - def test_login_blank_message - UserSession.login_blank_message = "message" - assert_equal "message", UserSession.login_blank_message - session = UserSession.new - assert_equal "message", session.login_blank_message - - UserSession.login_blank_message "can not be blank" - assert_equal "can not be blank", UserSession.login_blank_message - session = UserSession.new - assert_equal "can not be blank", session.login_blank_message - end - - def test_login_not_found_message - UserSession.login_not_found_message = "message" - assert_equal "message", UserSession.login_not_found_message - session = UserSession.new - assert_equal "message", session.login_not_found_message - - UserSession.login_not_found_message "does not exist" - assert_equal "does not exist", UserSession.login_not_found_message - session = UserSession.new - assert_equal "does not exist", session.login_not_found_message - end def test_login_field UserSession.methods_configured = false @@ -116,42 +92,6 @@ def test_login_field assert session.respond_to?(:login) end - def test_not_active_message - UserSession.not_active_message = "message" - assert_equal "message", UserSession.not_active_message - session = UserSession.new - assert_equal "message", session.not_active_message - - UserSession.not_active_message "Your account is not active" - assert_equal "Your account is not active", UserSession.not_active_message - session = UserSession.new - assert_equal "Your account is not active", session.not_active_message - end - - def test_not_approved_message - UserSession.not_approved_message = "message" - assert_equal "message", UserSession.not_approved_message - session = UserSession.new - assert_equal "message", session.not_approved_message - - UserSession.not_approved_message "Your account is not approved" - assert_equal "Your account is not approved", UserSession.not_approved_message - session = UserSession.new - assert_equal "Your account is not approved", session.not_approved_message - end - - def test_not_confirmed_message - UserSession.not_confirmed_message = "message" - assert_equal "message", UserSession.not_confirmed_message - session = UserSession.new - assert_equal "message", session.not_confirmed_message - - UserSession.not_confirmed_message "Your account is not confirmed" - assert_equal "Your account is not confirmed", UserSession.not_confirmed_message - session = UserSession.new - assert_equal "Your account is not confirmed", session.not_confirmed_message - end - def test_params_key UserSession.params_key = "my_params_key" assert_equal "my_params_key", UserSession.params_key @@ -163,18 +103,6 @@ def test_params_key session = UserSession.new assert_equal "user_credentials", session.params_key end - - def test_password_blank_message - UserSession.password_blank_message = "message" - assert_equal "message", UserSession.password_blank_message - session = UserSession.new - assert_equal "message", session.password_blank_message - - UserSession.password_blank_message "can not be blank" - assert_equal "can not be blank", UserSession.password_blank_message - session = UserSession.new - assert_equal "can not be blank", session.password_blank_message - end def test_password_field UserSession.methods_configured = false @@ -190,18 +118,6 @@ def test_password_field assert_equal :password, session.password_field assert session.respond_to?(:password) end - - def test_password_invalid_message - UserSession.password_invalid_message = "message" - assert_equal "message", UserSession.password_invalid_message - session = UserSession.new - assert_equal "message", session.password_invalid_message - - UserSession.password_invalid_message "is invalid" - assert_equal "is invalid", UserSession.password_invalid_message - session = UserSession.new - assert_equal "is invalid", session.password_invalid_message - end def test_remember_me UserSession.remember_me = true