Files
grecia/app/models/budget/group.rb
Julian Herrero e104369b67 Remove Budget::Group overrided translation class
Since Globalize gem update to v5.2.0 we cannot override translations
anymore in the same way that before the update. Milestone::Translation
class removed in this commit were no longer loaded correctly when
translation class is retrieved by translation_class method provided by
Globalize. Here is the diff between both gem versions:

https://github.com/globalize/globalize/compare/v5.0.0...v5.2.0diff-a1370b109e0dd567545b072bc6447b8fR51

This problem is not happening on test environment but is throwing an
exception in other environments as it has not loaded the delegation
definition inside our custom translation class.

To fix this we added a new class method inside globalizable model
concern to allow to define method delegation on translations classes from
parent globalizable classes when needed without having to override
Translation classes.

Another way to properly load our custom Translation class is
to place it inside parent model class.
2019-04-16 17:28:07 +02:00

45 lines
1.0 KiB
Ruby

class Budget
class Group < ActiveRecord::Base
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/
scope :sort_by_name, -> { includes(:translations).order(:name) }
def single_heading_group?
headings.count == 1
end
private
def generate_slug?
slug.nil? || budget.drafting?
end
end
end