From 29e5adc233a09b0430151a399eb343eab60a8cbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Sun, 16 Nov 2025 23:57:46 +0100 Subject: [PATCH] Move map location fields partial to a component This way it'll be easier to test it and refactor it. --- .../investments/form_component.html.erb | 11 ++++---- .../form_fields_component.html.erb | 12 ++++++++ .../map_locations/form_fields_component.rb | 15 ++++++++++ .../proposals/form_component.html.erb | 11 ++++---- app/views/map_locations/_form_fields.html.erb | 14 ---------- .../form_fields_component_spec.rb | 28 +++++++++++++++++++ 6 files changed, 67 insertions(+), 24 deletions(-) create mode 100644 app/components/map_locations/form_fields_component.html.erb create mode 100644 app/components/map_locations/form_fields_component.rb delete mode 100644 app/views/map_locations/_form_fields.html.erb create mode 100644 spec/components/map_locations/form_fields_component_spec.rb diff --git a/app/components/budgets/investments/form_component.html.erb b/app/components/budgets/investments/form_component.html.erb index ce157d9db..a7e6e2d21 100644 --- a/app/components/budgets/investments/form_component.html.erb +++ b/app/components/budgets/investments/form_component.html.erb @@ -44,11 +44,12 @@ <%= render Documents::NestedComponent.new(f) %> <% end %> - <%= render "map_locations/form_fields", - form: f, - map_location: map_location, - label: t("budgets.investments.form.map_location"), - help: t("budgets.investments.form.map_location_instructions") %> + <%= render MapLocations::FormFieldsComponent.new( + f, + map_location: map_location, + label: t("budgets.investments.form.map_location"), + help: t("budgets.investments.form.map_location_instructions") + ) %>
<%= f.text_field :location %> diff --git a/app/components/map_locations/form_fields_component.html.erb b/app/components/map_locations/form_fields_component.html.erb new file mode 100644 index 000000000..9d8d02e10 --- /dev/null +++ b/app/components/map_locations/form_fields_component.html.erb @@ -0,0 +1,12 @@ +
+ <%= label %> +

<%= help %>

+ + <%= form.fields_for :map_location, map_location do |m_l_fields| %> + <%= render_map(map_location, form: m_l_fields) %> + + <%= m_l_fields.hidden_field :latitude %> + <%= m_l_fields.hidden_field :longitude %> + <%= m_l_fields.hidden_field :zoom %> + <% end %> +
diff --git a/app/components/map_locations/form_fields_component.rb b/app/components/map_locations/form_fields_component.rb new file mode 100644 index 000000000..d4235b3e5 --- /dev/null +++ b/app/components/map_locations/form_fields_component.rb @@ -0,0 +1,15 @@ +class MapLocations::FormFieldsComponent < ApplicationComponent + attr_reader :form, :map_location, :label, :help + use_helpers :render_map + + def initialize(form, map_location:, label:, help:) + @form = form + @map_location = map_location + @label = label + @help = help + end + + def render? + feature?(:map) + end +end diff --git a/app/components/proposals/form_component.html.erb b/app/components/proposals/form_component.html.erb index 9aec4e6eb..6e7fed36c 100644 --- a/app/components/proposals/form_component.html.erb +++ b/app/components/proposals/form_component.html.erb @@ -52,11 +52,12 @@
<% end %> - <%= render "map_locations/form_fields", - form: f, - map_location: map_location, - label: t("proposals.form.map_location"), - help: t("proposals.form.map_location_instructions") %> + <%= render MapLocations::FormFieldsComponent.new( + f, + map_location: proposal.map_location || MapLocation.new, + label: t("proposals.form.map_location"), + help: t("proposals.form.map_location_instructions") + ) %>
<%= f.label :tag_list, t("proposals.form.tags_label") %> diff --git a/app/views/map_locations/_form_fields.html.erb b/app/views/map_locations/_form_fields.html.erb deleted file mode 100644 index 0b0c519c8..000000000 --- a/app/views/map_locations/_form_fields.html.erb +++ /dev/null @@ -1,14 +0,0 @@ -<% if feature?(:map) %> -
- <%= label %> -

<%= help %>

- - <%= form.fields_for :map_location, map_location do |m_l_fields| %> - <%= render_map(map_location, form: m_l_fields) %> - - <%= m_l_fields.hidden_field :latitude %> - <%= m_l_fields.hidden_field :longitude %> - <%= m_l_fields.hidden_field :zoom %> - <% end %> -
-<% end %> diff --git a/spec/components/map_locations/form_fields_component_spec.rb b/spec/components/map_locations/form_fields_component_spec.rb new file mode 100644 index 000000000..d81508877 --- /dev/null +++ b/spec/components/map_locations/form_fields_component_spec.rb @@ -0,0 +1,28 @@ +require "rails_helper" + +describe MapLocations::FormFieldsComponent do + let(:proposal) { Proposal.new } + let(:map_location) { MapLocation.new } + let(:form) { ConsulFormBuilder.new(:proposal, proposal, ApplicationController.new.view_context, {}) } + let(:label) { "Map location" } + let(:help) { "Add a marker" } + let(:component) do + MapLocations::FormFieldsComponent.new(form, map_location: map_location, label: label, help: help) + end + + it "is rendered when the map feature is enabled" do + Setting["feature.map"] = true + + render_inline component + + expect(page).to be_rendered + end + + it "is not rendered when the map feature is not enabled" do + Setting["feature.map"] = false + + render_inline component + + expect(page).not_to be_rendered + end +end