Skip to content
Browse files

Merge branch 'rails-3'

Conflicts:
	CHANGELOG
	README
	VERSION.yml
	lib/validation_reflection.rb
	validation_reflection.gemspec
  • Loading branch information...
2 parents 3017dad + be4086d commit 97f2e85bf4e4ab764dc1cd912b82bcf87ef7e4b8 @redinger committed
Showing with 102 additions and 118 deletions.
  1. +9 −0 CHANGELOG
  2. +3 −3 README
  3. +3 −4 VERSION.yml
  4. +81 −105 lib/validation_reflection.rb
  5. +4 −4 test/validation_reflection_test.rb
  6. +2 −2 validation_reflection.gemspec
View
9 CHANGELOG
@@ -1,3 +1,12 @@
+== 1.0.0 2010-10-08
+* 1.0 release for Rails 3
+
+== 1.0.0-rc.1 2010-07-30
+* Fixed reload bug
+
+== 1.0.0-beta4 2010-06-12
+* Changed initialization to reflect new initialization in Rails 3.
+
== 0.3.8 2010-07-30
* Enhancement from Sutto:
** Fix reload bug
View
6 README
@@ -1,7 +1,7 @@
Validation Reflection
=====================
-Version 0.3.8, 2010-07-30
+Version 1.0.0, 2010-10-08
This plugin adds reflective access to validations
@@ -31,9 +31,9 @@ ActiveRecord::Reflection::MacroReflection. For example
Usually, all the standard Rails validations are reflected.
You can change this -- add or remove validations -- in an
-application-specific configuration file,
+application-specific initializer,
- config/plugins/validation_reflection.rb
+ config/initializers/validation_reflection.rb
In that file change config.reflected_validations to suit your
needs. Say, you have a custom validation for email addresses,
View
7 VERSION.yml
@@ -1,5 +1,4 @@
---
-:major: 0
-:minor: 3
-:build:
-:patch: 8
+:major: 1
+:minor: 0
+:patch: 0
View
186 lib/validation_reflection.rb
@@ -1,126 +1,102 @@
-require 'active_record/reflection'
require 'ostruct'
-# Based on code by Sebastian Kanthak
-# See http://dev.rubyonrails.org/ticket/861
-#
-module ActiveRecordExtensions # :nodoc:
- module ValidationReflection # :nodoc:
-
- extend self
-
- require_path = ::File.join((defined?(Rails) ? Rails.root : RAILS_ROOT), 'config', '**', 'validation_reflection.rb').to_s rescue ''
-
- # Look for config/initializer here in:
- CONFIG_PATH = ::Dir.glob(require_path).first || ''
- CORE_VALIDATONS = [
- :validates_acceptance_of,
- :validates_associated,
- :validates_confirmation_of,
- :validates_exclusion_of,
- :validates_format_of,
- :validates_inclusion_of,
- :validates_length_of,
- :validates_numericality_of,
- :validates_presence_of,
- :validates_uniqueness_of,
- ].freeze
-
- @@reflected_validations = CORE_VALIDATONS.dup
- @@in_ignored_subvalidation = false
-
- mattr_accessor :reflected_validations,
- :in_ignored_subvalidation
-
- def included(base) # :nodoc:
- return if base.kind_of?(::ActiveRecordExtensions::ValidationReflection::ClassMethods)
- base.extend(ClassMethods)
- end
+module ValidationReflection # :nodoc:
+
+ extend self
+
+ CORE_VALIDATONS = [
+ :validates_acceptance_of,
+ :validates_associated,
+ :validates_confirmation_of,
+ :validates_exclusion_of,
+ :validates_format_of,
+ :validates_inclusion_of,
+ :validates_length_of,
+ :validates_numericality_of,
+ :validates_presence_of,
+ :validates_uniqueness_of,
+ ].freeze
+
+ @@reflected_validations = CORE_VALIDATONS.dup
+
+ @@in_ignored_subvalidation = false
+
+ mattr_accessor :reflected_validations,
+ :in_ignored_subvalidation
+
+ def included(base) # :nodoc:
+ return if base.kind_of?(::ValidationReflection::ClassMethods)
+ 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
- config.reflected_validations = @@reflected_validations
- silence_warnings do
- eval(::IO.read(CONFIG_PATH), binding, CONFIG_PATH)
- end
+ # Iterate through all validations and store/cache the info
+ # for later easy access.
+ #
+ def install(base)
+ @@reflected_validations.each do |validation_type|
+ next if base.respond_to?(:"#{validation_type}_with_reflection")
+ ignore_subvalidations = false
+
+ if validation_type.kind_of?(::Hash)
+ ignore_subvalidations = validation_type[:ignore_subvalidations]
+ validation_type = validation_type[:method]
end
- end
-
- # Iterate through all validations and store/cache the info
- # for later easy access.
- #
- def install(base)
- @@reflected_validations.each do |validation_type|
- next if base.respond_to?(:"#{validation_type}_with_reflection")
- ignore_subvalidations = false
-
- if validation_type.kind_of?(::Hash)
- ignore_subvalidations = validation_type[:ignore_subvalidations]
- validation_type = validation_type[:method]
- end
-
- base.class_eval %{
- class << self
- def #{validation_type}_with_reflection(*attr_names)
- ignoring_subvalidations(#{ignore_subvalidations}) do
- #{validation_type}_without_reflection(*attr_names)
- remember_validation_metadata(:#{validation_type}, *attr_names)
- end
+
+ base.class_eval %{
+ class << self
+ def #{validation_type}_with_reflection(*attr_names)
+ ignoring_subvalidations(#{ignore_subvalidations}) do
+ #{validation_type}_without_reflection(*attr_names)
+ remember_validation_metadata(:#{validation_type}, *attr_names)
end
- alias_method_chain :#{validation_type}, :reflection
end
- }, __FILE__, __LINE__
- end
+ alias_method_chain :#{validation_type}, :reflection
+ end
+ }, __FILE__, __LINE__
end
+ end
- module ClassMethods
+ module ClassMethods
- include ::ActiveRecordExtensions::ValidationReflection
+ include ::ValidationReflection
- # Returns an array of MacroReflection objects for all validations in the class
- def reflect_on_all_validations
- self.read_inheritable_attribute(:validations) || []
- end
+ # Returns an array of MacroReflection objects for all validations in the class
+ def reflect_on_all_validations
+ self.read_inheritable_attribute(:reflected_validations) || []
+ end
- # Returns an array of MacroReflection objects for all validations defined for the field +attr_name+.
- def reflect_on_validations_for(attr_name)
- self.reflect_on_all_validations.select do |reflection|
- reflection.name == attr_name.to_sym
- end
+ # Returns an array of MacroReflection objects for all validations defined for the field +attr_name+.
+ def reflect_on_validations_for(attr_name)
+ self.reflect_on_all_validations.select do |reflection|
+ reflection.name == attr_name.to_sym
end
+ end
- private
+ private
- # Store validation info for easy and fast access.
- #
- def remember_validation_metadata(validation_type, *attr_names)
- configuration = attr_names.last.is_a?(::Hash) ? attr_names.pop : {}
- attr_names.flatten.each do |attr_name|
- self.write_inheritable_array :validations,
- [::ActiveRecord::Reflection::MacroReflection.new(validation_type, attr_name.to_sym, configuration, self)]
- end
+ # Store validation info for easy and fast access.
+ #
+ def remember_validation_metadata(validation_type, *attr_names)
+ configuration = attr_names.last.is_a?(::Hash) ? attr_names.pop : {}
+ attr_names.flatten.each do |attr_name|
+ self.write_inheritable_array :reflected_validations,
+ [::ActiveRecord::Reflection::MacroReflection.new(validation_type, attr_name.to_sym, configuration, self)]
end
+ end
- def ignoring_subvalidations(ignore)
- save_ignore = self.in_ignored_subvalidation
- unless self.in_ignored_subvalidation
- self.in_ignored_subvalidation = ignore
- yield
- end
- ensure
- self.in_ignored_subvalidation = save_ignore
+ def ignoring_subvalidations(ignore)
+ save_ignore = self.in_ignored_subvalidation
+ unless self.in_ignored_subvalidation
+ self.in_ignored_subvalidation = ignore
+ yield
end
-
- end
+ ensure
+ self.in_ignored_subvalidation = save_ignore
+ end
end
end
-ActiveRecord::Base.class_eval do
- include ::ActiveRecordExtensions::ValidationReflection
- ::ActiveRecordExtensions::ValidationReflection.load_config
- ::ActiveRecordExtensions::ValidationReflection.install(self)
+ActiveSupport.on_load(:active_record) do
+ include ValidationReflection
+ ::ValidationReflection.install(self)
end
View
8 test/validation_reflection_test.rb
@@ -17,13 +17,13 @@ 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 << {
+ include ValidationReflection
+ ::ValidationReflection.reflected_validations << :validates_something_weird
+ ::ValidationReflection.reflected_validations << {
:method => :validates_something_selfcontained,
:ignore_subvalidations => true
}
- ::ActiveRecordExtensions::ValidationReflection.install(self)
+ ::ValidationReflection.install(self)
end
View
4 validation_reflection.gemspec
@@ -5,11 +5,11 @@
Gem::Specification.new do |s|
s.name = %q{validation_reflection}
- s.version = "0.3.8"
+ s.version = "1.0.0"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Christopher Redinger"]
- s.date = %q{2010-07-30}
+ s.date = %q{2010-10-08}
s.description = %q{Adds reflective access to validations}
s.email = %q{redinger@gmail.com}
s.extra_rdoc_files = [

0 comments on commit 97f2e85

Please sign in to comment.
Something went wrong with that request. Please try again.