Skip to content
Draft
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

module Decidim
module DecidimAwesome
module Comments
module CommentCellAttachments
extend ActiveSupport::Concern

included do
private

alias_method :decidim_original_cache_hash, :cache_hash

def cache_hash
extra_hash = attachments_allowed? ? 1 : 0
"#{decidim_original_cache_hash}#{Decidim.cache_key_separator}#{extra_hash}"
end

def attachments_allowed?
@attachments_allowed ||= attachments_config_from_context
end

def attachments_config_from_context
return false unless Decidim::DecidimAwesome::AwesomeConfig.find_by(var: :allow_attachments_in_comments, organization: current_organization)&.value

if model.respond_to?(:component)
awesome_config_instance.context_from_component(model.component)
elsif model.is_a?(Decidim::Participable)
awesome_config_instance.context_from_participatory_space(model)
else
awesome_config_instance.context_from_request(request)
end

awesome_config_instance.enabled_in_context?(:allow_attachments_in_comments)
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

module Decidim
module DecidimAwesome
module Comments
module CommentCellOverride
extend ActiveSupport::Concern

included do
include Decidim::AttachmentsHelper
include CommentCellAttachments

alias_method :decidim_original_comment_body, :comment_body

def comment_body
return render :body_with_attachments if current_user.present?

decidim_original_comment_body
end

private

def tab_panel_items
attachments_tab_panel_items(model).map do |panel|
panel[:id] = "#{panel[:id]}-comment-#{model.id}"
panel
end
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module Decidim
module DecidimAwesome
module Comments
module CommentFormCellOverride
extend ActiveSupport::Concern

included do
include CommentCellAttachments

alias_method :decidim_original_comment_as_for, :comment_as_for

def comment_as_for(form)
render view: :extended_comment_as, locals: { form: }
end
end
end
end
end
end
4 changes: 4 additions & 0 deletions app/cells/decidim/comments/comment/body_with_attachments.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<%= decidim_original_comment_body %>
</div>
<div>
<%= cell "decidim/tab_panels", tab_panel_items if current_user.present? %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<%= decidim_original_comment_as_for(form) %>

<% if attachments_allowed? %>
<div class="mt-4 mb-4">
<%= form.file_field(:add_documents, multiple:true, direct_upload: true) %>
</div>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# frozen_string_literal: true

module Decidim
module DecidimAwesome
module Comments
module CreateCommentOverride
extend ActiveSupport::Concern

included do
include ::Decidim::MultipleAttachmentsMethods

def call
return broadcast(:invalid) if form.invalid?

if form.add_documents.present? && attachments_allowed?
build_attachments
return broadcast(:invalid) if attachments_invalid?
end

with_events do
create_comment
add_attachments
end

broadcast(:ok, comment)
end

def add_attachments
return if form.add_documents.blank?
return unless attachments_allowed?

@attached_to = @comment

build_attachments
create_attachments
end

def title_for(attachment)
return { I18n.locale => attachment[:title] } if attachment.is_a?(Hash) && attachment.has_key?(:title)

{ I18n.locale => attachment.original_filename }
end

def attachments_allowed?
@attachments_allowed ||= begin
root_commentable = root_commentable(form.commentable)
if root_commentable.respond_to?(:component)
awesome_config_instance.context_from_component(root_commentable.component)
elsif root_commentable.is_a?(Decidim::Participable)
awesome_config_instance.context_from_participatory_space(root_commentable)
end

awesome_config_instance.enabled_in_context?(:allow_attachments_in_comments)
end
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Decidim
module DecidimAwesome
module Comments
module CommentFormOverride
extend ActiveSupport::Concern

included do
include Decidim::AttachmentAttributes

attribute :attachment, AttachmentForm

attachments_attribute :documents
end
end
end
end
end
1 change: 1 addition & 0 deletions app/forms/decidim/decidim_awesome/admin/config_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ConfigForm < Decidim::Form
attribute :allow_images_in_editors, Boolean
attribute :allow_videos_in_editors, Boolean
attribute :allow_images_in_proposals, Boolean
attribute :allow_attachments_in_comments, Boolean
attribute :auto_save_forms, Boolean
attribute :scoped_styles, Hash
attribute :scoped_admin_styles, Hash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@

<% append_javascript_pack_tag "decidim_decidim_awesome", defer: false %>
<% append_javascript_pack_tag("decidim_decidim_awesome_custom_fields") if Decidim::DecidimAwesome.enabled?(:proposal_custom_fields) %>
<% append_javascript_pack_tag("decidim_decidim_awesome_comments") if Decidim::DecidimAwesome.enabled?(:allow_attachments_in_comments) %>
1 change: 1 addition & 0 deletions app/packs/entrypoints/decidim_decidim_awesome_comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "src/decidim/decidim_awesome/comments/attachments"
21 changes: 21 additions & 0 deletions app/packs/src/decidim/decidim_awesome/comments/attachments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const addResetCommentForm = (form) => {
form.addEventListener("submit", (event) => {
event.preventDefault();
Reflect.apply(Rails.handleRemote, event.target, [event]);
event.target.reset();
});
};

document.querySelectorAll(".comment__form-submit button[type='submit']").forEach((item) => addResetCommentForm(item.form));

document.addEventListener("comments:loaded", (event) => {
const commentsIds = event.detail.commentsIds;
if (commentsIds) {
commentsIds.forEach((commentId) => {
const commentsContainer = document.getElementById(`comment_${commentId}`);
if (commentsContainer) {
commentsContainer.querySelectorAll(".comment__form-submit button[type='submit']").forEach((item) => addResetCommentForm(item.form));
}
});
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div class="form__wrapper decidim_awesome-form gap-0">
<% if config_enabled?(:allow_attachments_in_comments) %>
<div class="row column">
<%= form.check_box :allow_attachments_in_comments, help_text: t("help.allow_attachments_in_comments", scope: "decidim.decidim_awesome.admin.config.form") %>
<%= render(partial: "decidim/decidim_awesome/admin/config/constraints", locals: { key: :allow_attachments_in_comments, constraints: constraints_for(:allow_attachments_in_comments) }) %>
</div>
<% end %>
</div>
1 change: 1 addition & 0 deletions config/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
decidim_admin_decidim_awesome: "#{base_path}/app/packs/entrypoints/decidim_admin_decidim_awesome.js",
decidim_admin_decidim_awesome_global: "#{base_path}/app/packs/entrypoints/decidim_admin_decidim_awesome_global.js",
decidim_decidim_awesome_map: "#{base_path}/app/packs/entrypoints/decidim_decidim_awesome_map.js",
decidim_decidim_awesome_comments: "#{base_path}/app/packs/entrypoints/decidim_decidim_awesome_comments.js",
decidim_decidim_awesome_custom_fields: "#{base_path}/app/packs/entrypoints/decidim_decidim_awesome_custom_fields.js",
decidim_decidim_awesome_iframe: "#{base_path}/app/packs/entrypoints/decidim_decidim_awesome_iframe.scss"
)
4 changes: 4 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ en:
attributes:
config:
additional_proposal_sortings: Additional sorting options enabled
allow_attachments_in_comments: Allow attachments in comments
allow_images_in_editors: Allow images in the HTML editor
allow_images_in_proposals: Allow images in the proposals editor
allow_videos_in_editors: Allow videos in the HTML editor
Expand Down Expand Up @@ -301,6 +302,7 @@ en:
incorrect_css: 'CSS in box #%{key} is invalid'
incorrect_json: 'JSON definition in box #%{key} is invalid'
help:
allow_attachments_in_comments: This will allow users to attach files in comments
allow_images_in_editors: This will add an image uploader icon in all
the WYSIWYG editors
allow_images_in_proposals: This will allow to upload images in the proposals
Expand Down Expand Up @@ -427,6 +429,7 @@ en:
system: Everywhere except participatory spaces
title:
admins: Scoped Admins
comments: Comments Hacks
editors: Editor Hacks
livechat: Live Chat
proposal_custom_fields: 'Proposals Custom Fields: Public fields'
Expand Down Expand Up @@ -532,6 +535,7 @@ en:
time_ago: "%{time} ago"
menu:
admins: Scoped Admins
comments: Comments Hacks
custom_redirects: Custom Redirections
editors: Editor Hacks
livechat: Live Chat
Expand Down
4 changes: 4 additions & 0 deletions lib/decidim/decidim_awesome/awesome.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ module DecidimAwesome
false
end

config_accessor :allow_attachments_in_comments do
false
end

# used to save forms in localstorage
config_accessor :auto_save_forms do
true
Expand Down
8 changes: 8 additions & 0 deletions lib/decidim/decidim_awesome/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ class Engine < ::Rails::Engine
Decidim::Proposals::ProposalLCell.include(Decidim::DecidimAwesome::ProposalLCellOverride)
end

if DecidimAwesome.enabled?(:allow_attachments_in_comments)
Decidim::Comments::Comment.include(Decidim::HasAttachments)
Decidim::Comments::CommentForm.include(Decidim::DecidimAwesome::Comments::CommentFormOverride)
Decidim::Comments::CreateComment.include(Decidim::DecidimAwesome::Comments::CreateCommentOverride)
Decidim::Comments::CommentFormCell.include(Decidim::DecidimAwesome::Comments::CommentFormCellOverride)
Decidim::Comments::CommentCell.include(Decidim::DecidimAwesome::Comments::CommentCellOverride)
end

# override user's admin property
Decidim::User.include(Decidim::DecidimAwesome::UserOverride) if DecidimAwesome.enabled?(:scoped_admins)

Expand Down
26 changes: 17 additions & 9 deletions lib/decidim/decidim_awesome/menu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,32 @@ def register_awesome_admin_menu!
icon_name: "documents",
if: menus[:proposals]

menu.add_item :comments,
I18n.t("menu.comments", scope: "decidim.decidim_awesome.admin"),
decidim_admin_decidim_awesome.config_path(:comments),
position: 3,
icon_name: "chat-1-line",
if: menus[:comments]

menu.add_item :surveys,
I18n.t("menu.surveys", scope: "decidim.decidim_awesome.admin"),
decidim_admin_decidim_awesome.config_path(:surveys),
position: 3,
position: 4,
icon_name: "surveys",
if: menus[:surveys]

menu.add_item :styles,
I18n.t("menu.styles", scope: "decidim.decidim_awesome.admin"),
decidim_admin_decidim_awesome.config_path(:scoped_styles),
position: 4,
position: 5,
icon_name: "brush",
if: menus[:styles],
submenu: { target_menu: :custom_styles_submenu }

menu.add_item :custom_fields,
I18n.t("menu.proposal_custom_fields", scope: "decidim.decidim_awesome.admin"),
decidim_admin_decidim_awesome.config_path(menus[:proposal_custom_fields] ? :proposal_custom_fields : :proposal_private_custom_fields),
position: 5,
position: 6,
icon_name: "layers",
active: is_active_link?(decidim_admin_decidim_awesome.config_path(:proposal_custom_fields)) ||
is_active_link?(decidim_admin_decidim_awesome.config_path(:proposal_private_custom_fields)),
Expand All @@ -48,14 +55,14 @@ def register_awesome_admin_menu!
menu.add_item :admins,
I18n.t("menu.admins", scope: "decidim.decidim_awesome.admin"),
decidim_admin_decidim_awesome.config_path(:admins),
position: 6,
position: 7,
icon_name: "group-line",
if: menus[:admins]

menu.add_item :menu_hacks,
I18n.t("menu.menu_hacks", scope: "decidim.decidim_awesome.admin"),
decidim_admin_decidim_awesome.menu_hacks_path(menus[:menu_hacks_menu] ? :menu : :home_content_block_menu),
position: 7,
position: 8,
icon_name: "menu-line",
if: menus[:menu_hacks],
active: is_active_link?(decidim_admin_decidim_awesome.menu_hacks_path(:menu)) ||
Expand All @@ -65,28 +72,28 @@ def register_awesome_admin_menu!
menu.add_item :custom_redirects,
I18n.t("menu.custom_redirects", scope: "decidim.decidim_awesome.admin"),
decidim_admin_decidim_awesome.custom_redirects_path,
position: 8,
position: 9,
icon_name: "external-link-line",
if: menus[:custom_redirects]

menu.add_item :livechat,
I18n.t("menu.livechat", scope: "decidim.decidim_awesome.admin"),
decidim_admin_decidim_awesome.config_path(:livechat),
position: 9,
position: 10,
icon_name: "chat-1-line",
if: menus[:livechat]

menu.add_item :verifications,
I18n.t("menu.verifications", scope: "decidim.decidim_awesome.admin"),
decidim_admin_decidim_awesome.config_path(:verifications),
position: 10,
position: 11,
icon_name: "fingerprint-line",
if: menus[:verifications]

menu.add_item :maintenance,
I18n.t("maintenance", scope: "decidim.decidim_awesome.admin.menu.maintenance"),
decidim_admin_decidim_awesome.maintenance_path(:private_data),
position: 11,
position: 12,
icon_name: "tools-line",
active: is_active_link?(decidim_admin_decidim_awesome.maintenance_path(:private_data)) ||
is_active_link?(decidim_admin_decidim_awesome.checks_maintenance_index_path),
Expand Down Expand Up @@ -174,6 +181,7 @@ def menus
:validate_body_min_length, :validate_body_max_caps_percent,
:validate_body_max_marks_together, :validate_body_start_with_caps
),
comments: config_enabled?(:allow_attachments_in_comments),
surveys: config_enabled?(:auto_save_forms, :user_timezone),
styles: config_enabled?(:scoped_styles, :scoped_admin_styles),
scoped_styles: config_enabled?(:scoped_styles),
Expand Down
Loading