Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Devise translations when inheriting from core controllers #3407

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* `RegistrationsController#new` and `SessionsController#new` now yields the
current resource (by @mtarnovan, @deivid-rodriguez)
* Password length validation is now limited to 72 characters for newer apps (by @lleger)
* Controllers inheriting from any Devise core controller will now use appropriate translations.
The i18n scope can be overridden in `translation_scope`.

### 3.4.1 - 2014-10-29

Expand Down
4 changes: 4 additions & 0 deletions app/controllers/devise/confirmations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@ def after_confirmation_path_for(resource_name, resource)
new_session_path(resource_name)
end
end

def translation_scope
'devise.confirmations'
end
end
4 changes: 4 additions & 0 deletions app/controllers/devise/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ def failure_message
def after_omniauth_failure_path_for(scope)
new_session_path(scope)
end

def translation_scope
'devise.omniauth_callbacks'
end
end
4 changes: 4 additions & 0 deletions app/controllers/devise/passwords_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,8 @@ def unlockable?(resource)
resource.respond_to?(:unlock_strategy_enabled?) &&
resource.unlock_strategy_enabled?(:email)
end

def translation_scope
'devise.passwords'
end
end
4 changes: 4 additions & 0 deletions app/controllers/devise/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,8 @@ def sign_up_params
def account_update_params
devise_parameter_sanitizer.sanitize(:account_update)
end

def translation_scope
'devise.registrations'
end
end
4 changes: 4 additions & 0 deletions app/controllers/devise/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ def auth_options
{ scope: resource_name, recall: "#{controller_path}#new" }
end

def translation_scope
'devise.sessions'
end

private

# Check if there is no signed in user before doing the sign out.
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/devise/unlocks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@ def after_unlock_path_for(resource)
new_session_path(resource) if is_navigational_format?
end

def translation_scope
'devise.unlocks'
end
end
9 changes: 8 additions & 1 deletion app/controllers/devise_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,20 @@ def devise_i18n_options(options)

# Get message for given
def find_message(kind, options = {})
options[:scope] ||= "devise.#{controller_name}"
options[:scope] ||= translation_scope
options[:default] = Array(options[:default]).unshift(kind.to_sym)
options[:resource_name] = resource_name
options = devise_i18n_options(options)
I18n.t("#{options[:resource_name]}.#{kind}", options)
end

# Controllers inheriting DeviseController are advised to override this
# method so that other controllers inheriting from them would use
# existing translations.
def translation_scope
"devise.#{controller_name}"
end

def clean_up_passwords(object)
object.clean_up_passwords if object.respond_to?(:clean_up_passwords)
end
Expand Down
51 changes: 51 additions & 0 deletions test/controllers/inherited_controller_i18n_messages_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require 'test_helper'

class SessionsInheritedController < Devise::SessionsController
def test_i18n_scope
set_flash_message(:notice, :signed_in)
end
end

class AnotherInheritedController < SessionsInheritedController
protected

def translation_scope
'another'
end
end

class InheritedControllerTest < ActionController::TestCase
tests SessionsInheritedController

def setup
@mock_warden = OpenStruct.new
@controller.request.env['warden'] = @mock_warden
@controller.request.env['devise.mapping'] = Devise.mappings[:user]
end

test 'I18n scope is inherited from Devise::Sessions' do
I18n.expects(:t).with do |message, options|
message == 'user.signed_in' &&
options[:scope] == 'devise.sessions'
end
@controller.test_i18n_scope
end
end

class AnotherInheritedControllerTest < ActionController::TestCase
tests AnotherInheritedController

def setup
@mock_warden = OpenStruct.new
@controller.request.env['warden'] = @mock_warden
@controller.request.env['devise.mapping'] = Devise.mappings[:user]
end

test 'I18n scope is overridden' do
I18n.expects(:t).with do |message, options|
message == 'user.signed_in' &&
options[:scope] == 'another'
end
@controller.test_i18n_scope
end
end