rails3-active_form 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
data/LICENCE ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2012 TnT Web Solutions (http://www.tnt-web-solutions.de/)
2
+
3
+ Original:
4
+ Copyright (c) 2011 Christoph Schiessl (http://github.com/cs)
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining
7
+ a copy of this software and associated documentation files (the
8
+ "Software"), to deal in the Software without restriction, including
9
+ without limitation the rights to use, copy, modify, merge, publish,
10
+ distribute, sublicense, and/or sell copies of the Software, and to
11
+ permit persons to whom the Software is furnished to do so, subject to
12
+ the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,58 @@
1
+ # active_form
2
+
3
+ This gem is for Rails 3.x.x only!
4
+
5
+ ## Model
6
+
7
+ class ContactForm < ActiveForm
8
+ attr_accessor :name, :institution, :email, :contact_number, :address, :purpose
9
+ validates_presence_of :name, :email, :address, :purpose
10
+ validates_length_of :email, :within => 6..100, :unless => Proc.new {|m| m.email.blank?}
11
+ end
12
+
13
+ ## Controller
14
+
15
+ def show
16
+ @contact_form = ContactForm.new
17
+ end
18
+
19
+ def send_contact_form
20
+ @contact_form = ContactForm.new params[:contact_form]
21
+ if @contact_form.valid?
22
+ #...
23
+ else
24
+ render :action => "show"
25
+ end
26
+ end
27
+
28
+ ## Form
29
+
30
+ <%= form_for @contact_form, :url => send_contact_form do |f| %>
31
+ <%= f.error_messages %>
32
+ <%= f.text_field :name %>
33
+ <%= f.text_field :institution %>
34
+ <%= f.text_field :email %>
35
+ <%= f.text_field :contact_number %>
36
+ <%= f.text_field :address %>
37
+ <%= f.select :purpose, ["", "First", "Second"] %>
38
+ <%= f.submit "Request" %>
39
+ <% end %>
40
+
41
+ ## Compatibility
42
+
43
+ * MRI Ruby 1.8.7
44
+ * MRI Ruby 1.9.2
45
+ * JRuby 1.6
46
+
47
+ ## How to contribute?
48
+
49
+ 1. Fork on [GitHub](http://github.com/cs/active_form).
50
+ 2. Make sure, that all specs are still passing (run `bundle install && bundle exec rake spec`).
51
+ 3. Send Pull Request.
52
+
53
+ ## Copyright
54
+
55
+ This gem was changed by TnT Web Solutions 2012 to support i18n translation for attributes.
56
+
57
+ Original:
58
+ Copyright ©2011 [Christoph Schiessl](http://github.com/cs). See LICENSE for details.
@@ -0,0 +1,12 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ desc 'Default: run unit tests.'
5
+ task :default => :test
6
+
7
+ desc 'Test the ActiveForm plugin.'
8
+ Rake::TestTask.new(:test) do |t|
9
+ t.libs << 'lib'
10
+ t.pattern = 'test/**/*_test.rb'
11
+ t.verbose = true
12
+ end
@@ -0,0 +1,37 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'rails3-active_form'
3
+ s.version = '2.0.0'
4
+ s.platform = Gem::Platform::RUBY
5
+ s.summary = 'Validations for Non Active Record Models.'
6
+ s.description = 'Rails >= 3.0.0 is required.'
7
+
8
+ s.required_ruby_version = '>= 1.8.7'
9
+ s.required_rubygems_version = ">= 1.3.6"
10
+
11
+ s.author = 'Torsten Braun'
12
+ s.email = 'support@tnt-web-solutions.de'
13
+ s.homepage = 'http://github.com/tntwebsolutions/active_form'
14
+
15
+ s.require_paths = ["lib"]
16
+
17
+ s.files = [
18
+ "README.md",
19
+ "LICENCE",
20
+ "Rakefile",
21
+ "Gemfile",
22
+ "lib/active_form.rb",
23
+ "active_form.gemspec",
24
+ "test/test_helper.rb",
25
+ "test/basic_test.rb"
26
+ ]
27
+
28
+ s.test_files = [
29
+ "test/test_helper.rb",
30
+ "test/basic_test.rb"
31
+ ]
32
+
33
+ s.add_dependency('bundler', '>= 1.0')
34
+ s.add_dependency('activemodel', '>= 3.0.0')
35
+ s.add_dependency('activesupport', '>= 3.0.0')
36
+ s.add_development_dependency('rake', '>= 0.9')
37
+ end
@@ -0,0 +1,77 @@
1
+ require 'active_support/inflector'
2
+ require 'active_support/core_ext/hash/except'
3
+ require 'active_model'
4
+
5
+ class ActiveForm
6
+ include ActiveModel::Validations
7
+ include ActiveModel::Conversion
8
+ extend ActiveModel::Naming
9
+
10
+ def to_model; self; end
11
+ def persisted?; false; end
12
+ def to_key; nil; end
13
+ def to_param; nil; end
14
+
15
+ def initialize(attributes = nil)
16
+ # Mass Assignment implementation
17
+ if attributes
18
+ attributes.each do |key, value|
19
+ self[key] = value
20
+ end
21
+ end
22
+ yield self if block_given?
23
+ end
24
+
25
+ def [](key)
26
+ instance_variable_get("@#{key}")
27
+ end
28
+
29
+ def []=(key, value)
30
+ instance_variable_set("@#{key}", value)
31
+ end
32
+
33
+ def method_missing(method_id, *params)
34
+ # Implement _before_type_cast accessors
35
+ if md = /_before_type_cast$/.match(method_id.to_s)
36
+ attr_name = md.pre_match
37
+ return self[attr_name] if self.respond_to?(attr_name)
38
+ end
39
+ super
40
+ end
41
+
42
+ def new_record?
43
+ true
44
+ end
45
+
46
+ def id
47
+ nil
48
+ end
49
+
50
+ def raise_not_implemented_error(*params)
51
+ self.class.raise_not_implemented_error(params)
52
+ end
53
+
54
+ alias save raise_not_implemented_error
55
+ alias save! raise_not_implemented_error
56
+ alias update_attribute raise_not_implemented_error
57
+ alias update_attributes raise_not_implemented_error
58
+ alias save valid?
59
+ alias save! raise_not_implemented_error
60
+ alias update_attribute raise_not_implemented_error
61
+ alias update_attributes raise_not_implemented_error
62
+
63
+ class <<self
64
+ def raise_not_implemented_error(*params)
65
+ raise NotImplementedError
66
+ end
67
+
68
+ alias create raise_not_implemented_error
69
+ alias create! raise_not_implemented_error
70
+ alias validates_acceptance_of raise_not_implemented_error
71
+ alias validates_uniqueness_of raise_not_implemented_error
72
+ alias validates_associated raise_not_implemented_error
73
+ alias validates_on_create raise_not_implemented_error
74
+ alias validates_on_update raise_not_implemented_error
75
+ alias save_with_validation raise_not_implemented_error
76
+ end
77
+ end
@@ -0,0 +1,272 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
2
+
3
+ class BasicTest < Test::Unit::TestCase
4
+ def model
5
+ return @model if @model
6
+ self.class.class_eval %q{
7
+ class TestForm < ActiveForm
8
+ end
9
+ }
10
+ @model = TestForm.new
11
+ end
12
+ include ActiveModel::Lint::Tests
13
+ def test_class_loads
14
+ assert_nothing_raised { ActiveForm }
15
+ end
16
+
17
+ def test_mass_assignments
18
+ self.class.class_eval %q{
19
+ class ContactTest < ActiveForm
20
+ attr_accessor :name, :phone, :email, :subject
21
+ validates_presence_of :name, :phone, :email, :subject
22
+ validates_length_of :name, :phone, :subject, :minimum => 3
23
+ validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
24
+ end
25
+ }
26
+
27
+ params = {
28
+ :name => "Christoph",
29
+ :phone => "123123123",
30
+ :email => "c.schiessl@gmx.net",
31
+ :subject => "Test",
32
+ :not_defined => "Attribute without accessor!"
33
+ }
34
+
35
+ assert_nothing_raised do
36
+ ct = ContactTest.new(params)
37
+ assert_valid ct
38
+ end
39
+ end
40
+
41
+ def test_save_and_create_methods
42
+ assert_nothing_raised do
43
+ self.class.class_eval %q{
44
+ class SaveAndCreateTest < ActiveForm
45
+ attr_accessor :name
46
+ validates_presence_of :name
47
+ end
48
+ }
49
+ end
50
+
51
+ sact = SaveAndCreateTest.new :name => "Christoph"
52
+ assert_nothing_raised(NotImplementedError) { assert sact.save }
53
+ assert_raise(NotImplementedError) { sact.save! }
54
+ assert_raise(NotImplementedError) { sact.update_attribute :name, "Chris" }
55
+ assert_raise(NotImplementedError) { sact.update_attributes :name => "Chris" }
56
+ assert_raise(NotImplementedError) { SaveAndCreateTest.create :name => "Christoph" }
57
+ assert_raise(NotImplementedError) { SaveAndCreateTest.create! :name => "Christoph" }
58
+ end
59
+
60
+ def test_error_messages
61
+ assert_nothing_raised do
62
+ self.class.class_eval %q{
63
+ class ErrorMessagesTest < ActiveForm
64
+ attr_accessor :email
65
+ validates_presence_of :email, :message => 'is missing'
66
+ end
67
+ }
68
+ end
69
+
70
+ emt = ErrorMessagesTest.new
71
+ assert_invalid(emt)
72
+ assert_equal "is missing", emt.errors[:email][0]
73
+ assert_equal "Email is missing", emt.errors.full_messages.first
74
+ end
75
+
76
+ def test_attributes
77
+ assert_nothing_raised do
78
+ self.class.class_eval %q{
79
+ class AttributesTest < ActiveForm
80
+ end
81
+ }
82
+ end
83
+
84
+ at = AttributesTest.new
85
+ assert_raise(NoMethodError) { at.not_defined = "test" }
86
+ assert_nothing_raised { at[:not_defined] = "test" }
87
+ assert_equal "test", at[:not_defined]
88
+ assert_equal at[:not_defined], at["not_defined"]
89
+ end
90
+
91
+ def test_validates_confirmation_of
92
+ assert_nothing_raised do
93
+ self.class.class_eval %q{
94
+ class ConfirmationOfTest < ActiveForm
95
+ attr_accessor :email, :email_confirmation
96
+ validates_confirmation_of :email
97
+ end
98
+ }
99
+ end
100
+
101
+ cot = ConfirmationOfTest.new
102
+ assert_valid cot
103
+ cot.email_confirmation = cot.email = "someone@example.com"
104
+ assert_valid cot
105
+ cot.email_confirmation = "wrong@address.com"
106
+ assert_invalid cot, "Should be invalid now!"
107
+ assert_not_nil cot.errors[:email]
108
+ end
109
+
110
+ def test_validates_acceptance_of
111
+ assert_raise(NotImplementedError) do
112
+ self.class.class_eval %q{
113
+ class AcceptanceOfTest < ActiveForm
114
+ attr_accessor :terms_of_service
115
+ validates_acceptance_of :terms_of_service
116
+ end
117
+ }
118
+ end
119
+ end
120
+
121
+ def test_presence_of
122
+ assert_nothing_raised do
123
+ self.class.class_eval %q{
124
+ class PresenceOfTest < ActiveForm
125
+ attr_accessor :email
126
+ validates_presence_of :email
127
+ end
128
+ }
129
+ end
130
+
131
+ pot = PresenceOfTest.new
132
+ assert_invalid pot
133
+ pot.email = "someone@example.com"
134
+ assert_valid pot
135
+ end
136
+
137
+ def test_validates_length_of
138
+ assert_nothing_raised do
139
+ self.class.class_eval %q{
140
+ class LengthOfTest < ActiveForm
141
+ attr_accessor :name
142
+ validates_length_of :name, :minimum => 3
143
+ end
144
+ }
145
+ end
146
+
147
+ lot = LengthOfTest.new
148
+ assert_invalid lot
149
+ lot.name = "Christoph"
150
+ assert_valid lot
151
+ end
152
+
153
+ def test_validates_uniqueness_of
154
+ assert_raise(NotImplementedError) do
155
+ self.class.class_eval %q{
156
+ class UniquenessTest < ActiveForm
157
+ attr_accessor :email
158
+ validates_uniqueness_of :email
159
+ end
160
+ }
161
+ end
162
+ end
163
+
164
+ def test_validates_format_of
165
+ assert_nothing_raised do
166
+ self.class.class_eval %q{
167
+ class FormatOfTest < ActiveForm
168
+ attr_accessor :email
169
+ validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
170
+ end
171
+ }
172
+ end
173
+
174
+ fot = FormatOfTest.new
175
+ assert_invalid fot
176
+ fot.email = "abc"
177
+ assert_invalid fot
178
+ fot.email = "c.schiessl@gmx.net"
179
+ assert_valid fot
180
+ end
181
+
182
+ def test_validates_inclusion_of
183
+ assert_nothing_raised do
184
+ self.class.class_eval %q{
185
+ class InclusionOfTest < ActiveForm
186
+ attr_accessor :booltest, :texttest
187
+ validates_inclusion_of :booltest, :in => [true, false]
188
+ validates_inclusion_of :texttest, :in => %w{Joe Mike Jack}
189
+ end
190
+ }
191
+ end
192
+
193
+ iot = InclusionOfTest.new
194
+ assert_invalid iot
195
+ iot.booltest, iot.texttest = true, "Jack"
196
+ assert_valid iot
197
+ end
198
+
199
+ def test_validates_exclusion_of
200
+ assert_nothing_raised do
201
+ self.class.class_eval %q{
202
+ class ExclusionOfTest < ActiveForm
203
+ attr_accessor :name
204
+ validates_exclusion_of :name, :in => %w{Bill Gates}
205
+ end
206
+ }
207
+ end
208
+
209
+ eot = ExclusionOfTest.new
210
+ assert_valid eot
211
+ eot.name = "Bill"
212
+ assert_invalid eot
213
+ eot.name = "Christoph"
214
+ assert_valid eot
215
+ end
216
+
217
+ def test_validates_associated
218
+ assert_raise(NotImplementedError) do
219
+ self.class.class_eval %q{
220
+ class AssociatedTest < ActiveForm
221
+ attr_accessor :test
222
+ validates_associated :test
223
+ end
224
+ }
225
+ end
226
+ end
227
+
228
+ def test_validates_numericality_of
229
+ assert_nothing_raised do
230
+ self.class.class_eval %q{
231
+ class NumericalityTest < ActiveForm
232
+ attr_accessor :width, :height
233
+ validates_numericality_of :width, :height
234
+ end
235
+ }
236
+ end
237
+
238
+ nt = NumericalityTest.new
239
+ assert_invalid nt
240
+ nt.width, nt.height = 123, "123"
241
+ assert_valid nt
242
+ nt.width = "123sdf"
243
+ assert_invalid nt
244
+ assert_not_nil nt.errors[:width]
245
+ end
246
+
247
+ def test_validates_on_create
248
+ assert_raise(NotImplementedError) do
249
+ self.class.class_eval %q{
250
+ class OnCreateTest < ActiveForm
251
+ attr_accessor :name
252
+ validates_on_create do
253
+ # do something
254
+ end
255
+ end
256
+ }
257
+ end
258
+ end
259
+
260
+ def test_validates_on_update
261
+ assert_raise(NotImplementedError) do
262
+ self.class.class_eval %q{
263
+ class OnUpdateTest < ActiveForm
264
+ attr_accessor :name
265
+ validates_on_update do
266
+ # do something
267
+ end
268
+ end
269
+ }
270
+ end
271
+ end
272
+ end
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_model'
4
+
5
+ plugin_root = File.expand_path(File.join(File.dirname(__FILE__), '..'))
6
+ require File.join(plugin_root, 'lib/active_form')
7
+
8
+ class Test::Unit::TestCase
9
+ def assert_valid(model, message = nil)
10
+ assert model.valid?, message || "should be valid"
11
+ end
12
+
13
+ def assert_invalid(model, message = nil)
14
+ assert !model.valid?, message || "should be invalid"
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails3-active_form
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Torsten Braun
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-03 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: &22342160 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '1.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *22342160
25
+ - !ruby/object:Gem::Dependency
26
+ name: activemodel
27
+ requirement: &22341660 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 3.0.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *22341660
36
+ - !ruby/object:Gem::Dependency
37
+ name: activesupport
38
+ requirement: &22341200 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: 3.0.0
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *22341200
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: &22340680 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0.9'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *22340680
58
+ description: Rails >= 3.0.0 is required.
59
+ email: support@tnt-web-solutions.de
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - README.md
65
+ - LICENCE
66
+ - Rakefile
67
+ - Gemfile
68
+ - lib/active_form.rb
69
+ - active_form.gemspec
70
+ - test/test_helper.rb
71
+ - test/basic_test.rb
72
+ homepage: http://github.com/tntwebsolutions/active_form
73
+ licenses: []
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: 1.8.7
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: 1.3.6
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 1.8.10
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Validations for Non Active Record Models.
96
+ test_files:
97
+ - test/test_helper.rb
98
+ - test/basic_test.rb