Skip to content

Commit

Permalink
add proposal answer templates (#64)
Browse files Browse the repository at this point in the history
* add proposal answer templates

* fix tests
  • Loading branch information
microstudi authored Apr 4, 2023
1 parent 88c2617 commit 2b078f9
Show file tree
Hide file tree
Showing 33 changed files with 1,016 additions and 3 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"Decidim": false,
"$": false,
"jQuery": false,
"Quill": false,
"L": false
},
"rules": {
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ PATH
decidim-core (>= 0.25.0, < 0.27)
decidim-participatory_processes (>= 0.25.0, < 0.27)
decidim-proposals (>= 0.25.0, < 0.27)
decidim-templates (>= 0.25.0, < 0.27)
deface (>= 1.9)

GEM
Expand Down Expand Up @@ -481,6 +482,7 @@ GEM
mime-types-data (3.2023.0218.1)
mini_magick (4.12.0)
mini_mime (1.1.2)
mini_portile2 (2.8.1)
minitest (5.17.0)
mixlib-cli (2.1.8)
mixlib-config (3.0.27)
Expand All @@ -500,6 +502,9 @@ GEM
net-smtp (0.3.3)
net-protocol
nio4r (2.5.8)
nokogiri (1.13.10)
mini_portile2 (~> 2.8.0)
racc (~> 1.4)
nokogiri (1.13.10-arm64-darwin)
racc (~> 1.4)
nokogiri (1.13.10-x86_64-linux)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

module Decidim
module Templates
# A command with all the business logic when duplicating a proposal's answer template
module Admin
class CopyProposalAnswerTemplate < Rectify::Command
def initialize(template)
@template = template
end

def call
return broadcast(:invalid) unless @template.valid?

Template.transaction do
copy_template
end

broadcast(:ok, @copied_template)
end

def copy_template
@copied_template = Template.create!(
organization: @template.organization,
name: @template.name,
description: @template.description,
target: :proposal_answer,
field_values: @template.field_values,
templatable: @template.templatable
)
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

module Decidim
module Templates
# A command with all the business logic when duplicating a questionnaire template
module Admin
class CopyQuestionnaireTemplate < Rectify::Command
include Decidim::Templates::Admin::QuestionnaireCopier

# Public: Initializes the command.
#
# template - A template we want to duplicate
def initialize(template)
@template = template
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
return broadcast(:invalid) unless @template.valid?

Template.transaction do
copy_template
copy_questionnaire_questions(@template.templatable, @copied_template.templatable)
end

broadcast(:ok, @copied_template)
end

private

attr_reader :form

def copy_template
@copied_template = Template.create!(
organization: @template.organization,
name: @template.name,
description: @template.description,
target: :questionnaire
)
@resource = Decidim::Forms::Questionnaire.create!(
@template.templatable.dup.attributes.merge(
questionnaire_for: @copied_template
)
)

@copied_template.update!(templatable: @resource)
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

module Decidim
module Templates
module Admin
class CreateProposalAnswerTemplate < Rectify::Command
# Initializes the command.
#
# form - The source for this ProposalAnswerTemplate.
def initialize(form)
@form = form
end

def call
return broadcast(:invalid) unless @form.valid?

@template = Decidim.traceability.create!(
Template,
@form.current_user,
name: @form.name,
description: @form.description,
organization: @form.current_organization,
field_values: { internal_state: @form.internal_state },
target: :proposal_answer
)

resource = identify_templateable_resource
@template.update!(templatable: resource)

broadcast(:ok, @template)
end

private

def identify_templateable_resource
resource = @form.scope_for_availability.split("-")
case resource.first
when "organizations"
@form.current_organization
when "components"
component = Decidim::Component.find_by(id: resource.last)
component&.participatory_space&.decidim_organization_id == @form.current_organization.id ? component : nil
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

module Decidim
module Templates
module Admin
# Creates a QuestionnaireTemplate.
class CreateQuestionnaireTemplate < Rectify::Command
# Initializes the command.
#
# form - The source for this QuestionnaireTemplate.
def initialize(form)
@form = form
end

def call
return broadcast(:invalid) unless @form.valid?

@template = Decidim.traceability.create!(
Template,
@form.current_user,
name: @form.name,
description: @form.description,
organization: @form.current_organization,
target: :questionnaire
)

@questionnaire = Decidim::Forms::Questionnaire.create!(questionnaire_for: @template)
@template.update!(templatable: @questionnaire)

broadcast(:ok, @template)
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

module Decidim
module Templates
module Admin
class UpdateProposalAnswerTemplate < Rectify::Command
# Initializes the command.
#
# template - The Template to update.
# form - The form object containing the data to update.
# user - The user that updates the template.
def initialize(template, form, user)
@template = template
@form = form
@user = user
end

def call
return broadcast(:invalid) unless @form.valid?
return broadcast(:invalid) unless @user.organization == @template.organization

@template = Decidim.traceability.update!(
@template,
@user,
name: @form.name,
description: @form.description,
field_values: { internal_state: @form.internal_state },
target: :proposal_answer
)

resource = identify_templateable_resource
@template.update!(templatable: resource)

broadcast(:ok, @template)
end

private

def identify_templateable_resource
resource = @form.scope_for_availability.split("-")
case resource.first
when "organizations"
@form.current_organization
when "components"
component = Decidim::Component.find_by(id: resource.last)
component&.participatory_space&.decidim_organization_id == @form.current_organization.id ? component : nil
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Decidim
module Templates
module Admin
module ApplicationControllerOverride
extend ActiveSupport::Concern

included do
def template_types
@template_types ||= {
I18n.t("template_types.questionnaires", scope: "decidim.templates") => decidim_admin_templates.questionnaire_templates_path,
I18n.t("template_types.proposal_answer_templates", scope: "decidim.templates") => decidim_admin_templates.proposal_answer_templates_path
}
end
end
end
end
end
end
Loading

0 comments on commit 2b078f9

Please sign in to comment.