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

Add proposal answer templates #37

Open
wants to merge 18 commits into
base: release/0.26-stable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions decidim-core/app/models/decidim/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Component < ApplicationRecord
include Loggable
include Decidim::ShareableWithToken
include ScopableComponent
include Decidim::Templates::Templatable if defined? Decidim::Templates::Templatable

belongs_to :participatory_space, polymorphic: true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
</div>

<div class="card-section">
<% if defined?(Decidim::Templates) %>
<%= render "decidim/templates/admin/proposal_answer_templates/template_chooser", form: f %>
<% end %>

<div class="row column">
<%= f.collection_radio_buttons :internal_state, [["not_answered", t(".not_answered")], ["accepted", t(".accepted")], ["rejected", t(".rejected")], ["evaluating", t(".evaluating")]], :first, :last, prompt: true %>
</div>
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
Expand Up @@ -39,7 +39,8 @@ def copy_template
@copied_template = Template.create!(
organization: @template.organization,
name: @template.name,
description: @template.description
description: @template.description,
target: :questionnaire
)
@resource = Decidim::Forms::Questionnaire.create!(
@template.templatable.dup.attributes.merge(
Expand Down
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)
microstudi marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Up @@ -20,7 +20,8 @@ def call
@form.current_user,
name: @form.name,
description: @form.description,
organization: @form.current_organization
organization: @form.current_organization,
target: :questionnaire
)

@questionnaire = Decidim::Forms::Questionnaire.create!(questionnaire_for: @template)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module Decidim
module Templates
module Admin
class DestroyQuestionnaireTemplate < DestroyTemplate
protected

def destroy_template
Decidim.traceability.perform_action!(
:delete,
template,
current_user
) do
template.destroy!
template.templatable.destroy
microstudi marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def call
broadcast(:ok)
end

private
protected

attr_reader :template, :current_user

Expand Down
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
Expand Up @@ -4,7 +4,7 @@ module Decidim
module Templates
module Admin
# Updates the questionnaire template given form data.
class UpdateTemplate < Rectify::Command
class UpdateQuestionnaireTemplate < Rectify::Command
# Initializes the command.
#
# template - The Template to update.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def permission_class_chain

def template_types
@template_types ||= {
I18n.t("template_types.questionnaires", scope: "decidim.templates") => decidim_admin_templates.questionnaire_templates_path
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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require "active_support/concern"

module Decidim
module Templates
module Admin
module Concerns
module TemplatablePoposalAnswer
# Common logic to load template-related resources in controller
extend ActiveSupport::Concern

included do
helper_method :proposal_answers_template_options

def proposal_answers_template_options; end
end
end
end
end
end
end
Loading