diff --git a/app/controllers/resources_controller.rb b/app/controllers/resources_controller.rb index 90e0feb16..bcfbaf091 100644 --- a/app/controllers/resources_controller.rb +++ b/app/controllers/resources_controller.rb @@ -1,19 +1,25 @@ class ResourcesController < ApplicationController - def index - per_page = params[:number_of_items_per_page].presence || 25 - unpaginated = Resource.where(kind: Resource::PUBLISHED_KINDS) #TODO - #FIXME brittle - .includes(:main_image, :gallery_images, :attachments) - filtered = unpaginated.search_by_params(params) - .by_created - @resources = filtered.paginate(page: params[:page], per_page: per_page).decorate - - @count_display = if filtered.count == unpaginated.count - unpaginated.count - else - "#{filtered.count}/#{unpaginated.count}" - end - @sortable_fields = Resource::PUBLISHED_KINDS + if turbo_frame_request? + per_page = params[:number_of_items_per_page].presence || 25 + unfiltered = Resource.where(kind: Resource::PUBLISHED_KINDS) # TODO - #FIXME brittle + .includes(:main_image, :gallery_images, :attachments) + filtered = unfiltered.search_by_params(params) + .by_created + @resources = filtered.paginate(page: params[:page], per_page: per_page) + + total_count = unfiltered.count + filtered_count = filtered.count + @count_display = if filtered_count == total_count + total_count + else + "#{filtered_count}/#{total_count}" + end + + render :resource_results + else + render :index + end end def stories @@ -58,11 +64,11 @@ def update @resource = Resource.find(params[:id]) @resource.user ||= current_user if @resource.update(resource_params) - flash[:notice] = 'Resource updated.' + flash[:notice] = "Resource updated." redirect_to resources_path else set_form_variables - flash[:alert] = 'Failed to update Resource.' + flash[:alert] = "Failed to update Resource." render :edit end end @@ -73,7 +79,6 @@ def destroy redirect_to resources_path, notice: "Resource was successfully destroyed." end - def search process_search @sortable_fields = Resource::PUBLISHED_KINDS @@ -81,10 +86,10 @@ def search end def download - if params[:attachment_id].to_i > 0 - attachment = Attachment.where(owner_type: "Resource", id: params[:attachment_id]).last + attachment = if params[:attachment_id].to_i > 0 + Attachment.where(owner_type: "Resource", id: params[:attachment_id]).last else - attachment = Resource.find(params[:resource_id]).download_attachment + Resource.find(params[:resource_id]).download_attachment end if attachment&.file&.blob.present? @@ -92,13 +97,13 @@ def download else if params[:from] == "resources_index" path = resources_path - elsif params[:from] == "dashboard_index" - path = authenticated_root_path - else - resource_path(params[:resource_id]) - end + elsif params[:from] == "dashboard_index" + path = authenticated_root_path + else + resource_path(params[:resource_id]) + end redirect_to path, - alert: "File not found or not attached." + alert: "File not found or not attached." end end @@ -110,8 +115,8 @@ def set_form_variables @windows_types = WindowsType.all @authors = User.active.or(User.where(id: @resource.user_id)) - .order(:first_name, :last_name) - .map{|u| [u.full_name, u.id] } + .order(:first_name, :last_name) + .map { |u| [u.full_name, u.id] } end def process_search diff --git a/app/frontend/javascript/controllers/collection_controller.js b/app/frontend/javascript/controllers/collection_controller.js index cd8bba79b..3335ca5dd 100644 --- a/app/frontend/javascript/controllers/collection_controller.js +++ b/app/frontend/javascript/controllers/collection_controller.js @@ -2,14 +2,24 @@ import { Controller } from "@hotwired/stimulus" // Connects to data-controller="collection" export default class extends Controller { - +static classes = [ "unselected", "selected" ] connect() { this.element.addEventListener("change", (event) => { - if (event.target.type === "checkbox" || event.target.type === "radio") { - this.submitForm() + const { type } = event.target; + + if (type === "checkbox") { + this.toggleClass(event.target); } - }) + if ( + type === "checkbox" || + type === "radio" || + type === "select-one" || + type === "select-multiple" + ) { + this.submitForm(); + } + }); this.element.addEventListener("input", (event) => { if (event.target.type === "text") { this.debouncedSubmit() @@ -27,4 +37,19 @@ export default class extends Controller { this.submitForm() }, 400) } + + toggleClass(el) { + const button = el.closest("label") + if (!button || !this.selectedClasses) return + + // Toggle selected classes + this.selectedClasses.forEach(cls => { + button.classList.toggle(cls) + }) + + // Toggle unselected classes + this.unselectedClasses.forEach(cls => { + button.classList.toggle(cls) + }) + } } diff --git a/app/models/resource.rb b/app/models/resource.rb index 96a0a75f1..855df047c 100644 --- a/app/models/resource.rb +++ b/app/models/resource.rb @@ -59,8 +59,11 @@ class Resource < ApplicationRecord scope :category_names, ->(names) { tag_names(:categories, names) } scope :sector_names, ->(names) { tag_names(:sectors, names) } scope :featured, -> (featured=nil) { featured.present? ? where(featured: featured) : where(featured: true) } - scope :kind, -> (kind) { where("kind like ?", kind ) } - scope :leader_spotlights, -> { kind("LeaderSpotlight") } + scope :kinds, ->(kinds) { + kinds = Array(kinds).flatten.map(&:to_s) + where(kind: kinds) + } + scope :leader_spotlights, -> { kinds("LeaderSpotlight") } scope :published_kinds, -> { where(kind: PUBLISHED_KINDS) } scope :published, ->(published=nil) { if ["true", "false"].include?(published) @@ -86,7 +89,7 @@ def self.search_by_params(params) resources = resources.category_names(params[:category_names]) if params[:category_names].present? resources = resources.windows_type_name(params[:windows_type_name]) if params[:windows_type_name].present? resources = resources.title(params[:title]) if params[:title].present? - resources = resources.kind(params[:kind]) if params[:kind].present? + resources = resources.kinds(params[:kinds]) if params[:kinds].present? resources = resources.published_search(params[:published_search]) if params[:published_search].present? resources = resources.featured(params[:featured]) if params[:featured].present? resources diff --git a/app/views/resources/_resource_card.html.erb b/app/views/resources/_resource_card.html.erb index 72925825f..93b42363a 100644 --- a/app/views/resources/_resource_card.html.erb +++ b/app/views/resources/_resource_card.html.erb @@ -71,7 +71,8 @@ <%= link_to( content_tag(:span, "", class: "far fa-edit"), edit_resource_path(resource.object), - class: "admin-only bg-blue-100 btn btn-secondary-outline" + class: "admin-only bg-blue-100 btn btn-secondary-outline", + data: {turbo: false} ) %> <% end %> diff --git a/app/views/resources/_resource_count.html.erb b/app/views/resources/_resource_count.html.erb new file mode 100644 index 000000000..1212b44fa --- /dev/null +++ b/app/views/resources/_resource_count.html.erb @@ -0,0 +1,7 @@ +
There are no resources that match your search. Please try again.
- <% end %> + <%= turbo_frame_tag :resource_results, src: resources_path do %> +There are no resources that match your search. Please try again.
+ <% end %> + +
Search by emotional theme, materials needed, ease of set-up, and more.
@@ -49,7 +49,7 @@
<%= turbo_frame_tag "frame_list" do %>
<%= turbo_stream.replace("filters_applied", partial: "filters_applied") %>
<%= turbo_stream.replace("workshops_count", partial: "workshops_count") %>
- <%= turbo_stream.replace("copy_url", partial: "copy_url") %>
+ <%= turbo_stream.replace("copy_url", partial: "shared/copy_url") %>
Your search returned no results. Please try again.