Replace card id with card number

This commit is contained in:
Stanko K.R.
2025-11-11 18:48:27 +01:00
committed by Mike Dalessio
parent 915c1202f8
commit 32d96db7d9
11 changed files with 45 additions and 10 deletions
+6 -1
View File
@@ -3,7 +3,7 @@ class Card < ApplicationRecord
Golden, Mentions, Multistep, Pinnable, Postponable, Promptable,
Readable, Searchable, Stallable, Statuses, Taggable, Triageable, Watchable
belongs_to :account, default: -> { Current.account }
belongs_to :account, default: -> { board.account }
belongs_to :board, touch: true
belongs_to :creator, class_name: "User", default: -> { Current.user }
@@ -14,6 +14,7 @@ class Card < ApplicationRecord
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 }
@@ -81,4 +82,8 @@ class Card < ApplicationRecord
def grant_access_to_assignees
board.accesses.grant_to(assignees)
end
def assign_number
self.number ||= account.increment!(:cards_count).cards_count
end
end
@@ -4,7 +4,7 @@
<div class="card__board flex align-start">
<span class="card__id">
<span class="for-screen-reader">Card number</span>
<%= card.id %>
<%= card.number %>
</span>
<span class="card__board-name">
<span class="overflow-ellipsis"><%= card.board.name %></span>
@@ -1,7 +1,7 @@
<label class="card__board">
<span class="card__id">
<span class="for-screen-reader">Card number</span>
<%= card.id %>
<%= card.number %>
</span>
<span class="card__board-name">
<span class="overflow-ellipsis"><%= card.board.name %></span>
+1 -1
View File
@@ -11,7 +11,7 @@
<h4 class="card__board flex-inline align-start">
<span class="card__id">
<span class="for-screen-reader">Card number</span>
<%= card.id %>
<%= card.number %>
</span>
<span class="card__board-name">
<span class="overflow-ellipsis"><%= event.board.name %></span>
+2 -2
View File
@@ -1,8 +1,8 @@
<lexxy-prompt-item>
<template type="menu">
#<%= card.id %> <%= card.title %>
#<%= card.number %> <%= card.title %>
</template>
<template type="editor">
<%= link_to "##{card.id} #{card.title}", card_url(card) %>
<%= link_to "##{card.number} #{card.title}", card_url(card) %>
</template>
</lexxy-prompt-item>
@@ -0,0 +1,7 @@
class AddNumberToCards < ActiveRecord::Migration[8.2]
def change
add_column :cards, :number, :bigint, null: false
add_column :accounts, :cards_count, :bigint, default: 0, null: false
add_index :cards, [:account_id, :number], unique: true
end
end
Generated
+4 -1
View File
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.2].define(version: 2025_11_11_122540) do
ActiveRecord::Schema[8.2].define(version: 2025_11_11_153019) do
create_table "accesses", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.datetime "accessed_at"
t.bigint "board_id", null: false
@@ -35,6 +35,7 @@ ActiveRecord::Schema[8.2].define(version: 2025_11_11_122540) do
end
create_table "accounts", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.bigint "cards_count", default: 0, null: false
t.datetime "created_at", null: false
t.integer "external_account_id"
t.string "name", null: false
@@ -170,9 +171,11 @@ ActiveRecord::Schema[8.2].define(version: 2025_11_11_122540) do
t.integer "creator_id", null: false
t.date "due_on"
t.datetime "last_active_at", null: false
t.bigint "number", null: false
t.string "status", default: "drafted", null: false
t.string "title"
t.datetime "updated_at", null: false
t.index ["account_id", "number"], name: "index_cards_on_account_id_and_number", unique: true
t.index ["board_id"], name: "index_cards_on_board_id"
t.index ["column_id"], name: "index_cards_on_column_id"
t.index ["last_active_at", "status"], name: "index_cards_on_last_active_at_and_status"
+3 -3
View File
@@ -227,9 +227,10 @@ class Import
def copy_cards
step("Copying cards", "Copied %{count} cards in %{duration}") do
mapping[:cards] ||= {}
account.update_columns(cards_count: import.cards.maximum(:id) || 0)
next_id = (Card.maximum(:id) || 0) + 1
# Collect all associated records to batch insert
activity_spikes_to_insert = []
engagements_to_insert = []
goldnesses_to_insert = []
@@ -250,6 +251,7 @@ class Import
cards_to_insert << {
id: new_id,
number: old_card.id,
account_id: account.id,
board_id: mapping[:boards][old_card.board_id],
column_id: old_card.column_id ? mapping[:columns][old_card.column_id] : nil,
@@ -324,7 +326,6 @@ class Import
Card.insert_all(cards_to_insert)
end
# Batch insert all associated records
Card::ActivitySpike.insert_all(activity_spikes_to_insert) if activity_spikes_to_insert.any?
Card::Engagement.insert_all(engagements_to_insert) if engagements_to_insert.any?
Card::Goldness.insert_all(goldnesses_to_insert) if goldnesses_to_insert.any?
@@ -332,7 +333,6 @@ class Import
Assignment.insert_all(assignments_to_insert) if assignments_to_insert.any?
Closure.insert_all(closures_to_insert) if closures_to_insert.any?
# Second pass: copy rich text and attachments
import.cards.find_each do |old_card|
new_card_id = mapping[:cards][old_card.id]
new_card = Card.find(new_card_id)
+2
View File
@@ -1,7 +1,9 @@
37s:
name: 37signals
external_account_id: <%= ActiveRecord::FixtureSet.identify("37signals") %>
cards_count: 5
initech:
name: Initech LLC
external_account_id: <%= ActiveRecord::FixtureSet.identify("initech") %>
cards_count: 0
+5
View File
@@ -1,4 +1,5 @@
logo:
number: 1
board: writebook
creator: david
column: writebook_triage
@@ -10,6 +11,7 @@ logo:
account: 37s
layout:
number: 2
board: writebook
creator: david
column: writebook_triage
@@ -20,6 +22,7 @@ layout:
account: 37s
text:
number: 3
board: writebook
creator: kevin
column: writebook_in_progress
@@ -30,6 +33,7 @@ text:
account: 37s
shipping:
number: 4
board: writebook
creator: kevin
column: writebook_triage
@@ -40,6 +44,7 @@ shipping:
account: 37s
buy_domain:
number: 5
board: writebook
creator: david
title: Buy domain
+13
View File
@@ -5,6 +5,19 @@ class CardTest < ActiveSupport::TestCase
Current.session = sessions(:david)
end
test "create assigns a number to the card" do
user = users(:david)
board = boards(:writebook)
account = board.account
card = nil
assert_difference -> { account.reload.cards_count }, +1 do
card = Card.create!(title: "Test", board: board, creator: user)
end
assert_equal account.reload.cards_count, card.number
end
test "capturing messages" do
assert_difference -> { cards(:logo).comments.count }, +1 do
cards(:logo).comments.create!(body: "Agreed.")