Skip to content

Commit

Permalink
Add admin log when creating, updating or deleting area types (#9316)
Browse files Browse the repository at this point in the history
* add admin log when creating area type

* add admin log when updating area type

* add admin log when deleting area type
  • Loading branch information
eliegaboriau committed May 25, 2022
1 parent f33421f commit e6b29e0
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 9 deletions.
7 changes: 5 additions & 2 deletions decidim-admin/app/commands/decidim/admin/create_area_type.rb
Expand Up @@ -7,8 +7,9 @@ class CreateAreaType < Decidim::Command
# Public: Initializes the command.
#
# form - A form object with the params.
def initialize(form)
def initialize(form, user)
@form = form
@user = user
end

# Executes the command. Broadcasts these events:
Expand All @@ -29,7 +30,9 @@ def call
attr_reader :form

def create_area_type
AreaType.create!(
Decidim.traceability.create!(
AreaType,
@user,
name: form.name,
organization: form.organization,
plural: form.plural
Expand Down
9 changes: 7 additions & 2 deletions decidim-admin/app/commands/decidim/admin/update_area_type.rb
Expand Up @@ -8,9 +8,10 @@ class UpdateAreaType < Decidim::Command
#
# area_type - The AreaType to update
# form - A form object with the params.
def initialize(area_type, form)
def initialize(area_type, form, user)
@area_type = area_type
@form = form
@user = user
end

# Executes the command. Broadcasts these events:
Expand All @@ -31,7 +32,11 @@ def call
attr_reader :form

def update_area_type
@area_type.update!(attributes)
Decidim.traceability.update!(
@area_type,
@user,
attributes
)
end

def attributes
Expand Down
Expand Up @@ -21,7 +21,7 @@ def create
enforce_permission_to :create, :area_type
@form = form(AreaTypeForm).from_params(params)

CreateAreaType.call(@form) do
CreateAreaType.call(@form, current_user) do
on(:ok) do
flash[:notice] = I18n.t("area_types.create.success", scope: "decidim.admin")
redirect_to area_types_path
Expand All @@ -43,7 +43,7 @@ def update
enforce_permission_to :update, :area_type, area_type: area_type
@form = form(AreaTypeForm).from_params(params)

UpdateAreaType.call(area_type, @form) do
UpdateAreaType.call(area_type, @form, current_user) do
on(:ok) do
flash[:notice] = I18n.t("area_types.update.success", scope: "decidim.admin")
redirect_to area_types_path
Expand All @@ -58,7 +58,10 @@ def update

def destroy
enforce_permission_to :destroy, :area_type, area_type: area_type
area_type.destroy!

Decidim.traceability.perform_action!("delete", area_type, current_user) do
area_type.destroy!
end

flash[:notice] = I18n.t("area_types.destroy.success", scope: "decidim.admin")

Expand Down
Expand Up @@ -4,9 +4,10 @@

module Decidim::Admin
describe CreateAreaType do
subject { described_class.new(form) }
subject { described_class.new(form, user) }

let(:organization) { create :organization }
let(:user) { create(:user, organization: organization) }
let(:name) { Decidim::Faker::Localized.literal("territorial") }
let(:plural) { Decidim::Faker::Localized.literal("territorials") }

Expand Down Expand Up @@ -36,6 +37,18 @@ module Decidim::Admin
it "creates a new scope type for the organization" do
expect { subject.call }.to change { organization.area_types.count }.by(1)
end

it "traces the action", versioning: true do
expect(Decidim.traceability)
.to receive(:perform_action!)
.with(:create, Decidim::AreaType, user, {})
.and_call_original

expect { subject.call }.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
end
end
Expand Up @@ -4,9 +4,10 @@

module Decidim::Admin
describe UpdateAreaType do
subject { described_class.new(area_type, form) }
subject { described_class.new(area_type, form, user) }

let(:organization) { create :organization }
let(:user) { create(:user, organization: organization) }
let(:area_type) { create :area_type, organization: organization }
let(:name) { Decidim::Faker::Localized.literal("new name") }
let(:plural) { Decidim::Faker::Localized.literal("new names") }
Expand Down Expand Up @@ -41,6 +42,18 @@ module Decidim::Admin
it "updates the plural of the scope" do
expect(translated(area_type.plural)).to eq("new names")
end

it "traces the action", versioning: true do
expect(Decidim.traceability)
.to receive(:perform_action!)
.with(:update, area_type, user, {})
.and_call_original

expect { subject.call }.to change(Decidim::ActionLog, :count)
action_log = Decidim::ActionLog.last
expect(action_log.action).to eq("update")
expect(action_log.version).to be_present
end
end
end
end
5 changes: 5 additions & 0 deletions decidim-core/app/models/decidim/area_type.rb
Expand Up @@ -5,6 +5,7 @@ module Decidim
# (terriotrial, sectorial, etc.)
class AreaType < ApplicationRecord
include Decidim::TranslatableResource
include Decidim::Traceable

translatable_fields :name, :plural

Expand All @@ -20,5 +21,9 @@ class AreaType < ApplicationRecord
def translated_name
Decidim::AreaTypePresenter.new(self).translated_name
end

def self.log_presenter_class_for(_log)
Decidim::AdminLog::AreaTypePresenter
end
end
end
@@ -0,0 +1,34 @@
# frozen_string_literal: true

module Decidim
module AdminLog
# This class holds the logic to present a `Decidim::AreaType`
# for the `AdminLog` log.
#
# Usage should be automatic and you shouldn't need to call this class
# directly, but here's an example:
#
# action_log = Decidim::ActionLog.last
# view_helpers # => this comes from the views
# AreaTypePresenter.new(action_log, view_helpers).present
class AreaTypePresenter < Decidim::Log::BasePresenter
private

def diff_fields_mapping
{
name: :i18n,
plural: :i18n
}
end

def action_string
case action
when "create", "delete", "update"
"decidim.admin_log.area_type.#{action}"
else
super
end
end
end
end
end
4 changes: 4 additions & 0 deletions decidim-core/config/locales/en.yml
Expand Up @@ -160,6 +160,10 @@ en:
create: "%{user_name} created the %{resource_name} area"
delete: "%{user_name} deleted the %{resource_name} area"
update: "%{user_name} updated the %{resource_name} area"
area_type:
create: "%{user_name} created the %{resource_name} area type"
delete: "%{user_name} deleted the %{resource_name} area type"
update: "%{user_name} updated the %{resource_name} area type"
attachment_collection:
create: "%{user_name} created the %{resource_name} attachment collection"
delete: "%{user_name} deleted the %{resource_name} attachment collection"
Expand Down

0 comments on commit e6b29e0

Please sign in to comment.