Permalink
Browse files

Released v1.4.0. A new I18n soluton has been added. Please see Authlo…

…gic::I18n for more info. The old solution via configuration is no longer supported.
  • Loading branch information...
1 parent b5dd374 commit 7717a1b8b3ed297199cf46a5574398d188eec5a4 @binarylogic binarylogic committed Jan 29, 2009
View
3 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
View
4 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:
View
1 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
View
1 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)
View
52 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
View
14 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
View
117 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.
- #
- # * <tt>Default:</tt> "can not be blank"
- # * <tt>Accepts:</tt> 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.
- #
- # * <tt>Default:</tt> "does not exist"
- # * <tt>Accepts:</tt> 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?
- #
- # * <tt>Default:</tt> "Your account is not active"
- # * <tt>Accepts:</tt> 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?
- #
- # * <tt>Default:</tt> "Your account is not approved"
- # * <tt>Accepts:</tt> 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?
- #
- # * <tt>Default:</tt> "Your account is not confirmed"
- # * <tt>Accepts:</tt> 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.
- #
- # * <tt>Default:</tt> "can not be blank"
- # * <tt>Accepts:</tt> 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.
- #
- # * <tt>Default:</tt> "is invalid"
- # * <tt>Accepts:</tt> 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,38 +341,18 @@ 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
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
View
4 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)
View
84 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

0 comments on commit 7717a1b

Please sign in to comment.