Display description validation error in milestones
We had a validation rule for milestones which made sure either the status or the description was present. However, since the description is now translatable, the validation error wasn't being displayed in the admin form. Moving the validation rule to the translation object fixes the problem. However, the translation object needs to check an attribute in the milestone object in order to know whether the description is required or not. This is tricky because by default it loads the milestone object from the database, meaning it doesn't work with new records and it ignores params sent by the browser. The solution is to manually assign the globalized model before validating the object. It's a hack, but apparently Rails doesn't provide a better way to handle this case [1]. [1] https://github.com/rails/rails/issues/32024
This commit is contained in:
@@ -13,7 +13,9 @@ class Milestone < ActiveRecord::Base
|
|||||||
|
|
||||||
validates :milestoneable, presence: true
|
validates :milestoneable, presence: true
|
||||||
validates :publication_date, 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 :order_by_publication_date, -> { order(publication_date: :asc, created_at: :asc) }
|
||||||
scope :published, -> { where("publication_date <= ?", Date.current) }
|
scope :published, -> { where("publication_date <= ?", Date.current) }
|
||||||
@@ -23,9 +25,9 @@ class Milestone < ActiveRecord::Base
|
|||||||
80
|
80
|
||||||
end
|
end
|
||||||
|
|
||||||
def description_or_status_present?
|
private
|
||||||
unless description.present? || status_id.present?
|
|
||||||
errors.add(:description)
|
def assign_milestone_to_translations
|
||||||
|
translations.each { |translation| translation.globalized_model = self }
|
||||||
end
|
end
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
3
app/models/milestone/translation.rb
Normal file
3
app/models/milestone/translation.rb
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
class Milestone::Translation < Globalize::ActiveRecord::Translation
|
||||||
|
delegate :status_id, to: :globalized_model
|
||||||
|
end
|
||||||
@@ -68,6 +68,18 @@ shared_examples "admin_milestoneable" do |factory_name, path_name|
|
|||||||
expect(page).to have_content 'New description milestone'
|
expect(page).to have_content 'New description milestone'
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
context "Edit" do
|
context "Edit" do
|
||||||
|
|||||||
Reference in New Issue
Block a user