Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject Validations #6014

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions activemodel/lib/active_model/inject_validations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
module ActiveModel
# When you have a module
#
# module Foo
# extend ActiveModel::InjectValidations
#
# inject_validations do |c|
# c.validates_presence_of :price
# end
# end
#
# and a model
#
# class Offer < ActiveRecord::Base ; end
#
# and you extend an instance of +Offer+ like this
#
# offer = Offer.new
# offer.extend(Foo)
#
# The validation behaviour of +offer+, and only that instance,
# will be the same as if you had
#
# class Offer < ActiveRecord::base
# validates_presence_of :price
# end
#
# It assumes that all validation is done with the valid? method on the object,
# which is the case for the current version of ActiveModel (3.2.3).
module InjectValidations

def inject_validations(&block)
@validations = block
end

private

def self.extended(base)
base.module_eval do
def self.extended(real_base)
super(real_base)
context_sym = (self.name.gsub(":","").to_s + "Context").underscore.to_sym

# Do not redefine validations on base class, if already included once.
sym = ("@@" + context_sym.to_s).to_sym
unless real_base.class.class_variable_defined?(sym)
real_base.class.class_variable_set(sym, true)
vs = @validations
real_base.class.instance_eval do
with_options :on => context_sym, &vs
end
end

real_base.define_singleton_method(:valid?) do |context=nil|
valid = super(context_sym)
unless context.nil?
_errors = errors.to_hash
valid &= super(context)

# Merge errors from both contexts
_errors.each { |k,v| errors.set(k, (_errors[k] + v).uniq) }
end
valid
end
end
end
end
end
end