[WV2] Refactor outfit state params helper

This commit is contained in:
Emi Matchu 2025-11-03 06:07:35 +00:00
parent 079bcc8d1d
commit 88797bc165
3 changed files with 26 additions and 14 deletions

View file

@ -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(...)

View file

@ -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

View file

@ -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)