-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(chunk_references): add course_material_text_chunk_references
- Loading branch information
Showing
5 changed files
with
91 additions
and
19 deletions.
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
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 |
---|---|---|
@@ -1,10 +1,24 @@ | ||
# frozen_string_literal: true | ||
class Course::Material::TextChunk < ApplicationRecord | ||
has_neighbors :embedding | ||
belongs_to :material, inverse_of: :text_chunks, class_name: 'Course::Material', | ||
foreign_key: :course_material_id, autosave: true | ||
validates :creator, presence: true | ||
validates :content, presence: true | ||
validates :embedding, presence: true | ||
validates :course_id, presence: true | ||
validates :name, presence: true | ||
has_many :text_chunk_references, class_name: 'Course::Material::TextChunkReference', | ||
dependent: :destroy | ||
|
||
class << self | ||
def existing_chunks(attributes) | ||
file = attributes.delete(:file) | ||
attributes[:name] = file_digest(file) | ||
where(attributes) | ||
end | ||
|
||
private | ||
|
||
def file_digest(file) | ||
# Get the actual file by #tempfile if the file is an `ActionDispatch::Http::UploadedFile`. | ||
Digest::SHA256.file(file.try(:tempfile) || file).hexdigest | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
class Course::Material::TextChunkReference < ApplicationRecord | ||
include DuplicationStateTrackingConcern | ||
|
||
validates :creator, presence: true | ||
validates :updater, presence: true | ||
validates :text_chunk, presence: true | ||
belongs_to :text_chunk, inverse_of: :text_chunk_references, | ||
class_name: 'Course::Material::TextChunk' | ||
belongs_to :material, inverse_of: :text_chunk_references, class_name: 'Course::Material' | ||
after_destroy :destroy_text_chunk_if_no_references_left | ||
|
||
def initialize_duplicate(duplicator, other) | ||
self.material = duplicator.duplicate(other.material) | ||
self.updated_at = other.updated_at | ||
self.created_at = other.created_at | ||
self.text_chunk = other.text_chunk | ||
set_duplication_flag | ||
end | ||
|
||
private | ||
|
||
def destroy_text_chunk_if_no_references_left | ||
# Check if there are no other references left for the TextChunk | ||
return unless text_chunk.text_chunk_references.count == 0 | ||
|
||
text_chunk.destroy # This will delete the TextChunk if no references exist | ||
end | ||
end |