Move bubble filtering to the model

This commit is contained in:
Jose Farias
2024-10-17 22:51:02 -06:00
parent 91401c2e75
commit e7cc426fdc
7 changed files with 44 additions and 24 deletions
+3 -17
View File
@@ -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
+4 -4
View File
@@ -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
+1 -1
View File
@@ -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 }
+30
View File
@@ -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
+4
View File
@@ -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
+1 -1
View File
@@ -8,7 +8,7 @@
<% end %>
<header class="txt-align-center">
<%= render "bubbles/filters", bucket: @bucket, view: @view, tag_filters: @tag_filters, assignee_filters: @assignee_filters %>
<%= render "bubbles/filters", bucket: @bucket, view: @view %>
</header>
<%= button_to bucket_bubbles_path(@bucket), method: :post, class: "btn btn--plain", form_class: "flex-item-justify-end" do %>
+1 -1
View File
@@ -3,7 +3,7 @@
<li class="bucket flex flex-column txt-align-center max-width position-relative">
<%= link_to path, class: "windshield__container flex justify-center align-center position-relative" do %>
<div class="windshield bucket__windshield flex flex-wrap gap justify-center align-end" style="view-transition-name: windshield_<%= view.bucket.id %>">
<% 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| %>
<div class="bubble" style="--bubble-color: <%= bubble.color %>; <%= bubble_rotation(bubble) %> <%= bubble_size(bubble) %>">
<svg class="bubble__svg" style="fill: <%= bubble.color %>; stroke: <%= bubble.color %>;" viewBox="0 0 990 990" xmlns="http://www.w3.org/2000/svg">
<path d="m0 0h990v990h-990z" fill="none" stroke="none" />