korobkov-validates-constancy-rails-plugin 1.0.20090408
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +10 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +100 -0
- data/Rakefile +12 -0
- data/init.rb +1 -0
- data/lib/validates_constancy.rb +15 -0
- data/lib/validates_constancy/constancy_validation.rb +125 -0
- data/test/README.rdoc +7 -0
- metadata +74 -0
data/CHANGELOG
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright © 2007-2009 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.rdoc
ADDED
@@ -0,0 +1,100 @@
|
|
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.4 (Active Record v1.15.2 through
|
6
|
+
v2.0.4)
|
7
|
+
|
8
|
+
|
9
|
+
=== Introduction
|
10
|
+
|
11
|
+
This Rails plugin adds a +validates_constancy_of+ validation to Active Record.
|
12
|
+
It allows you to prevent particular database fields from being changed after a
|
13
|
+
record is created. A validation error occurs on updates if an attribute of a
|
14
|
+
model object is different from its value in the database.
|
15
|
+
|
16
|
+
|
17
|
+
=== Installing Validates Constancy
|
18
|
+
|
19
|
+
The code is packaged as a Rails plugin. It is compatible with the latest
|
20
|
+
released version of the Rails framework (and possibly also other versions -- see
|
21
|
+
the 'test' branch of this repository). You can install the plugin with the
|
22
|
+
command:
|
23
|
+
|
24
|
+
ruby script/plugin install git://github.com/njonsson/validates-constancy-rails-plugin.git
|
25
|
+
|
26
|
+
|
27
|
+
=== Using constancy validation
|
28
|
+
|
29
|
+
Here's how to use this validation in your code.
|
30
|
+
|
31
|
+
class Person < ActiveRecord::Base
|
32
|
+
|
33
|
+
# Prevent changes to Person#social_security_number.
|
34
|
+
validates_constancy_of :social_security_number
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
==== Options
|
40
|
+
|
41
|
+
The validation takes two options, <tt>:if</tt> and <tt>:message</tt>. These may
|
42
|
+
be familiar because several of Active Record's validations also use them. The
|
43
|
+
<tt>:if</tt> option takes a Proc, or a symbol, or string with a model object
|
44
|
+
argument and a return value of +true+ or +false+.
|
45
|
+
|
46
|
+
class Comment < ActiveRecord::Base
|
47
|
+
|
48
|
+
# Prevent changes to Comment#text if it is "locked."
|
49
|
+
validates_constancy_of :text, :if => Proc.new { |comment| comment.locked? }
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
The default error message is "can't be changed". Use your own error message by
|
54
|
+
specifying the <tt>:message</tt> option.
|
55
|
+
|
56
|
+
class LicensePlate < ActiveRecord::Base
|
57
|
+
|
58
|
+
# Prevent changes to LicensePlate#number.
|
59
|
+
validates_constancy_of :number,
|
60
|
+
:message => 'is off-limits! What are you thinking?'
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
More than one model attribute can be specified. Any specified options will be
|
65
|
+
applied to all the specified attributes.
|
66
|
+
|
67
|
+
|
68
|
+
==== Warning
|
69
|
+
|
70
|
+
With associations, validate the constancy of a foreign key, not the instance
|
71
|
+
variable itself: <tt>validates_constancy_of :invoice_id</tt> instead of
|
72
|
+
<tt>validates_constancy_of :invoice</tt>.
|
73
|
+
|
74
|
+
Also note the warning under <em>Inheritable callback queues</em> in
|
75
|
+
http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html. "In order for
|
76
|
+
inheritance to work for the callback queues, you must specify the callbacks
|
77
|
+
before specifying the associations. Otherwise, you might trigger the loading of
|
78
|
+
a child before the parent has registered the callbacks and they won't be
|
79
|
+
inherited." Validates Constancy uses these callback queues, so you'll want to
|
80
|
+
specify associations *after* +validates_constancy_of+ statements in your model
|
81
|
+
classes.
|
82
|
+
|
83
|
+
|
84
|
+
=== Running automated tests for Validates Constancy
|
85
|
+
|
86
|
+
There's a suite of tests that exercises all the functionality of Validates
|
87
|
+
Constancy. You can check out a version of the test suite from the repository
|
88
|
+
according to the version of Rails (Active Record) it works with.
|
89
|
+
|
90
|
+
git checkout test
|
91
|
+
git submodule update --init
|
92
|
+
|
93
|
+
Then read rails_*/doc/README_FOR_APP for instructions on how to run the tests.
|
94
|
+
|
95
|
+
|
96
|
+
=== Credits
|
97
|
+
|
98
|
+
Copyright © 2007-2009 Nils Jonsson (mailto:nils@alumni.rice.edu)
|
99
|
+
|
100
|
+
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.rdoc')
|
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,15 @@
|
|
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 }
|
10
|
+
|
11
|
+
if Object.const_defined?(:I18n)
|
12
|
+
I18n.load_path << File.join(File.dirname(__FILE__), '..', 'config', 'locales', 'en.yml')
|
13
|
+
else
|
14
|
+
ActiveRecord::Errors.default_error_messages[:constancy] = "can't be changed"
|
15
|
+
end
|
@@ -0,0 +1,125 @@
|
|
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
|
+
# The following validation is defined in the class scope of the model that
|
11
|
+
# you're interested in validating. It offers a declarative way of specifying
|
12
|
+
# when the model is valid and when it is not.
|
13
|
+
module ClassMethods
|
14
|
+
|
15
|
+
# Encapsulates the pattern of wanting to protect one or more model
|
16
|
+
# attributes from being changed after the model object is created. Example:
|
17
|
+
#
|
18
|
+
# class Person < ActiveRecord::Base
|
19
|
+
#
|
20
|
+
# # Prevent changes to Person#user_name and Person#member_since.
|
21
|
+
# validates_constancy_of :user_name, :member_since
|
22
|
+
#
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# This check is performed only on update.
|
26
|
+
#
|
27
|
+
# Configuration options:
|
28
|
+
#
|
29
|
+
# [<tt>:message</tt>] A custom error message (default is: "can't be changed")
|
30
|
+
# [<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+.
|
31
|
+
#
|
32
|
+
# Warning: With associations, validate the constancy of a foreign key, not
|
33
|
+
# the instance variable itself: <tt>validates_constancy_of :invoice_id</tt>
|
34
|
+
# instead of <tt>validates_constancy_of :invoice</tt>.
|
35
|
+
#
|
36
|
+
# Also note the warning under <em>Inheritable callback queues</em> in
|
37
|
+
# http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html. "In order
|
38
|
+
# for inheritance to work for the callback queues, you must specify the
|
39
|
+
# callbacks before specifying the associations. Otherwise, you might trigger
|
40
|
+
# the loading of a child before the parent has registered the callbacks and
|
41
|
+
# they won't be inherited." Validates Constancy uses these callback queues,
|
42
|
+
# so you'll want to specify associations *after* +validates_constancy_of+
|
43
|
+
# statements in your model classes.
|
44
|
+
def validates_constancy_of(*attribute_names)
|
45
|
+
options = {:message => Object.const_defined?(:I18n) ?
|
46
|
+
I18n.translate('activerecord.errors.messages.constancy') :
|
47
|
+
ActiveRecord::Errors.default_error_messages[:constancy]}
|
48
|
+
options.merge!(attribute_names.pop) if attribute_names.last.kind_of?(Hash)
|
49
|
+
|
50
|
+
constant_names = base_class.instance_variable_get(:@constant_attribute_names) ||
|
51
|
+
[]
|
52
|
+
constant_names.concat attribute_names.collect!(&:to_s)
|
53
|
+
base_class.instance_variable_set :@constant_attribute_names,
|
54
|
+
constant_names
|
55
|
+
|
56
|
+
ConstancyValidation::OriginalAttributesCapture.extend self
|
57
|
+
|
58
|
+
options.merge! :on => :update
|
59
|
+
validates_each(attribute_names, options) do |record, attribute_name, value|
|
60
|
+
unless value ==
|
61
|
+
record.instance_variable_get(:@original_attributes)[attribute_name]
|
62
|
+
record.errors.add attribute_name, options[:message]
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
self
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
class OriginalAttributesCapture #:nodoc:
|
72
|
+
|
73
|
+
class << self
|
74
|
+
|
75
|
+
def extend(klass)
|
76
|
+
return false if klass.method_defined?(:capture_original_attributes)
|
77
|
+
|
78
|
+
create_method_capture_original_attributes klass
|
79
|
+
|
80
|
+
create_method_after_find_unless_exists klass
|
81
|
+
klass.after_find :capture_original_attributes
|
82
|
+
klass.after_save :capture_original_attributes
|
83
|
+
|
84
|
+
true
|
85
|
+
end
|
86
|
+
|
87
|
+
private
|
88
|
+
|
89
|
+
def create_method(klass, method_name, &block)
|
90
|
+
klass.class_eval { define_method method_name, &block }
|
91
|
+
end
|
92
|
+
|
93
|
+
def create_method_after_find_unless_exists(klass)
|
94
|
+
# Active Record does not define Base#after_find -- it gets called
|
95
|
+
# dynamically if present. So we need to define a do-nothing method to
|
96
|
+
# serve as the head of the method chain.
|
97
|
+
return false if klass.method_defined?(:after_find)
|
98
|
+
|
99
|
+
create_method(klass, :after_find) { self }
|
100
|
+
|
101
|
+
true
|
102
|
+
end
|
103
|
+
|
104
|
+
def create_method_capture_original_attributes(klass)
|
105
|
+
create_method(klass, :capture_original_attributes) do
|
106
|
+
constant_names = self.class.base_class.instance_variable_get(:@constant_attribute_names) ||
|
107
|
+
[]
|
108
|
+
originals = constant_names.inject({}) do |result, attribute_name|
|
109
|
+
result[attribute_name] = read_attribute(attribute_name)
|
110
|
+
result
|
111
|
+
end
|
112
|
+
instance_variable_set :@original_attributes, originals
|
113
|
+
self
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
def self.included(other_module) #:nodoc:
|
122
|
+
other_module.extend ClassMethods
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
data/test/README.rdoc
ADDED
@@ -0,0 +1,7 @@
|
|
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. Those applications can be found at in the ‘test’ branch of this
|
7
|
+
repository.
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: korobkov-validates-constancy-rails-plugin
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.20090408
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nils Jonsson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-16 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activerecord
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
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.
|
26
|
+
email: nils@alumni.rice.edu
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- MIT-LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- CHANGELOG
|
36
|
+
- Rakefile
|
37
|
+
- MIT-LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- init.rb
|
40
|
+
- lib/validates_constancy.rb
|
41
|
+
- lib/validates_constancy/constancy_validation.rb
|
42
|
+
- test/README.rdoc
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: http://constancy.rubyforge.org/
|
45
|
+
licenses:
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options:
|
48
|
+
- --title
|
49
|
+
- Validates Constancy for Active Record
|
50
|
+
- --main
|
51
|
+
- README.rdoc
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
version:
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project: constancy
|
69
|
+
rubygems_version: 1.3.5
|
70
|
+
signing_key:
|
71
|
+
specification_version: 2
|
72
|
+
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."
|
73
|
+
test_files: []
|
74
|
+
|