[refs #105] Add language check on user save

This commit is contained in:
konstantin.grabar 2012-01-17 15:57:46 +04:00
parent 726eddc40e
commit d205eb2dd9
4 changed files with 10 additions and 8 deletions

View File

@ -18,17 +18,17 @@ class ApplicationController < ActionController::Base
protected
def set_locale
I18n.locale = extract_locale_from_request
end
def extract_locale_from_request
get_user_locale || request.env['HTTP_ACCEPT_LANGUAGE'].to_sym || :en
I18n.locale = check_locale( get_user_locale || request.env['HTTP_ACCEPT_LANGUAGE'].to_sym )
end
def get_user_locale
user_signed_in? ? current_user.language : nil
end
def check_locale(locale)
User::LANGUAGES.include?(locale.to_s) ? locale : :en
end
def get_owner
# params['user_id'] && User.find_by_id(params['user_id']) ||
# params['group_id'] && Group.find_by_id(params['group_id']) || current_user

View File

@ -1,6 +1,7 @@
class User < ActiveRecord::Base
ROLES = ['admin']
LANGUAGES = [['Russian', 'ru'], ['English', 'en']]
LANGUAGES = ['ru', 'en']
LANGUAGES_FOR_SELECT = [['Russian', 'ru'], ['English', 'en']]
devise :database_authenticatable, :registerable, :omniauthable, # :token_authenticatable, :encryptable, :timeoutable
:recoverable, :rememberable, :validatable #, :trackable, :confirmable, :lockable
@ -29,6 +30,7 @@ class User < ActiveRecord::Base
validate { errors.add(:uname, :taken) if Group.where('uname LIKE ?', uname).present? }
validates :ssh_key, :uniqueness => true, :allow_blank => true
validates :role, :inclusion => {:in => ROLES}, :allow_blank => true
validates :language, :inclusion => {:in => LANGUAGES}, :allow_blank => true
attr_accessible :email, :password, :password_confirmation, :remember_me, :login, :name, :ssh_key, :uname, :language
attr_readonly :uname

View File

@ -43,7 +43,7 @@
.left
= f.label :language, :class => "label"
.right
= f.select :language, User::LANGUAGES
= f.select :language, User::LANGUAGES_FOR_SELECT
/ .group.wat-cf
/ .left

View File

@ -41,7 +41,7 @@
.left
= f.label :language, :class => "label"
.right
= f.select :language, User::LANGUAGES
= f.select :language, User::LANGUAGES_FOR_SELECT
.group.navform.wat-cf
%button.button{ :type => "submit" }