From b2616d317fa8df76570995435e37a43157ed69ba Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Thu, 24 Oct 2019 15:18:25 +0200 Subject: [PATCH] Task to remove an unused enterprise Note this task is still rather naive and only covers the simple case where an enterprise was created but never used and thus, does not have any associated entities like orders. This is enough for the case I have at hand where a hub's manager created an enterprise while he wanted to create a user account #ux. He ended up with an enterprise named after him and now he asked us to clean that up. --- lib/tasks/enterprises.rake | 9 +++++++++ spec/lib/tasks/enterprises_rake_spec.rb | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 spec/lib/tasks/enterprises_rake_spec.rb diff --git a/lib/tasks/enterprises.rake b/lib/tasks/enterprises.rake index caca6041a1..0c1c0f4aa6 100644 --- a/lib/tasks/enterprises.rake +++ b/lib/tasks/enterprises.rake @@ -1,6 +1,15 @@ require 'csv' namespace :ofn do + # Note this task is still rather naive and only covers the simple case where + # an enterprise was created but never used and thus, does not have any + # associated entities like orders. + desc 'remove the specified enterprise' + task :remove_enterprise, [:enterprise_id] => :environment do |_task, args| + enterprise = Enterprise.find(args.enterprise_id) + enterprise.destroy + end + namespace :dev do desc 'export enterprises to CSV' task export_enterprises: :environment do diff --git a/spec/lib/tasks/enterprises_rake_spec.rb b/spec/lib/tasks/enterprises_rake_spec.rb new file mode 100644 index 0000000000..9fb1ef23f1 --- /dev/null +++ b/spec/lib/tasks/enterprises_rake_spec.rb @@ -0,0 +1,19 @@ +require 'spec_helper' +require 'rake' + +describe 'enterprises.rake' do + describe ':remove_enterprise' do + context 'when the enterprises exists' do + it 'removes the enterprise' do + enterprise = create(:enterprise) + + Rake.application.rake_require 'tasks/enterprises' + Rake::Task.define_task(:environment) + + expect { + Rake.application.invoke_task "ofn:remove_enterprise[#{enterprise.id}]" + }.to change(Enterprise, :count).by(-1) + end + end + end +end