Skip to content

Commit

Permalink
Add guide_card placeholder to save sub_guide_card
Browse files Browse the repository at this point in the history
Co-authored-by: Jane Sandberg <sandbergja@users.noreply.github.com>
  • Loading branch information
leefaisonr and sandbergja committed May 18, 2023
1 parent a252da4 commit 4adc844
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/models/sub_guide_card.rb
Original file line number Diff line number Diff line change
@@ -1,6 +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
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

0 comments on commit 4adc844

Please sign in to comment.