Files
fizzy/app/models/card.rb
T
Jorge Manrubia 793aace69a Remove implicit order when filtering by closed cards
We set the order in the dedicated column controllers and we don't want this in place when using composable filters.

Also, this raises an error with the MySQL move when combined with assignees. Because we inject a .distinct there,
that messes up with the query select, resulting in having to add a redundant select to the closed sorting scope, so that
the closure.created_at is not lost.
2025-11-18 11:36:47 +01:00

96 lines
2.8 KiB
Ruby

class Card < ApplicationRecord
include Assignable, Attachments, Broadcastable, Closeable, Colored, Entropic, Eventable,
Golden, Mentions, Multistep, Pinnable, Postponable, Promptable,
Readable, Searchable, Stallable, Statuses, Taggable, Triageable, Watchable
belongs_to :account, default: -> { board.account }
belongs_to :board, touch: true
belongs_to :creator, class_name: "User", default: -> { Current.user }
has_many :comments, dependent: :destroy
has_one_attached :image, dependent: :purge_later
has_rich_text :description
before_save :set_default_title, if: :published?
after_update :handle_board_change, if: :saved_change_to_board_id?
before_create :assign_number
scope :reverse_chronologically, -> { order created_at: :desc, id: :desc }
scope :chronologically, -> { order created_at: :asc, id: :asc }
scope :latest, -> { order last_active_at: :desc, id: :desc }
scope :with_users, -> { preload(creator: [ :avatar_attachment, :account ], assignees: [ :avatar_attachment, :account ]) }
scope :preloaded, -> { with_users.preload(:column, :tags, :steps, :closure, :goldness, :activity_spike, :image_attachment, board: [ :entropy ], not_now: [ :user ]).with_rich_text_description_and_embeds }
scope :indexed_by, ->(index) do
case index
when "stalled" then stalled
when "postponing_soon" then postponing_soon
when "closed" then closed
when "not_now" then postponed.latest
when "golden" then golden
when "draft" then drafted
else all
end
end
scope :sorted_by, ->(sort) do
case sort
when "newest" then reverse_chronologically
when "oldest" then chronologically
when "latest" then latest
else latest
end
end
delegate :accessible_to?, to: :board
def card
self
end
def to_param
number.to_s
end
def move_to(new_board)
transaction do
card.update!(board: new_board)
card.events.update_all(board_id: new_board.id)
end
end
def filled?
title.present? || description.present?
end
private
def set_default_title
self.title = "Untitled" if title.blank?
end
def handle_board_change
old_board = account.boards.find_by(id: board_id_before_last_save)
transaction do
update! column: nil
track_board_change_event(old_board.name)
grant_access_to_assignees unless board.all_access?
end
remove_inaccessible_notifications_later
end
def track_board_change_event(old_board_name)
track_event "board_changed", particulars: { old_board: old_board_name, new_board: board.name }
end
def grant_access_to_assignees
board.accesses.grant_to(assignees)
end
def assign_number
self.number ||= account.increment!(:cards_count).cards_count
end
end