-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow to choose notification settings when attachment added (#627)
* fix: Override Create attachment admin command * fix: Force email notification on attachment event * fix: Override attachment form * feat: Allow admins to toggle notification sending * refactor: Remove override and add extend * lint: Fix rubocop offenses --------- Co-authored-by: Lucie Grau <[email protected]>
- Loading branch information
1 parent
337c338
commit 1fe41e8
Showing
7 changed files
with
227 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
module Admin | ||
# A form object used to create attachments in a participatory process. | ||
# | ||
class AttachmentForm < Form | ||
include TranslatableAttributes | ||
|
||
attribute :file | ||
translatable_attribute :title, String | ||
translatable_attribute :description, String | ||
attribute :weight, Integer, default: 0 | ||
attribute :attachment_collection_id, Integer | ||
attribute :send_notification_to_followers, Boolean, default: false | ||
|
||
mimic :attachment | ||
|
||
validates :file, presence: true, unless: :persisted? | ||
validates :file, passthru: { to: Decidim::Attachment } | ||
validates :title, :description, translatable_presence: true | ||
validates :attachment_collection, presence: true, if: ->(form) { form.attachment_collection_id.present? } | ||
validates :attachment_collection_id, inclusion: { in: :attachment_collection_ids }, allow_blank: true | ||
|
||
delegate :attached_to, to: :context, prefix: false | ||
|
||
alias organization current_organization | ||
|
||
def attachment_collections | ||
@attachment_collections ||= attached_to.attachment_collections | ||
end | ||
|
||
def attachment_collection | ||
@attachment_collection ||= attachment_collections.find_by(id: attachment_collection_id) | ||
end | ||
|
||
private | ||
|
||
def attachment_collection_ids | ||
@attachment_collection_ids ||= attachment_collections.pluck(:id) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<div class="card"> | ||
<div class="card-divider"> | ||
<h2 class="card-title"> | ||
<%= title %> | ||
</h2> | ||
</div> | ||
|
||
<div class="card-section"> | ||
<div class="row column"> | ||
<%= form.translated :text_field, :title, autofocus: true %> | ||
</div> | ||
|
||
<div class="row column"> | ||
<%= form.number_field :weight %> | ||
</div> | ||
|
||
<div class="row column"> | ||
<%= form.translated :text_field, :description %> | ||
</div> | ||
|
||
<div class="row column"> | ||
<%= form.select :attachment_collection_id, @form.attachment_collections.collect { |c| [translated_attribute(c.name), c.id] }, include_blank: true %> | ||
</div> | ||
|
||
<div class="row column"> | ||
<%= form.upload :file, required: true %> | ||
</div> | ||
|
||
<div class="row column"> | ||
<%= form.check_box :send_notification_to_followers, label: t(".send_notification_to_followers") %> | ||
</div> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
lib/extends/commands/decidim/admin/create_attachment_extends.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_support/concern" | ||
|
||
module CreateAttachmentExtends | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
def notify_followers | ||
return unless @attachment.attached_to.is_a?(Decidim::Followable) | ||
return unless form.send_notification_to_followers | ||
|
||
Decidim::EventsManager.publish( | ||
event: "decidim.events.attachments.attachment_created", | ||
event_class: Decidim::AttachmentCreatedEvent, | ||
resource: @attachment, | ||
followers: @attachment.attached_to.followers, | ||
extra: { force_email: true }, | ||
force_send: true | ||
) | ||
end | ||
end | ||
end | ||
|
||
Decidim::Admin::CreateAttachment.include(CreateAttachmentExtends) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
module Decidim::Admin | ||
describe CreateAttachment do | ||
subject { described_class.call(form, attached_to, user) } | ||
let(:user) { create(:user) } | ||
let(:send_notification) { true } | ||
let(:form) do | ||
instance_double( | ||
AttachmentForm, | ||
title: { | ||
en: "An image", | ||
ca: "Una imatge", | ||
es: "Una imagen" | ||
}, | ||
description: { | ||
en: "A city", | ||
ca: "Una ciutat", | ||
es: "Una ciudad" | ||
}, | ||
file: file, | ||
attachment_collection: nil, | ||
send_notification_to_followers: send_notification, | ||
weight: 0 | ||
) | ||
end | ||
let(:file) { upload_test_file(Decidim::Dev.test_file("city.jpeg", "image/jpeg")) } | ||
let(:attached_to) { create(:participatory_process) } | ||
|
||
describe "when valid" do | ||
before do | ||
allow(form).to receive(:invalid?).and_return(false) | ||
end | ||
|
||
it "broadcasts :ok and creates the component" do | ||
expect do | ||
subject | ||
end.to broadcast(:ok) | ||
|
||
expect(Decidim::Attachment.count).to eq(1) | ||
end | ||
|
||
it "notifies the followers" do | ||
follower = create(:user, organization: attached_to.organization) | ||
create(:follow, followable: attached_to, user: follower) | ||
|
||
expect(Decidim::EventsManager) | ||
.to receive(:publish) | ||
.with( | ||
event: "decidim.events.attachments.attachment_created", | ||
event_class: Decidim::AttachmentCreatedEvent, | ||
resource: kind_of(Decidim::Attachment), | ||
followers: [follower], | ||
extra: { force_email: true }, | ||
force_send: true | ||
) | ||
|
||
subject | ||
end | ||
|
||
context "when send notification option is false" do | ||
let(:send_notification) { false } | ||
|
||
it "does not notify the followers" do | ||
follower = create(:user, organization: attached_to.organization) | ||
create(:follow, followable: attached_to, user: follower) | ||
|
||
expect(Decidim::EventsManager) | ||
.not_to receive(:publish) | ||
|
||
subject | ||
end | ||
end | ||
|
||
it "traces the action", versioning: true do | ||
expect(Decidim.traceability) | ||
.to receive(:perform_action!) | ||
.with(:create, Decidim::Attachment, user) | ||
.and_call_original | ||
|
||
expect { subject }.to change(Decidim::ActionLog, :count) | ||
action_log = Decidim::ActionLog.last | ||
expect(action_log.action).to eq("create") | ||
expect(action_log.version).to be_present | ||
end | ||
end | ||
|
||
describe "when invalid" do | ||
before do | ||
allow(form).to receive(:invalid?).and_return(true) | ||
end | ||
|
||
it "broadcasts invalid" do | ||
expect do | ||
subject | ||
end.to broadcast(:invalid) | ||
|
||
expect(Decidim::Attachment.count).to eq(0) | ||
end | ||
end | ||
end | ||
end |