markcatley-validates-constancy-rails-plugin 1.0.20090309 → 1.0.20090327
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/Rakefile +12 -0
- data/init.rb +1 -0
- data/lib/validates_constancy/constancy_validation.rb +126 -0
- data/lib/validates_constancy.rb +9 -0
- metadata +2 -2
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/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,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
|
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.20090327
|
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-03-21 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|