-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add in Sample::Query model which is a form object and refactor …
…the samples table component to no longer have a dependency on ransack
- Loading branch information
Showing
14 changed files
with
307 additions
and
214 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<%= render Viral::BaseComponent.new(tag: 'a', href: url, type: "button", classes: class_names('inline-flex items-center'), **system_arguments) do %> | ||
<%= label %> | ||
<span class="flex-grow-0 ml-2 flex items-center"><%= viral_icon(name: icon, classes: "w-4 h-4 inline-block") %></span> | ||
<% 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,20 @@ | ||
# frozen_string_literal: true | ||
|
||
# Component for sort in table headers | ||
class SortComponent < Component | ||
attr_reader :label, :field, :url, :system_arguments | ||
|
||
def initialize(sort:, label:, url:, field:, **system_arguments) | ||
@sort_key, @sort_direction = sort.split | ||
@label = label | ||
@url = url | ||
@field = field | ||
@system_arguments = system_arguments | ||
end | ||
|
||
def icon | ||
return unless @sort_key.to_s == URI.encode_www_form_component(@field.to_s) | ||
|
||
@sort_direction == 'asc' ? 'arrow_up' : 'arrow_down' | ||
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
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,52 @@ | ||
# frozen_string_literal: true | ||
|
||
# model to represent sample search form | ||
class Sample::Query # rubocop:disable Style/ClassAndModuleChildren | ||
include ActiveModel::Model | ||
include ActiveModel::Attributes | ||
|
||
attribute :column, :string | ||
attribute :direction, :string | ||
attribute :name_or_puid_cont, :string | ||
attribute :name_or_puid_in, default: -> { [] } | ||
attribute :project_ids, default: -> { [] } | ||
attribute :sort, :string, default: 'updated_at desc' | ||
|
||
validates :direction, inclusion: { in: %w[asc desc] } | ||
validates :project_ids, length: { minimum: 1 } | ||
|
||
def initialize(...) | ||
super | ||
self.sort = sort | ||
end | ||
|
||
def sort=(value) | ||
super | ||
column, direction = sort.split | ||
assign_attributes(column:, direction:) | ||
end | ||
|
||
def results | ||
return Sample.none unless valid? | ||
|
||
sort_samples.ransack(ransack_params).result | ||
end | ||
|
||
private | ||
|
||
def ransack_params | ||
{ | ||
name_or_puid_cont: name_or_puid_cont, | ||
name_or_puid_in: name_or_puid_in | ||
}.compact | ||
end | ||
|
||
def sort_samples(scope = Sample.where(project_id: project_ids)) | ||
if column.starts_with? 'metadata_' | ||
field = column.gsub('metadata_', '') | ||
scope.order(Sample.metadata_sort(field, dir)) | ||
else | ||
scope.order("#{column} #{direction}") | ||
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
Oops, something went wrong.