[Git][noosfero/noosfero][master-1.x] 2 commits: Uses environment locale on notification jobs

Rodrigo Souto gitlab at mg.gitlab.com
Thu May 10 15:10:59 BRT 2018


Rodrigo Souto pushed to branch master-1.x at Noosfero / noosfero


Commits:
be86e56d by Gabriel Silva at 2018-05-04T14:57:37Z
Uses environment locale on notification jobs

Signed-off-by: Gabriel Silva <gabriel93.silva at gmail.com>

- - - - -
a97a557e by Rodrigo Souto at 2018-05-10T18:10:54Z
Merge branch 'notification-locale' into 'master-1.x'

Fixes locale on notification messages

See merge request noosfero/noosfero!1459
- - - - -


4 changed files:

- app/mailers/user_mailer.rb
- app/models/concerns/notifiable.rb
- app/views/user_mailer/activation_code.text.erb
- test/unit/notifiable_test.rb


Changes:

=====================================
app/mailers/user_mailer.rb
=====================================
--- a/app/mailers/user_mailer.rb
+++ b/app/mailers/user_mailer.rb
@@ -22,17 +22,17 @@ class UserMailer < ApplicationMailer
     self.environment = user.environment
 
     @recipient = user.name
-    @activation_token = user.activation_code
     @short_activation_code = user.short_activation_code.try(:upcase)
     @url = user.environment.top_url
-    @redirection = (true if user.return_to)
-    @join = (user.community_to_join if user.community_to_join)
+    @activation_url = url_for(controller: :account, action: :activate,
+                              activation_token: user.activation_code,
+                              redirection: (true if user.return_to),
+                              join: user.community_to_join)
 
     mail_with_template(
       from: "#{user.environment.name} <#{user.environment.noreply_email}>",
       to: user.email,
       subject: _("[%s] Activate your account").html_safe % [user.environment.name],
-      template_params: {:environment => user.environment, :activation_code => @activation_code, :redirection => @redirection, :join => @join, :person => user.person, :url => @url},
       email_template: user.environment.email_templates.find_by_template_type(:user_activation),
     )
   end


=====================================
app/models/concerns/notifiable.rb
=====================================
--- a/app/models/concerns/notifiable.rb
+++ b/app/models/concerns/notifiable.rb
@@ -10,11 +10,23 @@ module Notifiable
       unless respond_to?("#{verb}_settings", true)
         raise UnregisteredVerb, "Notification verb not registered"
       end
-      notify_by_mail(verb, *args)
-      notify_by_push(verb, *args)
-      notify_by_plugins(verb, *args)
+      notify_later(verb, *args)
     end
 
+    def notify_later(verb, *args)
+      env = environment if respond_to?(:environment)
+      env ||= Environment.default
+      locale = env.default_language || 'en'
+
+      Noosfero.with_locale(locale) do
+        notify_by_mail(verb, *args)
+        notify_by_push(verb, *args)
+        notify_by_plugins(verb, *args)
+      end
+    end
+
+    handle_asynchronously :notify_later
+
     private
 
     def notify_by_mail(verb, *args)
@@ -46,10 +58,6 @@ module Notifiable
     def notify_by_plugins(verb, *args)
       plugins.dispatch(:custom_notification, verb, *args)
     end
-
-    handle_asynchronously :notify_by_mail
-    handle_asynchronously :notify_by_push
-
   end
 
   class_methods do
@@ -75,5 +83,4 @@ module Notifiable
       end
     end
   end
-
 end


=====================================
app/views/user_mailer/activation_code.text.erb
=====================================
--- a/app/views/user_mailer/activation_code.text.erb
+++ b/app/views/user_mailer/activation_code.text.erb
@@ -1,10 +1,9 @@
-<%= _('Hi, %{recipient}!') % { :recipient => @recipient } %>
+<%= _('Hi, %{recipient}!') % { recipient: @recipient } %>
 
-<%= _('Welcome to %{environment}!') %>
+<%= _('Welcome to %{environment}!') % { environment: @environment.name } %>
 <%= word_wrap(_('To activate your account you must enter your activation '\
                 'code. To do so, access your account or follow the link: '\
-                '%{activation_url}') % { environment: @environment.name,
-                                         activation_url: url_for(controller: :account, action: :activate, activation_token: @activation_token, redirection: @redirection, join: @join) }) %>
+                '%{activation_url}') % { activation_url: @activation_url }) %>
 
 <%= _('Your activation code is: %s') % @short_activation_code %>
 


=====================================
test/unit/notifiable_test.rb
=====================================
--- a/test/unit/notifiable_test.rb
+++ b/test/unit/notifiable_test.rb
@@ -48,6 +48,23 @@ class NotifiableTest < ActiveSupport::TestCase
     Foo.any_instance.expects(:notify_by_push).once
     Foo.any_instance.expects(:notify_by_plugins).once
     Foo.new.notify(:new_things)
+    process_delayed_job_queue
+  end
+
+  should 'use environment default locale' do
+    Environment.any_instance.expects(:default_language).returns('pt')
+    Noosfero.expects(:with_locale).with('pt')
+    Foo.will_notify :new_things
+    Foo.new.notify(:new_things)
+    process_delayed_job_queue
+  end
+
+  should 'use english locale if there is no default' do
+    Environment.any_instance.expects(:default_language).returns(nil)
+    Noosfero.expects(:with_locale).with('en')
+    Foo.will_notify :new_things
+    Foo.new.notify(:new_things)
+    process_delayed_job_queue
   end
 
   should 'deliver message when notifying by mail and mailer is defined' do
@@ -68,40 +85,42 @@ class NotifiableTest < ActiveSupport::TestCase
                     .returns({ recipients: [mock], title: 't', body: 'm' })
     WebPush.expects(:notify_users).once
     Foo.new.send(:notify_by_push, :new_things)
-    process_delayed_job_queue
   end
 
   should 'not push notification when push is enabled but data not defined' do
     Foo.will_notify :new_things, push: true
     WebPush.expects(:notify_users).never
     Foo.new.send(:notify_by_push, :new_things)
-    process_delayed_job_queue
   end
 
   should 'not push notification if recipients are invalid' do
     Foo.will_notify :new_things, push: true
     Foo.any_instance.stubs(:new_things_notification)
                     .returns({ title: 't', body: 'm' })
-    Foo.new.send(:notify_by_push, :new_things)
+
     WebPush.expects(:notify_users).never
-    process_delayed_job_queue
+    assert_raise Notifiable::InvalidPushRecipients do
+      Foo.new.send(:notify_by_push, :new_things)
+    end
   end
 
   should 'not push notification if recipients are empty' do
     Foo.will_notify :new_things, push: true
     Foo.any_instance.stubs(:new_things_notification)
                     .returns({ title: 't', body: 'm', recipients: [] })
-    Foo.new.send(:notify_by_push, :new_things)
     WebPush.expects(:notify_users).never
-    process_delayed_job_queue
+    assert_raise Notifiable::InvalidPushRecipients do
+      Foo.new.send(:notify_by_push, :new_things)
+    end
   end
 
   should 'not push notification if data is incomplete' do
     Foo.will_notify :new_things, push: true
     Foo.any_instance.stubs(:new_things_notification)
                     .returns({ recipients: [mock], title: nil })
-    Foo.new.send(:notify_by_push, :new_things)
     WebPush.expects(:notify_users).never
-    process_delayed_job_queue
+    assert_raise Notifiable::InvalidPushMessage do
+      Foo.new.send(:notify_by_push, :new_things)
+    end
   end
 end



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/35f6faa217bc0d0df9ad410882db62269450965c...a97a557e5e7880244d154f982241c16ff2f72150

-- 
View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/35f6faa217bc0d0df9ad410882db62269450965c...a97a557e5e7880244d154f982241c16ff2f72150
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20180510/85ef2ed6/attachment-0001.html>


More information about the Noosfero-dev mailing list