Joining the translations table caused duplicate records to appear. Ordering with SQL is simply too hard because we need to consider fallback locales. Thanks Senén for providing most of the tests in the poll spec.
47 lines
1.0 KiB
Ruby
47 lines
1.0 KiB
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
|
|
|
|
before_validation :assign_model_to_translations
|
|
|
|
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 single_heading_group?
|
|
headings.count == 1
|
|
end
|
|
|
|
private
|
|
|
|
def generate_slug?
|
|
slug.nil? || budget.drafting?
|
|
end
|
|
|
|
end
|
|
end
|