Skip to content

Commit 8e9e314

Browse files
committed
Added language attribute to user
1 parent 74925af commit 8e9e314

File tree

5 files changed

+26
-1
lines changed

5 files changed

+26
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
class ApplicationController < ActionController::Base
22
protect_from_forgery
3+
before_filter :set_language
4+
35
def can_access?
46
current_user.id == @note.user_id
57
end
68

9+
10+
private
11+
def set_language
12+
if !current_user.nil?
13+
I18n.locale = current_user.language || I18n.default_locale
14+
end
15+
end
716
end
817

app/models/user.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ class User < ActiveRecord::Base
66
:recoverable, :rememberable, :trackable, :validatable
77

88
# Setup accessible (or protected) attributes for your model
9-
attr_accessible :email, :password, :password_confirmation, :remember_me, :username
9+
attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :language
1010

1111
validates_presence_of :username
1212
validates_uniqueness_of :username, :case_sensitive => false
1313
validates_format_of :username, :with => /^[A-Za-z\d_]+$/
1414

15+
def self.languages
16+
{'pt-BR' => 'Portugues', 'en' => 'English'}
17+
end
1518
end
1619

app/views/devise/registrations/edit.html.haml

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
= I18n.t('devise.registrations.hint_current_password')
4141
%br/
4242
= f.password_field :current_password
43+
%p
44+
= f.label :language
45+
%br= f.select :language, User.languages.invert
4346
%p= f.submit I18n.t('update')
4447
= link_to I18n.t('back'), root_path
4548

config/locales/pt-BR.yml

+1
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ pt-BR:
212212
password: "Senha"
213213
remember_me: "lembrar login?"
214214
username: "Usuário"
215+
language: "Linguagem"
215216
errors:
216217
template:
217218
header:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class AddLanguageToUsers < ActiveRecord::Migration
2+
def self.up
3+
add_column :users, :language, :string
4+
end
5+
6+
def self.down
7+
remove_column :users, :language
8+
end
9+
end

0 commit comments

Comments
 (0)