diff --git a/app/helpers/wardrobe_helper.rb b/app/helpers/wardrobe_helper.rb index ee3df9d2..a6fbc71d 100644 --- a/app/helpers/wardrobe_helper.rb +++ b/app/helpers/wardrobe_helper.rb @@ -52,33 +52,30 @@ module WardrobeHelper end # Group outfit items by zone, applying smart multi-zone simplification. - # Returns an array of hashes: {zone_id:, zone_label:, items:} - # Each item entry is {item:, state: :worn/:closeted}. + # Returns an array of hashes: {zone_id:, zone_label:, items: [Item, ...]} # This matches the logic from wardrobe-2020's getZonesAndItems function. def outfit_items_by_zone(outfit) return [] if outfit.pet_type.nil? - # Build a list of all items with their state (:worn or :closeted) - all_items = outfit.worn_items.map { |i| {item: i, state: :worn} } + - outfit.closeted_items.map { |i| {item: i, state: :closeted} } + all_items = outfit.worn_items + outfit.closeted_items # Get item appearances for all items at once item_appearances = Item.appearances_for( - all_items.map { |e| e[:item] }, + all_items, outfit.pet_type, swf_asset_includes: [:zone] ) # Separate compatible and incompatible items - compatible_entries = [] - incompatible_entries = [] + compatible = {} + incompatible_items = [] - all_items.each do |entry| - appearance = item_appearances[entry[:item].id] + all_items.each do |item| + appearance = item_appearances[item.id] if appearance&.present? - compatible_entries << entry.merge(appearance: appearance) + compatible[item] = appearance else - incompatible_entries << entry + incompatible_items << item end end @@ -86,19 +83,19 @@ module WardrobeHelper items_by_zone = Hash.new { |h, k| h[k] = [] } zones_by_id = {} - compatible_entries.each do |entry| - entry[:appearance].swf_assets.map(&:zone).uniq.each do |zone| + compatible.each do |item, appearance| + appearance.swf_assets.map(&:zone).uniq.each do |zone| zones_by_id[zone.id] = zone - items_by_zone[zone.id] << {item: entry[:item], state: entry[:state]} + items_by_zone[zone.id] << item end end # Create zone groups with sorted items - zones_and_items = items_by_zone.map do |zone_id, entries| + zones_and_items = items_by_zone.map do |zone_id, items| { zone_id: zone_id, zone_label: zones_by_id[zone_id].label, - items: entries.sort_by { |e| e[:item].name.downcase } + items: items.sort_by { |item| item.name.downcase } } end @@ -114,13 +111,11 @@ module WardrobeHelper zones_and_items = disambiguate_zone_labels(zones_and_items) # Add incompatible items section if any - if incompatible_entries.any? + if incompatible_items.any? zones_and_items << { zone_id: nil, zone_label: "Incompatible", - items: incompatible_entries - .map { |e| {item: e[:item], state: e[:state]} } - .sort_by { |e| e[:item].name.downcase } + items: incompatible_items.sort_by { |item| item.name.downcase } } end @@ -132,14 +127,13 @@ module WardrobeHelper # Simplify zone groups by removing redundant single-item groups. # Keep groups with multiple items (conflicts). For single-item groups, # only keep them if the item doesn't appear in a multi-item group. - # Each item entry is {item:, state:}. def simplify_multi_zone_groups(zones_and_items) # Find groups with conflicts (multiple items) groups_with_conflicts = zones_and_items.select { |g| g[:items].length > 1 } # Track which items appear in conflict groups items_with_conflicts = Set.new( - groups_with_conflicts.flat_map { |g| g[:items].map { |e| e[:item].id } } + groups_with_conflicts.flat_map { |g| g[:items].map(&:id) } ) # Track which items we've already shown @@ -149,13 +143,13 @@ module WardrobeHelper zones_and_items.select do |group| # Always keep groups with multiple items if group[:items].length > 1 - group[:items].each { |e| items_we_have_seen.add(e[:item].id) } + group[:items].each { |item| items_we_have_seen.add(item.id) } true else # For single-item groups, only keep if: # - Item hasn't been seen yet AND # - Item won't appear in a conflict group - item_id = group[:items].first[:item].id + item_id = group[:items].first.id if items_we_have_seen.include?(item_id) || items_with_conflicts.include?(item_id) false diff --git a/app/views/wardrobe/show.html.haml b/app/views/wardrobe/show.html.haml index 47a7b591..5624a7f6 100644 --- a/app/views/wardrobe/show.html.haml +++ b/app/views/wardrobe/show.html.haml @@ -75,5 +75,5 @@ .zone-group %h3.zone-label= zone_group[:zone_label] %ul.items-list - - zone_group[:items].each do |entry| - = render "items/item_card", item: entry[:item] + - zone_group[:items].each do |item| + = render "items/item_card", item: item