Skip to content

Commit

Permalink
Remove existing SubGuide belong_to association with GuideCard (#84)
Browse files Browse the repository at this point in the history
* Remove existing SubGuide association to GuideCard

Co-authored-by: Anna Headley <hackartisan@users.noreply.github.com>

* rubocop corrections

* Documentation for extracting .tiff images from server

Co-authored-by: Bess Sadler <bess@users.noreply.github.com>

---------

Co-authored-by: Anna Headley <hackartisan@users.noreply.github.com>
Co-authored-by: Bess Sadler <bess@users.noreply.github.com>
  • Loading branch information
3 people authored Jun 5, 2023
1 parent 7ca0950 commit 01a6daf
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 13 deletions.
1 change: 0 additions & 1 deletion app/models/sub_guide_card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
# 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: 4 additions & 5 deletions app/services/sub_guide_loading_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,22 @@ def initialize(csv_location: nil)
@csv_location = csv_location || Rails.root.join('data', 'dbo-subguides', 'dbo.Subguides.17917.csv')
end

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

private

def import_sub_guide_card(card:, parent_guide:)
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.guide_card = parent_guide
sgc.save
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

# db migration for revert of belong_to association with GuideCard
class RemoveGuideCardFromSubGuideCards < ActiveRecord::Migration[7.0]
def change
remove_column :sub_guide_cards, :guide_card_id, :bigint
end
end
4 changes: 1 addition & 3 deletions db/schema.rb

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

2 changes: 0 additions & 2 deletions spec/models/sub_guide_card_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
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: 1 addition & 2 deletions spec/services/sub_guide_loading_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@

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

0 comments on commit 01a6daf

Please sign in to comment.