cs-activeform 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENCE +20 -0
- data/Rakefile +22 -0
- data/init.rb +1 -0
- data/lib/active_form.rb +67 -0
- data/test/basic_test.rb +263 -0
- data/test/test_helper.rb +20 -0
- metadata +67 -0
data/MIT-LICENCE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Christoph Schiessl
|
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,22 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the ActiveForm plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the ActiveForm plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'ActiveForm'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'active_form'
|
data/lib/active_form.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
class ActiveForm
|
2
|
+
def initialize(attributes = nil)
|
3
|
+
# Mass Assignment implementation
|
4
|
+
if attributes
|
5
|
+
attributes.each do |key, value|
|
6
|
+
self[key] = value
|
7
|
+
end
|
8
|
+
end
|
9
|
+
yield self if block_given?
|
10
|
+
end
|
11
|
+
|
12
|
+
def [](key)
|
13
|
+
instance_variable_get("@#{key}")
|
14
|
+
end
|
15
|
+
|
16
|
+
def []=(key, value)
|
17
|
+
instance_variable_set("@#{key}", value)
|
18
|
+
end
|
19
|
+
|
20
|
+
def method_missing(method_id, *params)
|
21
|
+
# Implement _before_type_cast accessors
|
22
|
+
if md = /_before_type_cast$/.match(method_id.to_s)
|
23
|
+
attr_name = md.pre_match
|
24
|
+
return self[attr_name] if self.respond_to?(attr_name)
|
25
|
+
end
|
26
|
+
super
|
27
|
+
end
|
28
|
+
|
29
|
+
def new_record?
|
30
|
+
true
|
31
|
+
end
|
32
|
+
|
33
|
+
def raise_not_implemented_error(*params)
|
34
|
+
self.class.raise_not_implemented_error(params)
|
35
|
+
end
|
36
|
+
|
37
|
+
alias save raise_not_implemented_error
|
38
|
+
alias save! raise_not_implemented_error
|
39
|
+
alias update_attribute raise_not_implemented_error
|
40
|
+
alias update_attributes raise_not_implemented_error
|
41
|
+
|
42
|
+
include ActiveRecord::Validations
|
43
|
+
|
44
|
+
alias save valid?
|
45
|
+
alias save! raise_not_implemented_error
|
46
|
+
alias update_attribute raise_not_implemented_error
|
47
|
+
alias update_attributes raise_not_implemented_error
|
48
|
+
|
49
|
+
class <<self
|
50
|
+
def human_attribute_name(attribute_key_name)
|
51
|
+
attribute_key_name.humanize
|
52
|
+
end
|
53
|
+
|
54
|
+
def raise_not_implemented_error(*params)
|
55
|
+
raise NotImplementedError
|
56
|
+
end
|
57
|
+
|
58
|
+
alias create raise_not_implemented_error
|
59
|
+
alias create! raise_not_implemented_error
|
60
|
+
alias validates_acceptance_of raise_not_implemented_error
|
61
|
+
alias validates_uniqueness_of raise_not_implemented_error
|
62
|
+
alias validates_associated raise_not_implemented_error
|
63
|
+
alias validates_on_create raise_not_implemented_error
|
64
|
+
alias validates_on_update raise_not_implemented_error
|
65
|
+
alias save_with_validation raise_not_implemented_error
|
66
|
+
end
|
67
|
+
end
|
data/test/basic_test.rb
ADDED
@@ -0,0 +1,263 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
|
2
|
+
|
3
|
+
class BasicTest < Test::Unit::TestCase
|
4
|
+
def test_class_loads
|
5
|
+
assert_nothing_raised { ActiveForm }
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_mass_assignments
|
9
|
+
self.class.class_eval %q{
|
10
|
+
class ContactTest < ActiveForm
|
11
|
+
attr_accessor :name, :phone, :email, :subject
|
12
|
+
validates_presence_of :name, :phone, :email, :subject
|
13
|
+
validates_length_of :name, :phone, :subject, :minimum => 3
|
14
|
+
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
|
15
|
+
end
|
16
|
+
}
|
17
|
+
|
18
|
+
params = {
|
19
|
+
:name => "Christoph",
|
20
|
+
:phone => "123123123",
|
21
|
+
:email => "c.schiessl@gmx.net",
|
22
|
+
:subject => "Test",
|
23
|
+
:not_defined => "Attribute without accessor!"
|
24
|
+
}
|
25
|
+
|
26
|
+
assert_nothing_raised do
|
27
|
+
ct = ContactTest.new(params)
|
28
|
+
assert_valid ct
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_save_and_create_methods
|
33
|
+
assert_nothing_raised do
|
34
|
+
self.class.class_eval %q{
|
35
|
+
class SaveAndCreateTest < ActiveForm
|
36
|
+
attr_accessor :name
|
37
|
+
validates_presence_of :name
|
38
|
+
end
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
sact = SaveAndCreateTest.new :name => "Christoph"
|
43
|
+
assert_nothing_raised(NotImplementedError) { assert sact.save }
|
44
|
+
assert_raise(NotImplementedError) { sact.save! }
|
45
|
+
assert_raise(NotImplementedError) { sact.update_attribute :name, "Chris" }
|
46
|
+
assert_raise(NotImplementedError) { sact.update_attributes :name => "Chris" }
|
47
|
+
assert_raise(NotImplementedError) { SaveAndCreateTest.create :name => "Christoph" }
|
48
|
+
assert_raise(NotImplementedError) { SaveAndCreateTest.create! :name => "Christoph" }
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_error_messages
|
52
|
+
assert_nothing_raised do
|
53
|
+
self.class.class_eval %q{
|
54
|
+
class ErrorMessagesTest < ActiveForm
|
55
|
+
attr_accessor :email
|
56
|
+
validates_presence_of :email, :message => 'is missing'
|
57
|
+
end
|
58
|
+
}
|
59
|
+
end
|
60
|
+
|
61
|
+
emt = ErrorMessagesTest.new
|
62
|
+
assert_invalid(emt)
|
63
|
+
assert_equal "is missing", emt.errors[:email]
|
64
|
+
assert_equal "Email is missing", emt.errors.full_messages.first
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_attributes
|
68
|
+
assert_nothing_raised do
|
69
|
+
self.class.class_eval %q{
|
70
|
+
class AttributesTest < ActiveForm
|
71
|
+
end
|
72
|
+
}
|
73
|
+
end
|
74
|
+
|
75
|
+
at = AttributesTest.new
|
76
|
+
assert_raise(NoMethodError) { at.not_defined = "test" }
|
77
|
+
assert_nothing_raised { at[:not_defined] = "test" }
|
78
|
+
assert_equal "test", at[:not_defined]
|
79
|
+
assert_equal at[:not_defined], at["not_defined"]
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_validates_confirmation_of
|
83
|
+
assert_nothing_raised do
|
84
|
+
self.class.class_eval %q{
|
85
|
+
class ConfirmationOfTest < ActiveForm
|
86
|
+
attr_accessor :email, :email_confirmation
|
87
|
+
validates_confirmation_of :email
|
88
|
+
end
|
89
|
+
}
|
90
|
+
end
|
91
|
+
|
92
|
+
cot = ConfirmationOfTest.new
|
93
|
+
assert_valid cot
|
94
|
+
cot.email_confirmation = cot.email = "someone@example.com"
|
95
|
+
assert_valid cot
|
96
|
+
cot.email_confirmation = "wrong@address.com"
|
97
|
+
assert_invalid cot, "Should be invalid now!"
|
98
|
+
assert_not_nil cot.errors.on(:email)
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_validates_acceptance_of
|
102
|
+
assert_raise(NotImplementedError) do
|
103
|
+
self.class.class_eval %q{
|
104
|
+
class AcceptanceOfTest < ActiveForm
|
105
|
+
attr_accessor :terms_of_service
|
106
|
+
validates_acceptance_of :terms_of_service
|
107
|
+
end
|
108
|
+
}
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def test_presence_of
|
113
|
+
assert_nothing_raised do
|
114
|
+
self.class.class_eval %q{
|
115
|
+
class PresenceOfTest < ActiveForm
|
116
|
+
attr_accessor :email
|
117
|
+
validates_presence_of :email
|
118
|
+
end
|
119
|
+
}
|
120
|
+
end
|
121
|
+
|
122
|
+
pot = PresenceOfTest.new
|
123
|
+
assert_invalid pot
|
124
|
+
pot.email = "someone@example.com"
|
125
|
+
assert_valid pot
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_validates_length_of
|
129
|
+
assert_nothing_raised do
|
130
|
+
self.class.class_eval %q{
|
131
|
+
class LengthOfTest < ActiveForm
|
132
|
+
attr_accessor :name
|
133
|
+
validates_length_of :name, :minimum => 3
|
134
|
+
end
|
135
|
+
}
|
136
|
+
end
|
137
|
+
|
138
|
+
lot = LengthOfTest.new
|
139
|
+
assert_invalid lot
|
140
|
+
lot.name = "Christoph"
|
141
|
+
assert_valid lot
|
142
|
+
end
|
143
|
+
|
144
|
+
def test_validates_uniqueness_of
|
145
|
+
assert_raise(NotImplementedError) do
|
146
|
+
self.class.class_eval %q{
|
147
|
+
class UniquenessTest < ActiveForm
|
148
|
+
attr_accessor :email
|
149
|
+
validates_uniqueness_of :email
|
150
|
+
end
|
151
|
+
}
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
def test_validates_format_of
|
156
|
+
assert_nothing_raised do
|
157
|
+
self.class.class_eval %q{
|
158
|
+
class FormatOfTest < ActiveForm
|
159
|
+
attr_accessor :email
|
160
|
+
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
|
161
|
+
end
|
162
|
+
}
|
163
|
+
end
|
164
|
+
|
165
|
+
fot = FormatOfTest.new
|
166
|
+
assert_invalid fot
|
167
|
+
fot.email = "abc"
|
168
|
+
assert_invalid fot
|
169
|
+
fot.email = "c.schiessl@gmx.net"
|
170
|
+
assert_valid fot
|
171
|
+
end
|
172
|
+
|
173
|
+
def test_validates_inclusion_of
|
174
|
+
assert_nothing_raised do
|
175
|
+
self.class.class_eval %q{
|
176
|
+
class InclusionOfTest < ActiveForm
|
177
|
+
attr_accessor :booltest, :texttest
|
178
|
+
validates_inclusion_of :booltest, :in => [true, false]
|
179
|
+
validates_inclusion_of :texttest, :in => %w{Joe Mike Jack}
|
180
|
+
end
|
181
|
+
}
|
182
|
+
end
|
183
|
+
|
184
|
+
iot = InclusionOfTest.new
|
185
|
+
assert_invalid iot
|
186
|
+
iot.booltest, iot.texttest = true, "Jack"
|
187
|
+
assert_valid iot
|
188
|
+
end
|
189
|
+
|
190
|
+
def test_validates_exclusion_of
|
191
|
+
assert_nothing_raised do
|
192
|
+
self.class.class_eval %q{
|
193
|
+
class ExclusionOfTest < ActiveForm
|
194
|
+
attr_accessor :name
|
195
|
+
validates_exclusion_of :name, :in => %w{Bill Gates}
|
196
|
+
end
|
197
|
+
}
|
198
|
+
end
|
199
|
+
|
200
|
+
eot = ExclusionOfTest.new
|
201
|
+
assert_valid eot
|
202
|
+
eot.name = "Bill"
|
203
|
+
assert_invalid eot
|
204
|
+
eot.name = "Christoph"
|
205
|
+
assert_valid eot
|
206
|
+
end
|
207
|
+
|
208
|
+
def test_validates_associated
|
209
|
+
assert_raise(NotImplementedError) do
|
210
|
+
self.class.class_eval %q{
|
211
|
+
class AssociatedTest < ActiveForm
|
212
|
+
attr_accessor :test
|
213
|
+
validates_associated :test
|
214
|
+
end
|
215
|
+
}
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
def test_validates_numericality_of
|
220
|
+
assert_nothing_raised do
|
221
|
+
self.class.class_eval %q{
|
222
|
+
class NumericalityTest < ActiveForm
|
223
|
+
attr_accessor :width, :height
|
224
|
+
validates_numericality_of :width, :height
|
225
|
+
end
|
226
|
+
}
|
227
|
+
end
|
228
|
+
|
229
|
+
nt = NumericalityTest.new
|
230
|
+
assert_invalid nt
|
231
|
+
nt.width, nt.height = 123, "123"
|
232
|
+
assert_valid nt
|
233
|
+
nt.width = "123sdf"
|
234
|
+
assert_invalid nt
|
235
|
+
assert_not_nil nt.errors[:width]
|
236
|
+
end
|
237
|
+
|
238
|
+
def test_validates_on_create
|
239
|
+
assert_raise(NotImplementedError) do
|
240
|
+
self.class.class_eval %q{
|
241
|
+
class OnCreateTest < ActiveForm
|
242
|
+
attr_accessor :name
|
243
|
+
validates_on_create do
|
244
|
+
# do something
|
245
|
+
end
|
246
|
+
end
|
247
|
+
}
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
def test_validates_on_update
|
252
|
+
assert_raise(NotImplementedError) do
|
253
|
+
self.class.class_eval %q{
|
254
|
+
class OnUpdateTest < ActiveForm
|
255
|
+
attr_accessor :name
|
256
|
+
validates_on_update do
|
257
|
+
# do something
|
258
|
+
end
|
259
|
+
end
|
260
|
+
}
|
261
|
+
end
|
262
|
+
end
|
263
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
unless ENV['RAILS_VERSION'] then gem 'activerecord', '>= 2.1.0'
|
5
|
+
else gem 'activerecord', ENV['RAILS_VERSION'] end
|
6
|
+
|
7
|
+
require 'active_record'
|
8
|
+
|
9
|
+
plugin_root = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
10
|
+
require File.join(plugin_root, 'lib/active_form')
|
11
|
+
|
12
|
+
class Test::Unit::TestCase
|
13
|
+
def assert_valid(model, message = nil)
|
14
|
+
assert model.valid?, message
|
15
|
+
end
|
16
|
+
|
17
|
+
def assert_invalid(model, message = nil)
|
18
|
+
assert !model.valid?, message
|
19
|
+
end
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cs-activeform
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Christoph Schiessl
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-09-08 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activerecord
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 2.1.0
|
23
|
+
version:
|
24
|
+
description:
|
25
|
+
email: c.schiessl@gmx.net
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files: []
|
31
|
+
|
32
|
+
files:
|
33
|
+
- init.rb
|
34
|
+
- lib
|
35
|
+
- lib/active_form.rb
|
36
|
+
- MIT-LICENCE
|
37
|
+
- Rakefile
|
38
|
+
- test
|
39
|
+
has_rdoc: false
|
40
|
+
homepage: http://github.com/cs/activeform
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.2.0
|
62
|
+
signing_key:
|
63
|
+
specification_version: 2
|
64
|
+
summary: Validations for Non Active Record Models
|
65
|
+
test_files:
|
66
|
+
- test/test_helper.rb
|
67
|
+
- test/basic_test.rb
|