From 68ee9e2c57c0e63f9f0438c2b53751f9eea99240 Mon Sep 17 00:00:00 2001
From: davidg73 <david.gunnarsson@univie.ac.at>
Date: Tue, 27 May 2025 17:54:36 +0200
Subject: [PATCH] Make sample searchable

---
 app/avo/resources/image_color.rb    | 2 +-
 app/avo/resources/sample.rb         | 6 +++---
 app/models/sample.rb                | 4 ++++
 app/policies/sample_policy.rb       | 4 ++++
 spec/policies/sample_policy_spec.rb | 4 ++++
 5 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/app/avo/resources/image_color.rb b/app/avo/resources/image_color.rb
index af72e27..e9447d3 100644
--- a/app/avo/resources/image_color.rb
+++ b/app/avo/resources/image_color.rb
@@ -6,7 +6,7 @@ class Avo::Resources::ImageColor < Avo::BaseResource
   # }
 
   def fields
-    field :sample, as: :belongs_to
+    field :sample, as: :belongs_to, searchable: true
     field :hue, as: :number
     field :saturation, as: :number
     field :brightness, as: :number
diff --git a/app/avo/resources/sample.rb b/app/avo/resources/sample.rb
index c2be836..e386baf 100644
--- a/app/avo/resources/sample.rb
+++ b/app/avo/resources/sample.rb
@@ -4,9 +4,9 @@ class Avo::Resources::Sample < Avo::BaseResource
 
   # self.includes = []
   # self.attachments = []
-  # self.search = {
-  #   query: -> { query.ransack(id_eq: params[:q], m: "or").result(distinct: false) }
-  # }
+  self.search = {
+    query: -> { query.ransack(sample_no_cont: params[:q], m: "or").result(distinct: false) }
+  }
 
   def fields
     field :sample_no, as: :text, sortable: true
diff --git a/app/models/sample.rb b/app/models/sample.rb
index 0131a0f..56b89a1 100644
--- a/app/models/sample.rb
+++ b/app/models/sample.rb
@@ -18,6 +18,10 @@ class Sample < ApplicationRecord
 
   validates :sample_no, presence: true
 
+  def self.ransackable_attributes(auth_object = nil)
+    %w[sample_no]
+  end
+
   serialize :analysis_method_ids, coder: MultiValueWrapper
   serialize :voids_form_ids, coder: MultiValueWrapper
   serialize :sorting_ids, coder: MultiValueWrapper
diff --git a/app/policies/sample_policy.rb b/app/policies/sample_policy.rb
index 1b78a67..1688e9f 100644
--- a/app/policies/sample_policy.rb
+++ b/app/policies/sample_policy.rb
@@ -5,6 +5,10 @@ class SamplePolicy < ApplicationPolicy
     user? && user.apprentice?
   end
 
+  def search?
+    user? && user.apprentice?
+  end
+
   class Scope < ApplicationPolicy::Scope
     def resolve
       scope.all
diff --git a/spec/policies/sample_policy_spec.rb b/spec/policies/sample_policy_spec.rb
index a473b45..3bf43d5 100644
--- a/spec/policies/sample_policy_spec.rb
+++ b/spec/policies/sample_policy_spec.rb
@@ -12,6 +12,7 @@ RSpec.describe SamplePolicy do
     it { is_expected.to forbid_new_and_create_actions }
     it { is_expected.to forbid_edit_and_update_actions }
     it { is_expected.to forbid_action(:destroy) }
+    it { is_expected.to forbid_action(:search) }
   end
 
   context "for an apprentice" do
@@ -22,6 +23,7 @@ RSpec.describe SamplePolicy do
     it { is_expected.to permit_new_and_create_actions }
     it { is_expected.to permit_edit_and_update_actions }
     it { is_expected.to permit_action(:destroy) }
+    it { is_expected.to permit_action(:search) }
   end
 
   context "for an editor" do
@@ -32,6 +34,7 @@ RSpec.describe SamplePolicy do
     it { is_expected.to permit_new_and_create_actions }
     it { is_expected.to permit_edit_and_update_actions }
     it { is_expected.to permit_action(:destroy) }
+    it { is_expected.to permit_action(:search) }
   end
 
   context "for an admin" do
@@ -42,6 +45,7 @@ RSpec.describe SamplePolicy do
     it { is_expected.to permit_new_and_create_actions }
     it { is_expected.to permit_edit_and_update_actions }
     it { is_expected.to permit_action(:destroy) }
+    it { is_expected.to permit_action(:search) }
   end
 
   describe "Scope" do
-- 
GitLab