diff --git a/app.json b/app.json index 0d6e5f9..4bb3f32 100644 --- a/app.json +++ b/app.json @@ -5,6 +5,9 @@ "postdeploy": "bundle exec rails db:seed" }, "env": { + "DETECTOR_VERSION": { + "required": false + }, "LINKRESOLVER_BASEURL": { "required": false }, diff --git a/app/models/detection.rb b/app/models/detection.rb new file mode 100644 index 0000000..ca2a0a5 --- /dev/null +++ b/app/models/detection.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +# == Schema Information +# +# Table name: detections +# +# id :integer not null, primary key +# term_id :integer not null +# detector_id :integer not null +# detector_version :string +# created_at :datetime not null +# updated_at :datetime not null +# +class Detection < ApplicationRecord + belongs_to :term + belongs_to :detector + + before_create :set_defaults, :check_for_current + + scope :current, -> { where(detector_version: ENV.fetch('DETECTOR_VERSION', 'unset')) } + scope :for_detector, ->(detector) { where(detector_id: detector.id) } + scope :for_term, ->(term) { where(term_id: term.id) } + + private + + def check_for_current + return unless Detection.for_term(term).for_detector(detector).current.exists? + + errors.add(:detector_version, 'already exists') + throw(:abort) + end + + def set_defaults + self.detector_version = ENV.fetch('DETECTOR_VERSION', 'unset') + end +end diff --git a/app/models/detector.rb b/app/models/detector.rb index b5204a6..7745e69 100644 --- a/app/models/detector.rb +++ b/app/models/detector.rb @@ -12,4 +12,5 @@ class Detector < ApplicationRecord has_many :detector_categories, dependent: :destroy has_many :categories, through: :detector_categories + has_many :detections, dependent: :destroy end diff --git a/app/models/term.rb b/app/models/term.rb index ce759d4..48d0e3a 100644 --- a/app/models/term.rb +++ b/app/models/term.rb @@ -11,4 +11,42 @@ # class Term < ApplicationRecord has_many :search_events, dependent: :destroy + has_many :detections, dependent: :destroy + + def record_detections + record_patterns + record_journals + record_suggested_resources + end + + def record_patterns + si = Detector::StandardIdentifiers.new(phrase) + + si.identifiers.each_key do |k| + Detection.create( + term: self, + detector: Detector.where("name = '#{k.to_s.upcase}'").first + ) + end + end + + def record_journals + result = Detector::Journal.full_term_match(phrase) + return unless result.any? + + Detection.create( + term: self, + detector: Detector.where("name = 'Journal'").first + ) + end + + def record_suggested_resources + result = Detector::SuggestedResource.full_term_match(phrase) + return unless result.any? + + Detection.create( + term: self, + detector: Detector.where("name = 'SuggestedResource'").first + ) + end end diff --git a/db/migrate/20240917160025_create_detections.rb b/db/migrate/20240917160025_create_detections.rb new file mode 100644 index 0000000..5edabe7 --- /dev/null +++ b/db/migrate/20240917160025_create_detections.rb @@ -0,0 +1,13 @@ +class CreateDetections < ActiveRecord::Migration[7.1] + def change + create_table :detections do |t| + t.belongs_to :term, null: false, foreign_key: true + t.belongs_to :detector, null: false, foreign_key: true + t.string :detector_version + + t.timestamps + end + add_index :detections, [:term_id, :detector_id, :detector_version], unique: true + add_index :detections, [:detector_id, :term_id, :detector_version], unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index f5ceb25..b092c8e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_09_09_183613) do +ActiveRecord::Schema[7.1].define(version: 2024_09_17_160025) do create_table "categories", force: :cascade do |t| t.string "name" t.text "description" @@ -19,6 +19,18 @@ t.index ["name"], name: "index_categories_on_name", unique: true end + create_table "detections", force: :cascade do |t| + t.integer "term_id", null: false + t.integer "detector_id", null: false + t.string "detector_version" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["detector_id", "term_id", "detector_version"], name: "idx_on_detector_id_term_id_detector_version_2afa383b1f", unique: true + t.index ["detector_id"], name: "index_detections_on_detector_id" + t.index ["term_id", "detector_id", "detector_version"], name: "idx_on_term_id_detector_id_detector_version_03898e846f", unique: true + t.index ["term_id"], name: "index_detections_on_term_id" + end + create_table "detector_categories", force: :cascade do |t| t.integer "detector_id", null: false t.integer "category_id", null: false @@ -96,6 +108,8 @@ t.index ["uid"], name: "index_users_on_uid", unique: true end + add_foreign_key "detections", "detectors" + add_foreign_key "detections", "terms" add_foreign_key "detector_categories", "categories" add_foreign_key "detector_categories", "detectors" end