diff --git a/lib/tasks/communities.rake b/lib/tasks/communities.rake new file mode 100644 index 000000000..ad08e4bd9 --- /dev/null +++ b/lib/tasks/communities.rake @@ -0,0 +1,21 @@ +namespace :communities do + + desc "Associate community to proposals and budget investments" + task associate_community: :environment do + + Proposal.all.each do |proposal| + if proposal.community.blank? + community = Community.create + proposal.update(community_id: community.id) + end + end + + Budget::Investment.all.each do |investment| + if investment.community.blank? + community = Community.create + investment.update(community_id: community.id) + end + end + end + +end diff --git a/spec/lib/tasks/communities_spec.rb b/spec/lib/tasks/communities_spec.rb new file mode 100644 index 000000000..20c1af841 --- /dev/null +++ b/spec/lib/tasks/communities_spec.rb @@ -0,0 +1,49 @@ +require 'rails_helper' +require 'rake' + +describe 'Communities Rake' do + + describe '#associate_community' do + + before do + Rake.application.rake_require "tasks/communities" + Rake::Task.define_task(:environment) + end + + let :run_rake_task do + Rake::Task['communities:associate_community'].reenable + Rake.application.invoke_task 'communities:associate_community' + end + + context 'Associate community to Proposal' do + + it 'When proposal has not community_id' do + proposal = create(:proposal) + proposal.update(community_id: nil) + expect(proposal.community).to be_nil + + run_rake_task + proposal.reload + + expect(proposal.community).to be_present + end + end + + context 'Associate community to Budget Investment' do + + it 'When budget investment has not community_id' do + investment = create(:budget_investment) + investment.update(community_id: nil) + expect(investment.community).to be_nil + + run_rake_task + investment.reload + + expect(investment.community).to be_present + end + + end + + end + +end