validates_constancy 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,6 @@
1
+ *SVN*
2
+
3
+
4
+ *1.0*
5
+
6
+ * 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,123 @@
1
+ == Validates Constancy for Active Record
2
+
3
+ http://constancy.rubyforge.org
4
+
5
+ Compatible with Rails v1.2.2 and v1.2.3 (Active Record v1.15.2 and v1.15.3)
6
+
7
+
8
+ === Introduction
9
+
10
+ This RubyGem and Rails plugin adds a +validates_constancy_of+ validation to
11
+ Active Record. It allows you to prevent particular database fields from being
12
+ changed after a record is created. A validation error occurs on updates if an
13
+ attribute of a model object is different from its value in the database.
14
+
15
+
16
+ === Installing Validates Constancy
17
+
18
+ The code is packaged as both a RubyGem and a Rails plugin. You can use either
19
+ one, depending on what your needs are.
20
+
21
+ <b>The Validates Constancy gem</b> is compatible with various versions of Rails
22
+ (Active Record) -- see the _test_ subdirectories of
23
+ http://constancy.rubyforge.org/svn/gem/branches. You can install the gem with
24
+ the command:
25
+
26
+ gem install validates_constancy
27
+
28
+ <b>The Validates Constancy plugin</b> is compatible with the latest released
29
+ version of the Rails framework (and possibly also other versions -- see
30
+ http://constancy.rubyforge.org/svn/plugin/test). You can install the plugin with
31
+ the command:
32
+
33
+ ruby script/plugin install http://constancy.rubyforge.org/svn/plugin/validates_constancy
34
+
35
+ Use the gem (http://constancy.rubyforge.org/svn/gem) if you're using Active
36
+ Record apart from Rails, or for compatibility with a version of Rails (Active
37
+ Record) that is not supported by the plugin. Use the plugin if your Rails
38
+ version is up to date and if you like the convenience of a Rails plugin.
39
+
40
+
41
+ === Using constancy validation
42
+
43
+ Here's how to use this validation in your code.
44
+
45
+ class Person < ActiveRecord::Base
46
+
47
+ # Prevent changes to Person#social_security_number.
48
+ validates_constancy_of :social_security_number
49
+
50
+ end
51
+
52
+
53
+ ==== Options
54
+
55
+ The validation takes two options, <tt>:if</tt> and <tt>:message</tt>. These may
56
+ be familiar because several of Active Record's validations also use them. The
57
+ <tt>:if</tt> option takes a Proc, or a symbol, or string with a model object
58
+ argument and a return value of +true+ or +false+.
59
+
60
+ class Comment < ActiveRecord::Base
61
+
62
+ # Prevent changes to Comment#text if it is "locked."
63
+ validates_constancy_of :text, :if => Proc.new { |comment| comment.locked? }
64
+
65
+ end
66
+
67
+ The default error message is "can't be changed". Use your own error message by
68
+ specifying the <tt>:message</tt> option.
69
+
70
+ class LicensePlate < ActiveRecord::Base
71
+
72
+ # Prevent changes to LicensePlate#number.
73
+ validates_constancy_of :number,
74
+ :message => 'is off-limits! What are you thinking?'
75
+
76
+ end
77
+
78
+ More than one model attribute can be specified. Any specified options will be
79
+ applied to all the specified attributes.
80
+
81
+
82
+ ==== Warning
83
+
84
+ With associations, validate the constancy of a foreign key, not the instance
85
+ variable itself: <tt>validates_constancy_of :invoice_id</tt> instead of
86
+ <tt>validates_constancy_of :invoice</tt>.
87
+
88
+ Also note the warning under <em>Inheritable callback queues</em> in
89
+ http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html. "In order for
90
+ inheritance to work for the callback queues, you must specify the callbacks
91
+ before specifying the associations. Otherwise, you might trigger the loading of
92
+ a child before the parent has registered the callbacks and they won't be
93
+ inherited." Validates Constancy uses these callback queues, so you'll want to
94
+ specify associations *after* +validates_constancy_of+ statements in your model
95
+ classes.
96
+
97
+
98
+ === Running automated tests for Validates Constancy
99
+
100
+ There's a suite of tests that exercises all the functionality of Validates
101
+ Constancy. You can check out a version of the test suite from the repository
102
+ according to the version of Rails (Active Record) it works with.
103
+
104
+ ==== Gem
105
+
106
+ [<b>Rails v1.2.2 (Active Record v1.15.2)</b>] Check out http://constancy.rubyforge.org/svn/gem/tags/rel_1-0-0/test/rails_1-2-2
107
+ [<b>Rails v1.2.3 (Active Record v1.15.3)</b>] Check out http://constancy.rubyforge.org/svn/gem/tags/rel_1-0-0/test/rails_1-2-3
108
+ [<b>Edge Rails</b>] Check out http://constancy.rubyforge.org/svn/gem/tags/rel_1-0-0/test/rails_edge
109
+
110
+ ==== Plugin
111
+
112
+ [<b>Rails v1.2.2 (Active Record v1.15.2)</b>] Check out http://constancy.rubyforge.org/svn/plugin/test/rails_1-2-2
113
+ [<b>Rails v1.2.3 (Active Record v1.15.3)</b>] Check out http://constancy.rubyforge.org/svn/plugin/test/rails_1-2-3
114
+ [<b>Edge Rails</b>] Check out http://constancy.rubyforge.org/svn/plugin/test/rails_edge
115
+
116
+ Then read rails_*/doc/README_FOR_APP for instructions on how to run the tests.
117
+
118
+
119
+ === Credits
120
+
121
+ Copyright � 2007 Nils Jonsson (mailto:nils@alumni.rice.edu)
122
+
123
+ 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 RubyGem.'
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
@@ -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,121 @@
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_attribute_names ||= []).concat attribute_names.collect!(&:to_s)
52
+
53
+ ConstancyValidation::OriginalAttributesCapture.extend self
54
+
55
+ options.merge! :on => :update
56
+ validates_each(attribute_names, options) do |record, attribute_name, value|
57
+ unless value ==
58
+ record.instance_variable_get(:@original_attributes)[attribute_name]
59
+ record.errors.add attribute_name, options[:message]
60
+ end
61
+ end
62
+
63
+ self
64
+ end
65
+
66
+ end
67
+
68
+ class OriginalAttributesCapture #:nodoc:
69
+
70
+ class << self
71
+
72
+ def extend(klass)
73
+ return false if klass.method_defined?(:capture_original_attributes)
74
+
75
+ create_method_capture_original_attributes klass
76
+
77
+ create_method_after_find_unless_exists klass
78
+ klass.after_find :capture_original_attributes
79
+ klass.after_save :capture_original_attributes
80
+
81
+ true
82
+ end
83
+
84
+ private
85
+
86
+ def create_method(klass, method_name, &block)
87
+ klass.class_eval { define_method method_name, &block }
88
+ end
89
+
90
+ def create_method_after_find_unless_exists(klass)
91
+ # Active Record does not define Base#after_find -- it gets called
92
+ # dynamically if present. So we need to define a do-nothing method to
93
+ # serve as the head of the method chain.
94
+ return false if klass.method_defined?(:after_find)
95
+
96
+ create_method(klass, :after_find) { self }
97
+
98
+ true
99
+ end
100
+
101
+ def create_method_capture_original_attributes(klass)
102
+ create_method(klass, :capture_original_attributes) do
103
+ constant_names = self.class.instance_variable_get(:@constant_attribute_names)
104
+ originals = constant_names.inject({}) do |result, attribute_name|
105
+ result[attribute_name] = read_attribute(attribute_name)
106
+ result
107
+ end
108
+ instance_variable_set :@original_attributes, originals
109
+ self
110
+ end
111
+ end
112
+
113
+ end
114
+
115
+ end
116
+
117
+ def self.included(other_module) #:nodoc:
118
+ other_module.extend ClassMethods
119
+ end
120
+
121
+ end
data/test/README ADDED
@@ -0,0 +1,6 @@
1
+ There are no unit tests for the Validates Constancy RubyGem. Instead, there are
2
+ suites of integration tests in the form of Active Record models that use the
3
+ gem 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/gem/tags/rel_1-0-0/test.
File without changes
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.2
3
+ specification_version: 1
4
+ name: validates_constancy
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.0
7
+ date: 2007-08-29 00:00:00 -05:00
8
+ 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."
9
+ require_paths:
10
+ - lib
11
+ email: nils@alumni.rice.edu
12
+ homepage: http://constancy.rubyforge.org/
13
+ rubyforge_project: constancy
14
+ 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.
15
+ autorequire: validates_constancy
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Nils Jonsson
31
+ files:
32
+ - CHANGELOG
33
+ - lib
34
+ - lib/validates_constancy
35
+ - lib/validates_constancy/constancy_validation.rb
36
+ - lib/validates_constancy.rb
37
+ - MIT-LICENSE
38
+ - Rakefile
39
+ - README
40
+ - test
41
+ - test/README
42
+ - validates_constancy-1.0.0.gem
43
+ test_files: []
44
+
45
+ rdoc_options: []
46
+
47
+ extra_rdoc_files:
48
+ - MIT-LICENSE
49
+ - README
50
+ executables: []
51
+
52
+ extensions: []
53
+
54
+ requirements:
55
+ - Active Record v1.15.2 through v1.15.x
56
+ dependencies:
57
+ - !ruby/object:Gem::Dependency
58
+ name: activerecord
59
+ version_requirement:
60
+ version_requirements: !ruby/object:Gem::Version::Requirement
61
+ requirements:
62
+ - - ~>
63
+ - !ruby/object:Gem::Version
64
+ version: 1.15.2
65
+ version: