[WV2] Refactor outfit state params helper
This commit is contained in:
parent
079bcc8d1d
commit
88797bc165
3 changed files with 26 additions and 14 deletions
|
|
@ -66,24 +66,21 @@ module OutfitsHelper
|
||||||
end
|
end
|
||||||
|
|
||||||
# Generate hidden fields to preserve outfit state in URL params.
|
# Generate hidden fields to preserve outfit state in URL params.
|
||||||
# Options:
|
# Use the `except` parameter to skip certain fields, e.g. to override
|
||||||
# - exclude_item_id: Item ID to exclude from objects[] array
|
# them with specific values, like in the species/color picker.
|
||||||
def outfit_state_params(exclude_item_id: nil)
|
def outfit_state_params(outfit = @outfit, except: [])
|
||||||
fields = []
|
fields = []
|
||||||
|
|
||||||
# Preserve species and color
|
fields << hidden_field_tag(:species, @outfit.species_id) unless except.include?(:species)
|
||||||
fields << hidden_field_tag(:species, params[:species]) if params[:species].present?
|
fields << hidden_field_tag(:color, @outfit.color_id) unless except.include?(:color)
|
||||||
fields << hidden_field_tag(:color, params[:color]) if params[:color].present?
|
|
||||||
|
|
||||||
# Preserve item IDs, optionally excluding one
|
unless except.include?(:worn_items)
|
||||||
if params[:objects].present?
|
outfit.worn_items.each do |item|
|
||||||
params[:objects].each do |item_id|
|
fields << hidden_field_tag('objects[]', item.id)
|
||||||
next if exclude_item_id && item_id.to_s == exclude_item_id.to_s
|
|
||||||
fields << hidden_field_tag("objects[]", item_id)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
safe_join(fields)
|
safe_join fields
|
||||||
end
|
end
|
||||||
|
|
||||||
def outfit_viewer(...)
|
def outfit_viewer(...)
|
||||||
|
|
|
||||||
|
|
@ -281,4 +281,19 @@ class Outfit < ApplicationRecord
|
||||||
i += 1
|
i += 1
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@
|
||||||
|
|
||||||
%species-color-picker
|
%species-color-picker
|
||||||
= form_with url: wardrobe_v2_path, method: :get do |f|
|
= form_with url: wardrobe_v2_path, method: :get do |f|
|
||||||
= outfit_state_params
|
= outfit_state_params except: [:color, :species]
|
||||||
= select_tag :color,
|
= select_tag :color,
|
||||||
options_from_collection_for_select(@colors, "id", "human_name",
|
options_from_collection_for_select(@colors, "id", "human_name",
|
||||||
@selected_color&.id),
|
@selected_color&.id),
|
||||||
|
|
@ -63,4 +63,4 @@
|
||||||
= render "items/badges/first_seen", item: item
|
= 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
|
= 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)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue