Quantities index WIP

This commit is contained in:
2025-01-05 20:47:49 +01:00
parent b3aea97087
commit aa862f0e90
5 changed files with 156 additions and 0 deletions

35
app/models/quantity.rb Normal file
View File

@@ -0,0 +1,35 @@
class Quantity < ApplicationRecord
belongs_to :user, optional: true
belongs_to :parent, optional: true, class_name: "Quantity"
has_many :subquantities, class_name: "Quantity", inverse_of: :parent,
dependent: :restrict_with_error
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 :description, length: {maximum: type_for_attribute(:description).limit}
scope :defaults, ->{ where(user: nil) }
scope :ordered, ->{
left_outer_joins(:parent).order(ordering)
}
def self.ordering
[arel_table.coalesce(Arel::Table.new(:parents_quantities)[:name], arel_table[:name]),
arel_table[:parent_id].not_eq(nil),
:name]
end
def to_s
name
end
def movable?
subunits.empty?
end
def default?
parent_id.nil?
end
end

View File

@@ -11,6 +11,7 @@ class User < ApplicationRecord
disabled: 0, # administratively disallowed to sign in
}, default: :active
has_many :quantities, dependent: :destroy
has_many :units, dependent: :destroy
validates :email, presence: true, uniqueness: true,