Skip to content

Commit

Permalink
Add Detections model
Browse files Browse the repository at this point in the history
This includes the database migration, but not an admin dashboard.

Implementing scopes and constraints
  • Loading branch information
matt-bernhardt committed Sep 18, 2024
1 parent d5256c1 commit 91438d2
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"postdeploy": "bundle exec rails db:seed"
},
"env": {
"DETECTOR_VERSION": {
"required": false
},
"LINKRESOLVER_BASEURL": {
"required": false
},
Expand Down
36 changes: 36 additions & 0 deletions app/models/detection.rb
Original file line number Diff line number Diff line change
@@ -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['DETECTOR_VERSION']) }
scope :for_detector, -> (detector) { where(detector_id: detector.id) }
scope :for_term, -> (term) { where(term_id: term.id) }

private

def check_for_current
if Detection.for_term(self.term).for_detector(self.detector).current.exists?
errors.add(:detector_version, "already exists")
throw(:abort)
end
end

def set_defaults
self.detector_version = ENV['DETECTOR_VERSION'] || 'unset'
end
end
1 change: 1 addition & 0 deletions app/models/detector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
38 changes: 38 additions & 0 deletions app/models/term.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
if result.any?
Detection.create(
term: self,
detector: Detector.where("name = 'Journal'").first
)
end
end

def record_suggested_resources
result = Detector::SuggestedResource.full_term_match(phrase)
if result.any?
Detection.create(
term: self,
detector: Detector.where("name = 'SuggestedResource'").first
)
end
end
end
13 changes: 13 additions & 0 deletions db/migrate/20240917160025_create_detections.rb
Original file line number Diff line number Diff line change
@@ -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
16 changes: 15 additions & 1 deletion db/schema.rb

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

0 comments on commit 91438d2

Please sign in to comment.