Skip to content

Commit

Permalink
Make it compatible with spree 4.8
Browse files Browse the repository at this point in the history
  • Loading branch information
mohit-bluebash committed Dec 5, 2024
1 parent d722d07 commit 1177f9e
Show file tree
Hide file tree
Showing 21 changed files with 212 additions and 267 deletions.
5 changes: 3 additions & 2 deletions app/controllers/spree/admin/campaigns_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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] =
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/jobs/email_send_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
43 changes: 0 additions & 43 deletions app/mailers/spree/base_mailer_decorator.rb

This file was deleted.

57 changes: 0 additions & 57 deletions app/mailers/spree/order_mailer.rb

This file was deleted.

24 changes: 0 additions & 24 deletions app/mailers/spree/reimbursement_mailer.rb

This file was deleted.

28 changes: 0 additions & 28 deletions app/mailers/spree/shipment_mailer.rb

This file was deleted.

45 changes: 45 additions & 0 deletions app/mailers/spree_dynamic_emails/spree/base_mailer_decorator.rb
Original file line number Diff line number Diff line change
@@ -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
61 changes: 61 additions & 0 deletions app/mailers/spree_dynamic_emails/spree/order_mailer_decorator.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion app/models/spree/campaign.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions app/models/spree/email_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ def record
private

def confirm_email
order_vaiables
order_variables
end

def cancel_email
order_vaiables
order_variables
end

def shipped_email
shipment_variables
end

def store_owner_notification_email
order_vaiables
order_variables
end

def return_authorization_email
Expand All @@ -61,7 +61,7 @@ def reimbursement_email
reimbursement_email_variables
end

def order_vaiables
def order_variables
{
'username' => @resource.name,
'user_email' => @resource.email,
Expand Down
Loading

0 comments on commit 1177f9e

Please sign in to comment.