From 2cad9cbb7ac817c4376c519480842d5fe4cb2def Mon Sep 17 00:00:00 2001 From: Phil Lee Date: Wed, 7 Dec 2022 12:27:16 +0000 Subject: [PATCH] extract rent ranges rake task to a class --- app/services/imports/rent_ranges_service.rb | 33 +++++++++++++++++++++ lib/tasks/rent_ranges.rake | 21 +++---------- 2 files changed, 37 insertions(+), 17 deletions(-) create mode 100644 app/services/imports/rent_ranges_service.rb diff --git a/app/services/imports/rent_ranges_service.rb b/app/services/imports/rent_ranges_service.rb new file mode 100644 index 000000000..ff7328bc0 --- /dev/null +++ b/app/services/imports/rent_ranges_service.rb @@ -0,0 +1,33 @@ +module Imports + class RentRangesService + attr_reader :start_year, :path, :count + + def initialize(start_year:, path:) + @start_year = start_year + @path = path + @count = 0 + end + + def call + CSV.foreach(path, headers: true) do |row| + LaRentRange.upsert( + { ranges_rent_id: row["ranges_rent_id"], + lettype: row["lettype"], + beds: row["beds"], + start_year:, + la: row["la"], + soft_min: row["soft_min"], + soft_max: row["soft_max"], + hard_min: row["hard_min"], + hard_max: row["hard_max"] }, + unique_by: %i[start_year lettype beds la], + ) + self.count = count + 1 + end + end + + private + + attr_writer :count + end +end diff --git a/lib/tasks/rent_ranges.rake b/lib/tasks/rent_ranges.rake index d7eb36850..e026ffe11 100644 --- a/lib/tasks/rent_ranges.rake +++ b/lib/tasks/rent_ranges.rake @@ -5,25 +5,12 @@ namespace :data_import do task :rent_ranges, %i[start_year path] => :environment do |_task, args| start_year = args[:start_year] path = args[:path] - count = 0 raise "Usage: rake data_import:rent_ranges[start_year,'path/to/csv_file']" if path.blank? || start_year.blank? - CSV.foreach(path, headers: true) do |row| - LaRentRange.upsert( - { ranges_rent_id: row["ranges_rent_id"], - lettype: row["lettype"], - beds: row["beds"], - start_year:, - la: row["la"], - soft_min: row["soft_min"], - soft_max: row["soft_max"], - hard_min: row["hard_min"], - hard_max: row["hard_max"] }, - unique_by: %i[start_year lettype beds la], - ) - count += 1 - end - pp "Created/updated #{count} LA Rent Range records for #{start_year}" unless Rails.env.test? + service = Imports::RentRangesService.new(start_year:, path:) + service.call + + pp "Created/updated #{service.count} LA Rent Range records for #{start_year}" unless Rails.env.test? end end