Permalink
Browse files

Removed the freezing of validations - it's only causing troubles anyw…

…ay (tests, etc.). Commenting the code to make the life easier for newly introduced developers. ;)
  • Loading branch information...
1 parent 75b76e3 commit 98c9701d134fbe48806ab31459567c4caa561ee9 @grimen grimen committed Oct 2, 2009
Showing with 52 additions and 44 deletions.
  1. +12 −5 lib/validation_reflection.rb
  2. +36 −36 test/validation_reflection_test.rb
  3. +4 −3 validation_reflection.gemspec
@@ -32,9 +32,9 @@ module ValidationReflection # :nodoc:
extend self
+ # Look for config/initalizer here in:
CONFIG_PATH = ::Dir.glob((::File.join(RAILS_ROOT, 'config', '**', 'validation_reflection.rb').to_s rescue '')).first || ''
-
- @@reflected_validations = [
+ CORE_VALIDATONS = [
:validates_acceptance_of,
:validates_associated,
:validates_confirmation_of,
@@ -45,7 +45,9 @@ module ValidationReflection # :nodoc:
:validates_numericality_of,
:validates_presence_of,
:validates_uniqueness_of,
- ]
+ ].freeze
+
+ @@reflected_validations = CORE_VALIDATONS.dup
@@in_ignored_subvalidation = false
mattr_accessor :reflected_validations,
@@ -56,6 +58,9 @@ def included(base)
base.extend(ClassMethods)
end
+ # Load config/initializer on load, where ValidationReflection defaults
+ # (such as which validations to reflect upon) cane be overridden/extended.
+ #
def load_config
if ::File.file?(CONFIG_PATH)
config = ::OpenStruct.new
@@ -66,9 +71,10 @@ def load_config
end
end
+ # Iterate through all validations and store/cache the info
+ # for later easy access.
+ #
def install(base)
- @@reflected_validations.freeze
-
@@reflected_validations.each do |validation_type|
next if base.respond_to?("#{validation_type}_with_reflection")
ignore_subvalidations = false
@@ -91,6 +97,7 @@ def #{validation_type}_with_reflection(*attr_names)
}, __FILE__, __LINE__
end
end
+ alias :reload :install
module ClassMethods
@@ -17,27 +17,27 @@ def self.validates_something_selfcontained(*cols)
require 'validation_reflection'
ActiveRecord::Base.class_eval do
- include ActiveRecordExtensions::ValidationReflection
- ActiveRecordExtensions::ValidationReflection.reflected_validations << :validates_something_weird
- ActiveRecordExtensions::ValidationReflection.reflected_validations << {
- :method => :validates_something_selfcontained,
- :ignore_subvalidations => true
- }
- ActiveRecordExtensions::ValidationReflection.install(self)
+ include ::ActiveRecordExtensions::ValidationReflection
+ ::ActiveRecordExtensions::ValidationReflection.reflected_validations << :validates_something_weird
+ ::ActiveRecordExtensions::ValidationReflection.reflected_validations << {
+ :method => :validates_something_selfcontained,
+ :ignore_subvalidations => true
+ }
+ ::ActiveRecordExtensions::ValidationReflection.install(self)
end
class ValidationReflectionTest < Test::Unit::TestCase
class Dummy < ActiveRecord::Base
class << self
-
+
def create_fake_column(name, null = true, limit = nil)
sql_type = limit ? "varchar (#{limit})" : nil
col = ActiveRecord::ConnectionAdapters::Column.new(name, nil, sql_type, null)
col
end
-
+
def columns
[
create_fake_column('col0'),
@@ -51,9 +51,9 @@ def columns
]
end
end
-
+
has_one :nothing
-
+
validates_presence_of :col1
validates_length_of :col2, :maximum => 100
validates_format_of :col3, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
@@ -63,61 +63,61 @@ def columns
validates_something_selfcontained :col7
end
-
def test_sanity
assert_equal [], Dummy.reflect_on_validations_for(:col0)
end
def test_validates_presence_of_is_reflected
- refls = Dummy.reflect_on_validations_for(:col1)
- assert refls.all? { |r| r.name.to_s == 'col1' }
- assert refls.find { |r| r.macro == :validates_presence_of }
+ reflections = Dummy.reflect_on_validations_for(:col1)
+ assert reflections.all? { |r| r.name.to_s == 'col1' }
+ assert reflections.find { |r| r.macro == :validates_presence_of }
end
def test_string_limit_is_reflected
- refls = Dummy.reflect_on_validations_for(:col2)
- assert refls.any? { |r| r.macro == :validates_length_of && r.options[:maximum] == 100 }
+ reflections = Dummy.reflect_on_validations_for(:col2)
+ assert reflections.any? { |r| r.macro == :validates_length_of && r.options[:maximum] == 100 }
end
def test_format_is_reflected
- refls = Dummy.reflect_on_validations_for(:col3)
- assert refls.any? { |r| r.macro == :validates_format_of && r.options[:with] == /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i }
+ reflections = Dummy.reflect_on_validations_for(:col3)
+ assert reflections.any? { |r| r.macro == :validates_format_of && r.options[:with] == /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i }
end
def test_numeric_integer_is_reflected
- refls = Dummy.reflect_on_validations_for(:col4)
- assert refls.any? { |r| r.macro == :validates_numericality_of && r.options[:only_integer] }
+ reflections = Dummy.reflect_on_validations_for(:col4)
+ assert reflections.any? { |r| r.macro == :validates_numericality_of && r.options[:only_integer] }
end
-
+
def test_numeric_is_reflected
- refls = Dummy.reflect_on_validations_for(:col5)
- assert refls.any? { |r| r.macro == :validates_numericality_of }
+ reflections = Dummy.reflect_on_validations_for(:col5)
+ assert reflections.any? { |r| r.macro == :validates_numericality_of }
end
-
+
def test_validation_options_are_reflected
- refls = Dummy.reflect_on_validations_for(:col5)
- refl = refls[0]
+ reflections = Dummy.reflect_on_validations_for(:col5)
+ refl = reflections[0]
assert_equal 5, refl.options[:less_than]
end
def test_custom_validations_are_reflected
- refls = Dummy.reflect_on_validations_for(:col6)
- assert refls.any? { |r| r.macro == :validates_something_weird }
- assert refls.any? { |r| r.macro == :validates_format_of }
+ reflections = Dummy.reflect_on_validations_for(:col6)
+ assert reflections.any? { |r| r.macro == :validates_something_weird }
+ assert reflections.any? { |r| r.macro == :validates_format_of }
end
def test_custom_validations_with_options_are_reflected
- refls = Dummy.reflect_on_validations_for(:col7)
- assert refls.any? { |r| r.macro == :validates_something_selfcontained }
+ reflections = Dummy.reflect_on_validations_for(:col7)
+ assert reflections.any? { |r| r.macro == :validates_something_selfcontained }
end
def test_subvalidations_are_reflected
- refls = Dummy.reflect_on_validations_for(:col6)
- assert_equal 2, refls.size
+ reflections = Dummy.reflect_on_validations_for(:col6)
+ assert_equal 2, reflections.size
end
def test_ignored_subvalidations_are_not_reflected
- refls = Dummy.reflect_on_validations_for(:col7)
- assert_equal 1, refls.size
+ reflections = Dummy.reflect_on_validations_for(:col7)
+ assert_equal 1, reflections.size
end
+
end
@@ -5,19 +5,20 @@
Gem::Specification.new do |s|
s.name = %q{validation_reflection}
- s.version = "0.3.3"
+ s.version = "0.3.4"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Christopher Redinger"]
- s.date = %q{2009-09-12}
+ s.date = %q{2009-10-02}
s.description = %q{Adds reflective access to validations}
s.email = %q{redinger@gmail.com}
s.extra_rdoc_files = [
"LICENSE",
"README"
]
s.files = [
- "CHANGELOG",
+ ".gitignore",
+ "CHANGELOG",
"LICENSE",
"README",
"Rakefile",

0 comments on commit 98c9701

Please sign in to comment.