markcatley-validates-constancy-rails-plugin 1.0.20090327 → 1.0.20090923
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/MIT-LICENSE +1 -1
- data/README.rdoc +100 -0
- data/config/locales/en.yml +5 -0
- data/config/locales/ru.yml +5 -0
- data/lib/validates_constancy/constancy_validation.rb +2 -3
- data/lib/validates_constancy.rb +6 -0
- data/test/README.rdoc +7 -0
- metadata +11 -8
data/MIT-LICENSE
CHANGED
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 (c) 2007-2009 Nils Jonsson (mailto:nils@alumni.rice.edu)
|
99
|
+
|
100
|
+
Released under the MIT license.
|
@@ -7,8 +7,6 @@ require 'active_record'
|
|
7
7
|
# models.
|
8
8
|
module ConstancyValidation
|
9
9
|
|
10
|
-
ActiveRecord::Errors.default_error_messages[:constancy] = "can't be changed"
|
11
|
-
|
12
10
|
# The following validation is defined in the class scope of the model that
|
13
11
|
# you're interested in validating. It offers a declarative way of specifying
|
14
12
|
# when the model is valid and when it is not.
|
@@ -44,7 +42,8 @@ module ConstancyValidation
|
|
44
42
|
# so you'll want to specify associations *after* +validates_constancy_of+
|
45
43
|
# statements in your model classes.
|
46
44
|
def validates_constancy_of(*attribute_names)
|
47
|
-
options = {:message =>
|
45
|
+
options = {:message => Object.const_defined?(:I18n) ?
|
46
|
+
I18n.translate('activerecord.errors.messages.constancy') :
|
48
47
|
ActiveRecord::Errors.default_error_messages[:constancy]}
|
49
48
|
options.merge!(attribute_names.pop) if attribute_names.last.kind_of?(Hash)
|
50
49
|
|
data/lib/validates_constancy.rb
CHANGED
@@ -7,3 +7,9 @@ Dir.glob(File.join(File.dirname(__FILE__),
|
|
7
7
|
end
|
8
8
|
|
9
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
|
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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: markcatley-validates-constancy-rails-plugin
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.20090923
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nils Jonsson
|
@@ -9,7 +9,7 @@ autorequire: validates_constancy
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-05-16 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -30,24 +30,27 @@ extensions: []
|
|
30
30
|
|
31
31
|
extra_rdoc_files:
|
32
32
|
- MIT-LICENSE
|
33
|
-
- README
|
33
|
+
- README.rdoc
|
34
34
|
files:
|
35
35
|
- CHANGELOG
|
36
|
-
- Rakefile
|
37
36
|
- MIT-LICENSE
|
38
|
-
- README
|
37
|
+
- README.rdoc
|
38
|
+
- Rakefile
|
39
39
|
- init.rb
|
40
|
+
- config/locales/en.yml
|
41
|
+
- config/locales/ru.yml
|
40
42
|
- lib/validates_constancy.rb
|
41
43
|
- lib/validates_constancy/constancy_validation.rb
|
42
|
-
- test/README
|
44
|
+
- test/README.rdoc
|
43
45
|
has_rdoc: true
|
44
46
|
homepage: http://constancy.rubyforge.org/
|
47
|
+
licenses:
|
45
48
|
post_install_message:
|
46
49
|
rdoc_options:
|
47
50
|
- --title
|
48
51
|
- Validates Constancy for Active Record
|
49
52
|
- --main
|
50
|
-
- README
|
53
|
+
- README.rdoc
|
51
54
|
require_paths:
|
52
55
|
- lib
|
53
56
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -65,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
65
68
|
requirements: []
|
66
69
|
|
67
70
|
rubyforge_project: constancy
|
68
|
-
rubygems_version: 1.
|
71
|
+
rubygems_version: 1.3.5
|
69
72
|
signing_key:
|
70
73
|
specification_version: 2
|
71
74
|
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."
|