Skip to content

Commit

Permalink
improve email: add link to the resource (admin) (#32)
Browse files Browse the repository at this point in the history
* improve email: add link to admin of the resource

* change translate

* fix lint

* fix lint

* refactoring: improve the ResourceLocatorPresenter

* move the method to private

* add tests #admin_url

* add: resource without a admin url

* fix test'

* fix detecting admin route name

Co-authored-by: antopalidi <[email protected]>
  • Loading branch information
microstudi and antopalidi authored Sep 21, 2022
1 parent a3e820c commit 34966b1
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 3 deletions.
8 changes: 7 additions & 1 deletion decidim-core/app/mailers/decidim/reported_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ module Decidim
class ReportedMailer < Decidim::ApplicationMailer
helper Decidim::ResourceHelper
helper Decidim::TranslationsHelper
helper Decidim::ComponentPathHelper

helper_method :reported_content_url, :report_url, :manage_moderations_url, :author_profile_url, :reported_content_cell
helper_method :reported_content_url, :report_url, :manage_moderations_url, :author_profile_url,
:reported_content_cell, :resource_admin_url

def report(user, report)
with_user(user) do
Expand Down Expand Up @@ -48,6 +50,10 @@ def report_url
@report_url ||= EngineRouter.admin_proxy(@participatory_space).moderation_report_url(host: @organization.host, moderation_id: @report.moderation.id, id: @report.id)
end

def resource_admin_url
@resource_admin_url ||= Decidim::ResourceLocatorPresenter.new(@reportable).admin_url
end

def manage_moderations_url
@manage_moderations_url ||= EngineRouter.admin_proxy(@participatory_space).moderations_url(host: @organization.host)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,15 @@ def show(options = {})
# Returns a String.
def edit(options = {})
options.merge!(options_for_polymorphic)

admin_route_proxy.send("edit_#{member_route_name}_path", target, options)
end

# Generates and admin url only if the manifest has the property :admin_route_name defined
# this allows to distinct from resources that can be administrated from those that are not
def admin_url(options = {})
admin_member_route("url", options.merge(host: root_resource.organization.host))
end

private

def polymorphic?
Expand Down Expand Up @@ -161,5 +166,26 @@ def route_proxy
def admin_route_proxy
@admin_route_proxy ||= EngineRouter.admin_proxy(component || target)
end

def admin_member_route(route_type, options)
return if manifest_for(target).admin_route_name.blank?

options.merge!(options_for_polymorphic)
admin_route_proxy.send("#{admin_member_route_name}_#{route_type}", target, options)
end

def admin_member_route_name
if polymorphic?
admin_polymorphic_member_route_name
else
manifest_for(target).admin_route_name
end
end

def admin_polymorphic_member_route_name
return unless polymorphic?

resource.map { |record| manifest_for(record).admin_route_name }.join("_")
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
<%= t(".report_html", { url: reported_content_url }) %>
</p>

<br>
<% if resource_admin_url.present? %>
<p><%= link_to t(".admin_resource"), resource_admin_url, target: "_blank" %></p>
<% end %>

<p><b><%= t(".date") %></b></p>
<p><%= l @report.created_at, format: :short %></p>
Expand Down
1 change: 1 addition & 0 deletions decidim-core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,7 @@ en:
report_html: <p>The following <a href="%{url}">content</a> has been hidden automatically.</p>
subject: A resource has been hidden automatically
report:
admin_resource: Admin this resource
authors: Authors
content: Reported content
content_original_language: Content original language
Expand Down
5 changes: 5 additions & 0 deletions decidim-core/lib/decidim/resource_manifest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class ResourceManifest
# When not explicitly set, it will use the model name.
attribute :route_name, String

# The name of the named Rails route to create the url to admin the resource
# If it is not defined, the resource will be considered non-administrable
# and no link will be generated in some places
attribute :admin_route_name, String

# The template to use to render the collection of the resource.
attribute :template, String

Expand Down
14 changes: 14 additions & 0 deletions decidim-core/spec/mailers/reported_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ module Decidim
expect(email_body(mail)).to match(moderation.participatory_space.title["en"])
end

it "includes link to admin this resource" do
expect(email_body(mail)).to have_link(href: Decidim::ResourceLocatorPresenter.new(reportable).admin_url)
end

context "when the reported content is a resource without a admin url " do
let(:meetings_component) { create :component, manifest_name: "meetings" }
let(:meeting) { create :meeting, component: meetings_component }
let(:moderation) { create(:moderation, reportable: meeting, participatory_space: meetings_component.participatory_space, report_count: 1) }

it "doesn't have the admin url" do
expect(email_body(mail)).not_to have_link(href: Decidim::ResourceLocatorPresenter.new(meeting).admin_url)
end
end

it "includes the report's reason" do
expect(email_body(mail)).to match(I18n.t(report.reason, scope: "decidim.shared.flag_modal"))
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ module Decidim

it { is_expected.to start_with("/admin/participatory_processes/my-process/edit") }
end

describe "#admin_url" do
subject { described_class.new(resource).admin_url }

it { is_expected.to be_nil }

context "when admin_route_name is defined" do
before do
allow(resource.resource_manifest).to receive(:admin_route_name).and_return("dummy_resource")
end

it { is_expected.to eq("http://1.lvh.me/admin/participatory_processes/my-process/components/1/manage/dummy_resources/1") }
end
end
end

context "with a polymorphic resource" do
Expand Down Expand Up @@ -104,6 +118,21 @@ module Decidim

it { is_expected.to start_with("/admin/participatory_processes/my-process/components/1/manage/dummy_resources/1/nested_dummy_resources/1/edit") }
end

describe "#admin_url" do
subject { described_class.new([resource, nested_resource]).admin_url }

it { is_expected.to be_nil }

context "when admin_route_name is defined" do
before do
allow(resource.resource_manifest).to receive(:admin_route_name).and_return("dummy_resource")
allow(nested_resource.resource_manifest).to receive(:admin_route_name).and_return("nested_dummy_resource")
end

it { is_expected.to eq("http://1.lvh.me/admin/participatory_processes/my-process/components/1/manage/dummy_resources/1/nested_dummy_resources/1") }
end
end
end

context "with a participatory_space" do
Expand All @@ -118,6 +147,20 @@ module Decidim

it { is_expected.to start_with("/processes/my-process") }
end

describe "#admin_url" do
subject { described_class.new(participatory_process).admin_url }

it { is_expected.to be_nil }

context "when admin_route_name is defined" do
before do
allow(participatory_process.resource_manifest).to receive(:admin_route_name).and_return("participatory_process")
end

it { is_expected.to start_with("http://1.lvh.me/admin/participatory_processes/my-process") }
end
end
end
end
end
1 change: 1 addition & 0 deletions decidim-proposals/lib/decidim/proposals/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
resource.reported_content_cell = "decidim/proposals/reported_content"
resource.actions = %w(endorse vote amend comment vote_comment)
resource.searchable = true
resource.admin_route_name = "proposal"
end

component.register_resource(:collaborative_draft) do |resource|
Expand Down

0 comments on commit 34966b1

Please sign in to comment.