From 19a084c0c74d672c3550eb266c9c3ae856cc4d47 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Tue, 19 Dec 2017 20:32:55 +0100 Subject: [PATCH] adds map validations --- .../budgets/investments_controller.rb | 2 +- app/controllers/proposals_controller.rb | 2 +- app/models/concerns/mappable.rb | 21 +++++++++++++++++++ app/views/map_locations/_form_fields.html.erb | 13 ++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/app/controllers/budgets/investments_controller.rb b/app/controllers/budgets/investments_controller.rb index 5e7c1ddf0..f6125c214 100644 --- a/app/controllers/budgets/investments_controller.rb +++ b/app/controllers/budgets/investments_controller.rb @@ -104,7 +104,7 @@ module Budgets def investment_params params.require(:budget_investment) .permit(:title, :description, :external_url, :heading_id, :tag_list, - :organization_name, :location, :terms_of_service, + :organization_name, :location, :terms_of_service, :skip_map, image_attributes: [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy], documents_attributes: [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy], map_location_attributes: [:latitude, :longitude, :zoom]) diff --git a/app/controllers/proposals_controller.rb b/app/controllers/proposals_controller.rb index 1f065db06..400887046 100644 --- a/app/controllers/proposals_controller.rb +++ b/app/controllers/proposals_controller.rb @@ -80,7 +80,7 @@ class ProposalsController < ApplicationController def proposal_params params.require(:proposal).permit(:title, :question, :summary, :description, :external_url, :video_url, - :responsible_name, :tag_list, :terms_of_service, :geozone_id, + :responsible_name, :tag_list, :terms_of_service, :geozone_id, :skip_map, image_attributes: [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy], documents_attributes: [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy], map_location_attributes: [:latitude, :longitude, :zoom]) diff --git a/app/models/concerns/mappable.rb b/app/models/concerns/mappable.rb index c108bdca7..cd4a498f3 100644 --- a/app/models/concerns/mappable.rb +++ b/app/models/concerns/mappable.rb @@ -2,8 +2,29 @@ module Mappable extend ActiveSupport::Concern included do + attr_accessor :skip_map + has_one :map_location, dependent: :destroy accepts_nested_attributes_for :map_location, allow_destroy: true + + validate :map_must_be_valid, if: :feature_maps? + + def map_must_be_valid + return true if skip_map? + + unless map_location.try(:available?) + errors.add(:skip_map, I18n.t('activerecord.errors.models.map_location.attributes.map.invalid')) + end + end + + def feature_maps? + Setting["feature.map"].present? + end + + def skip_map? + skip_map == "1" + end + end end diff --git a/app/views/map_locations/_form_fields.html.erb b/app/views/map_locations/_form_fields.html.erb index 39c87c7e4..41423b50d 100644 --- a/app/views/map_locations/_form_fields.html.erb +++ b/app/views/map_locations/_form_fields.html.erb @@ -16,4 +16,17 @@ <%= m_l_fields.hidden_field :zoom, value: map_location.zoom, id: map_location_input_id(parent_class, 'zoom') %> + +
+ <%= form.label :skip_map do %> + <%= form.check_box :skip_map, + title: t("proposals.form.map_skip_checkbox"), + label: false, + class: 'js-toggle-map' %> + + <%= t("proposals.form.map_skip_checkbox") %> + + <% end %> +
<% end %> +