Skip to content

Commit

Permalink
Add templates lucerne (#48)
Browse files Browse the repository at this point in the history
* Add Proposal Answer Template basic functionality

* Apply pagination, add functionality for copy + delete

* Add Answer Template to Proposal

* Linting decidim-templates

* Add more tests

* Linting

* Fix class name

* Add latest review changes

* Implement code review recommendations

* Add corect labels to table header

* Running EsLint

* Running ERBLinter

* improve texts

* make scopes resilient to missing classes

Co-authored-by: Alexandru-Emil Lupu <[email protected]>
  • Loading branch information
microstudi and alecslupu authored Jan 10, 2023
1 parent 5b6ef60 commit 8d28c77
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def identify_templateable_resource
when "organizations"
@form.current_organization
when "components"
component = Decidim::Component.where(manifest_name: :proposals).find(resource.last)
component.participatory_space.decidim_organization_id == @form.current_organization.id ? component : nil
component = Decidim::Component.find_by(id: resource.last)
component&.participatory_space&.decidim_organization_id == @form.current_organization.id ? component : nil
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def identify_templateable_resource
when "organizations"
@form.current_organization
when "components"
component = Decidim::Component.where(manifest_name: :proposals).find(resource.last)
component.participatory_space.decidim_organization_id == @form.current_organization.id ? component : nil
component = Decidim::Component.find_by(id: resource.last)
component&.participatory_space&.decidim_organization_id == @form.current_organization.id ? component : nil
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def create
@form = form(ProposalAnswerTemplateForm).from_params(params)

CreateProposalAnswerTemplate.call(@form) do
on(:ok) do |template|
on(:ok) do |_template|
flash[:notice] = I18n.t("templates.create.success", scope: "decidim.admin")
redirect_to edit_proposal_answer_template_path(template)
redirect_to proposal_answer_templates_path
end

on(:invalid) do
Expand Down Expand Up @@ -67,9 +67,9 @@ def update
enforce_permission_to :update, :template, template: template
@form = form(ProposalAnswerTemplateForm).from_params(params)
UpdateProposalAnswerTemplate.call(template, @form, current_user) do
on(:ok) do |questionnaire_template|
on(:ok) do |_questionnaire_template|
flash[:notice] = I18n.t("templates.update.success", scope: "decidim.admin")
redirect_to edit_proposal_answer_template_path(questionnaire_template)
redirect_to proposal_answer_templates_path
end

on(:invalid) do |template|
Expand Down Expand Up @@ -131,8 +131,10 @@ def proposal
end

def availability_option_as_text(template)
return unless template.templatable_type

key = "#{template.templatable_type.demodulize.tableize}-#{template.templatable_id}"
avaliablity_options.fetch(key)
avaliablity_options[key].presence || t("templates.missing_resource", scope: "decidim.admin")
end

def availability_options_for_select
Expand All @@ -141,7 +143,7 @@ def availability_options_for_select

def avaliablity_options
@avaliablity_options = {}
Decidim::Component.includes(:participatory_space).where(manifest_name: :proposals)
Decidim::Component.includes(:participatory_space).where(manifest_name: accepted_components)
.select { |a| a.participatory_space.decidim_organization_id == current_organization.id }.each do |component|
@avaliablity_options["components-#{component.id}"] = formated_name(component)
end
Expand All @@ -150,7 +152,12 @@ def avaliablity_options
end

def formated_name(component)
"#{t(component.participatory_space.class.name.underscore, scope: "activerecord.models", count: 1)}: #{translated_attribute(component.participatory_space.title)} "
space_type = t(component.participatory_space.class.name.underscore, scope: "activerecord.models", count: 1)
"#{space_type}: #{translated_attribute(component.participatory_space.title)} > #{translated_attribute(component.name)}"
end

def accepted_components
[:proposals]
end

def template
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
</div>

<div class="row column">
<%= form.select :scope_for_availability, availability_options_for_select %>
<%= form.select :scope_for_availability, availability_options_for_select, help_text: t(".scope_for_availability_help") %>
</div>
</div>
</div>
7 changes: 5 additions & 2 deletions decidim-templates/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ en:
template:
description: Description
name: Name
scope_for_availability: Restrict availability to the component
decidim:
admin:
menu:
Expand All @@ -24,6 +25,7 @@ en:
destroy:
success: Template deleted successfully
empty: There are no templates yet.
missing_resource: "(missing resource)"
update:
error: There was a problem updating this template.
success: Template updated successfully
Expand All @@ -45,15 +47,16 @@ en:
proposal_answer_templates:
form:
answer_template: Answer template
hint: "<strong>Hint:</strong> You can use theese variables anywhere on the answer template that will be replaced when using the template"
hint: "<strong>Hint:</strong> You can use these variables anywhere on the answer template that will be replaced when using the template"
hint1: "<strong>%{organization}</strong> will be replaced by the organization's name"
hint2: "<strong>%{name}</strong> will be replaced by the author's name"
hint3: "<strong>%{admin}</strong> will be replaced by the admin's name (the one answering the proposal)"
save: Save
scope_for_availability_help: Note that only participatory spaces having components of the type "proposals" will be listed.
template_title: Template information
index:
confirm_delete: Are you sure you want to delete this template?
global_scope: Global scope
global_scope: Global (available everywhere)
internal_state: Internal State
scope_for_availability: Scope
title: Proposal answers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,72 +22,106 @@
it "shows a table with the templates info" do
within ".questionnaire-templates" do
expect(page).to have_i18n_content(template.name)
expect(page).to have_i18n_content("Global scope")
expect(page).to have_i18n_content("Global (available everywhere)")
end
end

context "when a template is scoped to an invalid resource" do
let!(:template) { create(:template, :proposal_answer, organization: organization, templatable: create(:dummy_resource)) }

it "shows a table info about the invalid resource" do
within ".questionnaire-templates" do
expect(page).to have_i18n_content(template.name)
expect(page).to have_i18n_content("(missing resource)")
end
end
end
end

describe "creating a proposal_answer_template" do
let(:participatory_process) { create :participatory_process, title: { en: "A participatory process" }, organization: organization }
let!(:proposals_component) { create :component, manifest_name: :proposals, name: { en: "A component" }, participatory_space: participatory_process }

before do
within ".layout-content" do
click_link("New")
end
end

it "creates a new template with a Organization as templatable" do
within ".new_proposal_answer_template" do
fill_in_i18n(
:proposal_answer_template_name,
"#proposal_answer_template-name-tabs",
en: "My template",
es: "Mi plantilla",
ca: "La meva plantilla"
)
fill_in_i18n_editor(
:proposal_answer_template_description,
"#proposal_answer_template-description-tabs",
en: "Description",
es: "Descripción",
ca: "Descripció"
)

choose "Not answered"

page.find("*[type=submit]").click
shared_examples "creates a new template with scopes" do |scope_name|
it "creates a new template" do
within ".new_proposal_answer_template" do
fill_in_i18n(
:proposal_answer_template_name,
"#proposal_answer_template-name-tabs",
en: "My template",
es: "Mi plantilla",
ca: "La meva plantilla"
)
fill_in_i18n_editor(
:proposal_answer_template_description,
"#proposal_answer_template-description-tabs",
en: "Description",
es: "Descripción",
ca: "Descripció"
)

choose "Not answered"
select scope_name, from: :proposal_answer_template_scope_for_availability

page.find("*[type=submit]").click
end

expect(page).to have_admin_callout("successfully")
expect(page).to have_current_path decidim_admin_templates.proposal_answer_templates_path
within ".questionnaire-templates" do
expect(page).to have_i18n_content(scope_name)
expect(page).to have_content("My template")
end
end

expect(page).to have_admin_callout("successfully")
end

it_behaves_like "creates a new template with scopes", "Global (available everywhere)"
it_behaves_like "creates a new template with scopes", "Participatory process: A participatory process > A component"
end

describe "updating a template" do
let!(:template) { create(:template, :proposal_answer, organization: organization) }
let(:participatory_process) { create :participatory_process, title: { en: "A participatory process" }, organization: organization }
let!(:proposals_component) { create :component, manifest_name: :proposals, name: { en: "A component" }, participatory_space: participatory_process }

before do
visit decidim_admin_templates.proposal_answer_templates_path
click_link translated(template.name)
end

it "updates a template" do
fill_in_i18n(
:proposal_answer_template_name,
"#proposal_answer_template-name-tabs",
en: "My new name",
es: "Mi nuevo nombre",
ca: "El meu nou nom"
)
shared_examples "updates a template with scopes" do |scope_name|
it "updates a template" do
fill_in_i18n(
:proposal_answer_template_name,
"#proposal_answer_template-name-tabs",
en: "My new name",
es: "Mi nuevo nombre",
ca: "El meu nou nom"
)

within ".edit_proposal_answer_template" do
page.find("*[type=submit]").click
end
select scope_name, from: :proposal_answer_template_scope_for_availability

expect(page).to have_admin_callout("successfully")
within ".edit_proposal_answer_template" do
page.find("*[type=submit]").click
end

within ".container" do
expect(page).to have_current_path decidim_admin_templates.edit_proposal_answer_template_path(template)
expect(page.find("#proposal_answer_template_name_en").value).to eq("My new name")
expect(page).to have_admin_callout("successfully")
expect(page).to have_current_path decidim_admin_templates.proposal_answer_templates_path
within ".questionnaire-templates" do
expect(page).to have_i18n_content(scope_name)
expect(page).to have_content("My new name")
end
end
end

it_behaves_like "updates a template with scopes", "Global (available everywhere)"
it_behaves_like "updates a template with scopes", "Participatory process: A participatory process > A component"
end

describe "updating a template with invalid values" do
Expand Down

0 comments on commit 8d28c77

Please sign in to comment.