Skip to content

Commit

Permalink
Merge pull request #57 from pulibrary/i17-SubGuide-Data
Browse files Browse the repository at this point in the history
Load SubGuideCard i17-SubGuide-Data
  • Loading branch information
hackartisan authored Apr 10, 2023
2 parents 0f684d4 + aaebd7f commit d299279
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 6 deletions.
4 changes: 4 additions & 0 deletions app/models/sub_guide_card.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class SubGuideCard < ApplicationRecord
end
8 changes: 4 additions & 4 deletions app/services/guide_card_loading_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
require 'csv'
# service for loading GuideCard data
class GuideCardLoadingService
# location of source data for GuideCards
def csv_location
return Rails.root.join('spec', 'fixtures', 'guide_card_fixture.csv') if Rails.env.test?
attr_reader :csv_location

Rails.root.join('data', 'dbo-guides', 'dbo.Guides.31756.csv')
# @param csv_location [String] location of source data for GuideCards
def initialize(csv_location: nil)
@csv_location = csv_location || Rails.root.join('data', 'dbo-guides', 'dbo.Guides.31756.csv')
end

def import
Expand Down
31 changes: 31 additions & 0 deletions app/services/sub_guide_loading_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require 'csv'
# service for loading SubGuide cards data
class SubGuideLoadingService
attr_reader :csv_location

# @param csv_location [String] location of source data for SubGuideCards
def initialize(csv_location: nil)
@csv_location = csv_location || Rails.root.join('data', 'dbo-subguides', 'dbo.Subguides.17917.csv')
end

def import
sub_guide_card_data = CSV.parse(File.read(csv_location), headers: true)
sub_guide_card_data.each do |entry|
import_sub_guide_card(entry)
end
end

private

def import_sub_guide_card(card)
sgc = SubGuideCard.new
sgc.id = card[0]
sgc.heading = card[1]
sgc.sortid = card[2]
sgc.parentid = card[3]
sgc.path = card[4]
sgc.save
end
end
15 changes: 15 additions & 0 deletions db/migrate/20230330205517_create_sub_guide_cards.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

# Data for SubGuideCards
class CreateSubGuideCards < ActiveRecord::Migration[7.0]
def change
create_table :sub_guide_cards do |t|
t.string :heading
t.string :sortid
t.string :parentid
t.string :path

t.timestamps
end
end
end
11 changes: 10 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions lib/tasks/import.rake
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ namespace :import do
task import_guide_cards: :environment do
GuideCardLoadingService.new.import
end

desc 'Import SubGuideCard data'
task import_sub_guide_cards: :environment do
SubGuideLoadingService.new.import
end
end
6 changes: 6 additions & 0 deletions spec/fixtures/subguide_card_fixture.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ID,heading,sortid,parentid,path,heading,sortid,parentid,path
1,V-Z,50349.5,50347.5,9/0091/A3037,V-Z,50349.5,50347.5,9/0091/A3037
2,Afdeling natuurkunde,50350.5,50345.5,9/0091/A3067,Afdeling natuurkunde,50350.5,50345.5,9/0091/A3067
3,Afdeling voor ...,50351.5,50345.5,9/0091/A3038,Afdeling voor ...,50351.5,50345.5,9/0091/A3038
4,(Without subdivision),50352.5,540.5,sub,(Without subdivision),50352.5,540.5,sub
5,(As author),50353.5,50352.5,sub,(As author),50353.5,50352.5,sub
20 changes: 20 additions & 0 deletions spec/models/sub_guide_card_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe SubGuideCard, type: :model do
it 'has the SubGuideCard fields' do
subguidecard = SubGuideCard.new
subguidecard.id = 2
subguidecard.heading = 'Afdeling natuurkunde'
subguidecard.sortid = '50350.5'
subguidecard.parentid = '50345.5'
subguidecard.path = '9/0091/A3067'
subguidecard.save
expect(subguidecard.id).to eq 2
expect(subguidecard.heading).to eq 'Afdeling natuurkunde'
expect(subguidecard.sortid).to eq '50350.5'
expect(subguidecard.parentid).to eq '50345.5'
expect(subguidecard.path).to eq '9/0091/A3067'
end
end
2 changes: 1 addition & 1 deletion spec/services/guide_card_loading_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'rails_helper'

describe GuideCardLoadingService do
let(:gcls) { described_class.new }
let(:gcls) { described_class.new(csv_location: Rails.root.join('spec', 'fixtures', 'guide_card_fixture.csv')) }
it 'can instantiate' do
expect(gcls).to be_instance_of described_class
end
Expand Down
20 changes: 20 additions & 0 deletions spec/services/sub_guide_loading_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

require 'rails_helper'

describe SubGuideLoadingService do
let(:sgls) { described_class.new(csv_location: Rails.root.join('spec', 'fixtures', 'subguide_card_fixture.csv')) }
it 'can instantiate' do
expect(sgls).to be_instance_of described_class
end

it 'has a CSV file' do
expect(sgls.csv_location).to eq Rails.root.join('spec', 'fixtures', 'subguide_card_fixture.csv')
end

it 'imports all data from the CSV file' do
expect(SubGuideCard.count).to eq 0
sgls.import
expect(SubGuideCard.count).to eq 5
end
end

0 comments on commit d299279

Please sign in to comment.