diff --git a/app/models/milestone.rb b/app/models/milestone.rb index 1b4790040..5abd5cb4f 100644 --- a/app/models/milestone.rb +++ b/app/models/milestone.rb @@ -13,7 +13,9 @@ class Milestone < ActiveRecord::Base validates :milestoneable, presence: true validates :publication_date, presence: true - validate :description_or_status_present? + + before_validation :assign_milestone_to_translations + validates_translation :description, presence: true, unless: -> { status_id.present? } scope :order_by_publication_date, -> { order(publication_date: :asc, created_at: :asc) } scope :published, -> { where("publication_date <= ?", Date.current) } @@ -23,9 +25,9 @@ class Milestone < ActiveRecord::Base 80 end - def description_or_status_present? - unless description.present? || status_id.present? - errors.add(:description) + private + + def assign_milestone_to_translations + translations.each { |translation| translation.globalized_model = self } end - end end diff --git a/app/models/milestone/translation.rb b/app/models/milestone/translation.rb new file mode 100644 index 000000000..e1b589d96 --- /dev/null +++ b/app/models/milestone/translation.rb @@ -0,0 +1,3 @@ +class Milestone::Translation < Globalize::ActiveRecord::Translation + delegate :status_id, to: :globalized_model +end diff --git a/spec/shared/features/admin_milestoneable.rb b/spec/shared/features/admin_milestoneable.rb index 66b4d1301..17f43a012 100644 --- a/spec/shared/features/admin_milestoneable.rb +++ b/spec/shared/features/admin_milestoneable.rb @@ -68,6 +68,18 @@ shared_examples "admin_milestoneable" do |factory_name, path_name| expect(page).to have_content 'New description milestone' end end + + scenario "Show validation errors with no description nor status" do + visit path + click_link "Create new milestone" + + fill_in "Date", with: Date.current + click_button "Create milestone" + + within "#new_milestone" do + expect(page).to have_content "can't be blank", count: 1 + end + end end context "Edit" do