From 863b326142d085ab5464f89bb82fa45d4c09d6fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 11 Oct 2018 02:40:09 +0200 Subject: [PATCH] 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. --- app/models/admin_notification.rb | 7 ++----- app/models/banner.rb | 6 ++---- app/models/concerns/globalizable.rb | 7 +++++++ app/models/legislation/draft_version.rb | 7 ++----- app/models/legislation/process.rb | 5 +---- app/models/legislation/question.rb | 5 +---- app/models/legislation/question_option.rb | 5 +---- app/models/poll.rb | 5 +---- app/models/poll/question.rb | 5 +---- app/models/poll/question/answer.rb | 5 +---- app/models/site_customization/page.rb | 5 +---- 11 files changed, 20 insertions(+), 42 deletions(-) diff --git a/app/models/admin_notification.rb b/app/models/admin_notification.rb index 69c195c17..6fc3e83e3 100644 --- a/app/models/admin_notification.rb +++ b/app/models/admin_notification.rb @@ -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 diff --git a/app/models/banner.rb b/app/models/banner.rb index 1f3757644..cc05b3970 100644 --- a/app/models/banner.rb +++ b/app/models/banner.rb @@ -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 diff --git a/app/models/concerns/globalizable.rb b/app/models/concerns/globalizable.rb index 39b50f81f..b07112dfa 100644 --- a/app/models/concerns/globalizable.rb +++ b/app/models/concerns/globalizable.rb @@ -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 diff --git a/app/models/legislation/draft_version.rb b/app/models/legislation/draft_version.rb index 5b594dd7b..fbcac76c3 100644 --- a/app/models/legislation/draft_version.rb +++ b/app/models/legislation/draft_version.rb @@ -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') } diff --git a/app/models/legislation/process.rb b/app/models/legislation/process.rb index 51302fbb1..fcf450245 100644 --- a/app/models/legislation/process.rb +++ b/app/models/legislation/process.rb @@ -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? diff --git a/app/models/legislation/question.rb b/app/models/legislation/question.rb index 8d01e4a4f..f464920af 100644 --- a/app/models/legislation/question.rb +++ b/app/models/legislation/question.rb @@ -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') } diff --git a/app/models/legislation/question_option.rb b/app/models/legislation/question_option.rb index 0ca583478..a02d41554 100644 --- a/app/models/legislation/question_option.rb +++ b/app/models/legislation/question_option.rb @@ -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 diff --git a/app/models/poll.rb b/app/models/poll.rb index 6d062a86b..38c1a7d56 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -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) } diff --git a/app/models/poll/question.rb b/app/models/poll/question.rb index e253aab36..4f8988caa 100644 --- a/app/models/poll/question.rb +++ b/app/models/poll/question.rb @@ -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 diff --git a/app/models/poll/question/answer.rb b/app/models/poll/question/answer.rb index 8faf9a0c5..f3bd4297f 100644 --- a/app/models/poll/question/answer.rb +++ b/app/models/poll/question/answer.rb @@ -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 diff --git a/app/models/site_customization/page.rb b/app/models/site_customization/page.rb index b0c18c96a..9940e66d9 100644 --- a/app/models/site_customization/page.rb +++ b/app/models/site_customization/page.rb @@ -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 }