Files
sure/app/models/tag.rb
T
Blaž Dular 37301e15ae feat(mcp): add pagination to get_tags and get_categories tools (#2492)
* feat(mcp): add pagination to get_tags and get_categories tools

Both tools now accept a `page` param and return `total_results`,
`total_pages`, `page`, and `page_size` — matching the pattern used
by get_transactions and get_holdings. Adds a migration for composite
(family_id, name) indexes on tags and categories to support efficient
paginated ordering within a family scope.

* fix(mcp): make page param optional in get_tags/get_categories schema and fix migration version

Page defaults to 1 in call() so marking it required in the JSON schema was incorrect.
Also fixes migration to use ActiveRecord::Migration[7.2] instead of [8.0].

* fix: update schema.rb with family_id+name indexes for tags and categories

* fix(mcp): stable pagination sort and unique family/name indexes on tags and categories

- Make family_id+name indexes unique (matches existing AR uniqueness validations)
- Add id tie-breaker to alphabetically/alphabetically_by_hierarchy scopes
  so paginated results are deterministic

* refactor(mcp): replace Pagy::Backend mixin with Pagy.new in model layer

Pagy::Backend is designed for controllers; using it in plain model
classes is fragile. Switch all four assistant functions to call
Pagy.new(count:, page:, limit:) directly and apply offset/limit
on the scope, which is the correct approach for non-controller contexts.
2026-08-12 23:55:05 +02:00

28 lines
911 B
Ruby

class Tag < ApplicationRecord
belongs_to :family
has_many :taggings, dependent: :destroy
has_many :transactions, through: :taggings, source: :taggable, source_type: "Transaction"
has_many :import_mappings, as: :mappable, dependent: :destroy, class_name: "Import::Mapping"
validates :name, presence: true, uniqueness: { scope: :family }
validates :color, format: { with: /\A#[0-9A-Fa-f]{6}\z/ }, allow_nil: true
scope :alphabetically, -> { order(:name, :id) }
COLORS = %w[#e99537 #4da568 #6471eb #db5a54 #df4e92 #c44fe9 #eb5429 #61c9ea #805dee #6ad28a]
UNCATEGORIZED_COLOR = "#737373"
def replace_and_destroy!(replacement)
transaction do
raise ActiveRecord::RecordInvalid, "Replacement tag cannot be the same as the tag being destroyed" if replacement == self
if replacement
taggings.update_all tag_id: replacement.id
end
destroy!
end
end
end