Class: SampleModels::Initializer
- Inherits:
-
Object
- Object
- SampleModels::Initializer
- Defined in:
- lib/sample_models/initializer.rb
Instance Method Summary collapse
- #intercept_validation_definition(validation, recipient) ⇒ Object
- #intercept_validation_definitions ⇒ Object
- #run ⇒ Object
- #validation_recipients ⇒ Object
Instance Method Details
#intercept_validation_definition(validation, recipient) ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/sample_models/initializer.rb', line 8 def intercept_validation_definition(validation, recipient) method_name = "#{validation}_with_sample_models".to_sym recipient.send(:define_method, method_name) do |*args| send "#{validation}_without_sample_models".to_sym, *args SampleModels.models[self].record_validation(validation, *args) end recipient.alias_method_chain validation, :sample_models end |
#intercept_validation_definitions ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/sample_models/initializer.rb', line 17 def intercept_validation_definitions validations_to_intercept = [ :validates_email_format_of, :validates_inclusion_of, :validates_length_of, :validates_presence_of, :validates_uniqueness_of ] optional_interceptions = [:validates_email_format_of] validations_to_intercept.each do |validation| recipient = validation_recipients.detect { |vr| vr.method_defined?(validation) } if recipient intercept_validation_definition(validation, recipient) else unless optional_interceptions.include?(validation) raise "Can't find who defines the validation method #{validation}" end end end end |
#run ⇒ Object
3 4 5 6 |
# File 'lib/sample_models/initializer.rb', line 3 def run ActiveRecord::Base.send(:include, SampleModels) intercept_validation_definitions end |
#validation_recipients ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/sample_models/initializer.rb', line 37 def validation_recipients validation_recipients = [ActiveRecord::Validations::ClassMethods] if Object.const_defined?('ActiveModel') validation_recipients << ActiveModel::Validations::HelperMethods end if Object.const_defined?('ValidatesEmailFormatOf') validation_recipients << ValidatesEmailFormatOf::Validations end validation_recipients end |