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:"
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
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+
412435end
0 commit comments