markcatley-validates_constancy 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,10 @@
1
+ *SVN*
2
+
3
+ *1.0.1*
4
+
5
+ * Fix bug #14521 addressing subclasses of a constancy-validated model that do
6
+ not themselves have constancy validations
7
+
8
+ *1.0*
9
+
10
+ * First public release. Compatible with Rails v1.2.3 and v1.2.2
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright � 2007 Nils Jonsson (nils@alumni.rice.edu)
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,136 @@
1
+ == Validates Constancy for Ruby on Rails (Active Record)
2
+
3
+ http://constancy.rubyforge.org
4
+
5
+ Compatible with Rails v1.2.2 through v2.0.2 (Active Record v1.15.2 through
6
+ v2.0.2)
7
+
8
+
9
+ === Introduction
10
+
11
+ This RubyGem and Rails plugin adds a +validates_constancy_of+ validation to
12
+ Active Record. It allows you to prevent particular database fields from being
13
+ changed after a record is created. A validation error occurs on updates if an
14
+ attribute of a model object is different from its value in the database.
15
+
16
+
17
+ === Installing Validates Constancy
18
+
19
+ The code is packaged as both a RubyGem and a Rails plugin. You can use either
20
+ one, depending on what your needs are.
21
+
22
+ <b>The Validates Constancy gem</b> is compatible with various versions of Rails
23
+ (Active Record) -- see the _test_ subdirectories of
24
+ http://constancy.rubyforge.org/svn/gem/branches. You can install the gem with
25
+ the command:
26
+
27
+ gem install validates_constancy
28
+
29
+ <b>The Validates Constancy plugin</b> is compatible with the latest released
30
+ version of the Rails framework (and possibly also other versions -- see
31
+ http://constancy.rubyforge.org/svn/plugin/test). You can install the plugin with
32
+ the command:
33
+
34
+ ruby script/plugin install http://constancy.rubyforge.org/svn/plugin/validates_constancy
35
+
36
+ Use the gem (http://constancy.rubyforge.org/svn/gem) if you're using Active
37
+ Record apart from Rails, or for compatibility with a version of Rails (Active
38
+ Record) that is not supported by the plugin. Use the plugin if your Rails
39
+ version is up to date and if you like the convenience of a Rails plugin.
40
+
41
+
42
+ === Using constancy validation
43
+
44
+ Here's how to use this validation in your code.
45
+
46
+ class Person < ActiveRecord::Base
47
+
48
+ # Prevent changes to Person#social_security_number.
49
+ validates_constancy_of :social_security_number
50
+
51
+ end
52
+
53
+
54
+ ==== Options
55
+
56
+ The validation takes two options, <tt>:if</tt> and <tt>:message</tt>. These may
57
+ be familiar because several of Active Record's validations also use them. The
58
+ <tt>:if</tt> option takes a Proc, or a symbol, or string with a model object
59
+ argument and a return value of +true+ or +false+.
60
+
61
+ class Comment < ActiveRecord::Base
62
+
63
+ # Prevent changes to Comment#text if it is "locked."
64
+ validates_constancy_of :text, :if => Proc.new { |comment| comment.locked? }
65
+
66
+ end
67
+
68
+ The default error message is "can't be changed". Use your own error message by
69
+ specifying the <tt>:message</tt> option.
70
+
71
+ class LicensePlate < ActiveRecord::Base
72
+
73
+ # Prevent changes to LicensePlate#number.
74
+ validates_constancy_of :number,
75
+ :message => 'is off-limits! What are you thinking?'
76
+
77
+ end
78
+
79
+ More than one model attribute can be specified. Any specified options will be
80
+ applied to all the specified attributes.
81
+
82
+
83
+ ==== Warning
84
+
85
+ With associations, validate the constancy of a foreign key, not the instance
86
+ variable itself: <tt>validates_constancy_of :invoice_id</tt> instead of
87
+ <tt>validates_constancy_of :invoice</tt>.
88
+
89
+ Also note the warning under <em>Inheritable callback queues</em> in
90
+ http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html. "In order for
91
+ inheritance to work for the callback queues, you must specify the callbacks
92
+ before specifying the associations. Otherwise, you might trigger the loading of
93
+ a child before the parent has registered the callbacks and they won't be
94
+ inherited." Validates Constancy uses these callback queues, so you'll want to
95
+ specify associations *after* +validates_constancy_of+ statements in your model
96
+ classes.
97
+
98
+
99
+ === Running automated tests for Validates Constancy
100
+
101
+ There's a suite of tests that exercises all the functionality of Validates
102
+ Constancy. You can check out a version of the test suite from the repository
103
+ according to the version of Rails (Active Record) it works with.
104
+
105
+ ==== Gem
106
+
107
+ [<b>Rails v1.2.2 (Active Record v1.15.2)</b>] Check out http://constancy.rubyforge.org/svn/gem/branches/branch_1-x/test/rails_1-2-2
108
+ [<b>Rails v1.2.3 (Active Record v1.15.3)</b>] Check out http://constancy.rubyforge.org/svn/gem/branches/branch_1-x/test/rails_1-2-3
109
+ [<b>Rails v1.2.4 (Active Record v1.15.4)</b>] Check out http://constancy.rubyforge.org/svn/gem/branches/branch_1-x/test/rails_1-2-4
110
+ [<b>Rails v1.2.5 (Active Record v1.15.5)</b>] Check out http://constancy.rubyforge.org/svn/gem/branches/branch_1-x/test/rails_1-2-5
111
+ [<b>Rails v1.2.6 (Active Record v1.15.6)</b>] Check out http://constancy.rubyforge.org/svn/gem/branches/branch_1-x/test/rails_1-2-6
112
+ [<b>Rails v2.0.0 (Active Record v2.0.0)</b>] Check out http://constancy.rubyforge.org/svn/gem/branches/branch_1-x/test/rails_2-0-0
113
+ [<b>Rails v2.0.1 (Active Record v2.0.1)</b>] Check out http://constancy.rubyforge.org/svn/gem/branches/branch_1-x/test/rails_2-0-1
114
+ [<b>Rails v2.0.2 (Active Record v2.0.2)</b>] Check out http://constancy.rubyforge.org/svn/gem/branches/branch_1-x/test/rails_2-0-2
115
+ [<b>Edge Rails</b>] Check out http://constancy.rubyforge.org/svn/gem/branches/branch_1-x/test/rails_edge
116
+
117
+ ==== Plugin
118
+
119
+ [<b>Rails v1.2.2 (Active Record v1.15.2)</b>] Check out http://constancy.rubyforge.org/svn/plugin/test/rails_1-2-2
120
+ [<b>Rails v1.2.3 (Active Record v1.15.3)</b>] Check out http://constancy.rubyforge.org/svn/plugin/test/rails_1-2-3
121
+ [<b>Rails v1.2.4 (Active Record v1.15.4)</b>] Check out http://constancy.rubyforge.org/svn/plugin/test/rails_1-2-4
122
+ [<b>Rails v1.2.5 (Active Record v1.15.5)</b>] Check out http://constancy.rubyforge.org/svn/plugin/test/rails_1-2-5
123
+ [<b>Rails v1.2.6 (Active Record v1.15.6)</b>] Check out http://constancy.rubyforge.org/svn/plugin/test/rails_1-2-6
124
+ [<b>Rails v2.0.0 (Active Record v2.0.0)</b>] Check out http://constancy.rubyforge.org/svn/plugin/test/rails_2-0-0
125
+ [<b>Rails v2.0.1 (Active Record v2.0.1)</b>] Check out http://constancy.rubyforge.org/svn/plugin/test/rails_2-0-1
126
+ [<b>Rails v2.0.2 (Active Record v2.0.2)</b>] Check out http://constancy.rubyforge.org/svn/plugin/test/rails_2-0-2
127
+ [<b>Edge Rails</b>] Check out http://constancy.rubyforge.org/svn/plugin/test/rails_edge
128
+
129
+ Then read rails_*/doc/README_FOR_APP for instructions on how to run the tests.
130
+
131
+
132
+ === Credits
133
+
134
+ Copyright � 2007-2008 Nils Jonsson (mailto:nils@alumni.rice.edu)
135
+
136
+ Released under the MIT license.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'rake'
2
+ require 'rake/rdoctask'
3
+
4
+ desc 'Generate documentation for the Validates Constancy plugin.'
5
+ Rake::RDocTask.new(:rdoc) do |rdoc|
6
+ rdoc.rdoc_dir = 'rdoc'
7
+ rdoc.title = 'Validates Constancy'
8
+ rdoc.rdoc_files.include('README')
9
+ rdoc.rdoc_files.include('MIT-LICENSE')
10
+ rdoc.rdoc_files.include('lib/**/*.rb')
11
+ rdoc.options << '--line-numbers' << '--inline-source'
12
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), 'lib', 'validates_constancy')
@@ -0,0 +1,9 @@
1
+ # Includes ConstancyValidation in ActiveRecord::Base.
2
+
3
+ Dir.glob(File.join(File.dirname(__FILE__),
4
+ 'validates_constancy',
5
+ '*.rb')) do |filename|
6
+ require filename
7
+ end
8
+
9
+ ActiveRecord::Base.class_eval { include ConstancyValidation }
@@ -0,0 +1,126 @@
1
+ # Defines ConstancyValidation.
2
+
3
+ require 'active_record'
4
+
5
+ # When this module is included in ActiveRecord::Base, the validation method in
6
+ # ConstancyValidation::ClassMethods becomes available to all Active Record
7
+ # models.
8
+ module ConstancyValidation
9
+
10
+ ActiveRecord::Errors.default_error_messages[:constancy] = "can't be changed"
11
+
12
+ # The following validation is defined in the class scope of the model that
13
+ # you're interested in validating. It offers a declarative way of specifying
14
+ # when the model is valid and when it is not.
15
+ module ClassMethods
16
+
17
+ # Encapsulates the pattern of wanting to protect one or more model
18
+ # attributes from being changed after the model object is created. Example:
19
+ #
20
+ # class Person < ActiveRecord::Base
21
+ #
22
+ # # Prevent changes to Person#user_name and Person#member_since.
23
+ # validates_constancy_of :user_name, :member_since
24
+ #
25
+ # end
26
+ #
27
+ # This check is performed only on update.
28
+ #
29
+ # Configuration options:
30
+ #
31
+ # [<tt>:message</tt>] A custom error message (default is: "can't be changed")
32
+ # [<tt>:if</tt>] Specifies a method, Proc or string to call to determine if the validation should occur (e.g., <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The method, Proc or string should return or evaluate to +true+ or +false+.
33
+ #
34
+ # Warning: With associations, validate the constancy of a foreign key, not
35
+ # the instance variable itself: <tt>validates_constancy_of :invoice_id</tt>
36
+ # instead of <tt>validates_constancy_of :invoice</tt>.
37
+ #
38
+ # Also note the warning under <em>Inheritable callback queues</em> in
39
+ # http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html. "In order
40
+ # for inheritance to work for the callback queues, you must specify the
41
+ # callbacks before specifying the associations. Otherwise, you might trigger
42
+ # the loading of a child before the parent has registered the callbacks and
43
+ # they won't be inherited." Validates Constancy uses these callback queues,
44
+ # so you'll want to specify associations *after* +validates_constancy_of+
45
+ # statements in your model classes.
46
+ def validates_constancy_of(*attribute_names)
47
+ options = {:message =>
48
+ ActiveRecord::Errors.default_error_messages[:constancy]}
49
+ options.merge!(attribute_names.pop) if attribute_names.last.kind_of?(Hash)
50
+
51
+ constant_names = base_class.instance_variable_get(:@constant_attribute_names) ||
52
+ []
53
+ constant_names.concat attribute_names.collect!(&:to_s)
54
+ base_class.instance_variable_set :@constant_attribute_names,
55
+ constant_names
56
+
57
+ ConstancyValidation::OriginalAttributesCapture.extend self
58
+
59
+ options.merge! :on => :update
60
+ validates_each(attribute_names, options) do |record, attribute_name, value|
61
+ unless value ==
62
+ record.instance_variable_get(:@original_attributes)[attribute_name]
63
+ record.errors.add attribute_name, options[:message]
64
+ end
65
+ end
66
+
67
+ self
68
+ end
69
+
70
+ end
71
+
72
+ class OriginalAttributesCapture #:nodoc:
73
+
74
+ class << self
75
+
76
+ def extend(klass)
77
+ return false if klass.method_defined?(:capture_original_attributes)
78
+
79
+ create_method_capture_original_attributes klass
80
+
81
+ create_method_after_find_unless_exists klass
82
+ klass.after_find :capture_original_attributes
83
+ klass.after_save :capture_original_attributes
84
+
85
+ true
86
+ end
87
+
88
+ private
89
+
90
+ def create_method(klass, method_name, &block)
91
+ klass.class_eval { define_method method_name, &block }
92
+ end
93
+
94
+ def create_method_after_find_unless_exists(klass)
95
+ # Active Record does not define Base#after_find -- it gets called
96
+ # dynamically if present. So we need to define a do-nothing method to
97
+ # serve as the head of the method chain.
98
+ return false if klass.method_defined?(:after_find)
99
+
100
+ create_method(klass, :after_find) { self }
101
+
102
+ true
103
+ end
104
+
105
+ def create_method_capture_original_attributes(klass)
106
+ create_method(klass, :capture_original_attributes) do
107
+ constant_names = self.class.base_class.instance_variable_get(:@constant_attribute_names) ||
108
+ []
109
+ originals = constant_names.inject({}) do |result, attribute_name|
110
+ result[attribute_name] = read_attribute(attribute_name)
111
+ result
112
+ end
113
+ instance_variable_set :@original_attributes, originals
114
+ self
115
+ end
116
+ end
117
+
118
+ end
119
+
120
+ end
121
+
122
+ def self.included(other_module) #:nodoc:
123
+ other_module.extend ClassMethods
124
+ end
125
+
126
+ end
data/test/README ADDED
@@ -0,0 +1,6 @@
1
+ There are no unit tests for the Validates Constancy plugin. Instead, there are
2
+ suites of integration tests in the form of Active Record models that use the
3
+ plugin while running against different versions of Active Record.
4
+
5
+ See the doc/README_FOR_APP files that are included in the various Rails
6
+ applications in http://constancy.rubyforge.org/svn/plugin/test.
@@ -0,0 +1,25 @@
1
+ spec = Gem::Specification.new do |s|
2
+ s.name = 'validates_constancy'
3
+ s.version = '1.0.1'
4
+ s.author = 'Nils Jonsson'
5
+ s.email = 'nils@alumni.rice.edu'
6
+ s.homepage = 'http://constancy.rubyforge.org/'
7
+ s.summary = "[Rails] Adds a 'validates_constancy_of' validation to " +
8
+ 'Active Record. It allows you to protect model attributes ' +
9
+ 'from being changed after a record is created.'
10
+ s.description = 'This RubyGem allows you to prevent particular database ' +
11
+ 'fields from being changed after an Active Record object ' +
12
+ 'is created. A validation error occurs on updates if an ' +
13
+ 'attribute of a model object is different from its value ' +
14
+ 'in the database.'
15
+
16
+ s.files = %w(CHANGELOG Rakefile MIT-LICENSE init.rb README validates_constancy.gemspec lib/validates_constancy/constancy_validation.rb lib/validates_constancy.rb test/README)
17
+
18
+ s.add_dependency 'activerecord'
19
+ s.autorequire = 'validates_constancy'
20
+ s.has_rdoc = true
21
+ s.extra_rdoc_files = %w(MIT-LICENSE README)
22
+ s.rubyforge_project = 'constancy'
23
+ s.rdoc_options << '--title' << 'Validates Constancy for Active Record' <<
24
+ '--main' << 'README'
25
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: markcatley-validates_constancy
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nils Jonsson
8
+ autorequire: validates_constancy
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-06-05 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activerecord
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ version:
24
+ description: This RubyGem allows you to prevent particular database fields from being changed after an Active Record object is created. A validation error occurs on updates if an attribute of a model object is different from its value in the database.
25
+ email: nils@alumni.rice.edu
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - MIT-LICENSE
32
+ - README
33
+ files:
34
+ - CHANGELOG
35
+ - Rakefile
36
+ - MIT-LICENSE
37
+ - init.rb
38
+ - README
39
+ - validates_constancy.gemspec
40
+ - lib/validates_constancy/constancy_validation.rb
41
+ - lib/validates_constancy.rb
42
+ - test/README
43
+ has_rdoc: true
44
+ homepage: http://constancy.rubyforge.org/
45
+ post_install_message:
46
+ rdoc_options:
47
+ - --title
48
+ - Validates Constancy for Active Record
49
+ - --main
50
+ - README
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ requirements: []
66
+
67
+ rubyforge_project: constancy
68
+ rubygems_version: 1.0.1
69
+ signing_key:
70
+ specification_version: 2
71
+ summary: "[Rails] Adds a 'validates_constancy_of' validation to Active Record. It allows you to protect model attributes from being changed after a record is created."
72
+ test_files: []
73
+