redinger-validation_reflection 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,26 @@
1
+ == 0.3.2, 2009-09-12
2
+ * gemified by Christopher Redinger
3
+
4
+ == 0.3.1, 2008-01-03
5
+ * require 'ostruct'; thanks to Georg Friedrich.
6
+
7
+ == 0.3, 2008-01-01
8
+ * Added configurability in config/plugins/validation_reflection.rb
9
+
10
+ == 0.2.1, 2006-12-28
11
+ * Moved lib files into subfolder boiler_plate.
12
+
13
+ == 0.2, 2006-08-06
14
+ ?
15
+
16
+ = Deprecation Notice
17
+
18
+ Version 0.1 had supplied three methods
19
+
20
+ - validates_presence_of_mandatory_content_columns
21
+ - validates_lengths_of_string_attributes
22
+ - validates_all_associated
23
+
24
+ These have been removed. Please use the Enforce Schema Rules plugin instead
25
+
26
+ http://enforce-schema-rules.googlecode.com/svn/trunk/enforce_schema_rules/
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2006 Michael Schuerig
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,64 @@
1
+ Validation Reflection
2
+ =====================
3
+
4
+ Version 0.3.1, 2008-01-03
5
+
6
+ This plugin adds reflective access to validations
7
+
8
+ - ModelClass.reflect_on_all_validations
9
+ - ModelClass.reflect_on_validations_for(:property)
10
+
11
+ Both of these methods return arrays containing instances of
12
+ ActiveRecord::Reflection::MacroReflection. For example
13
+
14
+ class Person < ActiveRecord::Base
15
+ validates_presence_of :name
16
+ validates_numericality_of :size, :only_integer => true
17
+ end
18
+
19
+ refl = Person.reflect_on_validations_for(:name)
20
+ refl[0].macro
21
+ # => :validates_presence_of
22
+
23
+ refl = Person.reflect_on_validations_for(:size)
24
+ refl[0].macro
25
+ # => :validates_numericality_of
26
+ refl[0].options
27
+ # => { :only_integer => true }
28
+
29
+
30
+ == Customization
31
+
32
+ Usually, all the standard Rails validations are reflected.
33
+ You can change this -- add or remove validations -- in an
34
+ application-specific configuration file,
35
+
36
+ config/plugins/validation_reflection.rb
37
+
38
+ In that file change config.reflected_validations to suit your
39
+ needs. Say, you have a custom validation for email addresses,
40
+ validates_as_email, then you could add it like this
41
+
42
+ config.reflected_validations << :validates_as_email
43
+
44
+ If validates_as_email is implemented in terms of other validation
45
+ methods, these validations are added to the reflection metadata,
46
+ too. As that may not be what you want, you can disable reflection
47
+ for these subordinate validations
48
+
49
+ config.reflected_validations << {
50
+ :method => :validates_as_email,
51
+ :ignore_subvalidations => true
52
+ }
53
+
54
+ You have to make sure that all reflected validations are defined
55
+ before this plugin is loaded. To this end, you may have to
56
+ explicitly set the load order of plugins somewhere in the environment
57
+ configuration using
58
+
59
+ config.plugins = [...]
60
+
61
+
62
+
63
+
64
+ Copyright (c) 2006-2008, Michael Schuerig, michael@schuerig.de
data/Rakefile ADDED
@@ -0,0 +1,36 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the validation_reflection plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the validation_reflection plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'ValidationReflection'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
23
+
24
+ begin
25
+ require 'jeweler'
26
+ Jeweler::Tasks.new do |gemspec|
27
+ gemspec.name = "validation_reflection"
28
+ gemspec.summary = "Adds reflective access to validations"
29
+ gemspec.description = "Adds reflective access to validations"
30
+ gemspec.email = "redinger@gmail.com"
31
+ gemspec.homepage = "http://github.com/redinger/validation_reflection"
32
+ gemspec.authors = ["Christopher Redinger"]
33
+ end
34
+ rescue LoadError
35
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
36
+ end
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 3
3
+ :patch: 2
4
+ :major: 0
data/about.yml ADDED
@@ -0,0 +1,7 @@
1
+ author: Michael Schuerig
2
+ summary: Reflection on validations
3
+ homepage: http://rubyforge.org/projects/valirefl/validation_reflection/trunk
4
+ plugin: validation_reflection
5
+ license: MIT
6
+ version: 0.3.1
7
+ rails_version: >= 1.1.6
@@ -0,0 +1,127 @@
1
+ #--
2
+ # Copyright (c) 2006-2008, Michael Schuerig, michael@schuerig.de
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+
25
+ require 'active_record/reflection'
26
+ require 'ostruct'
27
+
28
+ # Based on code by Sebastian Kanthak
29
+ # See http://dev.rubyonrails.org/ticket/861
30
+ module ActiveRecordExtensions # :nodoc:
31
+ module ValidationReflection # :nodoc:
32
+ CONFIG_PATH = File.join(RAILS_ROOT, 'config', 'plugins', 'validation_reflection.rb')
33
+
34
+ mattr_accessor :reflected_validations
35
+ ActiveRecordExtensions::ValidationReflection.reflected_validations = %w(
36
+ validates_acceptance_of
37
+ validates_associated
38
+ validates_confirmation_of
39
+ validates_exclusion_of
40
+ validates_format_of
41
+ validates_inclusion_of
42
+ validates_length_of
43
+ validates_numericality_of
44
+ validates_presence_of
45
+ validates_uniqueness_of
46
+ )
47
+
48
+ mattr_accessor :in_ignored_subvalidation
49
+ ActiveRecordExtensions::ValidationReflection.in_ignored_subvalidation = false
50
+
51
+ def self.included(base)
52
+ return if base.kind_of?(ActiveRecordExtensions::ValidationReflection::ClassMethods)
53
+ base.extend(ClassMethods)
54
+ end
55
+
56
+ def self.load_config
57
+ if File.file?(CONFIG_PATH)
58
+ config = OpenStruct.new
59
+ config.reflected_validations = reflected_validations
60
+ silence_warnings do
61
+ eval(IO.read(CONFIG_PATH), binding, CONFIG_PATH)
62
+ end
63
+ end
64
+ end
65
+
66
+ def self.install(base)
67
+ reflected_validations.freeze
68
+ reflected_validations.each do |validation_type|
69
+ next if base.respond_to?("#{validation_type}_with_reflection")
70
+ ignore_subvalidations = false
71
+ if validation_type.kind_of?(Hash)
72
+ ignore_subvalidations = validation_type[:ignore_subvalidations]
73
+ validation_type = validation_type[:method]
74
+ end
75
+ base.class_eval <<-"end_eval"
76
+ class << self
77
+ def #{validation_type}_with_reflection(*attr_names)
78
+ ignoring_subvalidations(#{ignore_subvalidations}) do
79
+ #{validation_type}_without_reflection(*attr_names)
80
+ remember_validation_metadata(:#{validation_type}, *attr_names)
81
+ end
82
+ end
83
+
84
+ alias_method_chain :#{validation_type}, :reflection
85
+ end
86
+ end_eval
87
+ end
88
+ end
89
+
90
+ module ClassMethods
91
+
92
+ # Returns an array of MacroReflection objects for all validations in the class
93
+ def reflect_on_all_validations
94
+ read_inheritable_attribute(:validations) || []
95
+ end
96
+
97
+ # Returns an array of MacroReflection objects for all validations defined for the field +attr_name+.
98
+ def reflect_on_validations_for(attr_name)
99
+ attr_name = attr_name.to_sym
100
+ reflect_on_all_validations.select do |reflection|
101
+ reflection.name == attr_name
102
+ end
103
+ end
104
+
105
+ private
106
+
107
+ def remember_validation_metadata(validation_type, *attr_names)
108
+ configuration = attr_names.last.is_a?(Hash) ? attr_names.pop : {}
109
+ attr_names.each do |attr_name|
110
+ write_inheritable_array :validations,
111
+ [ ActiveRecord::Reflection::MacroReflection.new(validation_type, attr_name.to_sym, configuration, self) ]
112
+ end
113
+ end
114
+
115
+ def ignoring_subvalidations(ignore)
116
+ save_ignore = ActiveRecordExtensions::ValidationReflection.in_ignored_subvalidation
117
+ unless ActiveRecordExtensions::ValidationReflection.in_ignored_subvalidation
118
+ ActiveRecordExtensions::ValidationReflection.in_ignored_subvalidation = ignore
119
+ yield
120
+ end
121
+ ensure
122
+ ActiveRecordExtensions::ValidationReflection.in_ignored_subvalidation = save_ignore
123
+ end
124
+ end
125
+
126
+ end
127
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,5 @@
1
+ ActiveRecord::Base.class_eval do
2
+ include ActiveRecordExtensions::ValidationReflection
3
+ ActiveRecordExtensions::ValidationReflection.load_config
4
+ ActiveRecordExtensions::ValidationReflection.install(self)
5
+ end
@@ -0,0 +1,6 @@
1
+ ENV["RAILS_ENV"] = "test"
2
+ RAILS_ROOT = File.join(File.dirname(__FILE__))
3
+
4
+ require 'rubygems'
5
+ require 'test/unit'
6
+ require 'active_record'
@@ -0,0 +1,122 @@
1
+ require 'test_helper'
2
+
3
+ ActiveRecord::Base.class_eval do
4
+ def self.validates_something_weird(*cols)
5
+ cols.each do |col|
6
+ validates_format_of col, :with => /weird/
7
+ end
8
+ end
9
+ def self.validates_something_selfcontained(*cols)
10
+ cols.each do |col|
11
+ validates_format_of col, :with => /blablabla/
12
+ end
13
+ end
14
+ end
15
+
16
+ require 'validation_reflection'
17
+
18
+ ActiveRecord::Base.class_eval do
19
+ include ActiveRecordExtensions::ValidationReflection
20
+ ActiveRecordExtensions::ValidationReflection.reflected_validations << :validates_something_weird
21
+ ActiveRecordExtensions::ValidationReflection.reflected_validations << {
22
+ :method => :validates_something_selfcontained,
23
+ :ignore_subvalidations => true
24
+ }
25
+ ActiveRecordExtensions::ValidationReflection.install(self)
26
+ end
27
+
28
+
29
+ class ValidationReflectionTest < Test::Unit::TestCase
30
+
31
+ class Dummy < ActiveRecord::Base
32
+ class << self
33
+
34
+ def create_fake_column(name, null = true, limit = nil)
35
+ sql_type = limit ? "varchar (#{limit})" : nil
36
+ col = ActiveRecord::ConnectionAdapters::Column.new(name, nil, sql_type, null)
37
+ col
38
+ end
39
+
40
+ def columns
41
+ [
42
+ create_fake_column('col0'),
43
+ create_fake_column('col1'),
44
+ create_fake_column('col2', false, 100),
45
+ create_fake_column('col3'),
46
+ create_fake_column('col4'),
47
+ create_fake_column('col5'),
48
+ create_fake_column('col6'),
49
+ create_fake_column('col7')
50
+ ]
51
+ end
52
+ end
53
+
54
+ has_one :nothing
55
+
56
+ validates_presence_of :col1
57
+ validates_length_of :col2, :maximum => 100
58
+ validates_format_of :col3, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
59
+ validates_numericality_of :col4, :only_integer => true
60
+ validates_numericality_of :col5, :less_than => 5
61
+ validates_something_weird :col6
62
+ validates_something_selfcontained :col7
63
+ end
64
+
65
+
66
+ def test_sanity
67
+ assert_equal [], Dummy.reflect_on_validations_for(:col0)
68
+ end
69
+
70
+ def test_validates_presence_of_is_reflected
71
+ refls = Dummy.reflect_on_validations_for(:col1)
72
+ assert refls.all? { |r| r.name.to_s == 'col1' }
73
+ assert refls.find { |r| r.macro == :validates_presence_of }
74
+ end
75
+
76
+ def test_string_limit_is_reflected
77
+ refls = Dummy.reflect_on_validations_for(:col2)
78
+ assert refls.any? { |r| r.macro == :validates_length_of && r.options[:maximum] == 100 }
79
+ end
80
+
81
+ def test_format_is_reflected
82
+ refls = Dummy.reflect_on_validations_for(:col3)
83
+ assert refls.any? { |r| r.macro == :validates_format_of && r.options[:with] == /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i }
84
+ end
85
+
86
+ def test_numeric_integer_is_reflected
87
+ refls = Dummy.reflect_on_validations_for(:col4)
88
+ assert refls.any? { |r| r.macro == :validates_numericality_of && r.options[:only_integer] }
89
+ end
90
+
91
+ def test_numeric_is_reflected
92
+ refls = Dummy.reflect_on_validations_for(:col5)
93
+ assert refls.any? { |r| r.macro == :validates_numericality_of }
94
+ end
95
+
96
+ def test_validation_options_are_reflected
97
+ refls = Dummy.reflect_on_validations_for(:col5)
98
+ refl = refls[0]
99
+ assert_equal 5, refl.options[:less_than]
100
+ end
101
+
102
+ def test_custom_validations_are_reflected
103
+ refls = Dummy.reflect_on_validations_for(:col6)
104
+ assert refls.any? { |r| r.macro == :validates_something_weird }
105
+ assert refls.any? { |r| r.macro == :validates_format_of }
106
+ end
107
+
108
+ def test_custom_validations_with_options_are_reflected
109
+ refls = Dummy.reflect_on_validations_for(:col7)
110
+ assert refls.any? { |r| r.macro == :validates_something_selfcontained }
111
+ end
112
+
113
+ def test_subvalidations_are_reflected
114
+ refls = Dummy.reflect_on_validations_for(:col6)
115
+ assert_equal 2, refls.size
116
+ end
117
+
118
+ def test_ignored_subvalidations_are_not_reflected
119
+ refls = Dummy.reflect_on_validations_for(:col7)
120
+ assert_equal 1, refls.size
121
+ end
122
+ end
@@ -0,0 +1,51 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{validation_reflection}
8
+ s.version = "0.3.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Christopher Redinger"]
12
+ s.date = %q{2009-09-12}
13
+ s.description = %q{Adds reflective access to validations}
14
+ s.email = %q{redinger@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README"
18
+ ]
19
+ s.files = [
20
+ "CHANGELOG",
21
+ "LICENSE",
22
+ "README",
23
+ "Rakefile",
24
+ "VERSION.yml",
25
+ "about.yml",
26
+ "lib/validation_reflection.rb",
27
+ "rails/init.rb",
28
+ "test/test_helper.rb",
29
+ "test/validation_reflection_test.rb",
30
+ "validation_reflection.gemspec"
31
+ ]
32
+ s.homepage = %q{http://github.com/redinger/validation_reflection}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.5}
36
+ s.summary = %q{Adds reflective access to validations}
37
+ s.test_files = [
38
+ "test/test_helper.rb",
39
+ "test/validation_reflection_test.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
47
+ else
48
+ end
49
+ else
50
+ end
51
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redinger-validation_reflection
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.2
5
+ platform: ruby
6
+ authors:
7
+ - Christopher Redinger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-12 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Adds reflective access to validations
17
+ email: redinger@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README
25
+ files:
26
+ - CHANGELOG
27
+ - LICENSE
28
+ - README
29
+ - Rakefile
30
+ - VERSION.yml
31
+ - about.yml
32
+ - lib/validation_reflection.rb
33
+ - rails/init.rb
34
+ - test/test_helper.rb
35
+ - test/validation_reflection_test.rb
36
+ - validation_reflection.gemspec
37
+ has_rdoc: false
38
+ homepage: http://github.com/redinger/validation_reflection
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --charset=UTF-8
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.2.0
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Adds reflective access to validations
63
+ test_files:
64
+ - test/test_helper.rb
65
+ - test/validation_reflection_test.rb