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:
@@ -56,7 +56,6 @@
|
||||
label: t("budgets.investments.form.map_location"),
|
||||
help: t("budgets.investments.form.map_location_instructions"),
|
||||
remove_marker_label: t("budgets.investments.form.map_remove_marker"),
|
||||
parent_class: "budget_investment",
|
||||
i18n_namespace: "budgets.investments" %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<div class="map inline">
|
||||
<h2><%= t("budgets.index.map") %></h2>
|
||||
<%= render_map(nil, "budgets", investments_coordinates: coordinates) %>
|
||||
<%= render_map(nil, investments_coordinates: coordinates) %>
|
||||
</div>
|
||||
|
||||
@@ -62,7 +62,6 @@
|
||||
label: t("proposals.form.map_location"),
|
||||
help: t("proposals.form.map_location_instructions"),
|
||||
remove_marker_label: t("proposals.form.map_remove_marker"),
|
||||
parent_class: "proposal",
|
||||
i18n_namespace: "proposals" %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
class Shared::MapLocationComponent < ApplicationComponent
|
||||
attr_reader :parent_class, :remove_marker_label, :investments_coordinates
|
||||
delegate :map_location_input_id, to: :helpers
|
||||
attr_reader :remove_marker_label, :investments_coordinates, :form
|
||||
|
||||
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
|
||||
@parent_class = parent_class
|
||||
@remove_marker_label = remove_marker_label
|
||||
@investments_coordinates = investments_coordinates
|
||||
@form = form
|
||||
end
|
||||
|
||||
def map_location
|
||||
@@ -16,7 +15,7 @@ class Shared::MapLocationComponent < ApplicationComponent
|
||||
private
|
||||
|
||||
def editable?
|
||||
remove_marker_label.present?
|
||||
form.present?
|
||||
end
|
||||
|
||||
def latitude
|
||||
@@ -53,12 +52,25 @@ class Shared::MapLocationComponent < ApplicationComponent
|
||||
map_tiles_provider_attribution: Rails.application.secrets.map_tiles_provider_attribution,
|
||||
marker_editable: editable?,
|
||||
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_latitude: map_location.latitude.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
|
||||
|
||||
@@ -3,10 +3,6 @@ module MapLocationsHelper
|
||||
map_location.present? && map_location.available?
|
||||
end
|
||||
|
||||
def map_location_input_id(prefix, attribute)
|
||||
"#{prefix}_map_location_attributes_#{attribute}"
|
||||
end
|
||||
|
||||
def render_map(...)
|
||||
render Shared::MapLocationComponent.new(...)
|
||||
end
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
|
||||
<% if feature?(:map) && map_location_available?(@investment.map_location) %>
|
||||
<div class="margin">
|
||||
<%= render_map(investment.map_location, "budget_investment") %>
|
||||
<%= render_map(investment.map_location) %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
<div class="map">
|
||||
<%= render_map(@map_location, "budgets", investments_coordinates: @investments_map_coordinates) %>
|
||||
<%= render_map(@map_location, investments_coordinates: @investments_map_coordinates) %>
|
||||
</div>
|
||||
|
||||
@@ -1,16 +1,10 @@
|
||||
<%= form.label :map_location, label %>
|
||||
<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| %>
|
||||
<%= m_l_fields.hidden_field :latitude,
|
||||
value: map_location.latitude,
|
||||
id: map_location_input_id(parent_class, "latitude") %>
|
||||
<%= m_l_fields.hidden_field :longitude,
|
||||
value: map_location.longitude,
|
||||
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") %>
|
||||
<%= render_map(map_location, remove_marker_label: remove_marker_label, form: m_l_fields) %>
|
||||
|
||||
<%= m_l_fields.hidden_field :latitude, value: map_location.latitude %>
|
||||
<%= m_l_fields.hidden_field :longitude, value: map_location.longitude %>
|
||||
<%= m_l_fields.hidden_field :zoom, value: map_location.zoom %>
|
||||
<% end %>
|
||||
|
||||
@@ -42,7 +42,7 @@
|
||||
|
||||
<% if feature?(:map) && map_location_available?(@proposal.map_location) %>
|
||||
<div class="margin">
|
||||
<%= render_map(@proposal.map_location, "proposal") %>
|
||||
<%= render_map(@proposal.map_location) %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user