natdeanlewissoftwire
2 years ago
7 changed files with 1410 additions and 1 deletions
@ -0,0 +1,2 @@ |
|||||||
|
class LaPurchasePriceRange < ApplicationRecord |
||||||
|
end |
@ -0,0 +1,31 @@ |
|||||||
|
require "csv" |
||||||
|
|
||||||
|
module Imports |
||||||
|
class PurchasePriceRangesService |
||||||
|
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| |
||||||
|
LaPurchasePriceRange.upsert( |
||||||
|
{ start_year:, |
||||||
|
la: row["la"], |
||||||
|
bedrooms: row["bedrooms"], |
||||||
|
soft_min: row["soft_min"], |
||||||
|
soft_max: row["soft_max"], }, |
||||||
|
unique_by: %i[start_year bedrooms la], |
||||||
|
) |
||||||
|
self.count = count + 1 |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
private |
||||||
|
|
||||||
|
attr_writer :count |
||||||
|
end |
||||||
|
end |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,14 @@ |
|||||||
|
class CreateLaPurchasePriceRanges < ActiveRecord::Migration[7.0] |
||||||
|
def change |
||||||
|
create_table :la_purchase_price_ranges do |t| |
||||||
|
t.string :la |
||||||
|
t.integer :bedrooms |
||||||
|
t.decimal :soft_min, precision: 10, scale: 2 |
||||||
|
t.decimal :soft_max, precision: 10, scale: 2 |
||||||
|
t.integer :start_year |
||||||
|
|
||||||
|
t.index %i[start_year bedrooms la], unique: true, name: "index_la_purchase_price_ranges_on_start_year_bedrooms_la" |
||||||
|
t.timestamps |
||||||
|
end |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,14 @@ |
|||||||
|
namespace :data_import do |
||||||
|
desc "Import annual purchase price range data" |
||||||
|
task :purchase_price_ranges, %i[start_year path] => :environment do |_task, args| |
||||||
|
start_year = args[:start_year] |
||||||
|
path = args[:path] |
||||||
|
|
||||||
|
raise "Usage: rake data_import:purchase_price_ranges[start_year,'path/to/csv_file']" if path.blank? || start_year.blank? |
||||||
|
|
||||||
|
service = Imports::PurchasePriceRangesService.new(start_year:, path:) |
||||||
|
service.call |
||||||
|
|
||||||
|
pp "Created/updated #{service.count} LA Purchase Price Range records for #{start_year}" unless Rails.env.test? |
||||||
|
end |
||||||
|
end |
Loading…
Reference in new issue