Files
nairobi/app/models/concerns/mappable.rb
rgarcia 96066aee44 Validates map presence only on create
As we are using an attr_accessor to make sure a user accepts that the
proposal “has no map”, the validation was being run in every save.

Making other things break, for example the updating of the counter
cache `cached_votes_up`, when someone voted for the proposal, as the
attr_accessor `skip_map` was not present
2017-12-26 12:39:29 +01:00

31 lines
628 B
Ruby

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, on: :create, 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