Skip to content

Instantly share code, notes, and snippets.

@ashleyconnor
Created September 18, 2014 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashleyconnor/8cfecc5eeea300f69bcc to your computer and use it in GitHub Desktop.
Save ashleyconnor/8cfecc5eeea300f69bcc to your computer and use it in GitHub Desktop.
Context Validations
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