Make budget headings translatable

This commit is contained in:
Julian Herrero
2019-01-22 18:18:24 +01:00
parent 1c35ec99c1
commit 922600252c
14 changed files with 225 additions and 54 deletions

View File

@@ -4,13 +4,18 @@ class Budget
include Sluggable
translates :name, touch: true
include Globalizable
belongs_to :group
has_many :investments
has_many :content_blocks
before_validation :assign_model_to_translations
validates_translation :name, presence: true
validates :group_id, presence: true
validates :name, presence: true, uniqueness: { if: :name_exists_in_budget_headings }
validates :price, presence: true
validates :slug, presence: true, format: /\A[a-z0-9\-_]+\z/
validates :population, numericality: { greater_than: 0 }, allow_nil: true
@@ -21,19 +26,17 @@ class Budget
delegate :budget, :budget_id, to: :group, allow_nil: true
scope :order_by_group_name, -> do
joins(group: :translations).order("budget_group_translations.name DESC", "budget_headings.name")
end
scope :allow_custom_content, -> { where(allow_custom_content: true).order(:name) }
scope :i18n, -> { includes(:translations) }
scope :with_group, -> { joins(group: :translations).where("budget_group_translations.locale = ?", I18n.locale) }
scope :order_by_group_name, -> { i18n.with_group.order("budget_group_translations.name DESC") }
scope :order_by_heading_name, -> { i18n.with_group.order("budget_heading_translations.name") }
scope :order_by_name, -> { i18n.with_group.order_by_group_name.order_by_heading_name }
scope :allow_custom_content, -> { i18n.where(allow_custom_content: true).order(:name) }
def name_scoped_by_group
group.single_heading_group? ? name : "#{group.name}: #{name}"
end
def name_exists_in_budget_headings
group.budget.headings.where(name: name).where.not(id: id).any?
end
def can_be_deleted?
investments.empty?
end