Use Rails methods to get map location input IDs

We were manually generating the IDs in order to pass them as data
attributes in the HTML in a component where we don't have access to the
form which has the inputs.

However, these data attributes only make sense when there's a form
present, so we can pass the form as a parameter and use it to get the
IDs.

We can now define a map as editable when there's an associated form,
which makes sense IMHO.
This commit is contained in:
Javi Martín
2023-03-07 19:18:45 +01:00
parent 84fff2e9fb
commit b9518d64e1
9 changed files with 30 additions and 30 deletions

View File

@@ -56,7 +56,6 @@
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"),
remove_marker_label: t("budgets.investments.form.map_remove_marker"), remove_marker_label: t("budgets.investments.form.map_remove_marker"),
parent_class: "budget_investment",
i18n_namespace: "budgets.investments" %> i18n_namespace: "budgets.investments" %>
</div> </div>
<% end %> <% end %>

View File

@@ -1,4 +1,4 @@
<div class="map inline"> <div class="map inline">
<h2><%= t("budgets.index.map") %></h2> <h2><%= t("budgets.index.map") %></h2>
<%= render_map(nil, "budgets", investments_coordinates: coordinates) %> <%= render_map(nil, investments_coordinates: coordinates) %>
</div> </div>

View File

@@ -62,7 +62,6 @@
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"),
remove_marker_label: t("proposals.form.map_remove_marker"), remove_marker_label: t("proposals.form.map_remove_marker"),
parent_class: "proposal",
i18n_namespace: "proposals" %> i18n_namespace: "proposals" %>
</div> </div>
<% end %> <% end %>

View File

@@ -1,12 +1,11 @@
class Shared::MapLocationComponent < ApplicationComponent class Shared::MapLocationComponent < ApplicationComponent
attr_reader :parent_class, :remove_marker_label, :investments_coordinates attr_reader :remove_marker_label, :investments_coordinates, :form
delegate :map_location_input_id, to: :helpers
def initialize(map_location, parent_class, remove_marker_label: nil, investments_coordinates: nil) def initialize(map_location, remove_marker_label: nil, investments_coordinates: nil, form: nil)
@map_location = map_location @map_location = map_location
@parent_class = parent_class
@remove_marker_label = remove_marker_label @remove_marker_label = remove_marker_label
@investments_coordinates = investments_coordinates @investments_coordinates = investments_coordinates
@form = form
end end
def map_location def map_location
@@ -16,7 +15,7 @@ class Shared::MapLocationComponent < ApplicationComponent
private private
def editable? def editable?
remove_marker_label.present? form.present?
end end
def latitude def latitude
@@ -53,12 +52,25 @@ class Shared::MapLocationComponent < ApplicationComponent
map_tiles_provider_attribution: Rails.application.secrets.map_tiles_provider_attribution, map_tiles_provider_attribution: Rails.application.secrets.map_tiles_provider_attribution,
marker_editable: editable?, marker_editable: editable?,
marker_remove_selector: "##{remove_marker_link_id}", marker_remove_selector: "##{remove_marker_link_id}",
latitude_input_selector: "##{map_location_input_id(parent_class, "latitude")}",
longitude_input_selector: "##{map_location_input_id(parent_class, "longitude")}",
zoom_input_selector: "##{map_location_input_id(parent_class, "zoom")}",
marker_investments_coordinates: investments_coordinates, marker_investments_coordinates: investments_coordinates,
marker_latitude: map_location.latitude.presence, marker_latitude: map_location.latitude.presence,
marker_longitude: map_location.longitude.presence marker_longitude: map_location.longitude.presence
}.merge(input_selectors)
end
def input_selectors
if form
{
latitude_input_selector: "##{input_id(:latitude)}",
longitude_input_selector: "##{input_id(:longitude)}",
zoom_input_selector: "##{input_id(:zoom)}"
} }
else
{}
end
end
def input_id(attribute)
form.hidden_field(attribute).match(/ id="([^"]+)"/)[1]
end end
end end

View File

@@ -3,10 +3,6 @@ module MapLocationsHelper
map_location.present? && map_location.available? map_location.present? && map_location.available?
end end
def map_location_input_id(prefix, attribute)
"#{prefix}_map_location_attributes_#{attribute}"
end
def render_map(...) def render_map(...)
render Shared::MapLocationComponent.new(...) render Shared::MapLocationComponent.new(...)
end end

View File

@@ -28,7 +28,7 @@
<% if feature?(:map) && map_location_available?(@investment.map_location) %> <% if feature?(:map) && map_location_available?(@investment.map_location) %>
<div class="margin"> <div class="margin">
<%= render_map(investment.map_location, "budget_investment") %> <%= render_map(investment.map_location) %>
</div> </div>
<% end %> <% end %>

View File

@@ -1,3 +1,3 @@
<div class="map"> <div class="map">
<%= render_map(@map_location, "budgets", investments_coordinates: @investments_map_coordinates) %> <%= render_map(@map_location, investments_coordinates: @investments_map_coordinates) %>
</div> </div>

View File

@@ -1,16 +1,10 @@
<%= form.label :map_location, label %> <%= form.label :map_location, label %>
<p class="help-text" id="tag-list-help-text"><%= help %></p> <p class="help-text" id="tag-list-help-text"><%= help %></p>
<%= render_map(map_location, parent_class, remove_marker_label: remove_marker_label) %>
<%= form.fields_for :map_location, map_location do |m_l_fields| %> <%= form.fields_for :map_location, map_location do |m_l_fields| %>
<%= m_l_fields.hidden_field :latitude, <%= render_map(map_location, remove_marker_label: remove_marker_label, form: m_l_fields) %>
value: map_location.latitude,
id: map_location_input_id(parent_class, "latitude") %> <%= m_l_fields.hidden_field :latitude, value: map_location.latitude %>
<%= m_l_fields.hidden_field :longitude, <%= m_l_fields.hidden_field :longitude, value: map_location.longitude %>
value: map_location.longitude, <%= m_l_fields.hidden_field :zoom, value: map_location.zoom %>
id: map_location_input_id(parent_class, "longitude") %>
<%= m_l_fields.hidden_field :zoom,
value: map_location.zoom,
id: map_location_input_id(parent_class, "zoom") %>
<% end %> <% end %>

View File

@@ -42,7 +42,7 @@
<% if feature?(:map) && map_location_available?(@proposal.map_location) %> <% if feature?(:map) && map_location_available?(@proposal.map_location) %>
<div class="margin"> <div class="margin">
<%= render_map(@proposal.map_location, "proposal") %> <%= render_map(@proposal.map_location) %>
</div> </div>
<% end %> <% end %>