Move map location fields partial to a component
This way it'll be easier to test it and refactor it.
This commit is contained in:
@@ -44,11 +44,12 @@
|
|||||||
<%= render Documents::NestedComponent.new(f) %>
|
<%= render Documents::NestedComponent.new(f) %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<%= render "map_locations/form_fields",
|
<%= render MapLocations::FormFieldsComponent.new(
|
||||||
form: f,
|
f,
|
||||||
map_location: map_location,
|
map_location: map_location,
|
||||||
label: t("budgets.investments.form.map_location"),
|
label: t("budgets.investments.form.map_location"),
|
||||||
help: t("budgets.investments.form.map_location_instructions") %>
|
help: t("budgets.investments.form.map_location_instructions")
|
||||||
|
) %>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<%= f.text_field :location %>
|
<%= f.text_field :location %>
|
||||||
|
|||||||
12
app/components/map_locations/form_fields_component.html.erb
Normal file
12
app/components/map_locations/form_fields_component.html.erb
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<fieldset>
|
||||||
|
<legend><%= label %></legend>
|
||||||
|
<p class="help-text" id="tag-list-help-text"><%= help %></p>
|
||||||
|
|
||||||
|
<%= 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 %>
|
||||||
|
</fieldset>
|
||||||
15
app/components/map_locations/form_fields_component.rb
Normal file
15
app/components/map_locations/form_fields_component.rb
Normal file
@@ -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
|
||||||
@@ -52,11 +52,12 @@
|
|||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<%= render "map_locations/form_fields",
|
<%= render MapLocations::FormFieldsComponent.new(
|
||||||
form: f,
|
f,
|
||||||
map_location: map_location,
|
map_location: proposal.map_location || MapLocation.new,
|
||||||
label: t("proposals.form.map_location"),
|
label: t("proposals.form.map_location"),
|
||||||
help: t("proposals.form.map_location_instructions") %>
|
help: t("proposals.form.map_location_instructions")
|
||||||
|
) %>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<%= f.label :tag_list, t("proposals.form.tags_label") %>
|
<%= f.label :tag_list, t("proposals.form.tags_label") %>
|
||||||
|
|||||||
@@ -1,14 +0,0 @@
|
|||||||
<% if feature?(:map) %>
|
|
||||||
<fieldset>
|
|
||||||
<legend><%= label %></legend>
|
|
||||||
<p class="help-text" id="tag-list-help-text"><%= help %></p>
|
|
||||||
|
|
||||||
<%= 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 %>
|
|
||||||
</fieldset>
|
|
||||||
<% end %>
|
|
||||||
28
spec/components/map_locations/form_fields_component_spec.rb
Normal file
28
spec/components/map_locations/form_fields_component_spec.rb
Normal file
@@ -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
|
||||||
Reference in New Issue
Block a user