Validate both the model and its translations

This way we guarantee there will be at least one translation for a model
and we keep compatibility with the rest of the application, which
ideally isn't aware of globalize.
This commit is contained in:
Javi Martín
2018-10-11 02:40:09 +02:00
parent 2ab49a1832
commit 863b326142
11 changed files with 20 additions and 42 deletions

View File

@@ -5,11 +5,8 @@ class AdminNotification < ActiveRecord::Base
translates :body, touch: true
include Globalizable
translation_class.instance_eval do
validates :title, presence: true
validates :body, presence: true
end
validates_translation :title, presence: true
validates_translation :body, presence: true
validates :segment_recipient, presence: true
validate :validate_segment_recipient

View File

@@ -7,10 +7,8 @@ class Banner < ActiveRecord::Base
translates :description, touch: true
include Globalizable
translation_class.instance_eval do
validates :title, presence: true, length: { minimum: 2 }
validates :description, presence: true
end
validates_translation :title, presence: true, length: { minimum: 2 }
validates_translation :description, presence: true
validates :target_url, presence: true
validates :post_started_at, presence: true

View File

@@ -5,4 +5,11 @@ module Globalizable
globalize_accessors
accepts_nested_attributes_for :translations, allow_destroy: true
end
class_methods do
def validates_translation(method, options = {})
validates(method, options.merge(if: lambda { |resource| resource.translations.blank? }))
translation_class.instance_eval { validates method, options }
end
end
end

View File

@@ -14,11 +14,8 @@ class Legislation::DraftVersion < ActiveRecord::Base
belongs_to :process, class_name: 'Legislation::Process', foreign_key: 'legislation_process_id'
has_many :annotations, class_name: 'Legislation::Annotation', foreign_key: 'legislation_draft_version_id', dependent: :destroy
translation_class.instance_eval do
validates :title, presence: true
validates :body, presence: true
end
validates_translation :title, presence: true
validates_translation :body, presence: true
validates :status, presence: true, inclusion: { in: VALID_STATUSES }
scope :published, -> { where(status: 'published').order('id DESC') }

View File

@@ -24,10 +24,7 @@ class Legislation::Process < ActiveRecord::Base
has_many :questions, -> { order(:id) }, class_name: 'Legislation::Question', foreign_key: 'legislation_process_id', dependent: :destroy
has_many :proposals, -> { order(:id) }, class_name: 'Legislation::Proposal', foreign_key: 'legislation_process_id', dependent: :destroy
translation_class.instance_eval do
validates :title, presence: true
end
validates_translation :title, presence: true
validates :start_date, presence: true
validates :end_date, presence: true
validates :debate_start_date, presence: true, if: :debate_end_date?

View File

@@ -17,10 +17,7 @@ class Legislation::Question < ActiveRecord::Base
accepts_nested_attributes_for :question_options, reject_if: proc { |attributes| attributes.all? { |k, v| v.blank? } }, allow_destroy: true
validates :process, presence: true
translation_class.instance_eval do
validates :title, presence: true
end
validates_translation :title, presence: true
scope :sorted, -> { order('id ASC') }

View File

@@ -9,8 +9,5 @@ class Legislation::QuestionOption < ActiveRecord::Base
has_many :answers, class_name: 'Legislation::Answer', foreign_key: 'legislation_question_id', dependent: :destroy, inverse_of: :question
validates :question, presence: true
translation_class.instance_eval do
validates :value, presence: true # TODO: add uniqueness again
end
validates_translation :value, presence: true # TODO: add uniqueness again
end

View File

@@ -24,10 +24,7 @@ class Poll < ActiveRecord::Base
has_and_belongs_to_many :geozones
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'
translation_class.instance_eval do
validates :name, presence: true
end
validates_translation :name, presence: true
validate :date_range
scope :current, -> { where('starts_at <= ? and ? <= ends_at', Date.current.beginning_of_day, Date.current.beginning_of_day) }

View File

@@ -17,10 +17,7 @@ class Poll::Question < ActiveRecord::Base
has_many :partial_results
belongs_to :proposal
translation_class.instance_eval do
validates :title, presence: true, length: { minimum: 4 }
end
validates_translation :title, presence: true, length: { minimum: 4 }
validates :author, presence: true
validates :poll_id, presence: true

View File

@@ -14,10 +14,7 @@ class Poll::Question::Answer < ActiveRecord::Base
belongs_to :question, class_name: 'Poll::Question', foreign_key: 'question_id'
has_many :videos, class_name: 'Poll::Question::Answer::Video'
translation_class.instance_eval do
validates :title, presence: true
end
validates_translation :title, presence: true
validates :given_order, presence: true, uniqueness: { scope: :question_id }
before_validation :set_order, on: :create

View File

@@ -6,10 +6,7 @@ class SiteCustomization::Page < ActiveRecord::Base
translates :content, touch: true
include Globalizable
translation_class.instance_eval do
validates :title, presence: true
end
validates_translation :title, presence: true
validates :slug, presence: true,
uniqueness: { case_sensitive: false },
format: { with: /\A[0-9a-zA-Z\-_]*\Z/, message: :slug_format }