Skip to content

Commit

Permalink
Create solutions from public view (#71)
Browse files Browse the repository at this point in the history
* Create solutions by users and manage in admin

- Create solutions in public views
- Publish and unpusblish in admin
- Add settings to enable or disable this feature

* Apply Rubocop

* Add missing translations

* Add and fix tests

* Fix solutions test

* Fix CI and solution tests

* Remove simplecov

* Refactor

* Refactor

* Bump module version
  • Loading branch information
laurajaime authored Oct 9, 2024
1 parent 2f3f8c9 commit c6bdbec
Show file tree
Hide file tree
Showing 40 changed files with 1,584 additions and 895 deletions.
16 changes: 1 addition & 15 deletions .github/workflows/challenges_system_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,10 @@ jobs:
bundle exec rails decidim_challenges:install:migrations
RAILS_ENV=test bundle exec rails db:migrate
- name: Setup Code Climate test-reporter
run: |
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
chmod +x ./cc-test-reporter
./cc-test-reporter before-build
- name: Run RSpec
uses: nick-invision/retry@v2
with:
timeout_minutes: 30
max_attempts: 3
retry_on: error
command: CI=1 CHALLENGES_SIMPLECOV=1 bundle exec rake --backtrace -t spec[^spec/system/decidim/challenges]

- name: Publish code coverage
run: |
cp coverage/coverage.xml cobertura.xml
export GIT_BRANCH="${GITHUB_REF/refs\/heads\//}"
./cc-test-reporter after-build -r "$CC_TEST_REPORTER_ID" -t cobertura
env:
CC_TEST_REPORTER_ID: 91564b5d6fef4bea80bbc8e50bf97bfadd95bf460fd7d3f563d0f39a57c16cdc
command: bundle exec rspec --backtrace spec/system/decidim/challenges
16 changes: 1 addition & 15 deletions .github/workflows/problems_system_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,10 @@ jobs:
bundle exec rails decidim_challenges:install:migrations
RAILS_ENV=test bundle exec rails db:migrate
- name: Setup Code Climate test-reporter
run: |
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
chmod +x ./cc-test-reporter
./cc-test-reporter before-build
- name: Run RSpec
uses: nick-invision/retry@v2
with:
timeout_minutes: 30
max_attempts: 3
retry_on: error
command: CI=1 CHALLENGES_SIMPLECOV=1 bundle exec rake --backtrace -t spec[^spec/system/decidim/problems]

- name: Publish code coverage
run: |
cp coverage/coverage.xml cobertura.xml
export GIT_BRANCH="${GITHUB_REF/refs\/heads\//}"
./cc-test-reporter after-build -r "$CC_TEST_REPORTER_ID" -t cobertura
env:
CC_TEST_REPORTER_ID: 91564b5d6fef4bea80bbc8e50bf97bfadd95bf460fd7d3f563d0f39a57c16cdc
command: bundle exec rspec --backtrace spec/system/decidim/problems
16 changes: 1 addition & 15 deletions .github/workflows/sgds_system_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,10 @@ jobs:
bundle exec rails decidim_challenges:install:migrations
RAILS_ENV=test bundle exec rails db:migrate
- name: Setup Code Climate test-reporter
run: |
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
chmod +x ./cc-test-reporter
./cc-test-reporter before-build
- name: Run RSpec
uses: nick-invision/retry@v2
with:
timeout_minutes: 30
max_attempts: 3
retry_on: error
command: CI=1 CHALLENGES_SIMPLECOV=1 bundle exec rake --backtrace -t spec[^spec/system/decidim/sgds]

- name: Publish code coverage
run: |
cp coverage/coverage.xml cobertura.xml
export GIT_BRANCH="${GITHUB_REF/refs\/heads\//}"
./cc-test-reporter after-build -r "$CC_TEST_REPORTER_ID" -t cobertura
env:
CC_TEST_REPORTER_ID: 91564b5d6fef4bea80bbc8e50bf97bfadd95bf460fd7d3f563d0f39a57c16cdc
command: bundle exec rspec --backtrace spec/system/decidim/sdgs
16 changes: 1 addition & 15 deletions .github/workflows/solutions_system_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,10 @@ jobs:
bundle exec rails decidim_challenges:install:migrations
RAILS_ENV=test bundle exec rails db:migrate
- name: Setup Code Climate test-reporter
run: |
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
chmod +x ./cc-test-reporter
./cc-test-reporter before-build
- name: Run RSpec
uses: nick-invision/retry@v2
with:
timeout_minutes: 30
max_attempts: 3
retry_on: error
command: CI=1 CHALLENGES_SIMPLECOV=1 bundle exec rake --backtrace -t spec[^spec/system/decidim/solutions]

- name: Publish code coverage
run: |
cp coverage/coverage.xml cobertura.xml
export GIT_BRANCH="${GITHUB_REF/refs\/heads\//}"
./cc-test-reporter after-build -r "$CC_TEST_REPORTER_ID" -t cobertura
env:
CC_TEST_REPORTER_ID: 91564b5d6fef4bea80bbc8e50bf97bfadd95bf460fd7d3f563d0f39a57c16cdc
command: bundle exec rspec --backtrace spec/system/decidim/solutions
16 changes: 1 addition & 15 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,10 @@ jobs:
bundle exec rails decidim_challenges:install:migrations
RAILS_ENV=test bundle exec rails db:migrate
- name: Setup Code Climate test-reporter
run: |
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
chmod +x ./cc-test-reporter
./cc-test-reporter before-build
- name: Run RSpec
uses: nick-invision/retry@v2
with:
timeout_minutes: 30
max_attempts: 3
retry_on: error
command: CI=1 CHALLENGES_SIMPLECOV=1 bundle exec rake --backtrace -t spec['spec\/(?!(system))']

- name: Publish code coverage
run: |
cp coverage/coverage.xml cobertura.xml
export GIT_BRANCH="${GITHUB_REF/refs\/heads\//}"
./cc-test-reporter after-build -r "$CC_TEST_REPORTER_ID" -t cobertura
env:
CC_TEST_REPORTER_ID: 91564b5d6fef4bea80bbc8e50bf97bfadd95bf460fd7d3f563d0f39a57c16cdc
command: bundle exec rspec --exclude-pattern "spec/system/**/*_spec.rb"
1 change: 0 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ require: rubocop-rspec
AllCops:
NewCops: enable
Include:
- .simplecov
- "**/*.rb"
- "**/*.rake"
- "**/*.gemspec"
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ Following Semantic Versioning 2.

## next version:

## Version 0.4.0 (MINOR)
- Create solutions by users in public page and admins can show and manage the users solutions.

## Version 0.3.3 (PATCH)
- Fix update challenge with a card image

Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ GIT
PATH
remote: .
specs:
decidim-challenges (0.3.3)
decidim-challenges (0.4.0)
decidim-core (~> 0.27)

GEM
Expand Down
1 change: 1 addition & 0 deletions app/commands/decidim/solutions/admin/create_solution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def create_solution!
beneficiaries: parsed_attribute(:beneficiaries),
requirements: parsed_attribute(:requirements),
financing_type: parsed_attribute(:financing_type),
author_id: form.author_id,
}

@solution = Decidim.traceability.create!(
Expand Down
67 changes: 67 additions & 0 deletions app/commands/decidim/solutions/create_solution.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# frozen_string_literal: true

module Decidim
module Solutions
# A command with all the business logic when a user creates a new solution.
class CreateSolution < Decidim::Command
include ::Decidim::MultipleAttachmentsMethods
include Decidim::Challenges
include Decidim::CommandUtils

# Public: Initializes the command.
#
# form - A form object with the params.
def initialize(form)
super()
@form = form
end

# Executes the command. Broadcasts these events:
#
# - :ok when everything is valid, together with the solution.
# - :invalid if the form wasn't valid and we couldn't proceed.
#
# Returns nothing.
def call
return broadcast(:invalid) if form.invalid?

transaction do
create_solution!

if process_attachments?
build_attachments
create_attachments
end
end

broadcast(:ok, solution)
end

private

attr_reader :form, :solution, :attachment

def create_solution!
params = {
title: parsed_attribute(:title),
description: parsed_attribute(:description),
component: form.current_component,
decidim_challenges_challenge_id: challenge_id,
project_status: parsed_attribute(:project_status),
project_url: parsed_attribute(:project_url),
coordinating_entity: parsed_attribute(:coordinating_entity),
author_id: form.author_id,
}

@solution = Decidim.traceability.create!(
Decidim::Solutions::Solution,
form.current_user,
params,
visibility: "all"
)

@attached_to = @solution
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def new

def create
enforce_permission_to :create, :solution
@form = form(Decidim::Solutions::Admin::SolutionsForm).from_params(params)
@form = form(Decidim::Solutions::Admin::SolutionsForm).from_params(params, author_id: current_user.id)

Decidim::Solutions::Admin::CreateSolution.call(@form) do
on(:ok) do
Expand All @@ -37,6 +37,11 @@ def create
end
end

def show
enforce_permission_to :show, :solution
@solution = Decidim::Solutions::Solution.find(params[:id])
end

def edit
enforce_permission_to :edit, :solution, solution: solution
@form = form(Decidim::Solutions::Admin::SolutionsForm).from_model(solution)
Expand Down
37 changes: 33 additions & 4 deletions app/controllers/decidim/solutions/solutions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,42 @@ module Solutions
#
class SolutionsController < Decidim::Solutions::ApplicationController
include Decidim::ApplicationHelper
include Decidim::ShowFiltersHelper
include Decidim::Sdgs::SdgsHelper
include FilterResource
include Paginable
include OrderableSolutions
include Decidim::Sdgs::SdgsHelper
include Decidim::ShowFiltersHelper
include FormFactory

helper Decidim::CheckBoxesTreeHelper
helper Decidim::Sdgs::SdgsHelper
helper Decidim::ShowFiltersHelper
helper Decidim::Sdgs::SdgsHelper
helper Decidim::Solutions::ApplicationHelper

helper_method :solutions
helper_method :solutions, :form_presenter

def new
enforce_permission_to :create, :solution
@form = form(Decidim::Solutions::SolutionsForm).instance
end

def create
enforce_permission_to :create, :solution
@form = form(Decidim::Solutions::SolutionsForm).from_params(params,
author_id: current_user.id)

Decidim::Solutions::CreateSolution.call(@form) do
on(:ok) do
flash[:notice] = I18n.t("solutions.create.success", scope: "decidim.solutions")
redirect_to solutions_path
end

on(:invalid) do
flash.now[:alert] = I18n.t("solutions.create.error", scope: "decidim.solutions")
render action: "new"
end
end
end

def index
@solutions = search.result
Expand Down Expand Up @@ -88,6 +113,10 @@ def technological_scope
def search_collection
::Decidim::Solutions::Solution.where(component: current_component).published
end

def form_presenter
@form_presenter ||= present(@form, presenter_class: Decidim::Solutions::SolutionPresenter)
end
end
end
end
2 changes: 2 additions & 0 deletions app/forms/decidim/solutions/admin/solutions_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class SolutionsForm < Decidim::Form

attribute :decidim_problems_problem_id, Integer
attribute :decidim_challenges_challenge_id, Integer
attribute :author_id, Integer
attribute :tags, String
translatable_attribute :objectives, String
translatable_attribute :indicators, String
Expand All @@ -27,6 +28,7 @@ class SolutionsForm < Decidim::Form
validates :title, :description, translatable_presence: true
validates :decidim_challenges_challenge_id, presence: false
validates :decidim_problems_problem_id, presence: false
validates :author_id, presence: true

alias organization current_organization

Expand Down
50 changes: 50 additions & 0 deletions app/forms/decidim/solutions/solutions_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

module Decidim
module Solutions
# A form object to be used when public users want to create a solution.
#
class SolutionsForm < Decidim::Form
include Decidim::AttachmentAttributes
include Decidim::HasUploadValidations

mimic :solution

attribute :title, String
attribute :description, Decidim::Attributes::CleanString
attribute :project_status, String
attribute :project_url, String
attribute :coordinating_entity, String
attribute :decidim_challenges_challenge_id, Integer
attribute :decidim_problems_problem_id, Integer
attribute :author_id, Integer
attribute :attachment, AttachmentForm

attachments_attribute :documents

validates :title, :description, presence: true, etiquette: true
validates :project_status, :coordinating_entity, presence: true
validates :decidim_challenges_challenge_id, :author_id, presence: true
validates :decidim_problems_problem_id, presence: false

def map_model(model)
self.title = translated_attribute(model.title)
self.description = translated_attribute(model.description)

sefl.documents = model.attachments
return unless model.categorization
end

# Finds the Challenge from the given decidim_challenges_challenge_id
#
# Returns a Decidim::Challenges::Challenge
def challenge
@challenge ||= @problem.present? ? Decidim::Challenges::Challenge.find(@problem.challenge.id) : Decidim::Challenges::Challenge.find(@decidim_challenges_challenge_id)
end

def author
Decidim::User.find(author_id)
end
end
end
end
Loading

0 comments on commit c6bdbec

Please sign in to comment.