From 877d26aed512bae12a51b557166664e3e827b1a6 Mon Sep 17 00:00:00 2001 From: Daniela Soares Feitosa Date: Mon, 29 Oct 2012 02:05:52 -0200 Subject: [PATCH] Environment admin can configure the message after signup --- app/models/environment.rb | 26 ++++++++++++++++++++++++++ app/models/user.rb | 24 +++++++++++++++++++++++- app/views/admin_panel/_signup_welcome_text.rhtml | 7 +++++++ app/views/admin_panel/site_info.rhtml | 2 ++ app/views/user/mailer/signup_welcome_email.rhtml | 9 +++++++++ db/migrate/20121008185303_add_signup_welcome_text.rb | 9 +++++++++ db/schema.rb | 3 ++- public/stylesheets/application.css | 7 +++++++ test/functional/admin_panel_controller_test.rb | 21 +++++++++++++++++++++ test/unit/environment_test.rb | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/unit/user_test.rb | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 11 files changed, 279 insertions(+), 2 deletions(-) create mode 100644 app/views/admin_panel/_signup_welcome_text.rhtml create mode 100644 app/views/user/mailer/signup_welcome_email.rhtml create mode 100644 db/migrate/20121008185303_add_signup_welcome_text.rb diff --git a/app/models/environment.rb b/app/models/environment.rb index b02db16..0908939 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -124,6 +124,7 @@ class Environment < ActiveRecord::Base 'show_zoom_button_on_article_images' => _('Show a zoom link on all article images'), 'captcha_for_logged_users' => _('Ask captcha when a logged user comments too'), 'skip_new_user_email_confirmation' => _('Skip e-mail confirmation for new users'), + 'send_welcome_email_to_new_users' => _('Send welcome e-mail to new users'), 'allow_change_of_redirection_after_login' => _('Allow users to set the page to redirect after login') } end @@ -542,6 +543,31 @@ class Environment < ActiveRecord::Base signup_fields end + serialize :signup_welcome_text, Hash + def signup_welcome_text + self[:signup_welcome_text] ||= {} + end + + def signup_welcome_text_subject + self.signup_welcome_text[:subject] + end + + def signup_welcome_text_subject=(subject) + self.signup_welcome_text[:subject] = subject + end + + def signup_welcome_text_body + self.signup_welcome_text[:body] + end + + def signup_welcome_text_body=(body) + self.signup_welcome_text[:body] = body + end + + def has_signup_welcome_text? + signup_welcome_text && !signup_welcome_text_body.blank? + end + # ################################################# # Validations # ################################################# diff --git a/app/models/user.rb b/app/models/user.rb index da9b5f0..d387dbb 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -73,6 +73,18 @@ class User < ActiveRecord::Base :environment => user.environment.name, :url => user.environment.top_url end + + def signup_welcome_email(user) + email_body = user.environment.signup_welcome_text_body.gsub('{user_name}', user.name) + email_subject = user.environment.signup_welcome_text_subject + + content_type 'text/html' + recipients user.email + + from "#{user.environment.name} <#{user.environment.contact_email}>" + subject email_subject.blank? ? _("Welcome to environment %s") % [user.environment.name] : email_subject + body email_body + end end def signup! @@ -117,7 +129,17 @@ class User < ActiveRecord::Base self.activated_at = Time.now.utc self.activation_code = nil self.person.visible = true - self.person.save! && self.save! + begin + self.person.save! && self.save! + rescue Exception => exception + logger.error(exception.to_s) + false + else + if environment.enabled?('send_welcome_email_to_new_users') && environment.has_signup_welcome_text? + User::Mailer.delay.deliver_signup_welcome_email(self) + end + true + end end def activated? diff --git a/app/views/admin_panel/_signup_welcome_text.rhtml b/app/views/admin_panel/_signup_welcome_text.rhtml new file mode 100644 index 0000000..a81fa09 --- /dev/null +++ b/app/views/admin_panel/_signup_welcome_text.rhtml @@ -0,0 +1,7 @@ +
+ <%= _('This text will be sent to new users if the feature "Send welcome e-mail to new users" is enabled on environment.') %>

+ <%= _('Including %s on body, it will be replaced by the real name of the e-mail recipient.') % content_tag('code', '{user_name}') %> +
+ +<%= labelled_form_field(_('Subject'), text_field(:environment, :signup_welcome_text_subject, :style => 'width:100%')) %> +<%= labelled_form_field(_('Body'), text_area(:environment, :signup_welcome_text_body, :cols => 40, :style => 'width: 100%', :class => 'mceEditor')) %> diff --git a/app/views/admin_panel/site_info.rhtml b/app/views/admin_panel/site_info.rhtml index f1d8f7b..b8df356 100644 --- a/app/views/admin_panel/site_info.rhtml +++ b/app/views/admin_panel/site_info.rhtml @@ -10,6 +10,8 @@ :content => (render :partial => 'site_info', :locals => {:f => f})} %> <% tabs << {:title => _('Terms of use'), :id => 'terms-of-use', :content => (render :partial => 'terms_of_use', :locals => {:f => f})} %> + <% tabs << {:title => _('Signup welcome text'), :id => 'signup-welcome-text', + :content => (render :partial => 'signup_welcome_text', :locals => {:f => f})} %> <%= render_tabs(tabs) %> <% button_bar do %> <%= submit_button(:save, _('Save'), :cancel => {:action => 'index'}) %> diff --git a/app/views/user/mailer/signup_welcome_email.rhtml b/app/views/user/mailer/signup_welcome_email.rhtml new file mode 100644 index 0000000..6982a56 --- /dev/null +++ b/app/views/user/mailer/signup_welcome_email.rhtml @@ -0,0 +1,9 @@ + + + + + + +

<%= word_wrap @body %>

+ + diff --git a/db/migrate/20121008185303_add_signup_welcome_text.rb b/db/migrate/20121008185303_add_signup_welcome_text.rb new file mode 100644 index 0000000..e13d0e8 --- /dev/null +++ b/db/migrate/20121008185303_add_signup_welcome_text.rb @@ -0,0 +1,9 @@ +class AddSignupWelcomeText < ActiveRecord::Migration + def self.up + add_column :environments, :signup_welcome_text, :text + end + + def self.down + remove_column :environments, :signup_welcome_text + end +end diff --git a/db/schema.rb b/db/schema.rb index 067e25f..2110af7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -9,7 +9,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120825185219) do +ActiveRecord::Schema.define(:version => 20121008185303) do create_table "abuse_reports", :force => true do |t| t.integer "reporter_id" @@ -262,6 +262,7 @@ ActiveRecord::Schema.define(:version => 20120825185219) do t.datetime "updated_at" t.integer "reports_lower_bound", :default => 0, :null => false t.string "redirection_after_login", :default => "keep_on_same_page" + t.text "signup_welcome_text" end create_table "external_feeds", :force => true do |t| diff --git a/public/stylesheets/application.css b/public/stylesheets/application.css index 675f4a2..8edb02d 100644 --- a/public/stylesheets/application.css +++ b/public/stylesheets/application.css @@ -2436,6 +2436,13 @@ div#activation_enterprise label, div#activation_enterprise input, div#activation .controller-admin_panel #portal-folders select { width: 50%; } + +.description { + background-color: #E6E6E6; + border: 1px solid #CCC; + padding: 5px; +} + /* ==> public/stylesheets/controller_catalog.css <== */ /* ==> @import url('products.css'); <== */ /* * * Products catalog * * * * * * * * * * * * */ diff --git a/test/functional/admin_panel_controller_test.rb b/test/functional/admin_panel_controller_test.rb index d906a9a..b4d8ef6 100644 --- a/test/functional/admin_panel_controller_test.rb +++ b/test/functional/admin_panel_controller_test.rb @@ -71,6 +71,8 @@ class AdminPanelControllerTest < ActionController::TestCase assert_template 'site_info' assert_tag :tag => 'textarea', :attributes => { :name => 'environment[description]'} assert_tag :tag => 'textarea', :attributes => { :name => 'environment[terms_of_use]'} + assert_tag :tag => 'input', :attributes => { :name => 'environment[signup_welcome_text_subject]'} + assert_tag :tag => 'textarea', :attributes => { :name => 'environment[signup_welcome_text_body]'} end should 'display form for editing message for disabled enterprise' do @@ -110,6 +112,25 @@ class AdminPanelControllerTest < ActionController::TestCase assert !Environment.default.has_terms_of_use? end + should 'save subject and body of signup welcome text' do + subject = "This is my welcome subject" + body = "This is my welcome body" + post :site_info, :environment => { :signup_welcome_text_subject => subject, :signup_welcome_text_body => body } + assert_redirected_to :action => 'index' + + assert_equal subject, Environment.default.signup_welcome_text[:subject] + assert_equal body, Environment.default.signup_welcome_text[:body] + assert !Environment.default.signup_welcome_text.blank? + end + + should 'not save empty string as signup welcome text' do + content = "" + post :site_info, :environment => { :signup_welcome_text_body => content } + assert_redirected_to :action => 'index' + + assert !Environment.default.has_signup_welcome_text? + end + should 'sanitize message for disabled enterprise with white_list' do post :site_info, :environment => { :message_for_disabled_enterprise => "This is my new environment" } assert_redirected_to :action => 'index' diff --git a/test/unit/environment_test.rb b/test/unit/environment_test.rb index 510c595..38b1bce 100644 --- a/test/unit/environment_test.rb +++ b/test/unit/environment_test.rb @@ -1242,4 +1242,82 @@ class EnvironmentTest < ActiveSupport::TestCase end end + should 'respond to signup_welcome_text' do + assert_respond_to Environment.new, :signup_welcome_text + end + + should 'store welcome text in a hash serialized' do + environment = Environment.default + + environment.signup_welcome_text = { + :subject => 'Welcome to the environment', + :body => 'Thanks for signing up!', + } + environment.save + environment.reload + + assert_kind_of Hash, environment.signup_welcome_text + assert_equal ['Welcome to the environment', 'Thanks for signing up!'], [environment.signup_welcome_text[:subject], environment.signup_welcome_text[:body]] + end + + should 'not consider signup welcome text if not defined' do + env = Environment.default + assert !env.has_signup_welcome_text? + end + + should 'not consider signup welcome text if nil' do + env = Environment.default + + env.signup_welcome_text = nil + assert !env.has_signup_welcome_text? + end + + should 'not consider signup welcome text if body is nil' do + env = Environment.default + + env.signup_welcome_text = { + :subject => 'Welcome to the environment', + } + assert !env.has_signup_welcome_text? + end + + should 'consider signup welcome text if subject is nil but body is defined' do + env = Environment.default + + env.signup_welcome_text = { + :body => 'Thanks for signing up!', + } + assert env.has_signup_welcome_text? + end + + should 'consider signup welcome text if subject and body are defined' do + env = Environment.default + + env.signup_welcome_text = { + :subject => 'Welcome to the environment', + :body => 'Thanks for signing up!', + } + assert env.has_signup_welcome_text? + end + + should 'store welcome text subject' do + environment = Environment.default + + environment.signup_welcome_text_subject = 'Welcome to the environment' + environment.save + environment.reload + + assert_equal environment.signup_welcome_text[:subject], environment.signup_welcome_text_subject + end + + should 'store welcome text body' do + environment = Environment.default + + environment.signup_welcome_text_body = 'Thanks for signing up!' + environment.save + environment.reload + + assert_equal environment.signup_welcome_text[:body], environment.signup_welcome_text_body + end + end diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index 88d9cb1..69fe1a5 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -522,6 +522,101 @@ class UserTest < ActiveSupport::TestCase assert user.save! end + should 'not deliver welcome e-mail after user activation if not enabled on environment' do + env = Environment.default + env.signup_welcome_text = { + :subject => 'Welcome to the environment', + :body => 'Thanks for signing up!' + } + env.disable('send_welcome_email_to_new_users') + env.save + + user = new_user :email => 'pending@activation.com' + assert_no_difference(ActionMailer::Base.deliveries, :size) do + user.activate + end + end + + should 'deliver welcome e-mail after user activation if enabled on environment' do + env = Environment.default + env.signup_welcome_text = { + :subject => 'Welcome to this environment', + :body => 'Thanks for signing up!' + } + env.enable('send_welcome_email_to_new_users') + env.save + + user = new_user :email => 'pending@activation.com' + assert_difference(ActionMailer::Base.deliveries, :size, 1) do + user.activate + end + + sent = ActionMailer::Base.deliveries.last + assert_equal ['pending@activation.com'], sent.to + assert_equal 'Welcome to this environment', sent.subject + assert_equal 'Thanks for signing up!', sent.body + end + + should 'deliver welcome e-mail after user activation if enabled on environment with default subject if not defined' do + env = Environment.default + env.signup_welcome_text = { + :body => 'Thanks for signing up!' + } + env.enable('send_welcome_email_to_new_users') + env.save + + user = new_user :email => 'pending@activation.com' + assert_difference(ActionMailer::Base.deliveries, :size, 1) do + user.activate + end + + sent = ActionMailer::Base.deliveries.last + assert_equal "Welcome to environment #{env.name}", sent.subject + end + + should 'deliver welcome e-mail after user activation if enabled on environment and replace user_name' do + env = Environment.default + env.signup_welcome_text = { + :subject => 'Welcome to the environment', + :body => 'Thanks for signing up, {user_name}!', + } + env.enable('send_welcome_email_to_new_users') + env.save + + user = new_user :name => 'John Doe', :email => 'pending@activation.com' + assert_difference(ActionMailer::Base.deliveries, :size, 1) do + user.activate + end + + sent = ActionMailer::Base.deliveries.last + assert_equal "Thanks for signing up, #{user.name}!", sent.body + end + + should 'not deliver welcome e-mail after user activation if enabled on environment but body not filled in' do + env = Environment.default + env.signup_welcome_text = { + :subject => 'Welcome to the environment', + } + env.enable('send_welcome_email_to_new_users') + env.save + + user = new_user :email => 'pending@activation.com' + assert_no_difference(ActionMailer::Base.deliveries, :size) do + user.activate + end + end + + should 'not deliver welcome e-mail after user activation if enabled on environment but welcome text not defined' do + env = Environment.default + env.enable('send_welcome_email_to_new_users') + env.save + + user = new_user :email => 'pending@activation.com' + assert_no_difference(ActionMailer::Base.deliveries, :size) do + user.activate + end + end + protected def new_user(options = {}) user = User.new({ :login => 'quire', :email => 'quire@example.com', :password => 'quire', :password_confirmation => 'quire' }.merge(options)) -- libgit2 0.21.2