Make Quantity name unique among siblings

Remove Quantity domain - will be replaced by configurable per-domain
root Quantity, limiting selection to descendants only
This commit is contained in:
cryptogopher 2025-01-11 21:50:36 +01:00
parent 57f10c94a4
commit b6acb30785
3 changed files with 4 additions and 3 deletions

View File

@ -9,7 +9,8 @@ class Quantity < ApplicationRecord
validate if: ->{ parent.present? } do
errors.add(:parent, :user_mismatch) unless user == parent.user
end
validates :name, presence: true, length: {maximum: type_for_attribute(:name).limit}
validates :name, presence: true, uniqueness: {scope: [:user_id, :parent_id]},
length: {maximum: type_for_attribute(:name).limit}
validates :description, length: {maximum: type_for_attribute(:description).limit}
scope :defaults, ->{ where(user: nil) }

View File

@ -2,12 +2,12 @@ class CreateQuantities < ActiveRecord::Migration[7.2]
def change
create_table :quantities do |t|
t.references :user, foreign_key: true
t.integer :domain
t.string :name, null: false, limit: 31
t.text :description
t.references :parent, foreign_key: {to_table: :quantities}
t.timestamps null: false
end
add_index :quantities, [:user_id, :parent_id, :name], unique: true
end
end

View File

@ -13,13 +13,13 @@
ActiveRecord::Schema[7.2].define(version: 2025_01_04_194343) do
create_table "quantities", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.bigint "user_id"
t.integer "domain"
t.string "name", limit: 31, null: false
t.text "description"
t.bigint "parent_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["parent_id"], name: "index_quantities_on_parent_id"
t.index ["user_id", "parent_id", "name"], name: "index_quantities_on_user_id_and_parent_id_and_name", unique: true
t.index ["user_id"], name: "index_quantities_on_user_id"
end