diff --git a/app/helpers/email_template_helper.rb b/app/helpers/email_template_helper.rb new file mode 100644 index 0000000..5769e1f --- /dev/null +++ b/app/helpers/email_template_helper.rb @@ -0,0 +1,9 @@ +module EmailTemplateHelper + + def mail_with_template(params={}) + params[:body] = params[:email_template].present? ? params[:email_template].parsed_body(params[:template_params]) : params[:body] + params[:subject] = params[:email_template].present? ? params[:email_template].parsed_subject(params[:template_params]) : params[:subject] + mail(params.except(:email_template)) + end + +end diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb index 93efd25..259e83b 100644 --- a/app/mailers/user_mailer.rb +++ b/app/mailers/user_mailer.rb @@ -1,4 +1,7 @@ class UserMailer < ActionMailer::Base + + include EmailTemplateHelper + def activation_email_notify(user) user_email = "#{user.login}@#{user.email_domain}" @name = user.name @@ -22,10 +25,12 @@ class UserMailer < ActionMailer::Base @redirection = (true if user.return_to) @join = (user.community_to_join if user.community_to_join) - mail( + mail_with_template( from: "#{user.environment.name} <#{user.environment.contact_email}>", to: user.email, subject: _("[%s] Activate your account") % [user.environment.name], + template_params: {:environment => user.environment, :activation_code => @activation_code, :redirection => @redirection, :join => @join}, + email_template: user.environment.email_templates.find_by_template_type(:user_activation), ) end diff --git a/app/models/email_template.rb b/app/models/email_template.rb index 557bd56..3be426a 100644 --- a/app/models/email_template.rb +++ b/app/models/email_template.rb @@ -6,6 +6,10 @@ class EmailTemplate < ActiveRecord::Base validates_presence_of :name + validates :name, uniqueness: { scope: [:owner_type, :owner_id] } + + validates :template_type, uniqueness: { scope: [:owner_type, :owner_id] }, if: :unique_by_type? + def parsed_body(params) @parsed_body ||= parse(body, params) end @@ -18,10 +22,15 @@ class EmailTemplate < ActiveRecord::Base HashWithIndifferentAccess.new ({ :task_rejection => {:description => _('Task Rejection')}, :task_acceptance => {:description => _('Task Acceptance')}, - :organization_members => {:description => _('Organization Members')} + :organization_members => {:description => _('Organization Members')}, + :user_activation => {:description => _('User Activation'), :unique => true} }) end + def unique_by_type? + available_types.fetch(template_type, {})[:unique] + end + protected def parse(source, params) diff --git a/app/models/environment.rb b/app/models/environment.rb index a80323f..2ee1c98 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -21,6 +21,7 @@ class Environment < ActiveRecord::Base has_many :tasks, :dependent => :destroy, :as => 'target' has_many :search_terms, :as => :context + has_many :email_templates, :foreign_key => :owner_id IDENTIFY_SCRIPTS = /(php[0-9s]?|[sp]htm[l]?|pl|py|cgi|rb)/ diff --git a/test/unit/email_template_helper_test.rb b/test/unit/email_template_helper_test.rb new file mode 100644 index 0000000..eaaeb97 --- /dev/null +++ b/test/unit/email_template_helper_test.rb @@ -0,0 +1,20 @@ +require_relative "../test_helper" + +class EmailTemplateHelperTest < ActionView::TestCase + + should 'replace body and subject with parsed values from template' do + template = mock + template.expects(:parsed_body).returns('parsed body') + template.expects(:parsed_subject).returns('parsed subject') + params = {:subject => 'subject', :body => 'body', :email_template => template} + expects(:mail).with({:subject => 'parsed subject', :body => 'parsed body'}) + mail_with_template(params) + end + + should 'do not change params if there is no email template' do + params = {:subject => 'subject', :body => 'body'} + expects(:mail).with(params) + mail_with_template(params) + end + +end diff --git a/test/unit/email_template_test.rb b/test/unit/email_template_test.rb index d0f8e97..e29894f 100644 --- a/test/unit/email_template_test.rb +++ b/test/unit/email_template_test.rb @@ -19,4 +19,25 @@ class EmailTemplateTest < ActiveSupport::TestCase assert_equal 'Hi John', template.parsed_subject({:person => 'John'}) end + should 'not create template with the same name of other' do + template1 = EmailTemplate.new(:template_type => :type1, :name => 'template') + template2 = EmailTemplate.new(:template_type => :type1, :name => 'template') + assert template1.save + assert !template2.save + end + + should 'not create duplicated template when template type is unique' do + template1 = EmailTemplate.new(:template_type => :user_activation, :name => 'template1') + template2 = EmailTemplate.new(:template_type => :user_activation, :name => 'template2') + assert template1.save + assert !template2.save + end + + should 'create duplicated template when template type is not unique' do + template1 = EmailTemplate.new(:template_type => :task_rejection, :name => 'template1') + template2 = EmailTemplate.new(:template_type => :task_rejection, :name => 'template2') + assert template1.save + assert template2.save + end + end diff --git a/test/unit/user_mailer_test.rb b/test/unit/user_mailer_test.rb index b119336..92770aa 100644 --- a/test/unit/user_mailer_test.rb +++ b/test/unit/user_mailer_test.rb @@ -26,6 +26,24 @@ fast_create(Person)) assert_match /profile\/some-user\/friends\/suggest/, email.body.to_s end + should 'deliver activation code email' do + assert_difference 'ActionMailer::Base.deliveries.size' do + u = create_user('some-user') + UserMailer.activation_code(u).deliver + end + end + + should 'deliver activation code email with template' do + EmailTemplate.create!(:template_type => :user_activation, :name => 'template1', :subject => 'activation template subject', :body => 'activation template body', :owner => Environment.default) + assert_difference 'ActionMailer::Base.deliveries.size' do + u = create_user('some-user') + UserMailer.activation_code(u).deliver + end + mail = ActionMailer::Base.deliveries.last + assert_equal 'activation template subject', mail.subject.to_s + assert_equal 'activation template body', mail.body.to_s + end + private def read_fixture(action) -- libgit2 0.21.2