From 1177f9eeee5de2aa9ba6f259564fb9cef2f25bfe Mon Sep 17 00:00:00 2001 From: Mohit Date: Thu, 5 Dec 2024 17:50:01 +0530 Subject: [PATCH] Make it compatible with spree 4.8 --- .../spree/admin/campaigns_controller.rb | 5 +- app/jobs/email_send_job.rb | 4 +- app/mailers/spree/base_mailer_decorator.rb | 43 -------- app/mailers/spree/order_mailer.rb | 57 ----------- app/mailers/spree/reimbursement_mailer.rb | 24 ----- app/mailers/spree/shipment_mailer.rb | 28 ------ .../spree/base_mailer_decorator.rb | 45 +++++++++ .../spree/order_mailer_decorator.rb | 61 ++++++++++++ .../spree/reimbursement_mailer_decorator.rb | 28 ++++++ .../spree/shipment_mailer_decorator.rb | 32 ++++++ app/models/spree/campaign.rb | 1 - app/models/spree/email_template.rb | 8 +- .../admin/main_menu/dynamic_email_builder.rb | 27 +++++ .../spree_dynamic_emails/configuration.rb | 2 +- .../admin/email_templates/_form.html.erb | 2 +- .../spree/admin/shared/_main_menu.html.erb | 99 ------------------- .../admin/shared/sub_menu/_template.html.erb | 4 - config/initializers/dynamic_email.rb | 5 + lib/spree_dynamic_emails.rb | 1 + lib/spree_dynamic_emails/engine.rb | 2 +- spree_dynamic_emails.gemspec | 1 + 21 files changed, 212 insertions(+), 267 deletions(-) delete mode 100644 app/mailers/spree/base_mailer_decorator.rb delete mode 100644 app/mailers/spree/order_mailer.rb delete mode 100644 app/mailers/spree/reimbursement_mailer.rb delete mode 100644 app/mailers/spree/shipment_mailer.rb create mode 100644 app/mailers/spree_dynamic_emails/spree/base_mailer_decorator.rb create mode 100644 app/mailers/spree_dynamic_emails/spree/order_mailer_decorator.rb create mode 100644 app/mailers/spree_dynamic_emails/spree/reimbursement_mailer_decorator.rb create mode 100644 app/mailers/spree_dynamic_emails/spree/shipment_mailer_decorator.rb create mode 100644 app/models/spree_dynamic_emails/admin/main_menu/dynamic_email_builder.rb delete mode 100644 app/views/spree/admin/shared/_main_menu.html.erb delete mode 100644 app/views/spree/admin/shared/sub_menu/_template.html.erb create mode 100644 config/initializers/dynamic_email.rb diff --git a/app/controllers/spree/admin/campaigns_controller.rb b/app/controllers/spree/admin/campaigns_controller.rb index 639d6d3..44434a5 100644 --- a/app/controllers/spree/admin/campaigns_controller.rb +++ b/app/controllers/spree/admin/campaigns_controller.rb @@ -6,7 +6,7 @@ class CampaignsController < ResourceController def send_mail if @campaign.email_template.active if params[:campaign_job_details].present? - schedule = Time.zone.parse(JSON.parse(params[:campaign_job_details]).join(', ')) + schedule = Time.zone.parse(params[:campaign_job_details].join(', ')) end @campaign.run(schedule) flash[:success] = @@ -29,7 +29,8 @@ def campaign_log def delete_jobs job_ids = params[:format] - @campaign.delete_job(job_ids) + @campaign.campaign_job_details.delete(job_ids) + @campaign.save redirect_to edit_admin_campaign_path end end diff --git a/app/jobs/email_send_job.rb b/app/jobs/email_send_job.rb index 1ae44fc..84cfb8a 100644 --- a/app/jobs/email_send_job.rb +++ b/app/jobs/email_send_job.rb @@ -20,8 +20,8 @@ def recepients(template, id) end def create_log(response) - email = JSON.parse(response.from).join(', ') - sent_to = JSON.parse(response.to).join(', ') + email = response.from.join(', ') + sent_to = response.to.join(', ') date = Time.zone.parse(response.date.to_s) status = response.present? ? 'success' : 'fail' @campaign_logs = @campaign.campaign_logs.build(email: email, sent_to: sent_to, status: status, sent_at: date) diff --git a/app/mailers/spree/base_mailer_decorator.rb b/app/mailers/spree/base_mailer_decorator.rb deleted file mode 100644 index 4c65f58..0000000 --- a/app/mailers/spree/base_mailer_decorator.rb +++ /dev/null @@ -1,43 +0,0 @@ -# frozen_string_literal: true - -module Spree - module BaseMailerDecorator - def self.prepended(base) - base.before_action :ensure_default_action_mailer_url_host - base.after_action :attach_metadata - end - - private - - def ensure_default_action_mailer_url_host(store_url = nil) - ActionMailer::Base.default_url_options ||= {} - ActionMailer::Base.default_url_options[:protocol] = 'https' - ActionMailer::Base.default_url_options[:host] ||= store_url - end - - def build_template(resource, options = {}) - @template = current_store.email_templates.find_by(template_name: action_name) - return unless @template.present? - - @subject = @template.subject - @body = @template.render_body(resource, options) - end - - def attach_metadata - mailer_klass = self.class.to_s - mailer_action = action_name - - message.instance_variable_set(:@store_id, current_store.id) - message.instance_variable_set(:@mailer_klass, mailer_klass) - message.instance_variable_set(:@mailer_action, mailer_action) - message.instance_variable_set(:@test, @test_mail) - - message.class.send(:attr_reader, :store_id) - message.class.send(:attr_reader, :mailer_klass) - message.class.send(:attr_reader, :mailer_action) - message.class.send(:attr_reader, :test) - end - end -end - -::Spree::BaseMailer.prepend ::Spree::BaseMailerDecorator diff --git a/app/mailers/spree/order_mailer.rb b/app/mailers/spree/order_mailer.rb deleted file mode 100644 index e54904f..0000000 --- a/app/mailers/spree/order_mailer.rb +++ /dev/null @@ -1,57 +0,0 @@ -# frozen_string_literal: true - -module Spree - class OrderMailer < ::Spree::BaseMailer - def confirm_email(order, test=false, id=nil, email_to=nil) - @order = order.respond_to?(:id) ? order : ::Spree::Order.find(order) - @test_mail = test - current_store = @order.store - build_confirm_email_detail - email = id.present? ? email_to : @order.email - mail(to: email, from: from_address, subject: @subject, store_url: current_store.url) - end - - def store_owner_notification_email(order, test=false, id=nil, email_to=nil) - @order = order.respond_to?(:id) ? order : ::Spree::Order.find(order) - @test_mail = test - current_store = @order.store - build_store_owner_notification_email - email = id.present? ? email_to : current_store.new_order_notifications_email - mail(to: email, from: from_address, subject: @subject, store_url: current_store.url) - end - - def cancel_email(order, test=false, id=nil, email_to=nil) - @order = order.respond_to?(:id) ? order : ::Spree::Order.find(order) - @test_mail = test - current_store = @order.store - build_cancel_email_detail - email = id.present? ? email_to : @order.email - mail(to: email, from: from_address, subject: @subject, store_url: current_store.url) - end - - def build_confirm_email_detail - order_details - end - - def build_cancel_email_detail - order_details - end - - def build_store_owner_notification_email - order_details - end - - private - - def order_details - ensure_default_action_mailer_url_host(current_store.url) - line_item_details = render_to_string(partial: 'spree/shared/purchased_items_table', - locals: { - line_items: @order.line_items, order: @order - }) - line_items_table = render_to_string(collection: @order.line_items, - partial: 'spree/shared/purchased_items_table/line_item', as: :line_item) - @body = build_template(@order, { line_item_details: line_item_details, line_items_table: line_items_table }) - end - end -end diff --git a/app/mailers/spree/reimbursement_mailer.rb b/app/mailers/spree/reimbursement_mailer.rb deleted file mode 100644 index 44e5439..0000000 --- a/app/mailers/spree/reimbursement_mailer.rb +++ /dev/null @@ -1,24 +0,0 @@ -# frozen_string_literal: true - -module Spree - class ReimbursementMailer < BaseMailer - def reimbursement_email(reimbursement, test=false, id=nil, email_to=nil) - @reimbursement = reimbursement.respond_to?(:id) ? reimbursement : Spree::Reimbursement.find(reimbursement) - @test_mail = test - @order = @reimbursement.order - current_store = @reimbursement.store || Spree::Store.default - reimbursement_details - email = id.present? ? email_to : @order.email - mail(to: email, from: current_store.mail_from_address, subject: @subject, store_url: current_store.url) - end - - private - - def reimbursement_details - ensure_default_action_mailer_url_host(current_store.url) - reimbursement_item_details = render_to_string(partial: 'spree/reimbursement_mailer/shared/reimbursement_table', - locals: { reimbursement: @reimbursement }) - @body = build_template(@reimbursement, { reimbursement_item_details: reimbursement_item_details }) - end - end -end diff --git a/app/mailers/spree/shipment_mailer.rb b/app/mailers/spree/shipment_mailer.rb deleted file mode 100644 index 61c8113..0000000 --- a/app/mailers/spree/shipment_mailer.rb +++ /dev/null @@ -1,28 +0,0 @@ -# frozen_string_literal: true - -module Spree - class ShipmentMailer < BaseMailer - def shipped_email(shipment, test=false, id=nil, email_to=nil) - @shipment = shipment.respond_to?(:id) ? shipment : Spree::Shipment.find(shipment) - @test_mail = test - @order = @shipment.order - current_store = @shipment.store - shipment_details - email = id.present? ? email_to : @order.email - mail(to: email, from: from_address, subject: @subject, store_url: current_store.url) - end - - private - - def shipment_details - ensure_default_action_mailer_url_host(current_store.url) - line_item_details = render_to_string(partial: 'spree/shared/purchased_items_table', - locals: { - line_items: @shipment.manifest.map(&:line_item), order: @shipment.order - }) - line_items_table = render_to_string(collection: @shipment.order.line_items, - partial: 'spree/shared/purchased_items_table/line_item', as: :line_item) - @body = build_template(@shipment, { line_item_details: line_item_details, line_items_table: line_items_table }) - end - end -end diff --git a/app/mailers/spree_dynamic_emails/spree/base_mailer_decorator.rb b/app/mailers/spree_dynamic_emails/spree/base_mailer_decorator.rb new file mode 100644 index 0000000..5553314 --- /dev/null +++ b/app/mailers/spree_dynamic_emails/spree/base_mailer_decorator.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +module SpreeDynamicEmails + module Spree + module BaseMailerDecorator + def self.prepended(base) + base.before_action :ensure_default_action_mailer_url_host + base.after_action :attach_metadata + end + + private + + def ensure_default_action_mailer_url_host(store_url = nil) + ActionMailer::Base.default_url_options ||= {} + ActionMailer::Base.default_url_options[:protocol] = 'https' + ActionMailer::Base.default_url_options[:host] ||= store_url + end + + def build_template(resource, options = {}) + @template = current_store.email_templates.find_by(template_name: action_name) + return unless @template.present? + + @subject = @template.subject + @body = @template.render_body(resource, options) + end + + def attach_metadata + mailer_klass = self.class.to_s + mailer_action = action_name + + message.instance_variable_set(:@store_id, current_store.id) + message.instance_variable_set(:@mailer_klass, mailer_klass) + message.instance_variable_set(:@mailer_action, mailer_action) + message.instance_variable_set(:@test, @test_mail) + + message.class.send(:attr_reader, :store_id) + message.class.send(:attr_reader, :mailer_klass) + message.class.send(:attr_reader, :mailer_action) + message.class.send(:attr_reader, :test) + end + end + end +end + +::Spree::BaseMailer.prepend ::SpreeDynamicEmails::Spree::BaseMailerDecorator diff --git a/app/mailers/spree_dynamic_emails/spree/order_mailer_decorator.rb b/app/mailers/spree_dynamic_emails/spree/order_mailer_decorator.rb new file mode 100644 index 0000000..ec9eac8 --- /dev/null +++ b/app/mailers/spree_dynamic_emails/spree/order_mailer_decorator.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +module SpreeDynamicEmails + module Spree + module OrderMailerDecorator + def confirm_email(order, test=false, id=nil, email_to=nil) + @order = order.respond_to?(:id) ? order : ::Spree::Order.find(order) + @test_mail = test + current_store = @order.store + build_confirm_email_detail + email = id.present? ? email_to : @order.email + mail(to: email, from: from_address, subject: @subject, store_url: current_store.url) + end + + def store_owner_notification_email(order, test=false, id=nil, email_to=nil) + @order = order.respond_to?(:id) ? order : ::Spree::Order.find(order) + @test_mail = test + current_store = @order.store + build_store_owner_notification_email + email = id.present? ? email_to : current_store.new_order_notifications_email + mail(to: email, from: from_address, subject: @subject, store_url: current_store.url) + end + + def cancel_email(order, test=false, id=nil, email_to=nil) + @order = order.respond_to?(:id) ? order : ::Spree::Order.find(order) + @test_mail = test + current_store = @order.store + build_cancel_email_detail + email = id.present? ? email_to : @order.email + mail(to: email, from: from_address, subject: @subject, store_url: current_store.url) + end + + def build_confirm_email_detail + order_details + end + + def build_cancel_email_detail + order_details + end + + def build_store_owner_notification_email + order_details + end + + private + + def order_details + ensure_default_action_mailer_url_host(current_store.url) + line_item_details = render_to_string(partial: 'spree/shared/purchased_items_table', + locals: { + line_items: @order.line_items, order: @order + }) + line_items_table = render_to_string(collection: @order.line_items, + partial: 'spree/shared/purchased_items_table/line_item', as: :line_item) + @body = build_template(@order, { line_item_details: line_item_details, line_items_table: line_items_table }) + end + end + end +end + +Spree::OrderMailer.prepend ::SpreeDynamicEmails::Spree::OrderMailerDecorator diff --git a/app/mailers/spree_dynamic_emails/spree/reimbursement_mailer_decorator.rb b/app/mailers/spree_dynamic_emails/spree/reimbursement_mailer_decorator.rb new file mode 100644 index 0000000..d83d165 --- /dev/null +++ b/app/mailers/spree_dynamic_emails/spree/reimbursement_mailer_decorator.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +module SpreeDynamicEmails + module Spree + module ReimbursementMailerDecorator + def reimbursement_email(reimbursement, test=false, id=nil, email_to=nil) + @reimbursement = reimbursement.respond_to?(:id) ? reimbursement : Spree::Reimbursement.find(reimbursement) + @test_mail = test + @order = @reimbursement.order + current_store = @reimbursement.store || Spree::Store.default + reimbursement_details + email = id.present? ? email_to : @order.email + mail(to: email, from: current_store.mail_from_address, subject: @subject, store_url: current_store.url) + end + + private + + def reimbursement_details + ensure_default_action_mailer_url_host(current_store.url) + reimbursement_item_details = render_to_string(partial: 'spree/reimbursement_mailer/shared/reimbursement_table', + locals: { reimbursement: @reimbursement }) + @body = build_template(@reimbursement, { reimbursement_item_details: reimbursement_item_details }) + end + end + end +end + +Spree::ReimbursementMailer.prepend ::SpreeDynamicEmails::Spree::ReimbursementMailerDecorator \ No newline at end of file diff --git a/app/mailers/spree_dynamic_emails/spree/shipment_mailer_decorator.rb b/app/mailers/spree_dynamic_emails/spree/shipment_mailer_decorator.rb new file mode 100644 index 0000000..16ddfe1 --- /dev/null +++ b/app/mailers/spree_dynamic_emails/spree/shipment_mailer_decorator.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +module SpreeDynamicEmails + module Spree + module ShipmentMailerDecorator + def shipped_email(shipment, test=false, id=nil, email_to=nil) + @shipment = shipment.respond_to?(:id) ? shipment : Spree::Shipment.find(shipment) + @test_mail = test + @order = @shipment.order + current_store = @shipment.store + shipment_details + email = id.present? ? email_to : @order.email + mail(to: email, from: from_address, subject: @subject, store_url: current_store.url) + end + + private + + def shipment_details + ensure_default_action_mailer_url_host(current_store.url) + line_item_details = render_to_string(partial: 'spree/shared/purchased_items_table', + locals: { + line_items: @shipment.manifest.map(&:line_item), order: @shipment.order + }) + line_items_table = render_to_string(collection: @shipment.order.line_items, + partial: 'spree/shared/purchased_items_table/line_item', as: :line_item) + @body = build_template(@shipment, { line_item_details: line_item_details, line_items_table: line_items_table }) + end + end + end +end + +Spree::ShipmentMailer.prepend ::SpreeDynamicEmails::Spree::ShipmentMailerDecorator \ No newline at end of file diff --git a/app/models/spree/campaign.rb b/app/models/spree/campaign.rb index b36c7e1..d17e7bf 100644 --- a/app/models/spree/campaign.rb +++ b/app/models/spree/campaign.rb @@ -8,7 +8,6 @@ class Campaign < ApplicationRecord belongs_to :email_template has_many :campaign_logs, dependent: :destroy validates :email_template_id, presence: true - validate :check_email_addresses validates :to, presence: { message: 'or rule must be present' }, unless: -> { rule.present? } validates :rule, presence: { message: 'or to must be present' }, unless: -> { to.present? } validate :check_email_addresses diff --git a/app/models/spree/email_template.rb b/app/models/spree/email_template.rb index 48797af..4247430 100644 --- a/app/models/spree/email_template.rb +++ b/app/models/spree/email_template.rb @@ -38,11 +38,11 @@ def record private def confirm_email - order_vaiables + order_variables end def cancel_email - order_vaiables + order_variables end def shipped_email @@ -50,7 +50,7 @@ def shipped_email end def store_owner_notification_email - order_vaiables + order_variables end def return_authorization_email @@ -61,7 +61,7 @@ def reimbursement_email reimbursement_email_variables end - def order_vaiables + def order_variables { 'username' => @resource.name, 'user_email' => @resource.email, diff --git a/app/models/spree_dynamic_emails/admin/main_menu/dynamic_email_builder.rb b/app/models/spree_dynamic_emails/admin/main_menu/dynamic_email_builder.rb new file mode 100644 index 0000000..265764b --- /dev/null +++ b/app/models/spree_dynamic_emails/admin/main_menu/dynamic_email_builder.rb @@ -0,0 +1,27 @@ +module SpreeDynamicEmails + module Admin + module MainMenu + class DynamicEmailBuilder + include ::Spree::Core::Engine.routes.url_helpers + + def build + items = [ + ::Spree::Admin::MainMenu::ItemBuilder.new("template.email_templates", ::Spree::Core::Engine.routes.url_helpers.admin_email_templates_path). + with_manage_ability_check(::Spree::EmailTemplate). + with_match_path('/email_templates'). + build, + ::Spree::Admin::MainMenu::ItemBuilder.new("campaign.campaign", ::Spree::Core::Engine.routes.url_helpers.admin_campaigns_path). + with_manage_ability_check(::Spree::Campaign). + with_match_path('/campaigns'). + build + ] + + ::Spree::Admin::MainMenu::SectionBuilder.new('emails', 'envelope.svg'). + with_manage_ability_check(::Spree::Emails). + with_items(items). + build + end + end + end + end +end diff --git a/app/models/spree_dynamic_emails/configuration.rb b/app/models/spree_dynamic_emails/configuration.rb index 3d1aa67..51eb3a7 100644 --- a/app/models/spree_dynamic_emails/configuration.rb +++ b/app/models/spree_dynamic_emails/configuration.rb @@ -1,5 +1,5 @@ module SpreeDynamicEmails - class Configuration < Spree::Preferences::Configuration + class Configuration < ::Spree::Preferences::Configuration # Some example preferences are shown below, for more information visit: # https://dev-docs.spreecommerce.org/internals/preferences diff --git a/app/views/spree/admin/email_templates/_form.html.erb b/app/views/spree/admin/email_templates/_form.html.erb index 56533ef..2bc80f1 100644 --- a/app/views/spree/admin/email_templates/_form.html.erb +++ b/app/views/spree/admin/email_templates/_form.html.erb @@ -20,7 +20,7 @@ <%= f.error_message_on :description %> <% end %> -
+
<%= f.field_container :active, class: ["form-group"], "data-hook" => "subject" do %> <%= f.label :active, Spree.t(:active, scope: :template) %> <%= f.check_box :active, class: 'form-control' %> diff --git a/app/views/spree/admin/shared/_main_menu.html.erb b/app/views/spree/admin/shared/_main_menu.html.erb deleted file mode 100644 index fc079e3..0000000 --- a/app/views/spree/admin/shared/_main_menu.html.erb +++ /dev/null @@ -1,99 +0,0 @@ - diff --git a/app/views/spree/admin/shared/sub_menu/_template.html.erb b/app/views/spree/admin/shared/sub_menu/_template.html.erb deleted file mode 100644 index ea20d2e..0000000 --- a/app/views/spree/admin/shared/sub_menu/_template.html.erb +++ /dev/null @@ -1,4 +0,0 @@ - diff --git a/config/initializers/dynamic_email.rb b/config/initializers/dynamic_email.rb new file mode 100644 index 0000000..a8bf574 --- /dev/null +++ b/config/initializers/dynamic_email.rb @@ -0,0 +1,5 @@ +Rails.application.config.after_initialize do + if Spree::Core::Engine.backend_available? + Rails.application.config.spree_backend.main_menu.insert_after('settings', ::SpreeDynamicEmails::Admin::MainMenu::DynamicEmailBuilder.new.build) + end +end diff --git a/lib/spree_dynamic_emails.rb b/lib/spree_dynamic_emails.rb index 0e89222..6b77cb9 100644 --- a/lib/spree_dynamic_emails.rb +++ b/lib/spree_dynamic_emails.rb @@ -2,3 +2,4 @@ require 'spree_extension' require 'spree_dynamic_emails/engine' require 'spree_dynamic_emails/version' +require 'liquid' diff --git a/lib/spree_dynamic_emails/engine.rb b/lib/spree_dynamic_emails/engine.rb index d2f59e3..308efdd 100644 --- a/lib/spree_dynamic_emails/engine.rb +++ b/lib/spree_dynamic_emails/engine.rb @@ -9,7 +9,7 @@ class Engine < Rails::Engine g.test_framework :rspec end - initializer 'spree_dynamic_emails.environment', before: :load_config_initializers do |_app| + config.after_initialize do |_app| SpreeDynamicEmails::Config = SpreeDynamicEmails::Configuration.new end diff --git a/spree_dynamic_emails.gemspec b/spree_dynamic_emails.gemspec index 20b4eb7..5a1bcd5 100644 --- a/spree_dynamic_emails.gemspec +++ b/spree_dynamic_emails.gemspec @@ -23,6 +23,7 @@ Gem::Specification.new do |s| s.add_dependency 'spree', '>= 4.6.0' s.add_dependency 'spree_extension' + s.add_dependency 'liquid' s.add_development_dependency 'spree_dev_tools' end