Skip to content

Commit 2f25645

Browse files
committed
PC-58 update select parameter menu
1 parent 8c1b6ef commit 2f25645

5 files changed

Lines changed: 96 additions & 27 deletions

File tree

app/admin/items.rb

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@
7272
f.inputs "Pricing Parameters" do
7373
f.input :formula_parameters, as: :hidden
7474
div class: "formula-preview" do
75-
span class: "formula-label" do
76-
"Calculation Formula:"
77-
end
7875
div class: "formula-preview" do
7976
span class: "formula-label" do
8077
"Calculation Formula:"
@@ -83,7 +80,7 @@
8380
span class: "formula" do
8481
item_key = f.object.persisted? ? f.object.id.to_s : "new"
8582
session_formula = controller.view_context.session.dig(:tmp_params, item_key, "calculation_formula")
86-
f.object.calculation_formula.presence || session_formula.presence || "No formula yet"
83+
session_formula.presence || f.object.calculation_formula.presence || "No formula yet"
8784
end
8885
end
8986
end
@@ -108,7 +105,14 @@
108105
end
109106
end
110107

111-
f.actions
108+
f.actions do
109+
f.action :submit, label: "Update Item"
110+
f.action :cancel, label: "Cancel", button_html: {
111+
class: "custom-cancel-button",
112+
id: "custom-cancel-button",
113+
data: { item_id: item_key }
114+
}
115+
end
112116
end
113117

114118
show do
@@ -186,7 +190,8 @@ def update
186190
session[:tmp_params]&.delete(item_key)
187191
redirect_to admin_item_path(@item), notice: "Item was successfully updated."
188192
else
189-
flash[:error] = "Failed to update item"
193+
apply_tmp_params(@item, session[:tmp_params][item_key]) if session[:tmp_params]&.key?(item_key)
194+
flash[:error] = "Failed to update item: #{@item.errors.full_messages.to_sentence}"
190195
render :edit
191196
end
192197
end
@@ -201,7 +206,8 @@ def apply_tmp_params(item, tmp)
201206
item.open_parameters_label = data[:open] || []
202207
item.pricing_options = data[:select] || {}
203208
item.formula_parameters = data[:formula_parameters] || []
204-
item.calculation_formula = data[:calculation_formula]
209+
Rails.logger.info "🧮 TMP calculation_formula = #{data[:calculation_formula]}"
210+
item.calculation_formula = data[:calculation_formula] || nil
205211

206212
item.is_open = item.open_parameters_label.any?
207213
item.is_selectable_options = item.pricing_options.any?
@@ -281,11 +287,22 @@ def apply_tmp_params(item, tmp)
281287
desc = params["option_description_#{i}"]
282288
val = params["option_value_#{i}"]
283289
next if desc.blank? || val.blank?
284-
290+
285291
sub_hash[desc] = val
286292
end
287-
store[:select][param_name] = sub_hash if sub_hash.any?
288-
293+
294+
value_label = params[:value_label].to_s.strip
295+
296+
if param_name.blank? || value_label.blank? || sub_hash.empty?
297+
flash[:error] = "Select Name, Value Label and at least one option are required"
298+
return redirect_back(fallback_location: edit_admin_item_path(@item))
299+
end
300+
301+
store[:select][param_name] = {
302+
"options" => sub_hash,
303+
"value_label" => value_label
304+
}
305+
289306
else
290307
flash[:error] = "Unknown parameter type"
291308
return redirect_back(fallback_location: edit_admin_item_path(@item))
@@ -384,29 +401,35 @@ def apply_tmp_params(item, tmp)
384401
session[:tmp_params] ||= {}
385402
session[:tmp_params]["new"] ||= {}
386403
session[:tmp_params]["new"]["calculation_formula"] = params[:calculation_formula]
387-
388-
flash[:notice] = "Formula saved (in session)!"
389-
redirect_to new_resource_path
390404
else
391405
@item = Item.find(params[:id])
406+
407+
session[:tmp_params] ||= {}
408+
session[:tmp_params][@item.id.to_s] ||= {}
409+
session[:tmp_params][@item.id.to_s]["calculation_formula"] = params[:calculation_formula]
410+
392411
@item.calculation_formula = params[:calculation_formula]
393-
394-
if @item.save
395-
flash[:notice] = "Formula saved!"
396-
else
397-
flash[:error] = "Failed to save formula: #{@item.errors.full_messages.to_sentence}"
398-
end
399-
400-
redirect_to edit_admin_item_path(@item)
401412
end
413+
414+
flash[:notice] = "Formula saved (in session)!"
415+
redirect_to params[:id] == "new" ? new_resource_path : edit_admin_item_path(params[:id])
402416
end
417+
403418

404419
member_action :clear_session, method: :post do
405-
if params[:id] == "new"
406-
session[:tmp_params]&.delete("new")
407-
Rails.logger.info "🧹 Session[:tmp_params][\"new\"] cleared via JS"
420+
item_key = params[:id].to_s
421+
if session[:tmp_params].present?
422+
Rails.logger.info "⚠️ TRYING TO DELETE session[:tmp_params][#{item_key}]"
423+
Rails.logger.info "🔍 BEFORE DELETE: #{session[:tmp_params][item_key].inspect}"
424+
425+
session[:tmp_params].delete(item_key)
426+
427+
Rails.logger.info "🧹 DELETED! AFTER: #{session[:tmp_params].inspect}"
428+
else
429+
Rails.logger.warn "⚠️ tmp_params session is empty, nothing to delete"
408430
end
409431

410432
head :ok
411433
end
434+
412435
end

app/assets/javascripts/active_admin.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,24 @@ document.addEventListener("DOMContentLoaded", function () {
120120
}
121121
});
122122

123+
124+
document.addEventListener("DOMContentLoaded", function () {
125+
const cancelButton = document.querySelector(".custom-cancel-button");
126+
127+
if (cancelButton) {
128+
cancelButton.addEventListener("click", function () {
129+
const itemId = cancelButton.dataset.itemId;
130+
131+
fetch(`/admin/items/${itemId}/clear_session`, {
132+
method: "POST",
133+
headers: {
134+
"Content-Type": "application/json",
135+
"X-CSRF-Token": document.querySelector('meta[name="csrf-token"]').content
136+
}
137+
}).then(() => {
138+
window.location.href = "/admin/items";
139+
});
140+
});
141+
}
142+
});
143+

app/assets/stylesheets/active_admin/forms/_select.scss

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,16 @@ form.filter_form {
5353
font-family: monospace;
5454
font-size: 14px;
5555
white-space: pre-wrap;
56-
}
56+
}
57+
58+
.custom-cancel-button {
59+
@extend .button;
60+
background: #f3f3f3!important;
61+
color: #333 !important;
62+
border: 1px solid #ccc !important;
63+
text-shadow: 0 1px 0 white !important;
64+
box-shadow: inset 0 1px 0 white, 0 1px 2px rgba(0, 0, 0, 0.05) !important;
65+
}
66+
67+
.custom-cancel-button:hover {
68+
background: #e6e6e6 !important; }

app/views/admin/items/_parameters.html.erb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@
6868

6969
<% if select_parameters.any? %>
7070
<h3>Select Parameters</h3>
71-
<% select_parameters.each do |sel_name, options| %>
71+
<% select_parameters.each do |sel_name, raw_param_data| %>
72+
<% param_data = raw_param_data.with_indifferent_access %>
73+
<% value_label = param_data[:value_label] || "—" %>
74+
<% options_data = param_data[:options] || {} %>
75+
7276
<h4>
7377
Select name: <%= sel_name %>
7478
<% if mode == :edit %>
@@ -78,6 +82,9 @@
7882
) %>
7983
<% end %>
8084
</h4>
85+
86+
<p>Value Label: <%= value_label %></p>
87+
8188
<table class="parameter-table">
8289
<thead>
8390
<tr>
@@ -86,7 +93,7 @@
8693
</tr>
8794
</thead>
8895
<tbody>
89-
<% options.each do |desc, val| %>
96+
<% options_data.each do |desc, val| %>
9097
<tr>
9198
<td><%= desc %></td>
9299
<td><%= val %></td>
@@ -96,3 +103,4 @@
96103
</table>
97104
<% end %>
98105
<% end %>
106+

app/views/admin/items/new_parameter.html.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
<%= text_field_tag :select_parameter_name %>
3939
</div>
4040

41+
<div class="form-field">
42+
<%= label_tag :value_label, "Value Label" %>
43+
<%= text_field_tag :value_label, nil, class: "value-label-input" %>
44+
</div>
45+
4146
<% 10.times do |i| %>
4247
<div class="select-option-row">
4348
<div class="select-field">

0 commit comments

Comments
 (0)