josevalim-simple_form 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +4 -0
- data/README +12 -3
- data/lib/simple_form/base.rb +14 -6
- data/lib/simple_form/dsl.rb +11 -1
- data/test/base_test.rb +7 -0
- data/test/notifier_test.rb +3 -3
- data/test/test_helper.rb +5 -1
- metadata +4 -4
data/CHANGELOG
CHANGED
data/README
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Simple Form
|
2
2
|
License: MIT
|
3
|
-
Version: 0.1
|
3
|
+
Version: 0.2.1
|
4
4
|
|
5
5
|
You can also read this README in pretty html at the GitHub project Wiki page:
|
6
6
|
|
@@ -30,7 +30,8 @@ Then you start a script/console and type:
|
|
30
30
|
c = ContactForm.new(:name => 'José', :email => 'jose@email.com', :message => 'Cool!')
|
31
31
|
c.deliver
|
32
32
|
|
33
|
-
Check your inbox and the e-mail will be there, with the sent fields
|
33
|
+
Check your inbox and the e-mail will be there, with the sent fields (assuming
|
34
|
+
that you configured your smtp properly).
|
34
35
|
|
35
36
|
SimpleForm also support attachments, I18n, error messages like in ActiveRecord
|
36
37
|
(so it works with custom FormBuilders) and can also send the user request information
|
@@ -66,7 +67,7 @@ And in your controller:
|
|
66
67
|
@contact_form = ContactForm.new(params[:contact_form], request)
|
67
68
|
|
68
69
|
And the remote ip, user agent and session will be sent in the e-mail in a
|
69
|
-
request information session. You can
|
70
|
+
request information session. You can give to append any method that the
|
70
71
|
request object responds to.
|
71
72
|
|
72
73
|
API Overview
|
@@ -83,6 +84,9 @@ Options:
|
|
83
84
|
When a regexp is given, check if the attribute matches is not blank and
|
84
85
|
then if it matches the regexp.
|
85
86
|
|
87
|
+
Whenever :validate is a symbol, the method given as symbol will be
|
88
|
+
called. You can then add validations as you do in ActiveRecord (errors.add).
|
89
|
+
|
86
90
|
* :attachment - When given, expects a file to be sent and attaches
|
87
91
|
it to the e-mail. Don't forget to set your form to multitype.
|
88
92
|
|
@@ -95,7 +99,12 @@ Examples:
|
|
95
99
|
attributes :name, :validate => true
|
96
100
|
attributes :email, :validate => /[^@]+@[^\.]+\.[\w\.\-]+/
|
97
101
|
attributes :message
|
102
|
+
attributes :screenshot, :attachment => true, :validate => :screenshot_required?
|
98
103
|
attributes :nickname, :captcha => true
|
104
|
+
|
105
|
+
def screenshot_required?
|
106
|
+
# ...
|
107
|
+
end
|
99
108
|
end
|
100
109
|
|
101
110
|
c = ContactForm.new(:nickname => 'not_blank', :email => 'your@email.com', :name => 'José')
|
data/lib/simple_form/base.rb
CHANGED
@@ -38,19 +38,27 @@ class SimpleForm
|
|
38
38
|
!spam?
|
39
39
|
end
|
40
40
|
|
41
|
-
#
|
42
|
-
#
|
41
|
+
# To check if the form is valid, we run the validations.
|
42
|
+
#
|
43
|
+
# If the validation is true, we just check if the field is not blank. If it's
|
44
|
+
# a regexp, we check if it is not blank AND if the Regexp matches.
|
45
|
+
#
|
46
|
+
# You can have totally custom validations by sending a symbol. Then the method
|
47
|
+
# given as symbol will be called and then you cann hook your validations there.
|
43
48
|
#
|
44
49
|
def valid?
|
45
50
|
return false unless errors.empty?
|
46
51
|
|
47
52
|
form_validatable.each_pair do |field, validation|
|
48
|
-
|
53
|
+
next unless validation
|
54
|
+
|
55
|
+
if validation.is_a?(Symbol)
|
56
|
+
send(validation)
|
57
|
+
elsif send(field).blank?
|
49
58
|
errors.add(field, :blank)
|
50
|
-
|
59
|
+
elsif validation.is_a?(Regexp)
|
60
|
+
errors.add(field, :invalid) unless send(field) =~ validation
|
51
61
|
end
|
52
|
-
|
53
|
-
errors.add(field, :invalid) if validation.is_a?(Regexp) && send(field) !~ validation
|
54
62
|
end
|
55
63
|
|
56
64
|
errors.empty?
|
data/lib/simple_form/dsl.rb
CHANGED
@@ -12,6 +12,9 @@ class SimpleForm
|
|
12
12
|
# When a regexp is given, check if the attribute matches is not blank and
|
13
13
|
# then if it matches the regexp.
|
14
14
|
#
|
15
|
+
# Whenever :validate is a symbol, the method given as symbol will be
|
16
|
+
# called. You can then add validations as you do in ActiveRecord (errors.add).
|
17
|
+
#
|
15
18
|
# * <tt>:attachment</tt> - When given, expects a file to be sent and attaches
|
16
19
|
# it to the e-mail. Don't forget to set your form to multitype.
|
17
20
|
#
|
@@ -24,8 +27,15 @@ class SimpleForm
|
|
24
27
|
# attributes :name, :validate => true
|
25
28
|
# attributes :email, :validate => /^([^@]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
|
26
29
|
# attributes :message
|
27
|
-
# attributes :
|
30
|
+
# attributes :type
|
31
|
+
# attributes :screenshot, :attachment => true, :validate => :interface_bug?
|
28
32
|
# attributes :nickname, :captcha => true
|
33
|
+
#
|
34
|
+
# def interface_bug?
|
35
|
+
# if type == 'Interface bug' && screenshot.nil?
|
36
|
+
# self.errors.add(:screenshot, "can't be blank when you are reporting an interface bug")
|
37
|
+
# end
|
38
|
+
# end
|
29
39
|
# end
|
30
40
|
#
|
31
41
|
def attribute(*accessors)
|
data/test/base_test.rb
CHANGED
@@ -56,6 +56,13 @@ class SimpleFormBaseTest < ActiveSupport::TestCase
|
|
56
56
|
assert !form.invalid?
|
57
57
|
end
|
58
58
|
|
59
|
+
def test_symbols_given_to_validate_are_called
|
60
|
+
form = ContactForm.new
|
61
|
+
assert form.instance_variable_get('@_callback_run').nil?
|
62
|
+
form.valid?
|
63
|
+
assert form.instance_variable_get('@_callback_run')
|
64
|
+
end
|
65
|
+
|
59
66
|
def test_deliver_is_false_when_is_a_spam
|
60
67
|
form = ContactForm.new(:name => 'Jose', :email => 'is.valid@email.com', :nickname => 'not_blank')
|
61
68
|
assert form.valid?
|
data/test/notifier_test.rb
CHANGED
@@ -127,10 +127,10 @@ class SimpleFormNotifierTest < ActiveSupport::TestCase
|
|
127
127
|
end
|
128
128
|
|
129
129
|
def test_request_info_hashes_are_print_inside_lis
|
130
|
-
@request.session = { :my => :session, :user =>
|
130
|
+
@request.session = { :my => :session, :user => "data" }
|
131
131
|
@advanced.deliver
|
132
|
-
assert_match /<li>my: session<\/li>/, ActionMailer::Base.deliveries.last.body
|
133
|
-
assert_match /<li>user: data
|
132
|
+
assert_match /<li>my: :session<\/li>/, ActionMailer::Base.deliveries.last.body
|
133
|
+
assert_match /<li>user: "data"<\/li>/, ActionMailer::Base.deliveries.last.body
|
134
134
|
end
|
135
135
|
|
136
136
|
def test_error_is_raised_when_append_is_given_but_no_request_is_given
|
data/test/test_helper.rb
CHANGED
@@ -19,7 +19,11 @@ class ContactForm < SimpleForm
|
|
19
19
|
attribute :name, :validate => true
|
20
20
|
attribute :email, :validate => /[^@]+@[^\.]+\.[\w\.\-]+/
|
21
21
|
attribute :nickname, :captcha => true
|
22
|
-
attributes :tellphone, :message
|
22
|
+
attributes :tellphone, :message, :validate => :callback
|
23
|
+
|
24
|
+
def callback
|
25
|
+
@_callback_run = true
|
26
|
+
end
|
23
27
|
end
|
24
28
|
|
25
29
|
class AdvancedForm < ContactForm
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: josevalim-simple_form
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "Jos\xC3\xA9 Valim"
|
@@ -9,11 +9,11 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-04-
|
12
|
+
date: 2009-04-15 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
16
|
-
description: Simple easy contact form for Rails.
|
16
|
+
description: Simple easy contact form for Rails with I18n, validations, attachments and request information.
|
17
17
|
email: jose.valim@gmail.com
|
18
18
|
executables: []
|
19
19
|
|
@@ -59,7 +59,7 @@ rubyforge_project:
|
|
59
59
|
rubygems_version: 1.2.0
|
60
60
|
signing_key:
|
61
61
|
specification_version: 2
|
62
|
-
summary: Simple easy contact form for Rails.
|
62
|
+
summary: Simple easy contact form for Rails with I18n, validations, attachments and request information.
|
63
63
|
test_files:
|
64
64
|
- test/base_test.rb
|
65
65
|
- test/errors_test.rb
|