generated from OpenSourcePolitics/decidim-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: delete article in participatory text imported * feat: modify title of articles * chore: add translations * test: add controller and command test * fix: delete of section doesn't change articles's titles * fix: downgrade chromedriver * fix: update name indentation * refactor: add drafts scope for proposals * refactor: indentation
- Loading branch information
1 parent
707c697
commit 4e481c8
Showing
10 changed files
with
242 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
app/views/decidim/proposals/admin/participatory_texts/_article-preview.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<div class="grid-x"> | ||
<div class="cell"> | ||
<%= form.hidden_field :position, class: "position" %> | ||
<%= form.text_field :title, required: true %> | ||
</div> | ||
</div> | ||
<% if proposal.article? %> | ||
<div class="grid-x"> | ||
<div class="cell"> | ||
<%= form.text_area :body, required: true, rows: 5 %> | ||
</div> | ||
</div> | ||
<% end %> | ||
<%= link_to t(".delete"), discard_participatory_texts_path(proposal_id: proposal.id, level: proposal.participatory_text_level), method: "POST", data: { confirm: t(".are_you_sure") } %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
lib/extends/commands/decidim/proposals/admin/discard_participatory_text_extends.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
module DiscardParticipatoryTextExtends | ||
def initialize(component, proposal_id = nil, locale = "") | ||
@component = component | ||
@proposal_id = proposal_id | ||
@locale = locale | ||
end | ||
|
||
# Executes the command. Broadcasts these events: | ||
# | ||
# - :ok when everything is valid. | ||
# - :invalid if the form wasn't valid and we couldn't proceed. | ||
# | ||
# Returns nothing. | ||
def call | ||
transaction do | ||
if @proposal_id | ||
proposal = Decidim::Proposals::Proposal.find(@proposal_id) | ||
value = proposal.title[@locale].split.last.to_i | ||
is_article = proposal.article? | ||
Decidim::Proposals::Proposal.destroy(@proposal_id) | ||
update_later_proposals_title(@component, value, is_article, @locale) | ||
else | ||
discard_drafts | ||
end | ||
end | ||
|
||
broadcast(:ok) | ||
end | ||
|
||
private | ||
|
||
def update_later_proposals_title(component, value, is_article, locale) | ||
proposals = Decidim::Proposals::Proposal.drafts.where(decidim_component_id: component.id) | ||
.where(participatory_text_level: "article") | ||
.select { |proposal| proposal.title[locale].split.last.to_i > value } | ||
if proposals.any? && is_article | ||
proposals.sort_by(&:id).each_with_index do |proposal, index| | ||
proposal.update(title: { "#{locale}": "#{I18n.t("decidim.proposals.admin.participatory_texts.discard.paragraph")} #{value + index}" }) | ||
end | ||
end | ||
end | ||
end | ||
|
||
Decidim::Proposals::Admin::DiscardParticipatoryText.class_eval do | ||
prepend DiscardParticipatoryTextExtends | ||
end |
31 changes: 31 additions & 0 deletions
31
lib/extends/controllers/decidim/proposals/admin/participatory_texts_controller_extends.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_support/concern" | ||
|
||
module ParticipatoryTextsControllerExtends | ||
extend ActiveSupport::Concern | ||
included do | ||
def discard | ||
enforce_permission_to :manage, :participatory_texts | ||
|
||
if params[:proposal_id] | ||
locale = params[:locale] || "en" | ||
Decidim::Proposals::Admin::DiscardParticipatoryText.call(current_component, params[:proposal_id].to_i, locale) do | ||
on(:ok) do | ||
flash[:notice] = "#{params[:level]} #{I18n.t("decidim.proposals.admin.participatory_texts.discard.delete")}" | ||
redirect_to Decidim::EngineRouter.admin_proxy(current_component).participatory_texts_path | ||
end | ||
end | ||
else | ||
Decidim::Proposals::Admin::DiscardParticipatoryText.call(current_component) do | ||
on(:ok) do | ||
flash[:notice] = I18n.t("participatory_texts.discard.success", scope: "decidim.proposals.admin") | ||
redirect_to Decidim::EngineRouter.admin_proxy(current_component).participatory_texts_path | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
Decidim::Proposals::Admin::ParticipatoryTextsController.include(ParticipatoryTextsControllerExtends) |
19 changes: 19 additions & 0 deletions
19
lib/extends/lib/decidim/proposals/markdown_to_proposals_extends.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module MarkdownToProposalsExtends | ||
def paragraph(text) | ||
return if text.blank? | ||
|
||
create_proposal( | ||
"#{I18n.t("decidim.proposals.markdown_to_proposals.paragraph")} #{@last_position + 1 - @num_sections}", | ||
text, | ||
Decidim::Proposals::ParticipatoryTextSection::LEVELS[:article] | ||
) | ||
|
||
text | ||
end | ||
end | ||
|
||
Decidim::Proposals::MarkdownToProposals.class_eval do | ||
prepend(MarkdownToProposalsExtends) | ||
end |
43 changes: 43 additions & 0 deletions
43
spec/commands/decidim/proposals/admin/discard_participatory_text_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
module Decidim | ||
module Proposals | ||
module Admin | ||
describe DiscardParticipatoryText do | ||
describe "call" do | ||
let(:current_component) do | ||
create( | ||
:proposal_component, | ||
participatory_space: create(:participatory_process) | ||
) | ||
end | ||
let!(:proposals) do | ||
create_list(:proposal, 3, :draft, component: current_component) | ||
end | ||
|
||
describe "when discarding" do | ||
context "when there is no proposal_id in arguments" do | ||
let(:command) { described_class.new(current_component) } | ||
|
||
it "removes all drafts" do | ||
expect { command.call }.to broadcast(:ok) | ||
proposals = Decidim::Proposals::Proposal.drafts.where(component: current_component) | ||
expect(proposals).to be_empty | ||
end | ||
end | ||
|
||
context "when there is proposal_id in arguments" do | ||
it "removes one proposal" do | ||
id = Decidim::Proposals::Proposal.drafts.where(component: current_component).last.id | ||
command = described_class.new(current_component, id, "en") | ||
expect { command.call }.to change(Decidim::Proposals::Proposal, :count).by(-1) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
52 changes: 52 additions & 0 deletions
52
spec/controllers/decidim/proposals/admin/participatory_texts_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
module Decidim | ||
module Proposals | ||
module Admin | ||
describe ParticipatoryTextsController, type: :controller do | ||
routes { Decidim::Proposals::AdminEngine.routes } | ||
|
||
let(:user) { create(:user, :confirmed, :admin, organization: component.organization) } | ||
let(:component) { create :proposal_component, :with_participatory_texts_enabled } | ||
|
||
before do | ||
request.env["decidim.current_organization"] = component.organization | ||
request.env["decidim.current_component"] = component | ||
sign_in user | ||
end | ||
|
||
describe "discarding" do | ||
let!(:proposals) do | ||
create_list(:proposal, 3, :draft, component: component) | ||
end | ||
|
||
context "when there is no proposal_id in params" do | ||
it "suppresses document" do | ||
post :discard, params: { | ||
component_id: component.id, | ||
participatory_process_slug: component.participatory_space.slug | ||
} | ||
expect(response).to redirect_to EngineRouter.admin_proxy(component).participatory_texts_path | ||
expect(flash[:notice]).to eq("All participatory text drafts have been discarded.") | ||
end | ||
end | ||
|
||
context "when there is a proposal_id in params" do | ||
it "supresses one article" do | ||
post :discard, params: { | ||
component_id: component.id, | ||
participatory_process_slug: component.participatory_space.slug, | ||
proposal_id: Decidim::Proposals::Proposal.last.id, | ||
level: "article" | ||
} | ||
expect(response).to redirect_to EngineRouter.admin_proxy(component).participatory_texts_path | ||
expect(flash[:notice]).to eq("article deleted") | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |