Skip to content

Commit

Permalink
Fix document import (#9)
Browse files Browse the repository at this point in the history
* 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
Stef-Rousset authored Aug 26, 2024
1 parent 707c697 commit 4e481c8
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/ci_cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ env:
SIMPLECOV: "true"
RSPEC_FORMAT: "documentation"
RUBY_VERSION: 3.0.6
CHROME_VERSION: 126.0.6478.182
RAILS_ENV: test
NODE_VERSION: 16.9.1
RUBYOPT: '-W:no-deprecated'
Expand Down Expand Up @@ -87,6 +88,11 @@ jobs:
with:
ruby-version: ${{ env.RUBY_VERSION }}
bundler-cache: true
- run: |
wget --no-verbose -O /tmp/chrome.deb https://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_${{env.CHROME_VERSION}}-1_amd64.deb
sudo dpkg -i /tmp/chrome.deb
rm /tmp/chrome.deb
name: Install Chrome version ${{ env.CHROME_VERSION }}
- uses: actions/setup-node@v3
with:
node-version: ${{ env.NODE_VERSION }}
Expand All @@ -105,6 +111,8 @@ jobs:
- run: mkdir -p ./spec/tmp/screenshots
name: Create the screenshots folder
- uses: nanasess/setup-chromedriver@v2
with:
chromedriver-version: ${{ env.CHROME_VERSION }}
- run: bundle exec rake "test:run[exclude, spec/system/**/*_spec.rb, ${{ matrix.slice }}]"
name: RSpec
#- run: ./.github/upload_coverage.sh decidim-app $GITHUB_EVENT_PATH
Expand Down Expand Up @@ -171,6 +179,8 @@ jobs:
- run: mkdir -p ./spec/tmp/screenshots
name: Create the screenshots folder
- uses: nanasess/setup-chromedriver@v2
with:
chromedriver-version: ${{ env.CHROME_VERSION }}
- run: bundle exec rake "test:run[include, spec/system/**/*_spec.rb, ${{ matrix.slice }}]"
name: RSpec
#- run: ./.github/upload_coverage.sh decidim-app $GITHUB_EVENT_PATH
Expand Down
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") } %>
3 changes: 3 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class Application < Rails::Application
require "extends/commands/decidim/budgets/admin/import_proposals_to_budgets_extends"
require "extends/controllers/decidim/newsletters_controller_extends"
require "extends/controllers/decidim/proposals/proposals_controller_extends"
require "extends/controllers/decidim/proposals/admin/participatory_texts_controller_extends"
require "extends/lib/decidim/proposals/markdown_to_proposals_extends"
require "extends/commands/decidim/proposals/admin/discard_participatory_text_extends"

Decidim::GraphiQL::Rails.config.tap do |config|
config.initial_query = "{\n deployment {\n version\n branch\n remote\n upToDate\n currentCommit\n latestCommit\n locallyModified\n }\n}".html_safe
Expand Down
11 changes: 11 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ en:
show:
local_area: Organization area
proposals:
admin:
participatory_texts:
article-preview:
are_you_sure: Are you sure ?
delete: Delete
discard:
delete: deleted
paragraph: paragraph
success: All participatory text drafts have been discarded.
collaborative_drafts:
new:
add_file: Add file
Expand All @@ -130,6 +139,8 @@ en:
collaborative_drafts_list: Collaborative drafts list
new_proposal: New proposal
view_proposal: View proposal
markdown_to_proposals:
paragraph: paragraph
scopes:
global: Global
picker:
Expand Down
11 changes: 11 additions & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ fr:
show:
local_area: Espace d'organisation
proposals:
admin:
participatory_texts:
article-preview:
are_you_sure: Êtes-vous sûr ?
delete: Supprimer
discard:
delete: supprimé
paragraph: paragraphe
success: Tous les brouillons de texte participatifs ont été rejetés.
collaborative_drafts:
new:
add_file: Ajouter le fichier
Expand All @@ -132,6 +141,8 @@ fr:
collaborative_drafts_list: Accéder aux brouillons collaboratifs
new_proposal: Nouvelle proposition
view_proposal: Voir la proposition
markdown_to_proposals:
paragraph: paragraphe
scopes:
global: Portée générale
picker:
Expand Down
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
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 lib/extends/lib/decidim/proposals/markdown_to_proposals_extends.rb
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
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
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

0 comments on commit 4e481c8

Please sign in to comment.