-
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.
This includes the database migration, but not an admin dashboard. Implementing scopes and constraints Fixing bc linter
- Loading branch information
1 parent
d5256c1
commit 70e8840
Showing
6 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
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,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 |
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,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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.