Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adaptation to other proposals modules #18

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def initialize(form, current_user, proposal)
@selected_user_group = Decidim::UserGroup.find_by(organization: organization, id: form.user_group_id)
@proposal = proposal
@attached_to = proposal
@is_anonymous = allow_anonymous_proposals? && (current_user.blank? || proposal.authored_by?(anonymous_group))
@editable = allow_anonymous_proposals? && proposal.authored_by?(anonymous_group) || proposal.editable_by?(current_user)
@is_anonymous = allow_anonymous_proposals? && (current_user.blank? || (proposal.published? ? proposal.authored_by?(anonymous_group) : @selected_user_group == anonymous_group))
set_current_user(current_user)
end

Expand All @@ -22,6 +23,10 @@ def initialize(form, current_user, proposal)
def component
@component ||= form.current_component
end

def invalid?
!@editable || form.invalid? || proposal_limit_reached?
end
end
end
end
23 changes: 14 additions & 9 deletions app/helpers/decidim/anonymous_proposals/user_group_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,23 @@ module UserGroupHelper
# Returns nothing.
def user_group_with_anonymous_select_field(form, name, options = {})
return unless user_signed_in?
return user_group_select_field(form, name, options) unless allow_anonymous_proposals?

user_groups = Decidim::UserGroups::ManageableUserGroups.for(current_user).verified
anonymous_group_extension = anonymous_group.present? ? [[t("anonymous_user", scope: "decidim.proposals.proposals.new"), anonymous_group.id]] : []

form.select(
name,
user_groups.map { |g| [g.name, g.id] } + anonymous_group_extension,
selected: @form.user_group_id.presence,
include_blank: current_user.name,
label: options.has_key?(:label) ? options[:label] : true
)
if allow_anonymous_proposals?
anonymous_group_extension = anonymous_group.present? ? [[t("anonymous_user", scope: "decidim.proposals.proposals.new"), anonymous_group.id]] : []
selected_group_id = (@form.user_group_id || options[:select_anonymous] && anonymous_group&.id).presence

form.select(
name,
user_groups.map { |g| [g.name, g.id] } + anonymous_group_extension,
selected: selected_group_id,
include_blank: current_user.name,
label: options.has_key?(:label) ? options[:label] : true
)
else
return user_group_select_field(form, name, options) if current_organization.user_groups_enabled? && user_groups.any?
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

module Decidim
module AnonymousProposals
module CoauthorableOverrides
extend ActiveSupport::Concern

included do
def add_coauthor(author, extra_attributes = {})
return if author.blank? && persisted?
user_group = extra_attributes[:user_group]

if allow_anonymous_proposals? && (author.blank? || user_group == anonymous_group)
author = anonymous_group
user_group = nil
extra_attributes.delete(:user_group)
end

return if coauthorships.exists?(decidim_author_id: author.id, decidim_author_type: author.class.base_class.name) && user_group.blank?
return if user_group && coauthorships.exists?(user_group: user_group)

coauthorship_attributes = extra_attributes.merge(author: author)

if persisted?
coauthorships.create!(coauthorship_attributes)
else
coauthorships.build(coauthorship_attributes)
end

authors << author
end

private

def allow_anonymous_proposals?
component.settings.anonymous_proposals_enabled?
end

def anonymous_group
Decidim::UserGroup.where(organization: organization).anonymous.first
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
<!-- replace "erb[silent]:contains('if current_organization.user_groups_enabled?')" -->

<% if !is_anonymous? && current_organization.user_groups_enabled? && Decidim::UserGroups::ManageableUserGroups.for(current_user).verified.any? %>
<!-- remove "erb[loud]:contains('user_group_select_field form, :user_group_id')" -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!-- replace "erb[silent]:contains('if current_organization.user_groups_enabled?')" -->

<% if current_organization.user_groups_enabled? && Decidim::UserGroups::ManageableUserGroups.for(current_user).verified.any? %>
<% if @proposal.draft? %>
<div class="field">
<%= user_group_with_anonymous_select_field form, :user_group_id, select_anonymous: @proposal.persisted? && is_anonymous? %>
</div>
<% else %>
<% if !is_anonymous? %>
<div class="field">
<%= user_group_select_field form, :user_group_id %>
</div>
<% end %>
<% end %>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!-- insert_before "div.card div.actions" original "ac833c6738922b1b30e2240aa17dd45a1e5cef5e" -->
<!-- insert_before "div.card div.actions" -->

<div class="field">
<%= user_group_with_anonymous_select_field form, :user_group_id %>
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!-- remove "erb[loud]:contains('user_group_select_field form, :user_group_id')" original "ea82cc8a19cf272a5c085df8fcdff3fe65965dca" -->
<!-- remove "erb[loud]:contains('user_group_select_field form, :user_group_id')" -->
4 changes: 4 additions & 0 deletions lib/decidim/anonymous_proposals/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class Engine < ::Rails::Engine
include Decidim::AnonymousProposals::HasAnonymous
end

Decidim::Proposals::Proposal.class_eval do
include Decidim::AnonymousProposals::CoauthorableOverrides
end

Decidim::Proposals::Permissions.class_eval do
prepend Decidim::AnonymousProposals::PermissionsOverrides
end
Expand Down