Created
December 20, 2010 12:58
-
-
Save nbibler/748350 to your computer and use it in GitHub Desktop.
An ActiveModel::EachValidator to check local attribute value against "project" value.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
en: | |
errors: | |
messages: | |
project_effort_exceeded: 'cannot be larger than project effort' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Task < ActiveRecord::Base | |
belongs_to :project | |
validates :effort => true, :numericality => {:greater_than_or_equal_to => 1} | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TaskEffortValidator < ActiveModel::EachValidator | |
def validate_each(record, attribute, value) | |
if record.project && record.project.effort > value | |
record.errors.add(attribute, :project_effort_exceeded, options) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment