From 88797bc1653a23a26cc72d6fee66628bf9a1039e Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Mon, 3 Nov 2025 06:07:35 +0000 Subject: [PATCH] [WV2] Refactor outfit state params helper --- app/helpers/outfits_helper.rb | 21 +++++++++------------ app/models/outfit.rb | 15 +++++++++++++++ app/views/outfits/new_v2.html.haml | 4 ++-- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/app/helpers/outfits_helper.rb b/app/helpers/outfits_helper.rb index f8c9523a..13f36159 100644 --- a/app/helpers/outfits_helper.rb +++ b/app/helpers/outfits_helper.rb @@ -66,24 +66,21 @@ module OutfitsHelper end # Generate hidden fields to preserve outfit state in URL params. - # Options: - # - exclude_item_id: Item ID to exclude from objects[] array - def outfit_state_params(exclude_item_id: nil) + # Use the `except` parameter to skip certain fields, e.g. to override + # them with specific values, like in the species/color picker. + def outfit_state_params(outfit = @outfit, except: []) fields = [] - # Preserve species and color - fields << hidden_field_tag(:species, params[:species]) if params[:species].present? - fields << hidden_field_tag(:color, params[:color]) if params[:color].present? + fields << hidden_field_tag(:species, @outfit.species_id) unless except.include?(:species) + fields << hidden_field_tag(:color, @outfit.color_id) unless except.include?(:color) - # Preserve item IDs, optionally excluding one - if params[:objects].present? - params[:objects].each do |item_id| - next if exclude_item_id && item_id.to_s == exclude_item_id.to_s - fields << hidden_field_tag("objects[]", item_id) + unless except.include?(:worn_items) + outfit.worn_items.each do |item| + fields << hidden_field_tag('objects[]', item.id) end end - safe_join(fields) + safe_join fields end def outfit_viewer(...) diff --git a/app/models/outfit.rb b/app/models/outfit.rb index cc368184..0e066537 100644 --- a/app/models/outfit.rb +++ b/app/models/outfit.rb @@ -281,4 +281,19 @@ class Outfit < ApplicationRecord i += 1 end end + + # When creating Outfit copies, include items. They're considered a basic + # property of the record, in the grand scheme of things, despite being + # associations. + def dup + super.tap do |outfit| + outfit.worn_item_ids = self.worn_item_ids + outfit.closeted_item_ids = self.closeted_item_ids + end + end + + # Create a copy of this outfit, but *not* wearing the given item. + def without_item(item) + dup.tap { |o| o.worn_items.delete(item) } + end end diff --git a/app/views/outfits/new_v2.html.haml b/app/views/outfits/new_v2.html.haml index ec93f327..7f69cd1d 100644 --- a/app/views/outfits/new_v2.html.haml +++ b/app/views/outfits/new_v2.html.haml @@ -30,7 +30,7 @@ %species-color-picker = form_with url: wardrobe_v2_path, method: :get do |f| - = outfit_state_params + = outfit_state_params except: [:color, :species] = select_tag :color, options_from_collection_for_select(@colors, "id", "human_name", @selected_color&.id), @@ -63,4 +63,4 @@ = render "items/badges/first_seen", item: item = button_to wardrobe_v2_path, method: :get, class: "item-remove-button", title: "Remove #{item.name}", "aria-label": "Remove #{item.name}" do ❌ - = outfit_state_params exclude_item_id: item.id + = outfit_state_params @outfit.without_item(item)