markcatley-validates_constancy 1.0.20080714 → 1.0.20090308
Sign up to get free protection for your applications and to get access to all the features.
- metadata +3 -2
- data/CHANGELOG +0 -10
- data/MIT-LICENSE +0 -20
- data/README +0 -136
- data/Rakefile +0 -12
- data/init.rb +0 -1
- data/lib/validates_constancy.rb +0 -9
- data/lib/validates_constancy/constancy_validation.rb +0 -126
- data/test/README +0 -6
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: markcatley-validates_constancy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.20090308
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nils Jonsson
|
@@ -9,11 +9,12 @@ autorequire: validates_constancy
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2009-02-25 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activerecord
|
17
|
+
type: :runtime
|
17
18
|
version_requirement:
|
18
19
|
version_requirements: !ruby/object:Gem::Requirement
|
19
20
|
requirements:
|
data/CHANGELOG
DELETED
data/MIT-LICENSE
DELETED
@@ -1,20 +0,0 @@
|
|
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
DELETED
@@ -1,136 +0,0 @@
|
|
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
DELETED
@@ -1,12 +0,0 @@
|
|
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
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), 'lib', 'validates_constancy')
|
data/lib/validates_constancy.rb
DELETED
@@ -1,126 +0,0 @@
|
|
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
DELETED
@@ -1,6 +0,0 @@
|
|
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.
|