Make budgets translatable
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
class Admin::BudgetsController < Admin::BaseController
|
||||
include Translatable
|
||||
include FeatureFlags
|
||||
feature_flag :budgets
|
||||
|
||||
@@ -56,8 +57,8 @@ class Admin::BudgetsController < Admin::BaseController
|
||||
|
||||
def budget_params
|
||||
descriptions = Budget::Phase::PHASE_KINDS.map{|p| "description_#{p}"}.map(&:to_sym)
|
||||
valid_attributes = [:name, :phase, :currency_symbol] + descriptions
|
||||
params.require(:budget).permit(*valid_attributes)
|
||||
valid_attributes = [:phase, :currency_symbol] + descriptions
|
||||
params.require(:budget).permit(*valid_attributes, translation_params(Budget))
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -3,9 +3,14 @@ class Budget < ActiveRecord::Base
|
||||
include Measurable
|
||||
include Sluggable
|
||||
|
||||
translates :name, touch: true
|
||||
include Globalizable
|
||||
|
||||
CURRENCY_SYMBOLS = %w(€ $ £ ¥).freeze
|
||||
|
||||
validates :name, presence: true, uniqueness: true
|
||||
before_validation :assign_model_to_translations
|
||||
|
||||
validates_translation :name, presence: true
|
||||
validates :phase, inclusion: { in: Budget::Phase::PHASE_KINDS }
|
||||
validates :currency_symbol, presence: true
|
||||
validates :slug, presence: true, format: /\A[a-z0-9\-_]+\z/
|
||||
|
||||
11
app/models/budget/translation.rb
Normal file
11
app/models/budget/translation.rb
Normal file
@@ -0,0 +1,11 @@
|
||||
class Budget::Translation < Globalize::ActiveRecord::Translation
|
||||
validate :name_uniqueness_by_budget
|
||||
|
||||
def name_uniqueness_by_budget
|
||||
if Budget.joins(:translations)
|
||||
.where(name: name)
|
||||
.where.not("budget_translations.budget_id": budget_id).any?
|
||||
errors.add(:name, I18n.t("errors.messages.taken"))
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -8,6 +8,10 @@ module Globalizable
|
||||
def locales_not_marked_for_destruction
|
||||
translations.reject(&:_destroy).map(&:locale)
|
||||
end
|
||||
|
||||
def assign_model_to_translations
|
||||
translations.each { |translation| translation.globalized_model = self }
|
||||
end
|
||||
end
|
||||
|
||||
class_methods do
|
||||
|
||||
@@ -1,7 +1,16 @@
|
||||
<%= form_for [:admin, @budget] do |f| %>
|
||||
<%= render "admin/shared/globalize_locales", resource: @budget %>
|
||||
|
||||
<%= translatable_form_for [:admin, @budget] do |f| %>
|
||||
|
||||
<%= render 'shared/errors', resource: @budget %>
|
||||
|
||||
<div class="small-12 medium-9 column">
|
||||
<%= f.text_field :name, maxlength: Budget.title_max_length %>
|
||||
<%= f.translatable_fields do |translations_form| %>
|
||||
<%= translations_form.text_field :name,
|
||||
label: t("activerecord.attributes.budget.name"),
|
||||
maxlength: Budget.title_max_length,
|
||||
placeholder: t("activerecord.attributes.budget.name") %>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<div class="margin-top">
|
||||
|
||||
Reference in New Issue
Block a user