From e7cc426fdc1c261dab36d37ea1f540dd457b4bde Mon Sep 17 00:00:00 2001 From: Jose Farias Date: Thu, 17 Oct 2024 22:51:02 -0600 Subject: [PATCH] Move bubble filtering to the model --- app/controllers/bubbles_controller.rb | 20 +++--------------- app/helpers/filters_helper.rb | 8 +++---- app/models/bucket.rb | 2 +- app/models/bucket/filterable.rb | 30 +++++++++++++++++++++++++++ app/models/bucket/view.rb | 4 ++++ app/views/bubbles/index.html.erb | 2 +- app/views/buckets/_view.html.erb | 2 +- 7 files changed, 44 insertions(+), 24 deletions(-) create mode 100644 app/models/bucket/filterable.rb diff --git a/app/controllers/bubbles_controller.rb b/app/controllers/bubbles_controller.rb index 7cb4c0be3..dbbd4c3b1 100644 --- a/app/controllers/bubbles_controller.rb +++ b/app/controllers/bubbles_controller.rb @@ -3,15 +3,9 @@ class BubblesController < ApplicationController before_action :set_bubble, only: %i[ show edit update ] before_action :clear_assignees, only: :index - before_action :set_view, :set_tag_filters, :set_assignee_filters, only: :index + before_action :set_view, :set_bubbles, only: :index def index - @bubbles = @bucket.bubbles - @bubbles = @bubbles.ordered_by(params[:order_by] || Bubble.default_order_by) - @bubbles = @bubbles.with_status(params[:status] || Bubble.default_status) - @bubbles = @bubbles.tagged_with(@tag_filters) if @tag_filters - @bubbles = @bubbles.assigned_to(@assignee_filters) if @assignee_filters - @bubbles = @bubbles.mentioning(params[:term]) if params[:term] end def new @@ -53,15 +47,7 @@ class BubblesController < ApplicationController params[:view_id] = @view&.id end - def set_tag_filters - if params[:tag_ids] - @tag_filters = Current.account.tags.where id: params[:tag_ids] - end - end - - def set_assignee_filters - if params[:assignee_ids] - @assignee_filters = Current.account.users.where id: params[:assignee_ids] - end + def set_bubbles + @bubbles = @bucket.filtered_bubbles helpers.view_filter_params end end diff --git a/app/helpers/filters_helper.rb b/app/helpers/filters_helper.rb index cf64ff592..0d3b59ed7 100644 --- a/app/helpers/filters_helper.rb +++ b/app/helpers/filters_helper.rb @@ -4,16 +4,16 @@ module FiltersHelper end def tag_filter_text - if @tag_filters - @tag_filters.map(&:hashtag).to_choice_sentence + if @bucket&.tag_filters + @bucket.tag_filters.map(&:hashtag).to_choice_sentence else "any tag" end end def assignee_filter_text - if @assignee_filters - "assigned to #{@assignee_filters.map(&:name).to_choice_sentence}" + if @bucket&.assignee_filters + "assigned to #{@bucket.assignee_filters.map(&:name).to_choice_sentence}" else "assigned to anyone" end diff --git a/app/models/bucket.rb b/app/models/bucket.rb index 651b8e600..728cae12d 100644 --- a/app/models/bucket.rb +++ b/app/models/bucket.rb @@ -1,5 +1,5 @@ class Bucket < ApplicationRecord - include Accessible, Views + include Accessible, Filterable, Views belongs_to :account belongs_to :creator, class_name: "User", default: -> { Current.user } diff --git a/app/models/bucket/filterable.rb b/app/models/bucket/filterable.rb new file mode 100644 index 000000000..e40c2ac9d --- /dev/null +++ b/app/models/bucket/filterable.rb @@ -0,0 +1,30 @@ +module Bucket::Filterable + extend ActiveSupport::Concern + + included do + attr_accessor :tag_filters, :assignee_filters + end + + def filtered_bubbles(params = {}) + result = bubbles + result = result.ordered_by(params["order_by"] || Bubble.default_order_by) + result = result.with_status(params["status"] || Bubble.default_status) + result = result.tagged_with(self.tag_filters) if set_tag_filters(params["tag_ids"]) + result = result.assigned_to(self.assignee_filters) if set_assignee_filters(params["assignee_ids"]) + result = result.mentioning(params["term"]) if params["term"] + result + end + + private + def set_tag_filters(tag_ids) + if tag_ids + self.tag_filters = account.tags.where id: tag_ids + end + end + + def set_assignee_filters(assignee_ids) + if assignee_ids + self.assignee_filters = account.users.where id: assignee_ids + end + end +end diff --git a/app/models/bucket/view.rb b/app/models/bucket/view.rb index 4a974ae80..f56d51fcf 100644 --- a/app/models/bucket/view.rb +++ b/app/models/bucket/view.rb @@ -14,6 +14,10 @@ class Bucket::View < ApplicationRecord end end + def filtered_bubbles + bucket.filtered_bubbles filters + end + private def must_not_be_the_default_view if filters.values.all?(&:blank?) || filters == self.class.default_filters diff --git a/app/views/bubbles/index.html.erb b/app/views/bubbles/index.html.erb index 8280291ce..52ff3f0ab 100644 --- a/app/views/bubbles/index.html.erb +++ b/app/views/bubbles/index.html.erb @@ -8,7 +8,7 @@ <% end %>
- <%= render "bubbles/filters", bucket: @bucket, view: @view, tag_filters: @tag_filters, assignee_filters: @assignee_filters %> + <%= render "bubbles/filters", bucket: @bucket, view: @view %>
<%= button_to bucket_bubbles_path(@bucket), method: :post, class: "btn btn--plain", form_class: "flex-item-justify-end" do %> diff --git a/app/views/buckets/_view.html.erb b/app/views/buckets/_view.html.erb index 5223388b8..9e696331a 100644 --- a/app/views/buckets/_view.html.erb +++ b/app/views/buckets/_view.html.erb @@ -3,7 +3,7 @@
  • <%= link_to path, class: "windshield__container flex justify-center align-center position-relative" do %>
    - <% view.bucket.bubbles.not_popped.ordered_by_activity.limit(10).each do |bubble| %> + <% view.filtered_bubbles.not_popped.ordered_by_activity.limit(10).each do |bubble| %>