Created
September 18, 2014 15:36
-
-
Save ashleyconnor/8cfecc5eeea300f69bcc to your computer and use it in GitHub Desktop.
Context Validations
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 Article | |
with_options on: :view do |on_view| | |
on_view.validate :has_been_published | |
on_view.validate :is_not_spam | |
end | |
def viewable? | |
valid? :view | |
end | |
private | |
def has_been_published | |
if published_at.future? | |
errors.add(:published_at, "is in the future") | |
end | |
end | |
def is_not_spam | |
if is_spam?(body) | |
errors.add(:body, "has been detected as spam") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment