Skip to content

Instantly share code, notes, and snippets.

@delsoft
Created January 27, 2014 19:49
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 delsoft/8655967 to your computer and use it in GitHub Desktop.
Save delsoft/8655967 to your computer and use it in GitHub Desktop.
rails fix validation context on active record
# monkey patch on ActiveRecord::Base
class ActiveRecord::Base
alias_method :_ant_valid?, :valid?
def self.fix_validation_context context=nil
$glb_vld_ctx_3r4t_Arr ||= []
$glb_vld_ctx_3r4t_Arr.push context
$glb_vld_ctx_3r4t_ = context
begin
yield if block_given?
ensure
$glb_vld_ctx_3r4t_Arr.pop
$glb_vld_ctx_3r4t_ = $glb_vld_ctx_3r4t_Arr.last
end
end
def valid?(context = nil)
context ||= $glb_vld_ctx_3r4t_
_ant_valid?(context)
end
end
# Using the patch ;-)
ActiveRecord::Base.fix_validation_context :importacao do
DataImport::BusinessUnitFkConductor.new(valid_company).save(context: :importacao)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment