This repository has been archived by the owner on Jan 27, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
upgrading validation_reflection plugin
- Loading branch information
Showing
11 changed files
with
512 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.DS_STORE | ||
*~ | ||
pkg* | ||
validation_reflection*.gem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
== 0.3.7 2010-06-11 | ||
* Enhancement from Sutto: | ||
** Use Rails.root if available over RAILS_ROOT | ||
|
||
== 0.3.6 2010-02-14 | ||
* Enhancement from skoppensboer: | ||
** Changes Jeweler spec to reflect only Gemcutter | ||
* Enhancement from duritong: | ||
** fix remembering of attributes defined as an array | ||
|
||
== 0.3.5, 2009-10-09 | ||
* version bump | ||
|
||
== 0.3.4, 2009-10-09 | ||
* Enhancements from Jonas Grimfelt | ||
** Don't include instead of explicit namespaces to make the code much DRY:er and readable | ||
** Avoid mutable strings | ||
** Be clear about the namespaces for external classes (to avoid Ruby 1.9.x issues) | ||
** Fixing gem loading issues on Ruby 1.9.x | ||
** Removed the freezing of validations | ||
|
||
== 0.3.3, 2009-09-12 | ||
* version bump | ||
|
||
== 0.3.2, 2009-09-12 | ||
* gemified by Christopher Redinger | ||
|
||
== 0.3.1, 2008-01-03 | ||
* require 'ostruct'; thanks to Georg Friedrich. | ||
|
||
== 0.3, 2008-01-01 | ||
* Added configurability in config/plugins/validation_reflection.rb | ||
|
||
== 0.2.1, 2006-12-28 | ||
* Moved lib files into subfolder boiler_plate. | ||
|
||
== 0.2, 2006-08-06 | ||
? | ||
|
||
= Deprecation Notice | ||
|
||
Version 0.1 had supplied three methods | ||
|
||
- validates_presence_of_mandatory_content_columns | ||
- validates_lengths_of_string_attributes | ||
- validates_all_associated | ||
|
||
These have been removed. Please use the Enforce Schema Rules plugin instead | ||
|
||
http://enforce-schema-rules.googlecode.com/svn/trunk/enforce_schema_rules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
Copyright (c) 2009 Christopher Redinger | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
Validation Reflection | ||
===================== | ||
|
||
Version 0.3.7, 2010-06-11 | ||
|
||
This plugin adds reflective access to validations | ||
|
||
- ModelClass.reflect_on_all_validations | ||
- ModelClass.reflect_on_validations_for(:property) | ||
|
||
Both of these methods return arrays containing instances of | ||
ActiveRecord::Reflection::MacroReflection. For example | ||
|
||
class Person < ActiveRecord::Base | ||
validates_presence_of :name | ||
validates_numericality_of :size, :only_integer => true | ||
end | ||
|
||
refl = Person.reflect_on_validations_for(:name) | ||
refl[0].macro | ||
# => :validates_presence_of | ||
|
||
refl = Person.reflect_on_validations_for(:size) | ||
refl[0].macro | ||
# => :validates_numericality_of | ||
refl[0].options | ||
# => { :only_integer => true } | ||
|
||
|
||
== Customization | ||
|
||
Usually, all the standard Rails validations are reflected. | ||
You can change this -- add or remove validations -- in an | ||
application-specific configuration file, | ||
|
||
config/plugins/validation_reflection.rb | ||
|
||
In that file change config.reflected_validations to suit your | ||
needs. Say, you have a custom validation for email addresses, | ||
validates_as_email, then you could add it like this | ||
|
||
config.reflected_validations << :validates_as_email | ||
|
||
If validates_as_email is implemented in terms of other validation | ||
methods, these validations are added to the reflection metadata, | ||
too. As that may not be what you want, you can disable reflection | ||
for these subordinate validations | ||
|
||
config.reflected_validations << { | ||
:method => :validates_as_email, | ||
:ignore_subvalidations => true | ||
} | ||
|
||
You have to make sure that all reflected validations are defined | ||
before this plugin is loaded. To this end, you may have to | ||
explicitly set the load order of plugins somewhere in the environment | ||
configuration using | ||
|
||
config.plugins = [...] | ||
|
||
|
||
== Special Thanks | ||
|
||
To Michael Schuerig, michael@schuerig.de for his initial concept and implementation of this plugin. | ||
|
||
== License | ||
|
||
ValidationReflection uses the MIT license. Please check the LICENSE file for more details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# encoding: utf-8 | ||
require 'rake' | ||
require 'rake/testtask' | ||
require 'rake/rdoctask' | ||
|
||
desc 'Default: run unit tests.' | ||
task :default => :test | ||
|
||
desc 'Test the validation_reflection plugin.' | ||
Rake::TestTask.new(:test) do |t| | ||
t.libs << 'lib' | ||
t.pattern = 'test/**/*_test.rb' | ||
t.verbose = true | ||
end | ||
|
||
desc 'Generate documentation for the validation_reflection plugin.' | ||
Rake::RDocTask.new(:rdoc) do |rdoc| | ||
rdoc.rdoc_dir = 'rdoc' | ||
rdoc.title = 'ValidationReflection' | ||
rdoc.options << '--line-numbers' << '--inline-source' | ||
rdoc.rdoc_files.include('README') | ||
rdoc.rdoc_files.include('lib/**/*.rb') | ||
end | ||
|
||
begin | ||
require 'jeweler' | ||
Jeweler::Tasks.new do |gemspec| | ||
gemspec.name = "validation_reflection" | ||
gemspec.summary = "Adds reflective access to validations" | ||
gemspec.description = "Adds reflective access to validations" | ||
gemspec.email = "redinger@gmail.com" | ||
gemspec.homepage = "http://github.com/redinger/validation_reflection" | ||
gemspec.authors = ["Christopher Redinger"] | ||
end | ||
Jeweler::RubyforgeTasks.new | ||
rescue LoadError | ||
puts "Jeweler not available. Install it with: sudo gem install jeweler" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
:major: 0 | ||
:minor: 3 | ||
:build: | ||
:patch: 7 |
127 changes: 127 additions & 0 deletions
127
vendor/plugins/validation_reflection/lib/validation_reflection.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
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 | ||
|
||
# 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 | ||
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 | ||
end | ||
alias_method_chain :#{validation_type}, :reflection | ||
end | ||
}, __FILE__, __LINE__ | ||
end | ||
end | ||
alias :reload :install | ||
|
||
module ClassMethods | ||
|
||
include ::ActiveRecordExtensions::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 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 | ||
|
||
# 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 | ||
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 | ||
end | ||
|
||
end | ||
end | ||
end | ||
|
||
ActiveRecord::Base.class_eval do | ||
include ::ActiveRecordExtensions::ValidationReflection | ||
::ActiveRecordExtensions::ValidationReflection.load_config | ||
::ActiveRecordExtensions::ValidationReflection.install(self) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# encoding: utf-8 | ||
require 'validation_reflection' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# encoding: utf-8 | ||
ENV['RAILS_ENV'] = 'test' | ||
RAILS_ROOT = File.join(File.dirname(__FILE__)) | ||
|
||
require 'rubygems' | ||
|
||
begin | ||
require 'active_record' | ||
rescue LoadError | ||
gem 'activerecord', '>= 1.2.3' | ||
require 'active_record' | ||
end | ||
|
||
begin | ||
require 'test/unit' | ||
rescue LoadError | ||
gem 'test-unit', '>= 1.2.3' | ||
require 'test/unit' | ||
end |
Oops, something went wrong.