Skip to content

Commit

Permalink
Respond to code review feedback
Browse files Browse the repository at this point in the history
- Move the record population out of migrations and into db/seeds.rb

- Splits the migrations into three separate files, one per model

- Add two indices on the linking model, from each direction

- Removes the confidence value from the Detector model, leaving only
  the one on the DetectorCategory linking model
  • Loading branch information
matt-bernhardt committed Sep 10, 2024
1 parent 57b3959 commit b770c25
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 44 deletions.
1 change: 0 additions & 1 deletion app/models/detector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#
# id :integer not null, primary key
# name :string
# confidence :float
# created_at :datetime not null
# updated_at :datetime not null
#
Expand Down
33 changes: 0 additions & 33 deletions db/migrate/20240909183413_create_categories.rb
Original file line number Diff line number Diff line change
@@ -1,44 +1,11 @@
class CreateCategories < ActiveRecord::Migration[7.1]
def change
create_table :detectors do |t|
t.string :name
t.float :confidence

t.timestamps
end
add_index :detectors, :name, unique: true

create_table :categories do |t|
t.string :name
t.text :description

t.timestamps
end
add_index :categories, :name, unique: true

create_table :detector_categories do |t|
t.belongs_to :detector, null: false, foreign_key: true
t.belongs_to :category, null: false, foreign_key: true
t.float :confidence

t.timestamps
end

Detector.create(name: 'DOI', confidence: 0.95)
Detector.create(name: 'ISBN', confidence: 0.8)
Detector.create(name: 'ISSN', confidence: 0.6)
Detector.create(name: 'PMID', confidence: 0.95)
Detector.create(name: 'Journal', confidence: 0.2)
Detector.create(name: 'SuggestedResource', confidence: 0.95)

Category.create(name: 'Informational', description: 'A type of search where the user is looking for broad information, rather than an individual item. Also known as "open-ended" or "topical".')
Category.create(name: 'Navigational', description: 'A type of search where the user has a location in mind, and wants to go there. In library discovery, this should mean a URL that will not be in the searched index.')
Category.create(name: 'Transactional', description: 'A type of search where the user has an item in mind, and wants to get that item. Also known as "known-item".')

DetectorCategory.create(detector: Detector.find_by(name: 'DOI'), category: Category.find_by(name: 'Transactional'), confidence: 0.95)
DetectorCategory.create(detector: Detector.find_by(name: 'ISBN'), category: Category.find_by(name: 'Transactional'), confidence: 0.95)
DetectorCategory.create(detector: Detector.find_by(name: 'ISSN'), category: Category.find_by(name: 'Transactional'), confidence: 0.95)
DetectorCategory.create(detector: Detector.find_by(name: 'PMID'), category: Category.find_by(name: 'Transactional'), confidence: 0.95)
DetectorCategory.create(detector: Detector.find_by(name: 'Journal'), category: Category.find_by(name: 'Transactional'), confidence: 0.5)
end
end
10 changes: 10 additions & 0 deletions db/migrate/20240909183513_create_detectors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateDetectors < ActiveRecord::Migration[7.1]
def change
create_table :detectors do |t|
t.string :name

t.timestamps
end
add_index :detectors, :name, unique: true
end
end
13 changes: 13 additions & 0 deletions db/migrate/20240909183613_create_detector_categories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateDetectorCategories < ActiveRecord::Migration[7.1]
def change
create_table :detector_categories do |t|
t.belongs_to :detector, null: false, foreign_key: true
t.belongs_to :category, null: false, foreign_key: true
t.float :confidence

t.timestamps
end
add_index :detector_categories, [:detector_id, :category_id]
add_index :detector_categories, [:category_id, :detector_id]
end
end
5 changes: 3 additions & 2 deletions db/schema.rb

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

53 changes: 53 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,56 @@
# ["Action", "Comedy", "Drama", "Horror"].each do |genre_name|
# MovieGenre.find_or_create_by!(name: genre_name)
# end

Rails.logger.info('Seeding DB starting')

# Categories
Category.find_or_create_by(
name: 'Informational',
description: 'A type of search where the user is looking for broad information, rather than an individual item. Also known as "open-ended" or "topical".'
)
Category.find_or_create_by(
name: 'Navigational',
description: 'A type of search where the user has a location in mind, and wants to go there. In library discovery, this should mean a URL that will not be in the searched index.'
)
Category.find_or_create_by(
name: 'Transactional',
description: 'A type of search where the user has an item in mind, and wants to get that item. Also known as "known-item".'
)

# Detectors
Detector.find_or_create_by(name: 'DOI')
Detector.find_or_create_by(name: 'ISBN')
Detector.find_or_create_by(name: 'ISSN')
Detector.find_or_create_by(name: 'PMID')
Detector.find_or_create_by(name: 'Journal')
Detector.find_or_create_by(name: 'SuggestedResource')

# DetectorCategories
DetectorCategory.find_or_create_by(
detector: Detector.find_by(name: 'DOI'),
category: Category.find_by(name: 'Transactional'),
confidence: 0.95
)
DetectorCategory.find_or_create_by(
detector: Detector.find_by(name: 'ISBN'),
category: Category.find_by(name: 'Transactional'),
confidence: 0.8
)
DetectorCategory.find_or_create_by(
detector: Detector.find_by(name: 'ISSN'),
category: Category.find_by(name: 'Transactional'),
confidence: 0.6
)
DetectorCategory.find_or_create_by(
detector: Detector.find_by(name: 'PMID'),
category: Category.find_by(name: 'Transactional'),
confidence: 0.95
)
DetectorCategory.find_or_create_by(
detector: Detector.find_by(name: 'Journal'),
category: Category.find_by(name: 'Transactional'),
confidence: 0.2
)

Rails.logger.info('Seeding DB complete')
7 changes: 0 additions & 7 deletions test/fixtures/detectors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,23 @@
#
# id :integer not null, primary key
# name :string
# confidence :float
# created_at :datetime not null
# updated_at :datetime not null
#
doi:
name: 'DOI'
confidence: 0.95

isbn:
name: 'ISBN'
confidence: 0.8

issn:
name: 'ISSN'
confidence: 0.6

pmid:
name: 'PMID'
confidence: 0.95

journal:
name: 'Journal'
confidence: 0.2

suggestedresource:
name: 'SuggestedResource'
confidence: 0.95
1 change: 0 additions & 1 deletion test/models/detector_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#
# id :integer not null, primary key
# name :string
# confidence :float
# created_at :datetime not null
# updated_at :datetime not null
#
Expand Down

0 comments on commit b770c25

Please sign in to comment.