Skip to content

Commit

Permalink
Merge pull request #568 from codeforjapan/fix-digest-mailer
Browse files Browse the repository at this point in the history
Decidim::NotificationToMailerPresenter: support organization's locale
  • Loading branch information
ayuki-joto authored Oct 30, 2023
2 parents 1902699 + 2dcafd1 commit 6b73f1c
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
41 changes: 41 additions & 0 deletions app/presenters/decidim/notification_to_mailer_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

module Decidim
#
# Decorator for notifications in mail digest
#
class NotificationToMailerPresenter < SimpleDelegator
include Decidim::TranslatableAttributes

delegate :url_helpers, to: "Decidim::Core::Engine.routes"
delegate :resource_title, to: :event
delegate :resource_url, to: :event
delegate :email_intro, to: :event
delegate :resource_path, to: :event

def date_time
created_at_in_time_zone = created_at.in_time_zone(resource.organization.time_zone)
if frequency == :daily
I18n.l(created_at_in_time_zone, format: :time_of_day)
else
I18n.l(created_at_in_time_zone, format: :decidim_short)
end
end

private

def event
@event ||= event_class.constantize.new(
resource: resource,
user: user,
user_role: user_role,
event_name: event_name,
extra: extra
)
end

def frequency
@frequency ||= user.notifications_sending_frequency
end
end
end
51 changes: 51 additions & 0 deletions spec/presenters/decidim/notification_to_mailer_presenter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

require "rails_helper"

module Decidim
describe NotificationToMailerPresenter, type: :presenter do
subject { described_class.new(notification) }

context "with a daily frequency" do
let(:creating_date) { Time.parse("Wed, 1 Sep 2021 21:00:00 UTC +00:00").in_time_zone }
let(:notification) { create(:notification, created_at: creating_date) }

describe "#date_time" do
it "returns the hour formated" do
allow(subject).to receive(:frequency).and_return(:daily)
expect(subject.date_time).to eq("21:00")
end
end
end

context "with a daily frequency in timezone of Japan" do
let(:creating_date) { Time.zone.parse("Wed, 1 Sep 2021 21:00:00 +00:00").in_time_zone("Asia/Tokyo") }
let(:notification) do
create(:notification, created_at: creating_date) do |notification|
org = notification.resource.organization
org.time_zone = "Asia/Tokyo"
org.save!
end
end

describe "#date_time" do
it "returns the hour formated" do
allow(subject).to receive(:frequency).and_return(:daily)
expect(subject.date_time).to eq("06:00")
end
end
end

context "with a weekly frequency" do
let(:creating_date) { Time.parse("Wed, 1 Sep 2021 21:00:00 UTC +00:00").in_time_zone }
let(:notification) { create(:notification, created_at: creating_date) }

describe "#date_time" do
it "returns the date formated" do
allow(subject).to receive(:frequency).and_return(:weekly)
expect(subject.date_time).to eq("01/09/2021 21:00")
end
end
end
end
end

0 comments on commit 6b73f1c

Please sign in to comment.