Skip to content
Permalink
Browse files

Fix: ParticipatoryText workflow creates multiple versions (#5399)

* Fix: ParticipatoryText workflow creates multiple versions

* Add CHANGELOG entry
  • Loading branch information...
aitorlb authored and oriolgual committed Oct 8, 2019
1 parent ba0ab08 commit edfa1cfce4b1f14867350005d77292f6b193088b
@@ -36,6 +36,7 @@

**Fixed**:

- **decidim-proposals**: Fix: ParticipatoryText workflow creates multiple versions [#5399](https://github.com/decidim/decidim/pull/5399)
- **decidim-assemblies**: Fix: show the Assemblies button to allow managing nested assemblies [#5386](https://github.com/decidim/decidim/pull/5386)
- **decidim-admin**: Fix: Remove first `:header_snippets` field on organization admin apparence form. [#5352](https://github.com/decidim/decidim/pull/5352)
- **decidim-accountability**, **decidim-core**, **decidim-proposals**, **decidim-dev**: Fix: diffing empty versions of translatable attributes [\#5312](https://github.com/decidim/decidim/pull/5312)
@@ -39,14 +39,37 @@ def call

def publish_drafts
Decidim::Proposals::Proposal.where(component: form.current_component).drafts.find_each do |proposal|
add_failure(proposal) unless proposal.update(published_at: Time.current)
add_failure(proposal) unless publish_proposal(proposal)
end
raise ActiveRecord::Rollback if @failures.any?
end

def add_failure(proposal)
@failures[proposal.id] = proposal.errors.full_messages
end

# This will be the PaperTrail version shown in the version control feature (1 of 1).
# For an attribute to appear in the new version it has to be reset
# and reassigned, as PaperTrail only keeps track of object CHANGES.
def publish_proposal(proposal)
title, body = reset_proposal_title_and_body(proposal)

Decidim.traceability.perform_action!(:create, proposal, form.current_user, visibility: "all") do
proposal.update(title: title, body: body, published_at: Time.current)
end
end

# Reset the attributes to an empty string and return the old values.
def reset_proposal_title_and_body(proposal)
title = proposal.title
body = proposal.body

PaperTrail.request(enabled: false) do
proposal.update_columns(title: "", body: "") # rubocop:disable Rails/SkipsModelValidations
end

[title, body]
end
end
end
end
@@ -35,14 +35,18 @@ def call

attr_reader :form

# Prevents PaperTrail from creating versions while updating participatory text proposals.
# A first version will be created when publishing the Participatory Text.
def update_contents_and_resort_proposals(form)
form.proposals.each do |prop_form|
proposal = Decidim::Proposals::Proposal.where(component: form.current_component).find(prop_form.id)
proposal.set_list_position(prop_form.position) if proposal.position != prop_form.position
proposal.title = prop_form.title
proposal.body = prop_form.body if proposal.participatory_text_level == Decidim::Proposals::ParticipatoryTextSection::LEVELS[:article]

add_failure(proposal) unless proposal.save
PaperTrail.request(enabled: false) do
form.proposals.each do |prop_form|
proposal = Proposal.where(component: form.current_component).find(prop_form.id)
proposal.set_list_position(prop_form.position) if proposal.position != prop_form.position
proposal.title = prop_form.title
proposal.body = prop_form.body if proposal.participatory_text_level == ParticipatoryTextSection::LEVELS[:article]

add_failure(proposal) unless proposal.save
end
end
raise ActiveRecord::Rollback if @failures.any?
end
@@ -126,6 +126,8 @@ def underline(text)

private

# Prevents PaperTrail from creating versions while producing proposals from a document.
# A first version will be created when publishing the Participatory Text.
def create_proposal(title, body, participatory_text_level)
attributes = {
component: @component,
@@ -134,15 +136,17 @@ def create_proposal(title, body, participatory_text_level)
participatory_text_level: participatory_text_level
}

proposal = Decidim::Proposals::ProposalBuilder.create(
attributes: attributes,
author: @component.organization,
action_user: @current_user
)
PaperTrail.request(enabled: false) do
proposal = Decidim::Proposals::ProposalBuilder.create(
attributes: attributes,
author: @component.organization,
action_user: @current_user
)

@last_position = proposal.position
@last_position = proposal.position

proposal
proposal
end
end
end
end
@@ -34,17 +34,28 @@ module Admin
let(:command) { described_class.new(form) }

shared_examples "import participatory_text succeeds" do
let(:proposals) { Proposal.where(component: current_component) }

it "broadcasts ok and creates the proposals" do
levels = Decidim::Proposals::ParticipatoryTextSection::LEVELS
sections = 2
sub_sections = 5
expect { command.call }.to(
broadcast(:ok) &&
change { ParticipatoryText.where(component: current_component).count }.by(1) &&
change { Proposal.where(component: current_component, participatory_text_level: Decidim::Proposals::ParticipatoryTextSection::LEVELS[:section]).count }.by(sections) &&
change { Proposal.where(component: current_component, participatory_text_level: Decidim::Proposals::ParticipatoryTextSection::LEVELS[:sub_section]).count }.by(sub_sections) &&
change { Proposal.where(component: current_component, participatory_text_level: Decidim::Proposals::ParticipatoryTextSection::LEVELS[:article]).count }.by(articles)
change(proposals, :count).by(1) &&
change { proposals.where(participatory_text_level: levels[:section]).count }.by(sections) &&
change { proposals.where(participatory_text_level: levels[:sub_section]).count }.by(sub_sections) &&
change { proposals.where(participatory_text_level: levels[:article]).count }.by(articles)
)
end

it "does not create a version for each proposal", versioning: true do
expect { command.call }.to broadcast(:ok)

proposals.each do |proposal|
expect(proposal.reload.versions.count).to be_zero
end
end
end

describe "when the form is not valid" do
@@ -14,10 +14,11 @@ module Admin
)
end
let(:proposals) do
proposals = create_list(:proposal, 3, component: current_component)
proposals = create_list(:proposal, 3, :draft, component: current_component)
proposals.each_with_index do |proposal, idx|
level = Decidim::Proposals::ParticipatoryTextSection::LEVELS.keys[idx]
proposal.update(participatory_text_level: level)
proposal.versions.destroy_all
end
proposals
end
@@ -38,10 +39,19 @@ module Admin
instance_double(
PreviewParticipatoryTextForm,
current_component: current_component,
current_user: create(:user, organization: current_component.organization),
proposals: proposal_modifications
)
end
let(:command) { described_class.new(form) }
let!(:command) { described_class.new(form) }

it "creates a version for each proposal", versioning: true do
expect { command.call }.to broadcast(:ok)

proposals.each do |proposal|
expect(proposal.reload.versions.count).to eq(1)
end
end

describe "when form modifies proposals" do
context "with valid values" do
@@ -18,6 +18,7 @@ module Admin
proposals.each_with_index do |proposal, idx|
level = Decidim::Proposals::ParticipatoryTextSection::LEVELS.keys[idx]
proposal.update(participatory_text_level: level)
proposal.versions.destroy_all
end
proposals
end
@@ -43,6 +44,14 @@ module Admin
end
let(:command) { described_class.new(form) }

it "does not create a version for each proposal", versioning: true do
expect { command.call }.to broadcast(:ok)

proposals.each do |proposal|
expect(proposal.reload.versions.count).to be_zero
end
end

describe "when form modifies proposals" do
context "with valid values" do
it "persists modifications" do

0 comments on commit edfa1cf

Please sign in to comment.
You can’t perform that action at this time.