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

Create solutions from public view #71

Merged
merged 10 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 @@ -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
36 changes: 32 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,41 @@ 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)

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 +112,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) : false
tramuntanal marked this conversation as resolved.
Show resolved Hide resolved
end

def author
Decidim::User.find(author_id)
end
end
end
end
17 changes: 17 additions & 0 deletions app/helpers/decidim/solutions/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@ module Solutions
# Custom helpers, scoped to the solutions engine.
#
module ApplicationHelper
include Decidim::RichTextEditorHelper

def text_editor_for_description(form)
options = {
class: "js-hashtags",
hashtaggable: true,
value: form_presenter.description(extras: false).strip,
}

text_editor_for(form, :description, options)
end

def participatory_space_challenges
challenge_component = current_participatory_space.components.find_by(manifest_name: "challenges")

::Decidim::Challenges::Challenge.where(decidim_component_id: challenge_component.id)
end
end
end
end
3 changes: 3 additions & 0 deletions app/models/decidim/solutions/solution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ class Solution < Solutions::ApplicationRecord
include Decidim::Traceable
include Decidim::TranslatableAttributes
include Decidim::Randomable
include Decidim::HasAttachments
include Decidim::Publicable

component_manifest_name "solutions"

belongs_to :problem, foreign_key: "decidim_problems_problem_id", class_name: "Decidim::Problems::Problem", optional: true
belongs_to :challenge, foreign_key: "decidim_challenges_challenge_id", class_name: "Decidim::Challenges::Challenge", optional: true
belongs_to :author, class_name: "Decidim::User"

scope :published, -> { where.not(published_at: nil) }

Expand Down
Loading
Loading