-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add proposal answer templates * fix tests
- Loading branch information
1 parent
88c2617
commit 2b078f9
Showing
33 changed files
with
1,016 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
"Decidim": false, | ||
"$": false, | ||
"jQuery": false, | ||
"Quill": false, | ||
"L": false | ||
}, | ||
"rules": { | ||
|
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
35 changes: 35 additions & 0 deletions
35
app/commands/decidim/templates/admin/copy_proposal_answer_template.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,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 |
56 changes: 56 additions & 0 deletions
56
app/commands/decidim/templates/admin/copy_questionnaire_template.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,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 |
48 changes: 48 additions & 0 deletions
48
app/commands/decidim/templates/admin/create_proposal_answer_template.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 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 |
35 changes: 35 additions & 0 deletions
35
app/commands/decidim/templates/admin/create_questionnaire_template.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,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 |
52 changes: 52 additions & 0 deletions
52
app/commands/decidim/templates/admin/update_proposal_answer_template.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 | ||
|
||
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 |
20 changes: 20 additions & 0 deletions
20
app/controllers/concerns/decidim/templates/admin/application_controller_override.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,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 |
Oops, something went wrong.