Skip to content

Commit

Permalink
applying i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
ksierks committed Jan 10, 2025
1 parent 72985ea commit b337d94
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/components/advanced_search/group.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<div id="group-error-messages-<%= @group_index %>"></div>
<div
data-advanced-search-target="groupsContainer"
class="bg-slate-100 border border-gray-300 rounded-lg"
>
<div id="group-error-messages-<%= @group_index %>"></div>
<%= @form.fields_for :groups, @group do |groups_form| %>
<% @group.conditions.each do |condition| %>
<%= render AdvancedSearch::Condition.new(
Expand Down
3 changes: 3 additions & 0 deletions app/components/advanced_search_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
<p class="text-base leading-relaxed text-slate-500 dark:text-slate-400">
<%= t(".description") %>
</p>
<p class="text-base leading-relaxed text-slate-500 dark:text-slate-400">
<%= t(".rules") %>
</p>
<% @search.groups.each_with_index do |group, group_index| %>
<%= render AdvancedSearch::Group.new(
form: @form,
Expand Down
24 changes: 16 additions & 8 deletions app/validators/advanced_search_group_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def validate(record)
end
return if groups_valid

record.errors.add :base, 'There are group errors.'
record.errors.add :base, I18n.t('validators.advanced_search_group_validator.base_error')
end

private
Expand All @@ -22,15 +22,15 @@ def validate_blank_fields(record, group, group_index)
elsif group.conditions.any? do |condition|
condition.field.blank? || condition.operator.blank? || condition.value.blank?
end
record.groups[group_index].errors.add :base, 'Fields, operators, and values cannot be blank.'
record.groups[group_index].errors.add :base, I18n.t('validators.advanced_search_group_validator.blank_error')
end
end

def validate_blank_fields_for_condition(record, group, group_index)
condition = group.conditions.first
unless (condition.field.blank? && condition.operator.blank? && condition.value.blank?) ||
(condition.field.present? && condition.operator.present? && condition.value.present?)
record.groups[group_index].errors.add :base, 'Fields, operators, and values cannot be blank.'
record.groups[group_index].errors.add :base, I18n.t('validators.advanced_search_group_validator.blank_error')
end
end

Expand Down Expand Up @@ -62,32 +62,40 @@ def validate_unique_field(record, group, group_index, unique_field)
def validate_contains(record, group_index, unique_field, unique_field_conditions)
return if unique_field_conditions.count == 1

record.groups[group_index].errors.add :base, "'#{unique_field}' can use 'contains' only once."
record.groups[group_index].errors.add :base,
I18n.t('validators.advanced_search_group_validator.contains_error',
unique_field:)
end

def validate_equals(record, group_index, unique_field, unique_field_conditions)
return if unique_field_conditions.all? { |condition| condition.operator == '=' }

record.groups[group_index].errors.add :base, "'#{unique_field}' cannot use '=' with other operators."
record.groups[group_index].errors.add :base, I18n.t('validators.advanced_search_group_validator.equals_error',
unique_field:)
end

def validate_not_equals(record, group_index, unique_field, unique_field_conditions)
return if unique_field_conditions.all? { |condition| condition.operator == '!=' }

record.groups[group_index].errors.add :base, "'#{unique_field}' cannot use '!=' with other operators."
record.groups[group_index].errors.add :base, I18n.t('validators.advanced_search_group_validator.not_equals_error',
unique_field:)
end

def validate_less_than_equals(record, group_index, unique_field, unique_field_conditions)
unless unique_field_conditions.count == 1 ||
(unique_field_conditions.count == 2 && unique_field_conditions[1].operator == '>=')
record.groups[group_index].errors.add :base, "'#{unique_field}' can use '<=' and '>=' only once."
record.groups[group_index].errors.add :base,
I18n.t('validators.advanced_search_group_validator.between_error',
unique_field:)
end
end

def validate_greater_than_equals(record, group_index, unique_field, unique_field_conditions)
unless unique_field_conditions.count == 1 ||
(unique_field_conditions.count == 2 && unique_field_conditions[1].operator == '<=')
record.groups[group_index].errors.add :base, "'#{unique_field}' can use '<=' and '>=' only once."
record.groups[group_index].errors.add :base,
I18n.t('validators.advanced_search_group_validator.between_error',
unique_field:)
end
end
end
2 changes: 1 addition & 1 deletion app/views/projects/samples/search/_errors.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% if errors.any? %>
<%= viral_alert(type: "alert", classes: "mb-4 h-30 overflow-y-auto") do %>
<%= viral_alert(type: "alert", classes: "h-30 rounded-t-lg overflow-y-auto ") do %>
<ul class="mt-1.5 ml-4 list-disc list-inside">
<% errors.each do |error| %>
<li><%= error.full_message %></li>
Expand Down
11 changes: 10 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,12 @@ en:
add_group_button: Add Group
apply_filter_button: Apply filter
clear_filter_button: Clear filter
description: This advanced search uses fields and operators to filter samples. Add and/or conditions to define the search criteria. To filter by date range, the metadata field should end with '_date'.
description: This advanced search uses fields, operators and values to filter samples. Add groups with conditions to define the search criteria.
field: Field
operator: Operator
remove_condition_aria_label: Remove condition
remove_group_button: Remove group
rules: Operators cannot be used on the same field within a group more than once, except for '=' and '!='. To filter by date range, the metadata field should end with '_date'.
title: Advanced search
value: Value
application:
Expand Down Expand Up @@ -1697,6 +1698,14 @@ en:
abbreviated: "%a %b%e %Y %H:%M"
default: "%Y-%m-%d %H:%M:%S"
full_date: "%B %d, %Y"
validators:
advanced_search_group_validator:
base_error: There are group errors.
between_error: "'%{unique_field}' can use '>=' and '<=' only once."
blank_error: Fields, operators, and values cannot be blank.
contains_error: "'%{unique_field}' can use 'contains' only once."
equals_error: "'%{unique_field}' cannot use '=' with other operators."
not_equals_error: "'%{unique_field}' cannot use '!=' with other operators."
viral:
pagy:
limit_component:
Expand Down
11 changes: 10 additions & 1 deletion config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,12 @@ fr:
add_group_button: Add Group
apply_filter_button: Apply filter
clear_filter_button: Clear filter
description: This advanced search uses fields and operators to filter samples. Add and/or conditions to define the search criteria. To filter by date range, the metadata field should end with '_date'.
description: This advanced search uses fields, operators and values to filter samples. Add groups with conditions to define the search criteria.
field: Field
operator: Operator
remove_condition_aria_label: Remove condition
remove_group_button: Remove group
rules: Operators cannot be used on the same field within a group more than once, except for '=' and '!='. To filter by date range, the metadata field should end with '_date'.
title: Advanced search
value: Value
application:
Expand Down Expand Up @@ -1697,6 +1698,14 @@ fr:
abbreviated: "%a %b%e %Y %H:%M"
default: "%Y-%m-%d %H:%M:%S"
full_date: "%B %d, %Y"
validators:
advanced_search_group_validator:
base_error: There are group errors.
between_error: "'%{unique_field}' can use '<=' and '>=' only once."
blank_error: Fields, operators, and values cannot be blank.
contains_error: "'%{unique_field}' can use 'contains' only once."
equals_error: "'%{unique_field}' cannot use '=' with other operators."
not_equals_error: "'%{unique_field}' cannot use '!=' with other operators."
viral:
pagy:
limit_component:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def default # rubocop:disable Metrics/MethodLength
groups: [
Sample::Group.new(
conditions: [
Sample::Condition.new(field: ' metadata.country', operator: '=', value: 'Canada'),
Sample::Condition.new(field: 'metadata.country', operator: '=', value: 'Canada'),
Sample::Condition.new(field: 'metadata.collection_date', operator: '>=', value: '2024-01-01'),
Sample::Condition.new(field: 'metadata.collection_date', operator: '<=', value: '2024-12-01')
]
Expand Down

0 comments on commit b337d94

Please sign in to comment.