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:
@@ -5,11 +5,8 @@ class AdminNotification < ActiveRecord::Base
|
|||||||
translates :body, touch: true
|
translates :body, touch: true
|
||||||
include Globalizable
|
include Globalizable
|
||||||
|
|
||||||
translation_class.instance_eval do
|
validates_translation :title, presence: true
|
||||||
validates :title, presence: true
|
validates_translation :body, presence: true
|
||||||
validates :body, presence: true
|
|
||||||
end
|
|
||||||
|
|
||||||
validates :segment_recipient, presence: true
|
validates :segment_recipient, presence: true
|
||||||
validate :validate_segment_recipient
|
validate :validate_segment_recipient
|
||||||
|
|
||||||
|
|||||||
@@ -7,10 +7,8 @@ class Banner < ActiveRecord::Base
|
|||||||
translates :description, touch: true
|
translates :description, touch: true
|
||||||
include Globalizable
|
include Globalizable
|
||||||
|
|
||||||
translation_class.instance_eval do
|
validates_translation :title, presence: true, length: { minimum: 2 }
|
||||||
validates :title, presence: true, length: { minimum: 2 }
|
validates_translation :description, presence: true
|
||||||
validates :description, presence: true
|
|
||||||
end
|
|
||||||
|
|
||||||
validates :target_url, presence: true
|
validates :target_url, presence: true
|
||||||
validates :post_started_at, presence: true
|
validates :post_started_at, presence: true
|
||||||
|
|||||||
@@ -5,4 +5,11 @@ module Globalizable
|
|||||||
globalize_accessors
|
globalize_accessors
|
||||||
accepts_nested_attributes_for :translations, allow_destroy: true
|
accepts_nested_attributes_for :translations, allow_destroy: true
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
@@ -14,11 +14,8 @@ class Legislation::DraftVersion < ActiveRecord::Base
|
|||||||
belongs_to :process, class_name: 'Legislation::Process', foreign_key: 'legislation_process_id'
|
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
|
has_many :annotations, class_name: 'Legislation::Annotation', foreign_key: 'legislation_draft_version_id', dependent: :destroy
|
||||||
|
|
||||||
translation_class.instance_eval do
|
validates_translation :title, presence: true
|
||||||
validates :title, presence: true
|
validates_translation :body, presence: true
|
||||||
validates :body, presence: true
|
|
||||||
end
|
|
||||||
|
|
||||||
validates :status, presence: true, inclusion: { in: VALID_STATUSES }
|
validates :status, presence: true, inclusion: { in: VALID_STATUSES }
|
||||||
|
|
||||||
scope :published, -> { where(status: 'published').order('id DESC') }
|
scope :published, -> { where(status: 'published').order('id DESC') }
|
||||||
|
|||||||
@@ -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 :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
|
has_many :proposals, -> { order(:id) }, class_name: 'Legislation::Proposal', foreign_key: 'legislation_process_id', dependent: :destroy
|
||||||
|
|
||||||
translation_class.instance_eval do
|
validates_translation :title, presence: true
|
||||||
validates :title, presence: true
|
|
||||||
end
|
|
||||||
|
|
||||||
validates :start_date, presence: true
|
validates :start_date, presence: true
|
||||||
validates :end_date, presence: true
|
validates :end_date, presence: true
|
||||||
validates :debate_start_date, presence: true, if: :debate_end_date?
|
validates :debate_start_date, presence: true, if: :debate_end_date?
|
||||||
|
|||||||
@@ -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
|
accepts_nested_attributes_for :question_options, reject_if: proc { |attributes| attributes.all? { |k, v| v.blank? } }, allow_destroy: true
|
||||||
|
|
||||||
validates :process, presence: true
|
validates :process, presence: true
|
||||||
|
validates_translation :title, presence: true
|
||||||
translation_class.instance_eval do
|
|
||||||
validates :title, presence: true
|
|
||||||
end
|
|
||||||
|
|
||||||
scope :sorted, -> { order('id ASC') }
|
scope :sorted, -> { order('id ASC') }
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
has_many :answers, class_name: 'Legislation::Answer', foreign_key: 'legislation_question_id', dependent: :destroy, inverse_of: :question
|
||||||
|
|
||||||
validates :question, presence: true
|
validates :question, presence: true
|
||||||
|
validates_translation :value, presence: true # TODO: add uniqueness again
|
||||||
translation_class.instance_eval do
|
|
||||||
validates :value, presence: true # TODO: add uniqueness again
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -24,10 +24,7 @@ class Poll < ActiveRecord::Base
|
|||||||
has_and_belongs_to_many :geozones
|
has_and_belongs_to_many :geozones
|
||||||
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'
|
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'
|
||||||
|
|
||||||
translation_class.instance_eval do
|
validates_translation :name, presence: true
|
||||||
validates :name, presence: true
|
|
||||||
end
|
|
||||||
|
|
||||||
validate :date_range
|
validate :date_range
|
||||||
|
|
||||||
scope :current, -> { where('starts_at <= ? and ? <= ends_at', Date.current.beginning_of_day, Date.current.beginning_of_day) }
|
scope :current, -> { where('starts_at <= ? and ? <= ends_at', Date.current.beginning_of_day, Date.current.beginning_of_day) }
|
||||||
|
|||||||
@@ -17,10 +17,7 @@ class Poll::Question < ActiveRecord::Base
|
|||||||
has_many :partial_results
|
has_many :partial_results
|
||||||
belongs_to :proposal
|
belongs_to :proposal
|
||||||
|
|
||||||
translation_class.instance_eval do
|
validates_translation :title, presence: true, length: { minimum: 4 }
|
||||||
validates :title, presence: true, length: { minimum: 4 }
|
|
||||||
end
|
|
||||||
|
|
||||||
validates :author, presence: true
|
validates :author, presence: true
|
||||||
validates :poll_id, presence: true
|
validates :poll_id, presence: true
|
||||||
|
|
||||||
|
|||||||
@@ -14,10 +14,7 @@ class Poll::Question::Answer < ActiveRecord::Base
|
|||||||
belongs_to :question, class_name: 'Poll::Question', foreign_key: 'question_id'
|
belongs_to :question, class_name: 'Poll::Question', foreign_key: 'question_id'
|
||||||
has_many :videos, class_name: 'Poll::Question::Answer::Video'
|
has_many :videos, class_name: 'Poll::Question::Answer::Video'
|
||||||
|
|
||||||
translation_class.instance_eval do
|
validates_translation :title, presence: true
|
||||||
validates :title, presence: true
|
|
||||||
end
|
|
||||||
|
|
||||||
validates :given_order, presence: true, uniqueness: { scope: :question_id }
|
validates :given_order, presence: true, uniqueness: { scope: :question_id }
|
||||||
|
|
||||||
before_validation :set_order, on: :create
|
before_validation :set_order, on: :create
|
||||||
|
|||||||
@@ -6,10 +6,7 @@ class SiteCustomization::Page < ActiveRecord::Base
|
|||||||
translates :content, touch: true
|
translates :content, touch: true
|
||||||
include Globalizable
|
include Globalizable
|
||||||
|
|
||||||
translation_class.instance_eval do
|
validates_translation :title, presence: true
|
||||||
validates :title, presence: true
|
|
||||||
end
|
|
||||||
|
|
||||||
validates :slug, presence: true,
|
validates :slug, presence: true,
|
||||||
uniqueness: { case_sensitive: false },
|
uniqueness: { case_sensitive: false },
|
||||||
format: { with: /\A[0-9a-zA-Z\-_]*\Z/, message: :slug_format }
|
format: { with: /\A[0-9a-zA-Z\-_]*\Z/, message: :slug_format }
|
||||||
|
|||||||
Reference in New Issue
Block a user