Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SubGuide association to GuideCard #77

Merged
merged 4 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/models/sub_guide_card.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# frozen_string_literal: true

# SubGuideCard class which each GuideCard belongs to
# Line 6 is a validation. The validation states that in order for a sub_guide to save it needs an associated guide_card
class SubGuideCard < ApplicationRecord
belongs_to :guide_card
end
9 changes: 5 additions & 4 deletions app/services/sub_guide_loading_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,23 @@ def initialize(csv_location: nil)
@csv_location = csv_location || Rails.root.join('data', 'dbo-subguides', 'dbo.Subguides.17917.csv')
end

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

private

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

# db migration for GuideCard and SubGuide association
class AddGuideCardIdToSubGuideCard < ActiveRecord::Migration[7.0]
def change
change_table :sub_guide_cards do |t|
t.belongs_to :guide_card
end
end
end
4 changes: 3 additions & 1 deletion db/schema.rb

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

2 changes: 2 additions & 0 deletions spec/models/sub_guide_card_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
sub_guide_card.sortid = '50350.5'
sub_guide_card.parentid = '50345.5'
sub_guide_card.path = '9/0091/A3067'
sub_guide_card.guide_card = GuideCard.create
sub_guide_card.save
expect(sub_guide_card.id).to eq 2
expect(sub_guide_card.heading).to eq 'Afdeling natuurkunde'
expect(sub_guide_card.sortid).to eq '50350.5'
expect(sub_guide_card.parentid).to eq '50345.5'
expect(sub_guide_card.path).to eq '9/0091/A3067'
expect(sub_guide_card.guide_card.class).to eq GuideCard
end
end
3 changes: 2 additions & 1 deletion spec/services/sub_guide_loading_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

it 'imports all data from the CSV file' do
expect(SubGuideCard.count).to eq 0
sgls.import
new_guide_card = GuideCard.new
sgls.import(parent_guide: new_guide_card)
expect(SubGuideCard.count).to eq 5
end
end