Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added Sop.related_samples #2054

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/data_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class DataFile < ApplicationRecord

belongs_to :file_template
has_many :extracted_samples, class_name: 'Sample', foreign_key: :originating_data_file_id
has_many :sample_resource_links, -> { where(resource_type: 'DataFile') }, class_name: 'SampleResourceLink', foreign_key: :resource_id
has_many :sample_resource_links, -> { where(resource_type: 'DataFile') }, foreign_key: :resource_id
has_many :linked_samples, through: :sample_resource_links, source: :sample

has_many :unzipped_files, class_name: 'DataFile', foreign_key: :zip_origin_id
Expand Down
7 changes: 6 additions & 1 deletion app/models/sop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ class Sop < ApplicationRecord

#don't add a dependent=>:destroy, as the content_blob needs to remain to detect future duplicates
has_one :content_blob, -> (r) { where('content_blobs.asset_version =? AND deleted=?', r.version, false) }, :as => :asset, :foreign_key => :asset_id

has_and_belongs_to_many :workflows
has_many :sample_resource_links, -> { where(resource_type: 'Sop') }, foreign_key: :resource_id
has_many :linked_samples, through: :sample_resource_links, source: :sample

has_filter assay_type: Seek::Filtering::Filter.new(
value_field: 'assays.assay_type_uri',
Expand Down Expand Up @@ -50,4 +51,8 @@ def use_mime_type_for_avatar?
true
end

def related_samples
linked_samples
end

end
23 changes: 23 additions & 0 deletions test/unit/sop_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -343,4 +343,27 @@ def test_project_for_sop_and_sop_version_match
refute v3.can_change_visibility?
end

test 'related samples' do
project = FactoryBot.create(:project)

sample_type = FactoryBot.create(:sop_sample_type, project_ids: [project.id])
sop_attr = FactoryBot.build(:sop_sample_attribute, title: 'sop 2', sample_type: sample_type)
sample_type.sample_attributes << sop_attr
sop = FactoryBot.create(:sop)
another_sop = FactoryBot.create(:sop)
sop_without_samples = FactoryBot.create(:sop)

sample1 = Sample.new(sample_type: sample_type, project_ids: [project.id])
sample1.update(data: { 'sop': sop.id })
sample1.update(data: { 'sop 2': another_sop.id })
sample1.save!
sample2 = Sample.new(sample_type: sample_type, project_ids: [project.id])
sample2.update(data: { 'sop': another_sop.id })
sample2.update(data: { 'sop 2': sop.id })
sample2.save!

assert_empty sop_without_samples.related_samples
assert_equal [sample1, sample2].sort_by(&:id), sop.related_samples.sort_by(&:id)
end

end