Skip to content

Instantly share code, notes, and snippets.

@dchandekstark
Created February 6, 2015 00:26
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 dchandekstark/eb835d7d4e8750875395 to your computer and use it in GitHub Desktop.
Save dchandekstark/eb835d7d4e8750875395 to your computer and use it in GitHub Desktop.
Validation
class Model
attr_accessor :title
# has-a validator
def validator
@validator ||= ModelValidator.new(self)
end
end
# Represents a validation attempt
class Validation
attr_reader :errors
def initialize(errors)
@errors = errors
end
end
require "delegate"
class Validator < SimpleDelegator
include ActiveModel::Validations
# @return [Validation]
def validate
super
Validation.new(errors)
end
end
class ModelValidator < Validator
validates_presence_of :title
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment