|
7 | 7 | # |
8 | 8 | # Table name: terms |
9 | 9 | # |
10 | | -# id :integer not null, primary key |
11 | | -# phrase :string |
12 | | -# created_at :datetime not null |
13 | | -# updated_at :datetime not null |
14 | | -# flag :boolean |
| 10 | +# id :integer not null, primary key |
| 11 | +# phrase :string |
| 12 | +# created_at :datetime not null |
| 13 | +# updated_at :datetime not null |
| 14 | +# flag :boolean |
| 15 | +# fingerprint_id :integer |
15 | 16 | # |
16 | 17 | class Term < ApplicationRecord |
17 | 18 | has_many :search_events, dependent: :destroy |
18 | 19 | has_many :detections, dependent: :destroy |
19 | 20 | has_many :categorizations, dependent: :destroy |
20 | 21 | has_many :confirmations, dependent: :destroy |
| 22 | + belongs_to :fingerprint, optional: true |
| 23 | + |
| 24 | + before_save :register_fingerprint |
| 25 | + after_destroy :check_fingerprint_count |
21 | 26 |
|
22 | 27 | scope :user_confirmed, -> { where.associated(:confirmations).distinct } |
23 | 28 | scope :user_unconfirmed, -> { where.missing(:confirmations).distinct } |
24 | 29 |
|
| 30 | + # The fingerprint method returns the constructed fingerprint field from the related Fingerprint record. In the |
| 31 | + # rare condition when no Fingerprint record exists, this method returns Nil. |
| 32 | + delegate :fingerprint_value, to: :fingerprint, allow_nil: true |
| 33 | + |
| 34 | + # The cluster method returns an array of all Term records which share a fingerprint with the current term. The term |
| 35 | + # itself is not returned, so if a term has no related records, this method returns an empty array. |
| 36 | + # |
| 37 | + # @note In the rare case when a Term has no fingerprint, this method returns Nil. |
| 38 | + # |
| 39 | + # @return array |
| 40 | + def cluster |
| 41 | + fingerprint&.terms&.filter { |rel| rel != self } |
| 42 | + end |
| 43 | + |
25 | 44 | # The record_detections method is the one-stop method to call every Detector's record method that is defined within |
26 | 45 | # the application. |
27 | 46 | # |
@@ -68,6 +87,22 @@ def calculate_categorizations |
68 | 87 |
|
69 | 88 | private |
70 | 89 |
|
| 90 | + # register_fingerprint method gets called before a Term record is saved, ensuring that Terms should always have a |
| 91 | + # related Fingerprint method. |
| 92 | + def register_fingerprint |
| 93 | + new_record = { |
| 94 | + value: Fingerprint.calculate(phrase) |
| 95 | + } |
| 96 | + self.fingerprint = Fingerprint.find_or_create_by(new_record) |
| 97 | + end |
| 98 | + |
| 99 | + # This is called during the after_destroy hook. If removing that term means that its fingerprint is now abandoned, |
| 100 | + # then we destroy the fingerprint too. In the rare case when a Term does not have a fingerprint, this method does not |
| 101 | + # cause problems because of the safe operators in the conditional. |
| 102 | + def check_fingerprint_count |
| 103 | + fingerprint.destroy if fingerprint&.terms&.count&.zero? |
| 104 | + end |
| 105 | + |
71 | 106 | # This method looks up all current detections for the given term, and assembles their confidence scores in a format |
72 | 107 | # usable by the calculate_categorizations method. It exists to transform data like: |
73 | 108 | # [{3=>0.91}, {1=>0.1}] and [{3=>0.95}] |
|
0 commit comments