Skip to content

Commit

Permalink
Added validation for at least one formula in calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
SleekMutt committed Nov 22, 2024
1 parent 9130d91 commit ea16bd0
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/models/calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,15 @@ class Calculator < ApplicationRecord
validates :en_name, :uk_name, presence: true
validates :en_name, :uk_name, length: { minimum: 3, maximum: 50 }
validates :slug, presence: true, uniqueness: true
validate :must_have_at_least_one_formula

def self.ransackable_attributes(auth_object = nil)
["created_at", "id", "name", "preferable", "slug", "updated_at", "uuid"]
end

def must_have_at_least_one_formula
return if formulas.present?

errors.add(:formulas, :not_present)
end
end
1 change: 1 addition & 0 deletions app/views/account/calculators/partials/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<!-- formula input-->
<div id="formulas" class="space-y-4">
<%= f.error_notification message: f.object.errors[:formulas].to_sentence if f.object.errors[:formulas].present? %>
<%= f.simple_fields_for :formulas do |formula_fields| %>
<%= render "account/calculators/partials/formula_fields", f: formula_fields %>
<% end %>
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,8 @@ en:
too_long: "is too long (maximum is %{count} characters)"
invalid: "contains invalid characters"
taken: "has already been taken, please choose another one"
formulas:
not_present: "Calculator must have at least one formula present"
category:
attributes:
name:
Expand Down
2 changes: 2 additions & 0 deletions config/locales/uk/uk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,8 @@ uk:
other: "занадто коротка (мінімум %{count} знаків)"
taken: "вже викориcтовується, будь ласка, оберіть іншу"
invalid: "містить неприпустимі символи"
formulas:
not_present: "Калькулятор має містити принаймні одну формулу"
category:
attributes:
uk_name: &name_errors
Expand Down

0 comments on commit ea16bd0

Please sign in to comment.