Skip to content

Commit ba7cb6b

Browse files
committed
PC-50: Add validation for pricing option with value_label
1 parent 368bb41 commit ba7cb6b

2 files changed

Lines changed: 13 additions & 12 deletions

File tree

app/assets/javascripts/active_admin.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ document.addEventListener("DOMContentLoaded", function () {
7373
category_id: categoryField?.value || ""
7474
};
7575

76-
const itemId = link.dataset.itemId || "new"; // 💡 правильний спосіб
76+
const itemId = link.dataset.itemId || "new";
7777

7878
fetch(`/admin/items/${itemId}/save_meta_to_session`, {
7979
method: "POST",
@@ -84,7 +84,7 @@ document.addEventListener("DOMContentLoaded", function () {
8484
body: JSON.stringify(data)
8585
}).then(response => {
8686
if (response.ok) {
87-
window.location.href = link.dataset.redirect; // 💥 редірект тільки після успіху
87+
window.location.href = link.dataset.redirect;
8888
} else {
8989
alert("⚠️ Failed to save temporary data to session");
9090
}
@@ -162,7 +162,6 @@ document.addEventListener("DOMContentLoaded", () => {
162162
<button type="button" class="delete-option-button button light">Delete</button>
163163
`;
164164

165-
// кнопка Delete
166165
row.querySelector(".delete-option-button").addEventListener("click", () => {
167166
if (wrapper.querySelectorAll(".select-option-row").length > 2) {
168167
row.remove();
@@ -174,13 +173,12 @@ document.addEventListener("DOMContentLoaded", () => {
174173
return row;
175174
};
176175

177-
// Додати новий рядок
178176
addBtn.addEventListener("click", () => {
179177
const newRow = createOptionRow();
180178
wrapper.appendChild(newRow);
181179
});
182180

183-
// Додати обробники на існуючі 2 рядки
181+
184182
wrapper.querySelectorAll(".select-option-row").forEach((row) => {
185183
const delBtn = row.querySelector(".delete-option-button");
186184
delBtn.addEventListener("click", () => {

app/models/item.rb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Item < ApplicationRecord
55
validates :name, uniqueness: { scope: :category_id, message: "Item name must be unique within category" }
66
validate :category_must_be_active
77
validate :fixed_parameters_values_must_be_numeric
8-
# validate :pricing_options_values_must_be_numeric
8+
validate :pricing_options_values_must_be_numeric
99
validates :calculation_formula, presence: true, if: :requires_calculation_formula?
1010

1111
def self.ransackable_attributes(_auth_object = nil)
@@ -29,26 +29,29 @@ def fixed_parameters_values_must_be_numeric
2929
end
3030

3131
def pricing_options_values_must_be_numeric
32-
validate_jsonb_numeric_values(:pricing_options)
32+
validate_jsonb_numeric_values(:pricing_options) do |key_path, val|
33+
key_path.last != 'value_label' && !numeric?(val)
34+
end
3335
end
3436

35-
def validate_jsonb_numeric_values(attribute)
37+
def validate_jsonb_numeric_values(attribute, &block)
3638
value = self[attribute] || {}
3739
unless value.is_a?(Hash)
3840
errors.add(attribute, "must be a JSON object")
3941
return
4042
end
4143

42-
validate_nested_numeric_values(value, attribute)
44+
validate_nested_numeric_values(value, attribute, [], &block)
4345
end
4446

45-
def validate_nested_numeric_values(hash, attribute, key_path = [])
47+
def validate_nested_numeric_values(hash, attribute, key_path = [], &block)
4648
hash.each do |key, val|
4749
current_path = key_path + [key]
4850
if val.is_a?(Hash)
49-
validate_nested_numeric_values(val, attribute, current_path)
51+
validate_nested_numeric_values(val, attribute, current_path, &block)
5052
else
51-
errors.add(attribute, "value for '#{current_path.join(' -> ')}' must be a number") unless numeric?(val)
53+
should_error = block_given? ? yield(current_path, val) : !numeric?(val)
54+
errors.add(attribute, "value for '#{current_path.join(' -> ')}' must be a number") if should_error
5255
end
5356
end
5457
end

0 commit comments

Comments
 (0)