From 96066aee44fe706f2da38ac8c2718b134b61b89e Mon Sep 17 00:00:00 2001 From: rgarcia Date: Tue, 26 Dec 2017 12:39:29 +0100 Subject: [PATCH 1/2] Validates map presence only on create MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- app/models/concerns/mappable.rb | 2 +- spec/shared/features/mappable.rb | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/models/concerns/mappable.rb b/app/models/concerns/mappable.rb index cd4a498f3..eb0414fe2 100644 --- a/app/models/concerns/mappable.rb +++ b/app/models/concerns/mappable.rb @@ -7,7 +7,7 @@ module Mappable has_one :map_location, dependent: :destroy accepts_nested_attributes_for :map_location, allow_destroy: true - validate :map_must_be_valid, if: :feature_maps? + validate :map_must_be_valid, on: :create, if: :feature_maps? def map_must_be_valid return true if skip_map? diff --git a/spec/shared/features/mappable.rb b/spec/shared/features/mappable.rb index ca9ece6e7..77da67145 100644 --- a/spec/shared/features/mappable.rb +++ b/spec/shared/features/mappable.rb @@ -175,22 +175,22 @@ shared_examples "mappable" do |mappable_factory_name, mappable_association_name, expect(page).not_to have_css(".map_location") end - scenario 'Errors on update', :js do + scenario 'No errors on update', :js do + skip "" login_as mappable.author visit send(mappable_edit_path, id: mappable.id) click_link "Remove map marker" click_on "Save changes" - expect(page).to have_content "Map location can't be blank" + expect(page).to_not have_content "Map location can't be blank" end - scenario 'Skip map on update' do + scenario 'No need to skip map on update' do login_as mappable.author visit send(mappable_edit_path, id: mappable.id) click_link "Remove map marker" - check "#{mappable_factory_name}_skip_map" click_on "Save changes" expect(page).to_not have_content "Map location can't be blank" From 2b9b78e38ed30fdc61b2254cc0e812e9d6cd3ca3 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Tue, 26 Dec 2017 12:40:26 +0100 Subject: [PATCH 2/2] Add rake task to reset the cached votes counter --- lib/tasks/votes.rake | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 lib/tasks/votes.rake diff --git a/lib/tasks/votes.rake b/lib/tasks/votes.rake new file mode 100644 index 000000000..0cc595662 --- /dev/null +++ b/lib/tasks/votes.rake @@ -0,0 +1,16 @@ +namespace :votes do + + desc "Resets cached_votes_up counter to its latest value" + task reset_vote_counter: :environment do + models = [Proposal, Budget::Investment] + + models.each do |model| + model.find_each do |resource| + resource.update_cached_votes + print "." + end + end + + end + +end