From 032cff3cf34a8481f39f064479dbc77f150b81d0 Mon Sep 17 00:00:00 2001 From: Priit Tark Date: Tue, 25 Aug 2015 21:11:36 +0300 Subject: [PATCH] Added initial mail template editor #2369 --- Gemfile | 1 + Gemfile.lock | 5 ++ .../admin/mail_templates_controller.rb | 61 +++++++++++++++++++ app/models/ability.rb | 1 + app/models/mail_template.rb | 10 +++ app/views/admin/mail_templates/_form.haml | 54 ++++++++++++++++ app/views/admin/mail_templates/edit.haml | 3 + app/views/admin/mail_templates/index.haml | 26 ++++++++ app/views/admin/mail_templates/new.haml | 3 + app/views/admin/mail_templates/show.haml | 41 +++++++++++++ app/views/layouts/admin/application.haml | 1 + config/locales/en.yml | 3 + config/routes.rb | 1 + .../20150825125118_create_email_templates.rb | 19 ++++++ db/schema-read-only.rb | 14 ++++- db/structure.sql | 56 +++++++++++++++++ 16 files changed, 298 insertions(+), 1 deletion(-) create mode 100644 app/controllers/admin/mail_templates_controller.rb create mode 100644 app/models/mail_template.rb create mode 100644 app/views/admin/mail_templates/_form.haml create mode 100644 app/views/admin/mail_templates/edit.haml create mode 100644 app/views/admin/mail_templates/index.haml create mode 100644 app/views/admin/mail_templates/new.haml create mode 100644 app/views/admin/mail_templates/show.haml create mode 100644 db/migrate/20150825125118_create_email_templates.rb diff --git a/Gemfile b/Gemfile index 57d252870..2505655cb 100644 --- a/Gemfile +++ b/Gemfile @@ -51,6 +51,7 @@ gem 'html5_validators', '~> 1.2.0' # model requements now automatically on htm gem 'coderay', '~> 1.1.0' # xml console visualize gem 'select2-rails', '~> 3.5.9.3' # for autocomplete gem 'bootstrap-datepicker-rails', '~> 1.3.1.1' # datepicker +gem 'liquid', '~> 3.0.6' # for email templates # rights gem 'devise', '~> 3.5.1' # authenitcation diff --git a/Gemfile.lock b/Gemfile.lock index d8d12b077..5a137da04 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -278,6 +278,7 @@ GEM addressable (~> 2.3) libv8 (3.16.14.11) libxml-ruby (2.8.0) + liquid (3.0.6) listen (3.0.3) rb-fsevent (>= 0.9.3) rb-inotify (>= 0.9) @@ -583,6 +584,7 @@ DEPENDENCIES jquery-validation-rails (~> 1.13.1) kaminari (~> 0.16.3) launchy (~> 2.4.3) + liquid (~> 3.0.6) mina (~> 0.3.1) money-rails (~> 1.4.1) newrelic_rpm (~> 3.12.0.288) @@ -622,3 +624,6 @@ DEPENDENCIES uuidtools (~> 2.1.4) validates_email_format_of (~> 1.6.3) whenever (~> 0.9.4) + +BUNDLED WITH + 1.10.6 diff --git a/app/controllers/admin/mail_templates_controller.rb b/app/controllers/admin/mail_templates_controller.rb new file mode 100644 index 000000000..9d7d88c3d --- /dev/null +++ b/app/controllers/admin/mail_templates_controller.rb @@ -0,0 +1,61 @@ +class Admin::MailTemplatesController < AdminController + load_and_authorize_resource + + def index + @q = MailTemplate.search(params[:q]) + @mail_templates = @q.result.page(params[:page]) + end + + def new + @mail_tempalte = MailTemplate.new + end + + def show + @mail_template = MailTemplate.find(params[:id]) + @subject = Liquid::Template.parse(@mail_template.subject).render.html_safe + @html_body = Liquid::Template.parse(@mail_template.body).render.html_safe + @text_body = Liquid::Template.parse(@mail_template.text_body).render.html_safe + end + + def edit + @mail_template = MailTemplate.find(params[:id]) + end + + def create + @mail_template = MailTemplate.new(mail_template_params) + + if @mail_template.save + redirect_to [:admin, @mail_template] + else + flash.now[:alert] = I18n.t(:failure) + render 'new' + end + end + + def update + @mail_template = MailTemplate.find(params[:id]) + + if @mail_template.update_attributes(mail_template_params) + redirect_to [:admin, @mail_template] + else + flash.now[:alert] = I18n.t(:failure) + render 'edit' + end + end + + def destroy + @mail_template = MailTemplate.find(params[:id]) + if @mail_template.destroy + redirect_to admin_mail_templates_path, notise: t(:deleted) + else + flash.now[:alert] = I18n.t(:failure) + render 'show' + end + end + + private + + def mail_template_params + params.require(:mail_template).permit(:name, :subject, :from, :bcc, :cc, :body, :text_body) + end +end diff --git a/app/models/ability.rb b/app/models/ability.rb index 0c659026b..fc874f129 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -96,6 +96,7 @@ class Ability can :manage, LegalDocument can :manage, BankStatement can :manage, BankTransaction + can :manage, MailTemplate can :manage, Invoice can :manage, WhiteIp can :read, ApiLog::EppLog diff --git a/app/models/mail_template.rb b/app/models/mail_template.rb new file mode 100644 index 000000000..ed0e78919 --- /dev/null +++ b/app/models/mail_template.rb @@ -0,0 +1,10 @@ +class MailTemplate < ActiveRecord::Base + + validates :name, :subject, :from, :body, :text_body, presence: true + + def to_html(body) + template = Erubis::Eruby.new(content, escape: true) + template_result = template.result(context) + Sanitize.clean(RDiscount.new(template_result).to_html.encode('UTF-8', undef: :replace), Sanitize::Config::RELAXED) + end +end diff --git a/app/views/admin/mail_templates/_form.haml b/app/views/admin/mail_templates/_form.haml new file mode 100644 index 000000000..6e9d828c5 --- /dev/null +++ b/app/views/admin/mail_templates/_form.haml @@ -0,0 +1,54 @@ += form_for([:admin, mail_template], html: {class: 'form-horizontal'}) do |f| + = render 'shared/full_errors', object: mail_template + + + - liquid_help = link_to 'Liquid info', 'https://github.com/Shopify/liquid/wiki/Liquid-for-Designers' + + .row + .col-md-12 + .panel.panel-default + .panel-body + .form-group + .col-md-4.control-label + = f.label :name + .col-md-7 + = f.text_field(:name, class: 'form-control') + .form-group + .col-md-4.control-label + = f.label :subject + %br + = liquid_help + .col-md-7 + = f.text_field(:subject, class: 'form-control') + .form-group + .col-md-4.control-label + = f.label :from + .col-md-7 + = f.text_field(:from, class: 'form-control', lax_email: true) + .form-group + .col-md-4.control-label + = f.label :cc + .col-md-7 + = f.text_field(:cc, class: 'form-control', lax_email: true) + .form-group + .col-md-4.control-label + = f.label :bcc + .col-md-7 + = f.text_field(:bcc, class: 'form-control', lax_email: true) + .form-group + .col-md-12 + = f.label :body, t(:html_body) + = liquid_help + .col-md-12 + = f.text_area(:body, class: 'form-control', size: '15x15') + .form-group + .col-md-12 + = f.label :text_body + = liquid_help + .col-md-12 + = f.text_area(:text_body, class: 'form-control', size: '15x15') + + %hr + .row + .col-md-12.text-right + = button_tag(t(:save), class: 'btn btn-primary') diff --git a/app/views/admin/mail_templates/edit.haml b/app/views/admin/mail_templates/edit.haml new file mode 100644 index 000000000..9516671c6 --- /dev/null +++ b/app/views/admin/mail_templates/edit.haml @@ -0,0 +1,3 @@ += render 'shared/title', name: "#{t(:edit)}: #{@mail_template.name}" + += render 'form', mail_template: @mail_template diff --git a/app/views/admin/mail_templates/index.haml b/app/views/admin/mail_templates/index.haml new file mode 100644 index 000000000..7fa1913ae --- /dev/null +++ b/app/views/admin/mail_templates/index.haml @@ -0,0 +1,26 @@ +- content_for :actions do + = link_to(t(:new), new_admin_mail_template_path, class: 'btn btn-primary') += render 'shared/title', name: t(:mail_templates) + +.row + .col-md-12 + .table-responsive + %table.table.table-hover.table-bordered.table-condensed + %thead + %tr + %th{class: 'col-xs-3'}= sort_link(@q, 'name', t(:name)) + %th{class: 'col-xs-3'}= sort_link(@q, 'subject') + %th{class: 'col-xs-3'}= sort_link(@q, 'updated_at') + %th{class: 'col-xs-3'}= t(:actions) + %tbody + - @mail_templates.each do |mt| + %tr + %td= link_to(truncate(mt.name), admin_mail_template_path(mt)) + %td= truncate(mt.subject) + %td= l(mt.updated_at) + %td + = link_to(t(:edit), edit_admin_mail_template_path(mt), class: 'btn btn-primary btn-xs') + +.row + .col-md-12 + = paginate @mail_templates diff --git a/app/views/admin/mail_templates/new.haml b/app/views/admin/mail_templates/new.haml new file mode 100644 index 000000000..e5889e2b3 --- /dev/null +++ b/app/views/admin/mail_templates/new.haml @@ -0,0 +1,3 @@ += render 'shared/title', name: t(:new_mail_template) + += render 'form', mail_template: @mail_template diff --git a/app/views/admin/mail_templates/show.haml b/app/views/admin/mail_templates/show.haml new file mode 100644 index 000000000..31739bbf9 --- /dev/null +++ b/app/views/admin/mail_templates/show.haml @@ -0,0 +1,41 @@ +- content_for :actions do + = link_to(t(:edit), edit_admin_mail_template_path(@mail_template.id), class: 'btn btn-primary') + = link_to(t(:delete), admin_mail_template_path(@mail_template.id), + method: :delete, data: { confirm: t(:are_you_sure) }, class: 'btn btn-danger') + = link_to(t(:email_templates), admin_mail_templates_path, class: 'btn btn-default') += render 'shared/title', name: @mail_template.name + +.row + .col-md-12 + .panel.panel-default + .panel-heading + %h3.panel-title= t(:general) + .panel-body + %dl.dl-horizontal + %dt= t(:subject) + %dd= @mail_template.subject + + %dt= t(:from) + %dd= @mail_template.from + + - if @mail_template.cc.present? + %dt= t(:cc) + %dd= @mail_template.cc + + - if @mail_template.bcc.present? + %dt= t(:bcc) + %dd= @mail_template.bcc + + .col-md-12 + .panel.panel-default + .panel-heading + %h3.panel-title= t(:html_body) + .panel-body + = @html_body + + .col-md-12 + .panel.panel-default + .panel-heading + %h3.panel-title= t(:text_body) + .panel-body + = @text_body diff --git a/app/views/layouts/admin/application.haml b/app/views/layouts/admin/application.haml index da411864c..d224b1464 100644 --- a/app/views/layouts/admin/application.haml +++ b/app/views/layouts/admin/application.haml @@ -61,6 +61,7 @@ %li= link_to t(:zonefile), admin_zonefile_settings_path %li= link_to t(:blocked_domains), admin_blocked_domains_path %li= link_to t(:reserved_domains), admin_reserved_domains_path + %li= link_to t(:mail_templates), admin_mail_templates_path -# %li= link_to t(:domains_history), admin_domain_versions_path %li= link_to t(:epp_logs), admin_epp_logs_path %li= link_to t(:repp_logs), admin_repp_logs_path diff --git a/config/locales/en.yml b/config/locales/en.yml index 900ea3b8f..5bd4c0f63 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -914,3 +914,6 @@ en: created_at_until: 'Created at until' is_registrant: 'Is registrant' force_delete_set_on_domain: 'Force delete set on domain %{domain}' + mail_templates: Mail Templates + new_mail_template: New mail template + failure: "It was not saved" diff --git a/config/routes.rb b/config/routes.rb index feffb2665..5ea7beff7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -160,6 +160,7 @@ Rails.application.routes.draw do resources :legal_documents resources :keyrelays resources :pricelists + resources :mail_templates resources :bank_statements do resources :bank_transactions diff --git a/db/migrate/20150825125118_create_email_templates.rb b/db/migrate/20150825125118_create_email_templates.rb new file mode 100644 index 000000000..60e374247 --- /dev/null +++ b/db/migrate/20150825125118_create_email_templates.rb @@ -0,0 +1,19 @@ +class CreateEmailTemplates < ActiveRecord::Migration + def self.up + create_table :mail_templates do |t| + t.string :name, null: false + t.string :subject + t.string :from + t.string :bcc + t.string :cc + t.text :body, null: false + t.text :text_body, null: false + t.timestamps + end + end + + def self.down + drop_table :mail_templates + end +end + diff --git a/db/schema-read-only.rb b/db/schema-read-only.rb index 5eee65c59..4839d519e 100644 --- a/db/schema-read-only.rb +++ b/db/schema-read-only.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150810114746) do +ActiveRecord::Schema.define(version: 20150825125118) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -878,6 +878,18 @@ ActiveRecord::Schema.define(version: 20150810114746) do add_index "log_zonefile_settings", ["item_type", "item_id"], name: "index_log_zonefile_settings_on_item_type_and_item_id", using: :btree add_index "log_zonefile_settings", ["whodunnit"], name: "index_log_zonefile_settings_on_whodunnit", using: :btree + create_table "mail_templates", force: :cascade do |t| + t.string "name", null: false + t.string "subject" + t.string "from" + t.string "bcc" + t.string "cc" + t.text "body", null: false + t.text "text_body", null: false + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "messages", force: :cascade do |t| t.integer "registrar_id" t.string "body" diff --git a/db/structure.sql b/db/structure.sql index 62d98a861..65b344c97 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -2228,6 +2228,43 @@ CREATE SEQUENCE log_zonefile_settings_id_seq ALTER SEQUENCE log_zonefile_settings_id_seq OWNED BY log_zonefile_settings.id; +-- +-- Name: mail_templates; Type: TABLE; Schema: public; Owner: -; Tablespace: +-- + +CREATE TABLE mail_templates ( + id integer NOT NULL, + name character varying NOT NULL, + subject character varying, + "from" character varying, + bcc character varying, + cc character varying, + body text NOT NULL, + text_body text NOT NULL, + created_at timestamp without time zone, + updated_at timestamp without time zone +); + + +-- +-- Name: mail_templates_id_seq; Type: SEQUENCE; Schema: public; Owner: - +-- + +CREATE SEQUENCE mail_templates_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: mail_templates_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - +-- + +ALTER SEQUENCE mail_templates_id_seq OWNED BY mail_templates.id; + + -- -- Name: messages; Type: TABLE; Schema: public; Owner: -; Tablespace: -- @@ -3154,6 +3191,13 @@ ALTER TABLE ONLY log_white_ips ALTER COLUMN id SET DEFAULT nextval('log_white_ip ALTER TABLE ONLY log_zonefile_settings ALTER COLUMN id SET DEFAULT nextval('log_zonefile_settings_id_seq'::regclass); +-- +-- Name: id; Type: DEFAULT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY mail_templates ALTER COLUMN id SET DEFAULT nextval('mail_templates_id_seq'::regclass); + + -- -- Name: id; Type: DEFAULT; Schema: public; Owner: - -- @@ -3676,6 +3720,14 @@ ALTER TABLE ONLY log_zonefile_settings ADD CONSTRAINT log_zonefile_settings_pkey PRIMARY KEY (id); +-- +-- Name: mail_templates_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: +-- + +ALTER TABLE ONLY mail_templates + ADD CONSTRAINT mail_templates_pkey PRIMARY KEY (id); + + -- -- Name: messages_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: -- @@ -4874,3 +4926,7 @@ INSERT INTO schema_migrations (version) VALUES ('20150803080914'); INSERT INTO schema_migrations (version) VALUES ('20150810114746'); +INSERT INTO schema_migrations (version) VALUES ('20150810114747'); + +INSERT INTO schema_migrations (version) VALUES ('20150825125118'); +