Files
grecia/app/models/budget/group.rb
Javi Martín c8827f5c7f Hide max votable field on single heading budgets
IMHO selecting in how many headings it's possible to support investments
isn't necessary when there's only one option to choose from. It's
obvious that if there's only one heading, it will be impossible to
select investments from more than one heading.
2021-10-25 18:01:47 +02:00

44 lines
993 B
Ruby

class Budget
class Group < ApplicationRecord
include Sluggable
translates :name, touch: true
include Globalizable
translation_class_delegate :budget
class Translation
validate :name_uniqueness_by_budget
def name_uniqueness_by_budget
if budget.groups.joins(:translations)
.where(name: name)
.where.not("budget_group_translations.budget_group_id": budget_group_id).any?
errors.add(:name, I18n.t("errors.messages.taken"))
end
end
end
belongs_to :budget
has_many :headings, dependent: :destroy
validates_translation :name, presence: true
validates :budget_id, presence: true
validates :slug, presence: true, format: /\A[a-z0-9\-_]+\z/
def self.sort_by_name
all.sort_by(&:name)
end
def multiple_headings?
headings.size > 1
end
private
def generate_slug?
slug.nil? || budget.drafting?
end
end
end