-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from pulibrary/i17-SubGuide-Data
Load SubGuideCard i17-SubGuide-Data
- Loading branch information
Showing
10 changed files
with
116 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
class SubGuideCard < ApplicationRecord | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |