Commit e083ea4a59ff8b84c7aec9fecfa24543f6cf5536
1 parent
9a1c00d4
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
A new model to store email templates
Showing
4 changed files
with
59 additions
and
0 deletions
Show diff stats
Gemfile
@@ -19,6 +19,7 @@ gem 'gettext', '~> 2.2.1', :require => false | @@ -19,6 +19,7 @@ gem 'gettext', '~> 2.2.1', :require => false | ||
19 | gem 'locale', '~> 2.0.5' | 19 | gem 'locale', '~> 2.0.5' |
20 | gem 'whenever', :require => false | 20 | gem 'whenever', :require => false |
21 | gem 'eita-jrails', '>= 0.9.5', :require => 'jrails' | 21 | gem 'eita-jrails', '>= 0.9.5', :require => 'jrails' |
22 | +gem 'liquid', '~> 3.0.3' | ||
22 | 23 | ||
23 | # asset pipeline | 24 | # asset pipeline |
24 | gem 'uglifier', '>= 1.0.3' | 25 | gem 'uglifier', '>= 1.0.3' |
@@ -0,0 +1,22 @@ | @@ -0,0 +1,22 @@ | ||
1 | +class EmailTemplate < ActiveRecord::Base | ||
2 | + | ||
3 | + belongs_to :owner, :polymorphic => true | ||
4 | + | ||
5 | + attr_accessible :template_type, :subject, :body, :owner, :name | ||
6 | + | ||
7 | + def parsed_body(params) | ||
8 | + @parsed_body ||= parse(body, params) | ||
9 | + end | ||
10 | + | ||
11 | + def parsed_subject(params) | ||
12 | + @parsed_subject ||= parse(subject, params) | ||
13 | + end | ||
14 | + | ||
15 | + protected | ||
16 | + | ||
17 | + def parse(source, params) | ||
18 | + template = Liquid::Template.parse(source) | ||
19 | + template.render(HashWithIndifferentAccess.new(params)) | ||
20 | + end | ||
21 | + | ||
22 | +end |
@@ -0,0 +1,14 @@ | @@ -0,0 +1,14 @@ | ||
1 | +class CreateEmailTemplate < ActiveRecord::Migration | ||
2 | + | ||
3 | + def change | ||
4 | + create_table :email_templates do |t| | ||
5 | + t.string :name | ||
6 | + t.string :template_type | ||
7 | + t.string :subject | ||
8 | + t.text :body | ||
9 | + t.references :owner, :polymorphic => true | ||
10 | + t.timestamps | ||
11 | + end | ||
12 | + end | ||
13 | + | ||
14 | +end |
@@ -0,0 +1,22 @@ | @@ -0,0 +1,22 @@ | ||
1 | +require_relative "../test_helper" | ||
2 | + | ||
3 | +class EmailTemplateTest < ActiveSupport::TestCase | ||
4 | + | ||
5 | + should 'filter templates by type' do | ||
6 | + EmailTemplate.create!(:template_type => :type1, :subject => 'template1') | ||
7 | + EmailTemplate.create!(:template_type => :type2, :subject => 'template2') | ||
8 | + EmailTemplate.create!(:template_type => :type2, :subject => 'template3') | ||
9 | + assert_equal ['template2', 'template3'], EmailTemplate.find_all_by_template_type(:type2).map(&:subject) | ||
10 | + end | ||
11 | + | ||
12 | + should 'parse body using params' do | ||
13 | + template = EmailTemplate.new(:body => 'Hi {{person}}') | ||
14 | + assert_equal 'Hi John', template.parsed_body({:person => 'John'}) | ||
15 | + end | ||
16 | + | ||
17 | + should 'parse subject using params' do | ||
18 | + template = EmailTemplate.new(:subject => 'Hi {{person}}') | ||
19 | + assert_equal 'Hi John', template.parsed_subject({:person => 'John'}) | ||
20 | + end | ||
21 | + | ||
22 | +end |