-
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.
Refactor SuggestedResource to leverage new Fingerprint model
Why these changes are being introduced: We [recently decided](#145) to make a separate Fingerprint model, associated with Term, as multiple detectors are likely to use fringerprinting (implemented in #138). We have also begun to split the ActiveRecord components of detectors into separate models (implemented for Detector::Journal in #162). Relevant ticket(s): * [TCO-111](https://mitlibraries.atlassian.net/browse/TCO-111) * [TCO-122](https://mitlibraries.atlassian.net/browse/TCO-122) How this addresses that need: * Splits the ActiveRecord components of Detector::SuggestedResource into a separate SuggestedResource model. * Associates SuggestedResource with Fingerprint, via Term, such that a suggested resource can have multiple terms and fingerprints. * Removes the suggested resource dashboard (see side effects). Side effects of this change: * Terms that are associated with a suggested resource should not be destroyed. Rails does not allow the `:dependent` option on `belongs_to` associations, so this commit instead adds a `before_destroy` hook with a custom method that aborts the callback and logs the attempt in Sentry. * Because administrate does not handle has_many relationships well, we will need to build a custom dashboard to manage suggested resources. This is ticketed as [TCO-145](https://mitlibraries.atlassian.net/browse/TCO-145). Until that UI is ready, we will use the Rails console to make any requested changes to suggested resources.
- Loading branch information
Showing
20 changed files
with
214 additions
and
325 deletions.
There are no files selected for viewing
This file was deleted.
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,33 @@ | ||
# frozen_string_literal: true | ||
|
||
# SuggestedResource stores custom hints that we want to send to the | ||
# user in response to specific strings. For example, a search for "web of | ||
# science" should be met with our custom login link to Web of Science via MIT. | ||
class SuggestedResource < ApplicationRecord | ||
has_many :terms | ||
has_many :fingerprints, through: :terms, dependent: :nullify | ||
|
||
# This replaces all current SuggestedResource records with a new set from an imported CSV. | ||
# | ||
# @note This method is called by the suggested_resource:reload rake task. | ||
# | ||
# @param input [CSV::Table] An imported CSV file containing all Suggested Resource records. The CSV file must have | ||
# at least three headers, named "Title", "URL", and "Phrase". Please note: these values | ||
# are case sensitive. | ||
def self.bulk_replace(input) | ||
raise ArgumentError.new, 'Tabular CSV is required' unless input.instance_of?(CSV::Table) | ||
|
||
# Need to check what columns exist in input | ||
required_headers = %w[Title URL Phrase] | ||
missing_headers = required_headers - input.headers | ||
raise ArgumentError.new, "Some CSV columns missing: #{missing_headers}" unless missing_headers.empty? | ||
|
||
SuggestedResource.destroy_all | ||
|
||
input.each do |line| | ||
record = SuggestedResource.new({ title: line['Title'], url: line['URL'] }) | ||
record.save | ||
record.terms.find_or_create_by(phrase: line['Phrase']) | ||
end | ||
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
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,10 @@ | ||
class CreateSuggestedResources < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :suggested_resources do |t| | ||
t.string :title | ||
t.string :url | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
18 changes: 18 additions & 0 deletions
18
db/migrate/20250107204813_drop_detector_suggested_resources.rb
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,18 @@ | ||
class DropDetectorSuggestedResources < ActiveRecord::Migration[7.1] | ||
def up | ||
drop_table :detector_suggested_resources | ||
end | ||
|
||
def down | ||
create_table :detector_suggested_resources do |t| | ||
t.string :title | ||
t.string :url | ||
t.string :phrase | ||
t.string :fingerprint | ||
|
||
t.timestamps | ||
end | ||
add_index :detector_suggested_resources, :phrase, unique: true | ||
add_index :detector_suggested_resources, :fingerprint, unique: true | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
db/migrate/20250107213433_add_suggested_resource_to_terms.rb
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,11 @@ | ||
class AddSuggestedResourceToTerms < ActiveRecord::Migration[7.1] | ||
def up | ||
add_reference :terms, :suggested_resource | ||
add_foreign_key :terms, :suggested_resources, on_delete: :nullify | ||
end | ||
|
||
def down | ||
remove_foreign_key :terms, :suggested_resource, on_delete: :nullify | ||
remove_reference :terms, :suggested_resources | ||
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
Oops, something went wrong.