diff --git a/app/controllers/wardrobe_controller.rb b/app/controllers/wardrobe_controller.rb index d104f574..a19e4364 100644 --- a/app/controllers/wardrobe_controller.rb +++ b/app/controllers/wardrobe_controller.rb @@ -99,17 +99,23 @@ class WardrobeController < ApplicationController # Handle search mode @search_mode = params[:q].present? if @search_mode - search_filters = build_search_filters(params[:q], @outfit) - query_params = ActionController::Parameters.new( - search_filters.each_with_index.map { |filter, i| [i.to_s, filter] }.to_h - ) - @query = Item::Search::Query.from_params(query_params, current_user) - @search_results = @query.results.paginate(page: params.dig(:q, :page), per_page: 30) + begin + @query = Item::Search::Query.from_text(params[:q], current_user) + results = @query.results - # Load appearances for search results to get zone information - target = @alt_style || @pet_type - if target - @appearances_by_item_id = Item.appearances_for(@search_results, target, swf_asset_includes: [:zone]) + # Auto-fit filter: apply body_id filter from outfit state + body_id = @outfit.alt_style&.body_id || @outfit.pet_type&.body_id + results = results.merge(Item.fits(body_id)) if body_id + + @search_results = results.paginate(page: params[:page], per_page: 30) + + # Load appearances for search results to get zone information + target = @alt_style || @pet_type + if target + @appearances_by_item_id = Item.appearances_for(@search_results, target, swf_asset_includes: [:zone]) + end + rescue Item::Search::Error => e + @search_error = e.message end end @@ -152,51 +158,4 @@ class WardrobeController < ApplicationController def outfit_state_params_present? params[:species].present? || params[:color].present? || params[:objects].present? || params[:closet].present? end - - def build_search_filters(query_params, outfit) - filters = [] - - # Add name filter if present - if query_params[:name].present? - filters << { key: "name", value: query_params[:name] } - end - - # Add item kind filter if present - if query_params[:item_kind].present? - case query_params[:item_kind] - when "nc" - filters << { key: "is_nc", value: "true" } - when "np" - filters << { key: "is_np", value: "true" } - when "pb" - filters << { key: "is_pb", value: "true" } - end - end - - # Add zone filter if present - if query_params[:zone].present? - filters << { key: "occupied_zone_set_name", value: query_params[:zone] } - end - - # Always add auto-filter for items that fit the current pet - pet_type = outfit.pet_type - if pet_type - fit_filter = { - key: "fits", - value: { - species_id: pet_type.species_id.to_s, - color_id: pet_type.color_id.to_s - } - } - - # Include alt_style_id if present - if outfit.alt_style_id.present? - fit_filter[:value][:alt_style_id] = outfit.alt_style_id.to_s - end - - filters << fit_filter - end - - filters - end end diff --git a/app/helpers/wardrobe_helper.rb b/app/helpers/wardrobe_helper.rb index a6fbc71d..ead48372 100644 --- a/app/helpers/wardrobe_helper.rb +++ b/app/helpers/wardrobe_helper.rb @@ -24,8 +24,8 @@ module WardrobeHelper end unless except.include?(:q) - (params[:q] || {}).each do |key, value| - fields << hidden_field_tag("q[#{key}]", value) if value.present? + if params[:q].present? + fields << hidden_field_tag(:q, params[:q]) end end diff --git a/app/views/wardrobe/items/_search_results.html.haml b/app/views/wardrobe/items/_search_results.html.haml index 00d3f0d9..ada84210 100644 --- a/app/views/wardrobe/items/_search_results.html.haml +++ b/app/views/wardrobe/items/_search_results.html.haml @@ -1,13 +1,16 @@ .search-results - - if @search_results.any? - = will_paginate @search_results, page_links: false, param_name: "q[page]", params: @outfit.wardrobe_params.merge(q: params[:q]) + - if @search_error + .error-state + %p.error-message= @search_error + - elsif @search_results.any? + = will_paginate @search_results, page_links: false, param_name: :page, params: @outfit.wardrobe_params.merge(q: params[:q]) %ul.search-results-list - @search_results.each do |item| - appearance = @appearances_by_item_id&.dig(item.id) = render "items/item_card", item: item, appearance: appearance - = will_paginate @search_results, param_name: "q[page]", params: @outfit.wardrobe_params.merge(q: params[:q]) + = will_paginate @search_results, param_name: :page, params: @outfit.wardrobe_params.merge(q: params[:q]) - else .empty-state diff --git a/app/views/wardrobe/show.html.haml b/app/views/wardrobe/show.html.haml index 22b585df..e7f1e40f 100644 --- a/app/views/wardrobe/show.html.haml +++ b/app/views/wardrobe/show.html.haml @@ -59,7 +59,7 @@ = outfit_state_params except: [:q] = form_with url: @wardrobe_path, method: :get, class: "search-form" do |f| = outfit_state_params - = f.text_field "q[name]", placeholder: "Search for items...", value: params.dig(:q, :name), "aria-label": "Search for items" + = f.text_field :q, placeholder: "Search items… (try is:nc, occupies:hat, user:owns)", value: params[:q], "aria-label": "Search for items" = f.submit "Search" - if @search_mode