Commit 877d26aed512bae12a51b557166664e3e827b1a6

Authored by Daniela Feitosa
1 parent b18cb4fe

Environment admin can configure the message after signup

Added field sigup_welcome_text on environment
New tab on admin panel to edit the text
When the environment doesn't have a text, will use the default text
Added content_type to email

(ActionItem2497)
app/models/environment.rb
... ... @@ -124,6 +124,7 @@ class Environment < ActiveRecord::Base
124 124 'show_zoom_button_on_article_images' => _('Show a zoom link on all article images'),
125 125 'captcha_for_logged_users' => _('Ask captcha when a logged user comments too'),
126 126 'skip_new_user_email_confirmation' => _('Skip e-mail confirmation for new users'),
  127 + 'send_welcome_email_to_new_users' => _('Send welcome e-mail to new users'),
127 128 'allow_change_of_redirection_after_login' => _('Allow users to set the page to redirect after login')
128 129 }
129 130 end
... ... @@ -542,6 +543,31 @@ class Environment < ActiveRecord::Base
542 543 signup_fields
543 544 end
544 545  
  546 + serialize :signup_welcome_text, Hash
  547 + def signup_welcome_text
  548 + self[:signup_welcome_text] ||= {}
  549 + end
  550 +
  551 + def signup_welcome_text_subject
  552 + self.signup_welcome_text[:subject]
  553 + end
  554 +
  555 + def signup_welcome_text_subject=(subject)
  556 + self.signup_welcome_text[:subject] = subject
  557 + end
  558 +
  559 + def signup_welcome_text_body
  560 + self.signup_welcome_text[:body]
  561 + end
  562 +
  563 + def signup_welcome_text_body=(body)
  564 + self.signup_welcome_text[:body] = body
  565 + end
  566 +
  567 + def has_signup_welcome_text?
  568 + signup_welcome_text && !signup_welcome_text_body.blank?
  569 + end
  570 +
545 571 # #################################################
546 572 # Validations
547 573 # #################################################
... ...
app/models/user.rb
... ... @@ -73,6 +73,18 @@ class User < ActiveRecord::Base
73 73 :environment => user.environment.name,
74 74 :url => user.environment.top_url
75 75 end
  76 +
  77 + def signup_welcome_email(user)
  78 + email_body = user.environment.signup_welcome_text_body.gsub('{user_name}', user.name)
  79 + email_subject = user.environment.signup_welcome_text_subject
  80 +
  81 + content_type 'text/html'
  82 + recipients user.email
  83 +
  84 + from "#{user.environment.name} <#{user.environment.contact_email}>"
  85 + subject email_subject.blank? ? _("Welcome to environment %s") % [user.environment.name] : email_subject
  86 + body email_body
  87 + end
76 88 end
77 89  
78 90 def signup!
... ... @@ -117,7 +129,17 @@ class User &lt; ActiveRecord::Base
117 129 self.activated_at = Time.now.utc
118 130 self.activation_code = nil
119 131 self.person.visible = true
120   - self.person.save! && self.save!
  132 + begin
  133 + self.person.save! && self.save!
  134 + rescue Exception => exception
  135 + logger.error(exception.to_s)
  136 + false
  137 + else
  138 + if environment.enabled?('send_welcome_email_to_new_users') && environment.has_signup_welcome_text?
  139 + User::Mailer.delay.deliver_signup_welcome_email(self)
  140 + end
  141 + true
  142 + end
121 143 end
122 144  
123 145 def activated?
... ...
app/views/admin_panel/_signup_welcome_text.rhtml 0 → 100644
... ... @@ -0,0 +1,7 @@
  1 +<div class='description'>
  2 + <%= _('This text will be sent to new users if the feature "Send welcome e-mail to new users" is enabled on environment.') %><br/><br/>
  3 + <%= _('Including %s on body, it will be replaced by the real name of the e-mail recipient.') % content_tag('code', '{user_name}') %>
  4 +</div>
  5 +
  6 +<%= labelled_form_field(_('Subject'), text_field(:environment, :signup_welcome_text_subject, :style => 'width:100%')) %>
  7 +<%= labelled_form_field(_('Body'), text_area(:environment, :signup_welcome_text_body, :cols => 40, :style => 'width: 100%', :class => 'mceEditor')) %>
... ...
app/views/admin_panel/site_info.rhtml
... ... @@ -10,6 +10,8 @@
10 10 :content => (render :partial => 'site_info', :locals => {:f => f})} %>
11 11 <% tabs << {:title => _('Terms of use'), :id => 'terms-of-use',
12 12 :content => (render :partial => 'terms_of_use', :locals => {:f => f})} %>
  13 + <% tabs << {:title => _('Signup welcome text'), :id => 'signup-welcome-text',
  14 + :content => (render :partial => 'signup_welcome_text', :locals => {:f => f})} %>
13 15 <%= render_tabs(tabs) %>
14 16 <% button_bar do %>
15 17 <%= submit_button(:save, _('Save'), :cancel => {:action => 'index'}) %>
... ...
app/views/user/mailer/signup_welcome_email.rhtml 0 → 100644
... ... @@ -0,0 +1,9 @@
  1 +<!DOCTYPE html>
  2 +<html>
  3 + <head>
  4 + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  5 + </head>
  6 + <body>
  7 + <p><%= word_wrap @body %></p>
  8 + </body>
  9 +</html>
... ...
db/migrate/20121008185303_add_signup_welcome_text.rb 0 → 100644
... ... @@ -0,0 +1,9 @@
  1 +class AddSignupWelcomeText < ActiveRecord::Migration
  2 + def self.up
  3 + add_column :environments, :signup_welcome_text, :text
  4 + end
  5 +
  6 + def self.down
  7 + remove_column :environments, :signup_welcome_text
  8 + end
  9 +end
... ...
db/schema.rb
... ... @@ -9,7 +9,7 @@
9 9 #
10 10 # It's strongly recommended to check this file into your version control system.
11 11  
12   -ActiveRecord::Schema.define(:version => 20120825185219) do
  12 +ActiveRecord::Schema.define(:version => 20121008185303) do
13 13  
14 14 create_table "abuse_reports", :force => true do |t|
15 15 t.integer "reporter_id"
... ... @@ -262,6 +262,7 @@ ActiveRecord::Schema.define(:version =&gt; 20120825185219) do
262 262 t.datetime "updated_at"
263 263 t.integer "reports_lower_bound", :default => 0, :null => false
264 264 t.string "redirection_after_login", :default => "keep_on_same_page"
  265 + t.text "signup_welcome_text"
265 266 end
266 267  
267 268 create_table "external_feeds", :force => true do |t|
... ...
public/stylesheets/application.css
... ... @@ -2436,6 +2436,13 @@ div#activation_enterprise label, div#activation_enterprise input, div#activation
2436 2436 .controller-admin_panel #portal-folders select {
2437 2437 width: 50%;
2438 2438 }
  2439 +
  2440 +.description {
  2441 + background-color: #E6E6E6;
  2442 + border: 1px solid #CCC;
  2443 + padding: 5px;
  2444 +}
  2445 +
2439 2446 /* ==> public/stylesheets/controller_catalog.css <== */
2440 2447 /* ==> @import url('products.css'); <== */
2441 2448 /* * * Products catalog * * * * * * * * * * * * */
... ...
test/functional/admin_panel_controller_test.rb
... ... @@ -71,6 +71,8 @@ class AdminPanelControllerTest &lt; ActionController::TestCase
71 71 assert_template 'site_info'
72 72 assert_tag :tag => 'textarea', :attributes => { :name => 'environment[description]'}
73 73 assert_tag :tag => 'textarea', :attributes => { :name => 'environment[terms_of_use]'}
  74 + assert_tag :tag => 'input', :attributes => { :name => 'environment[signup_welcome_text_subject]'}
  75 + assert_tag :tag => 'textarea', :attributes => { :name => 'environment[signup_welcome_text_body]'}
74 76 end
75 77  
76 78 should 'display form for editing message for disabled enterprise' do
... ... @@ -110,6 +112,25 @@ class AdminPanelControllerTest &lt; ActionController::TestCase
110 112 assert !Environment.default.has_terms_of_use?
111 113 end
112 114  
  115 + should 'save subject and body of signup welcome text' do
  116 + subject = "This is my welcome subject"
  117 + body = "This is my welcome body"
  118 + post :site_info, :environment => { :signup_welcome_text_subject => subject, :signup_welcome_text_body => body }
  119 + assert_redirected_to :action => 'index'
  120 +
  121 + assert_equal subject, Environment.default.signup_welcome_text[:subject]
  122 + assert_equal body, Environment.default.signup_welcome_text[:body]
  123 + assert !Environment.default.signup_welcome_text.blank?
  124 + end
  125 +
  126 + should 'not save empty string as signup welcome text' do
  127 + content = ""
  128 + post :site_info, :environment => { :signup_welcome_text_body => content }
  129 + assert_redirected_to :action => 'index'
  130 +
  131 + assert !Environment.default.has_signup_welcome_text?
  132 + end
  133 +
113 134 should 'sanitize message for disabled enterprise with white_list' do
114 135 post :site_info, :environment => { :message_for_disabled_enterprise => "This <strong>is</strong> <script>alert('alow')</script>my new environment" }
115 136 assert_redirected_to :action => 'index'
... ...
test/unit/environment_test.rb
... ... @@ -1242,4 +1242,82 @@ class EnvironmentTest &lt; ActiveSupport::TestCase
1242 1242 end
1243 1243 end
1244 1244  
  1245 + should 'respond to signup_welcome_text' do
  1246 + assert_respond_to Environment.new, :signup_welcome_text
  1247 + end
  1248 +
  1249 + should 'store welcome text in a hash serialized' do
  1250 + environment = Environment.default
  1251 +
  1252 + environment.signup_welcome_text = {
  1253 + :subject => 'Welcome to the environment',
  1254 + :body => 'Thanks for signing up!',
  1255 + }
  1256 + environment.save
  1257 + environment.reload
  1258 +
  1259 + assert_kind_of Hash, environment.signup_welcome_text
  1260 + assert_equal ['Welcome to the environment', 'Thanks for signing up!'], [environment.signup_welcome_text[:subject], environment.signup_welcome_text[:body]]
  1261 + end
  1262 +
  1263 + should 'not consider signup welcome text if not defined' do
  1264 + env = Environment.default
  1265 + assert !env.has_signup_welcome_text?
  1266 + end
  1267 +
  1268 + should 'not consider signup welcome text if nil' do
  1269 + env = Environment.default
  1270 +
  1271 + env.signup_welcome_text = nil
  1272 + assert !env.has_signup_welcome_text?
  1273 + end
  1274 +
  1275 + should 'not consider signup welcome text if body is nil' do
  1276 + env = Environment.default
  1277 +
  1278 + env.signup_welcome_text = {
  1279 + :subject => 'Welcome to the environment',
  1280 + }
  1281 + assert !env.has_signup_welcome_text?
  1282 + end
  1283 +
  1284 + should 'consider signup welcome text if subject is nil but body is defined' do
  1285 + env = Environment.default
  1286 +
  1287 + env.signup_welcome_text = {
  1288 + :body => 'Thanks for signing up!',
  1289 + }
  1290 + assert env.has_signup_welcome_text?
  1291 + end
  1292 +
  1293 + should 'consider signup welcome text if subject and body are defined' do
  1294 + env = Environment.default
  1295 +
  1296 + env.signup_welcome_text = {
  1297 + :subject => 'Welcome to the environment',
  1298 + :body => 'Thanks for signing up!',
  1299 + }
  1300 + assert env.has_signup_welcome_text?
  1301 + end
  1302 +
  1303 + should 'store welcome text subject' do
  1304 + environment = Environment.default
  1305 +
  1306 + environment.signup_welcome_text_subject = 'Welcome to the environment'
  1307 + environment.save
  1308 + environment.reload
  1309 +
  1310 + assert_equal environment.signup_welcome_text[:subject], environment.signup_welcome_text_subject
  1311 + end
  1312 +
  1313 + should 'store welcome text body' do
  1314 + environment = Environment.default
  1315 +
  1316 + environment.signup_welcome_text_body = 'Thanks for signing up!'
  1317 + environment.save
  1318 + environment.reload
  1319 +
  1320 + assert_equal environment.signup_welcome_text[:body], environment.signup_welcome_text_body
  1321 + end
  1322 +
1245 1323 end
... ...
test/unit/user_test.rb
... ... @@ -522,6 +522,101 @@ class UserTest &lt; ActiveSupport::TestCase
522 522 assert user.save!
523 523 end
524 524  
  525 + should 'not deliver welcome e-mail after user activation if not enabled on environment' do
  526 + env = Environment.default
  527 + env.signup_welcome_text = {
  528 + :subject => 'Welcome to the environment',
  529 + :body => 'Thanks for signing up!'
  530 + }
  531 + env.disable('send_welcome_email_to_new_users')
  532 + env.save
  533 +
  534 + user = new_user :email => 'pending@activation.com'
  535 + assert_no_difference(ActionMailer::Base.deliveries, :size) do
  536 + user.activate
  537 + end
  538 + end
  539 +
  540 + should 'deliver welcome e-mail after user activation if enabled on environment' do
  541 + env = Environment.default
  542 + env.signup_welcome_text = {
  543 + :subject => 'Welcome to this environment',
  544 + :body => 'Thanks for signing up!'
  545 + }
  546 + env.enable('send_welcome_email_to_new_users')
  547 + env.save
  548 +
  549 + user = new_user :email => 'pending@activation.com'
  550 + assert_difference(ActionMailer::Base.deliveries, :size, 1) do
  551 + user.activate
  552 + end
  553 +
  554 + sent = ActionMailer::Base.deliveries.last
  555 + assert_equal ['pending@activation.com'], sent.to
  556 + assert_equal 'Welcome to this environment', sent.subject
  557 + assert_equal 'Thanks for signing up!', sent.body
  558 + end
  559 +
  560 + should 'deliver welcome e-mail after user activation if enabled on environment with default subject if not defined' do
  561 + env = Environment.default
  562 + env.signup_welcome_text = {
  563 + :body => 'Thanks for signing up!'
  564 + }
  565 + env.enable('send_welcome_email_to_new_users')
  566 + env.save
  567 +
  568 + user = new_user :email => 'pending@activation.com'
  569 + assert_difference(ActionMailer::Base.deliveries, :size, 1) do
  570 + user.activate
  571 + end
  572 +
  573 + sent = ActionMailer::Base.deliveries.last
  574 + assert_equal "Welcome to environment #{env.name}", sent.subject
  575 + end
  576 +
  577 + should 'deliver welcome e-mail after user activation if enabled on environment and replace user_name' do
  578 + env = Environment.default
  579 + env.signup_welcome_text = {
  580 + :subject => 'Welcome to the environment',
  581 + :body => 'Thanks for signing up, {user_name}!',
  582 + }
  583 + env.enable('send_welcome_email_to_new_users')
  584 + env.save
  585 +
  586 + user = new_user :name => 'John Doe', :email => 'pending@activation.com'
  587 + assert_difference(ActionMailer::Base.deliveries, :size, 1) do
  588 + user.activate
  589 + end
  590 +
  591 + sent = ActionMailer::Base.deliveries.last
  592 + assert_equal "Thanks for signing up, #{user.name}!", sent.body
  593 + end
  594 +
  595 + should 'not deliver welcome e-mail after user activation if enabled on environment but body not filled in' do
  596 + env = Environment.default
  597 + env.signup_welcome_text = {
  598 + :subject => 'Welcome to the environment',
  599 + }
  600 + env.enable('send_welcome_email_to_new_users')
  601 + env.save
  602 +
  603 + user = new_user :email => 'pending@activation.com'
  604 + assert_no_difference(ActionMailer::Base.deliveries, :size) do
  605 + user.activate
  606 + end
  607 + end
  608 +
  609 + should 'not deliver welcome e-mail after user activation if enabled on environment but welcome text not defined' do
  610 + env = Environment.default
  611 + env.enable('send_welcome_email_to_new_users')
  612 + env.save
  613 +
  614 + user = new_user :email => 'pending@activation.com'
  615 + assert_no_difference(ActionMailer::Base.deliveries, :size) do
  616 + user.activate
  617 + end
  618 + end
  619 +
525 620 protected
526 621 def new_user(options = {})
527 622 user = User.new({ :login => 'quire', :email => 'quire@example.com', :password => 'quire', :password_confirmation => 'quire' }.merge(options))
... ...