Skip to content

Commit

Permalink
Add attachments to debate (decidim#13521)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexandru Emil Lupu <[email protected]>
  • Loading branch information
antopalidi and alecslupu authored Dec 6, 2024
1 parent f749d39 commit 08d79b6
Show file tree
Hide file tree
Showing 25 changed files with 850 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
require "spec_helper"

describe "User creates debate", type: :system do
let(:attachments) { [] }
let(:form) do
double(
invalid?: false,
title:,
description:,
user_group_id: nil,
taxonomizations:,
add_documents: attachments,
documents: [],
current_user: author,
current_component: component,
current_organization: organization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,27 @@ module Admin
# This command is executed when the user creates a Debate from the admin
# panel.
class CreateDebate < Decidim::Commands::CreateResource
include ::Decidim::MultipleAttachmentsMethods

fetch_form_attributes :taxonomizations, :component, :information_updates, :instructions, :start_time, :end_time, :comments_enabled

def call
return broadcast(:invalid) if invalid?

if process_attachments?
build_attachments
return broadcast(:invalid) if attachments_invalid?
end

perform!
broadcast(:ok, resource)
rescue ActiveRecord::RecordInvalid
add_file_attribute_errors!
broadcast(:invalid)
rescue Decidim::Commands::HookError
broadcast(:invalid)
end

protected

def resource_class = Decidim::Debates::Debate
Expand All @@ -27,6 +46,9 @@ def attributes
end

def run_after_hooks
@attached_to = resource
create_attachments(first_weight: 1) if process_attachments?

Decidim::EventsManager.publish(
event: "decidim.events.debates.debate_created",
event_class: Decidim::Debates::CreateDebateEvent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,27 @@ module Admin
# This command is executed when the user changes a Debate from the admin
# panel.
class UpdateDebate < Decidim::Commands::UpdateResource
include Decidim::MultipleAttachmentsMethods

fetch_form_attributes :taxonomizations, :information_updates, :instructions, :start_time, :end_time, :comments_enabled

def call
return broadcast(:invalid) if invalid?

if process_attachments?
build_attachments
return broadcast(:invalid) if attachments_invalid?
end

perform!
broadcast(:ok, resource)
rescue ActiveRecord::RecordInvalid
add_file_attribute_errors!
broadcast(:invalid)
rescue Decidim::Commands::HookError
broadcast(:invalid)
end

private

def attributes
Expand All @@ -19,6 +38,12 @@ def attributes
description: parsed_description
})
end

def run_after_hooks
@attached_to = resource
document_cleanup!(include_all_attachments: true)
create_attachments(first_weight: 1) if process_attachments?
end
end
end
end
Expand Down
21 changes: 21 additions & 0 deletions decidim-debates/app/commands/decidim/debates/create_debate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,27 @@ module Debates
# This command is executed when the user creates a Debate from the public
# views.
class CreateDebate < Decidim::Commands::CreateResource
include ::Decidim::MultipleAttachmentsMethods

fetch_form_attributes :taxonomizations

def call
return broadcast(:invalid) if invalid?

if process_attachments?
build_attachments
return broadcast(:invalid) if attachments_invalid?
end

perform!
broadcast(:ok, resource)
rescue ActiveRecord::RecordInvalid
add_file_attribute_errors!
broadcast(:invalid)
rescue Decidim::Commands::HookError
broadcast(:invalid)
end

private

def resource_class = Decidim::Debates::Debate
Expand All @@ -20,6 +39,8 @@ def create_resource
end

def run_after_hooks
@attached_to = resource
create_attachments(first_weight: 1) if process_attachments?
send_notification_to_author_followers
send_notification_to_space_followers
follow_debate
Expand Down
25 changes: 25 additions & 0 deletions decidim-debates/app/commands/decidim/debates/update_debate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,27 @@ module Decidim
module Debates
# A command with all the business logic when a user updates a debate.
class UpdateDebate < Decidim::Commands::UpdateResource
include Decidim::MultipleAttachmentsMethods

fetch_form_attributes :taxonomizations

def call
return broadcast(:invalid) if invalid?

if process_attachments?
build_attachments
return broadcast(:invalid) if attachments_invalid?
end

perform!
broadcast(:ok, resource)
rescue ActiveRecord::RecordInvalid
add_file_attribute_errors!
broadcast(:invalid)
rescue Decidim::Commands::HookError
broadcast(:invalid)
end

private

def update_resource
Expand Down Expand Up @@ -34,6 +53,12 @@ def attributes
description: { I18n.locale => parsed_description }
})
end

def run_after_hooks
@attached_to = resource
document_cleanup!(include_all_attachments: true)
create_attachments(first_weight: 1) if process_attachments?
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ def index

def new
enforce_permission_to :create, :debate

@form = form(Decidim::Debates::Admin::DebateForm).instance
@form = form(Decidim::Debates::Admin::DebateForm).from_params(
attachment: form(AttachmentForm).from_params({})
)
end

def create
Expand All @@ -40,7 +41,6 @@ def create

def edit
enforce_permission_to(:update, :debate, debate:)

@form = form(Decidim::Debates::Admin::DebateForm).from_model(debate)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ class DebatesController < Decidim::Debates::ApplicationController
include Paginable
include Flaggable
include Decidim::Debates::Orderable
include Decidim::AttachmentsHelper

helper_method :debates, :debate, :form_presenter, :paginated_debates, :close_debate_form
helper_method :debates, :debate, :form_presenter, :paginated_debates, :close_debate_form, :tab_panel_items
before_action :authenticate_user!, only: [:new, :create]

def new
Expand Down Expand Up @@ -120,6 +121,10 @@ def default_filter_params
with_any_state: %w(open closed)
}
end

def tab_panel_items
@tab_panel_items ||= attachments_tab_panel_items(debate)
end
end
end
end
16 changes: 16 additions & 0 deletions decidim-debates/app/forms/decidim/debates/admin/debate_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module Debates
module Admin
# This class holds a Form to create/update debates from Decidim's admin panel.
class DebateForm < Decidim::Form
include Decidim::HasUploadValidations
include Decidim::AttachmentAttributes
include Decidim::TranslatableAttributes
include Decidim::HasTaxonomyFormAttributes

Expand All @@ -16,19 +18,25 @@ class DebateForm < Decidim::Form
attribute :end_time, Decidim::Attributes::TimeWithZone
attribute :finite, Boolean, default: true
attribute :comments_enabled, Boolean, default: true
attribute :attachment, AttachmentForm

attachments_attribute :documents

validates :title, translatable_presence: true
validates :description, translatable_presence: true
validates :instructions, translatable_presence: true
validates :start_time, presence: { if: :validate_start_time? }, date: { before: :end_time, allow_blank: true, if: :validate_start_time? }
validates :end_time, presence: { if: :validate_end_time? }, date: { after: :start_time, allow_blank: true, if: :validate_end_time? }

validate :notify_missing_attachment_if_errored

def map_model(model)
self.finite = model.start_time.present? && model.end_time.present?
presenter = DebatePresenter.new(model)

self.title = presenter.title(all_locales: title.is_a?(Hash))
self.description = presenter.description(all_locales: description.is_a?(Hash))
self.documents = model.attachments
end

def participatory_space_manifest
Expand All @@ -44,6 +52,14 @@ def validate_end_time?
def validate_start_time?
end_time.present?
end

# This method will add an error to the `add_documents` field only if there is
# any error in any other field. This is needed because when the form has
# an error, the attachment is lost, so we need a way to inform the user of
# this problem.
def notify_missing_attachment_if_errored
errors.add(:add_documents, :needs_to_be_reattached) if errors.any? && add_documents.present?
end
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions decidim-debates/app/forms/decidim/debates/debate_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ module Decidim
module Debates
# This class holds a Form to create/update debates from Decidim's public views.
class DebateForm < Decidim::Form
include Decidim::HasUploadValidations
include Decidim::AttachmentAttributes
include Decidim::TranslatableAttributes
include Decidim::HasTaxonomyFormAttributes

attribute :title, String
attribute :description, String
attribute :user_group_id, Integer
attribute :attachment, AttachmentForm

attachments_attribute :documents

validates :title, presence: true
validates :description, presence: true
Expand All @@ -23,6 +28,7 @@ def map_model(debate)
self.title = debate.title.values.first
self.description = debate.description.values.first
self.user_group_id = debate.decidim_user_group_id
self.documents = debate.attachments
end

def participatory_space_manifest
Expand Down
1 change: 1 addition & 0 deletions decidim-debates/app/models/decidim/debates/debate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Debate < Debates::ApplicationRecord
include Decidim::ScopableResource
include Decidim::Authorable
include Decidim::Reportable
include Decidim::HasAttachments
include Decidim::HasReference
include Decidim::Traceable
include Decidim::Loggable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@
<div class="row column">
<%= form.check_box :comments_enabled, label: t("enabled", scope: "decidim.comments.admin.shared.availability_fields") %>
</div>

<% if component_settings.attachments_allowed? %>
<div class="row column">
<%= form.attachment :documents,
multiple: true,
label: t("decidim.debates.admin.debates.form.add_documents"),
button_label: t("decidim.debates.admin.debates.form.add_documents"),
button_edit_label: t("decidim.debates.admin.debates.form.edit_documents"),
button_class: "button button__sm button__transparent-secondary",
help_i18n_scope: "decidim.forms.file_help.file",
help_text: t("decidim.debates.admin.debates.form.attachment_legend") %>
</div>
<% end %>
</div>
</div>
</div>
Expand Down
13 changes: 13 additions & 0 deletions decidim-debates/app/views/decidim/debates/debates/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,17 @@
<% if @form.id.blank? && Decidim::UserGroups::ManageableUserGroups.for(current_user).verified.any? %>
<%= form.select :user_group_id, Decidim::UserGroups::ManageableUserGroups.for(current_user).verified.map { |g| [g.name, g.id] }, prompt: current_user.name %>
<% end %>

<% if component_settings.attachments_allowed? %>
<div class="row column">
<%= form.attachment :documents,
multiple: true,
label: t("decidim.debates.admin.debates.form.add_documents"),
button_label: t("decidim.debates.admin.debates.form.add_documents"),
button_edit_label: t("decidim.debates.admin.debates.form.edit_documents"),
button_class: "button button__lg button__transparent-secondary w-full",
help_i18n_scope: "decidim.forms.file_help.file",
help_text: t("decidim.debates.admin.debates.form.attachment_legend") %>
</div>
<% end %>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ edit_link(
</div>
</section>

<%= cell "decidim/tab_panels", tab_panel_items %>

<% if debate.closed? || translated_attribute(debate.instructions).present? || translated_attribute(debate.information_updates).present? %>
<section class="layout-main__section">
<%= cell("decidim/announcement", { title: t("debate_conclusions_are", scope: "decidim.debates.debates.show", date: l(debate.closed_at, format: :decidim_short)), body: simple_format(translated_attribute(debate.conclusions)) }, callout_class: "success") if debate.closed? %>
Expand Down
4 changes: 4 additions & 0 deletions decidim-debates/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ en:
settings:
global:
announcement: Announcement
attachments_allowed: Allow attachments
clear_all: Clear all
comments_enabled: Comments enabled
comments_max_length: Comments max length (Leave 0 for default value)
Expand Down Expand Up @@ -70,7 +71,10 @@ en:
title: Edit debate
update: Update debate
form:
add_documents: Add documents
attachment_legend: "(Optional) Add an attachment"
debate_type: Debate type
edit_documents: Edit documents
finite: Finite (With start and end times)
open: Open (No start or end times)
index:
Expand Down
1 change: 1 addition & 0 deletions decidim-debates/lib/decidim/debates/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
settings.attribute :comments_enabled, type: :boolean, default: true
settings.attribute :comments_max_length, type: :integer, required: true
settings.attribute :announcement, type: :text, translated: true, editor: true
settings.attribute :attachments_allowed, type: :boolean, default: false
end

component.settings(:step) do |settings|
Expand Down
Loading

0 comments on commit 08d79b6

Please sign in to comment.