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

Allow users to edit proposals after imports #234

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,8 @@ GEM
nio4r (2.5.9)
nokogiri (1.14.5-arm64-darwin)
racc (~> 1.4)
nokogiri (1.14.5-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.14.5-x86_64-linux)
racc (~> 1.4)
oauth (1.1.0)
Expand Down Expand Up @@ -801,6 +803,7 @@ GEM

PLATFORMS
arm64-darwin-22
x86_64-darwin-22
x86_64-linux

DEPENDENCIES
Expand Down
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 @@ -11,6 +11,7 @@ class ConfigForm < Decidim::Form
attribute :allow_images_in_full_editor, Boolean
attribute :allow_images_in_small_editor, Boolean
attribute :allow_images_in_proposals, Boolean
attribute :allow_to_edit_proposals_after_import, Boolean
attribute :use_markdown_editor, Boolean
attribute :allow_images_in_markdown_editor, Boolean
attribute :auto_save_forms, Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def check(status)
def menus
@menus ||= {
editors: config_enabled?([:allow_images_in_full_editor, :allow_images_in_small_editor, :use_markdown_editor, :allow_images_in_markdown_editor]),
proposals: config_enabled?([:allow_images_in_proposals,
proposals: config_enabled?([:allow_images_in_proposals, :allow_to_edit_proposals_after_import,
:validate_title_min_length, :validate_title_max_caps_percent,
:validate_title_max_marks_together, :validate_title_start_with_caps,
:validate_body_min_length, :validate_body_max_caps_percent,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# frozen_string_literal: true

module Decidim
module DecidimAwesome
module Proposals
module ProposalOverride
extend ActiveSupport::Concern

included do
def editable_by?(user)
return true if draft? && created_by?(user)

if allow_to_edit_proposals_after_import.value && copied_from_other_component?
can_edit_copied_component?(user)
else
default_edit_permissions(user)
end
end

private

def can_edit_copied_component?(user)
return false unless within_edit_time_limit? && created_by?(user)

awesome_config_allows_editing?
end

def default_edit_permissions(user)
!published_state? && within_edit_time_limit? && !copied_from_other_component? && created_by?(user)
end

def awesome_config_allows_editing?
return true if allow_to_edit_proposals_after_import.constraints.blank?

allow_to_edit_proposals_after_import.constraints.any? do |constraint|
check_constraint(constraint)
end
end

def allow_to_edit_proposals_after_import
Decidim::DecidimAwesome::AwesomeConfig.find_or_initialize_by(var: :allow_to_edit_proposals_after_import)
end

def check_constraint(constraint)
constraint.settings.all? do |key, value|
case key
when "participatory_space_manifest"
value == participatory_space.manifest.name.to_s
when "participatory_space_slug"
value == participatory_space.slug
when "component_manifest"
value == component.manifest.name.to_s
when "component_id"
value == component.id
else
false
end
end
end
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@
<% end %>
</div>

<div class="row column decidim_awesome-form">
<% if config_enabled? :allow_to_edit_proposals_after_import %>
<p class="text-info"><%= t("edit_proposals_after_import", scope: "decidim.decidim_awesome.admin.config") %></p>

<%= form.check_box :allow_to_edit_proposals_after_import %>
<p class="help-text"><%= t("help.allow_to_edit_proposals_after_import", scope: "decidim.decidim_awesome.admin.config.form") %></p>

<%= render(partial: "decidim/decidim_awesome/admin/config/constraints", locals: { key: :allow_to_edit_proposals_after_import, constraints: constraints_for(:allow_to_edit_proposals_after_import) }) %>
<% end %>
</div>

<% if config_enabled? %i(validate_title_min_length validate_title_max_caps_percent validate_title_max_marks_together validate_title_start_with_caps) %>
</div> <!-- .card-section -->
</div><!-- .card -->
Expand Down Expand Up @@ -60,6 +71,7 @@
<h2 class="card-title"><%= t("validators.body", scope: "decidim.decidim_awesome.admin.config.form") %></h2>
</div>
<div class="card-section">
<div class="row column decidim_awesome-form">
<% if config_enabled? :validate_body_start_with_caps %>
<%= form.check_box :validate_body_start_with_caps %>

Expand Down Expand Up @@ -87,4 +99,5 @@
<%= render(partial: "decidim/decidim_awesome/admin/config/constraints", locals: { key: :validate_body_max_marks_together, constraints: constraints_for(:validate_body_max_marks_together) }) %>
<% end %>
</div>
</div>
<% end %>
5 changes: 5 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ en:
destroy_scoped_style:
error: Error removing CSS box! %{error}
success: CSS box %{key} removed successfully
edit_proposals_after_import: When proposals are imported between components,
not the original or the duplciated can be edited by the owners. This setting
changes that
form:
edit_label: Rename label
errors:
Expand All @@ -227,6 +230,8 @@ en:
editor, available to any user
allow_images_in_small_editor: This will add an image uploader icon in
all the editors WYSIWYG with minimal options in the toolbar enabled.
allow_to_edit_proposals_after_import: Other component settings still
apply (time limit, owner only, etc)
auto_save_forms: This will use LocalStorage to automatically save data
introduced by users in surveys and other forms while they are filling
it. Data will be restored in a future visit with the same browser
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 @@ -43,6 +43,10 @@ module DecidimAwesome
false
end

config_accessor :allow_to_edit_proposals_after_import do
false
end

config_accessor :use_markdown_editor do
false
end
Expand Down
3 changes: 3 additions & 0 deletions lib/decidim/decidim_awesome/checksums.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ decidim-proposals:
decidim-0.27: c0ebeac39ebe4926bf0e5fc585a384d7
decidim-0.27.1: a4f902d1c4829a7f7f62299686f8604e
decidim-0.27.3: a9c9ed5eedaf7bf80afaf9ff5a89c254
/app/models/decidim/proposals/proposal.rb:
decidim-0.26.8: 7aba8a3b12b2fa0214ad6d83f5e6e0ce
decidim-0.27.4: 079dbce3c0d567b1f0feb75fe7f88919
/app/views/decidim/proposals/collaborative_drafts/_edit_form_fields.html.erb:
# this file is not overriden anymore but it needs to be the last version
decidim-0.26: 519a0d34a9ffbd8b9c26d9b68ff4e2a3
Expand Down
3 changes: 3 additions & 0 deletions lib/decidim/decidim_awesome/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class Engine < ::Rails::Engine
Decidim::Proposals::ProposalWizardCreateStepForm.include(Decidim::DecidimAwesome::Proposals::ProposalWizardCreateStepFormOverride)
end

# override Proposal to edit proposals after import
Decidim::Proposals::Proposal.include(Decidim::DecidimAwesome::Proposals::ProposalOverride) if DecidimAwesome.enabled?(:allow_to_edit_proposals_after_import)

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

Expand Down
1 change: 1 addition & 0 deletions lib/decidim/decidim_awesome/test/initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
:allow_images_in_full_editor,
:allow_images_in_small_editor,
:allow_images_in_proposals,
:allow_to_edit_proposals_after_import,
:use_markdown_editor,
:allow_images_in_markdown_editor,
:auto_save_forms,
Expand Down
1 change: 1 addition & 0 deletions spec/awesome_summary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
let!(:allow_images_in_proposals) { create(:awesome_config, organization: organization, var: :allow_images_in_proposals, value: true) }
let!(:allow_images_in_small_editor) { create(:awesome_config, organization: organization, var: :allow_images_in_small_editor, value: true) }
let!(:allow_images_in_full_editor) { create(:awesome_config, organization: organization, var: :allow_images_in_full_editor, value: true) }
let!(:allow_to_edit_proposals_after_import) { create(:awesome_config, organization: organization, var: :allow_to_edit_proposals_after_import, value: true) }
let!(:use_markdown_editor) { create(:awesome_config, organization: organization, var: :use_markdown_editor, value: true) }
let!(:allow_images_in_markdown_editor) { create(:awesome_config, organization: organization, var: :allow_images_in_markdown_editor, value: true) }
let!(:auto_save_forms) { create(:awesome_config, organization: organization, var: :auto_save_forms, value: true) }
Expand Down
1 change: 1 addition & 0 deletions spec/controllers/admin/config_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ module Admin
context "and proposals is disabled" do
let(:disabled) do
editors + [:allow_images_in_proposals,
:allow_to_edit_proposals_after_import,
:validate_title_min_length,
:validate_title_max_caps_percent,
:validate_title_max_marks_together,
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/system_checker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module Decidim::DecidimAwesome
end

it "has 5 modified files in proposals" do
expect(subject.overrides["decidim-proposals"].files.length).to eq(5)
expect(subject.overrides["decidim-proposals"].files.length).to eq(6)
end

context "when file" do
Expand Down
4 changes: 2 additions & 2 deletions spec/system/admin/admin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@
end

context "when some proposals hacks are disabled" do
[:allow_images_in_proposals, :validate_title_min_length, :validate_title_max_caps_percent, :validate_title_max_marks_together, :validate_title_start_with_caps, :validate_body_min_length, :validate_body_max_caps_percent, :validate_body_max_marks_together, :validate_body_start_with_caps].each do |var|
[:allow_images_in_proposals, :allow_to_edit_proposals_after_import, :validate_title_min_length, :validate_title_max_caps_percent, :validate_title_max_marks_together, :validate_title_start_with_caps, :validate_body_min_length, :validate_body_max_caps_percent, :validate_body_max_marks_together, :validate_body_start_with_caps].each do |var|
let(:disabled_features) { [var] }

it_behaves_like "has menu link", "proposals"
end
end

context "when all proposals hacks are disabled" do
let(:disabled_features) { [:allow_images_in_proposals, :validate_title_min_length, :validate_title_max_caps_percent, :validate_title_max_marks_together, :validate_title_start_with_caps, :validate_body_min_length, :validate_body_max_caps_percent, :validate_body_max_marks_together, :validate_body_start_with_caps] }
let(:disabled_features) { [:allow_images_in_proposals, :allow_to_edit_proposals_after_import, :validate_title_min_length, :validate_title_max_caps_percent, :validate_title_max_marks_together, :validate_title_start_with_caps, :validate_body_min_length, :validate_body_max_caps_percent, :validate_body_max_marks_together, :validate_body_start_with_caps] }

it_behaves_like "do not have menu link", "proposals"
end
Expand Down
92 changes: 92 additions & 0 deletions spec/system/edit_proposal_after_import_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# frozen_string_literal: true

require "spec_helper"

describe "Edit proposals after import", type: :system do
include_context "with a component"
let(:organization) { create :organization }
let(:user) { create :user, :confirmed, organization: organization }
let(:manifest_name) { "proposals" }
let!(:component) { create(:proposal_component, manifest: manifest, participatory_space: participatory_process) }
let!(:proposal) { create(:proposal, users: [user], component: component) }
let(:proposal_title) { translated(proposal.title) }
let!(:allow_to_edit_proposals_after_import) { create(:awesome_config, organization: organization, var: :allow_to_edit_proposals_after_import, value: edit_proposal) }
let(:copied_component) { create(:proposal_component, manifest: manifest, participatory_space: participatory_process) }
let!(:copied_proposal) { create :proposal, component: copied_component, users: [user] }

context "when editing proposal after import is enabled" do
let(:edit_proposal) { true }

before do
visit_proposal(user)
end

context "when constrains are not present" do
it "allows editing the proposal" do
click_link proposal_title
expect(page).to have_content("EDIT PROPOSAL")
end
end

context "when constrains are present" do
let!(:constraint) { create(:config_constraint, awesome_config: allow_to_edit_proposals_after_import, settings: settings) }

context "when participatory space is not the same" do
let(:settings) do
{ "participatory_space_manifest" => "assemblies" }
end

it "does not allows editing the proposal" do
click_link proposal_title
expect(page).not_to have_content("EDIT PROPOSAL")
end
end

context "when participatory space is the same" do
let(:settings) do
{ "participatory_space_manifest" => "participatory_processes" }
end

it "allows editing the proposal" do
click_link proposal_title
expect(page).to have_content("EDIT PROPOSAL")
end
end
end
end

context "when editing a proposal after import is disabled" do
let(:edit_proposal) { false }

before do
visit_proposal(user)
end

it "does not allow editing the proposal" do
click_link proposal_title
expect(page).not_to have_content("EDIT PROPOSAL")
end
end

context "when editing a proposal after import by another user" do
let(:another_user) { create :user, :confirmed, organization: organization }
let(:edit_proposal) { true }

before do
visit_proposal(another_user)
end

it "does not allow editing the proposal" do
click_link proposal_title
expect(page).not_to have_content("EDIT PROPOSAL")
end
end

private

def visit_proposal(user)
login_as user, scope: :user
copied_proposal.link_resources([proposal], "copied_from_component")
visit_component
end
end
Loading