mail_form 1.3.0 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +7 -2
- data/MIT-LICENSE +1 -1
- data/README.md +226 -0
- data/lib/mail_form.rb +1 -2
- data/lib/mail_form/delivery.rb +15 -4
- data/lib/mail_form/shim.rb +2 -11
- data/lib/mail_form/version.rb +1 -1
- data/lib/mail_form/views/mail_form/contact.erb +1 -1
- data/test/mail_form_test.rb +28 -28
- data/test/resource_test.rb +2 -2
- data/test/test_file.txt +1 -0
- data/test/test_helper.rb +7 -4
- data/test/views/mail_form/custom_template.erb +1 -0
- metadata +58 -50
- data/README.rdoc +0 -199
- data/Rakefile +0 -41
data/CHANGELOG
CHANGED
@@ -1,7 +1,12 @@
|
|
1
|
+
# Version 1.4
|
2
|
+
|
3
|
+
* Fixed bug that was causing all activerecord attributes be saved as nil
|
4
|
+
* Avoid symbol injection on forms
|
5
|
+
|
1
6
|
# Version 1.3
|
2
7
|
|
3
|
-
* Removed deprecated methods in version 1.2
|
4
|
-
* Added persisted? header and a generator
|
8
|
+
* Removed deprecated methods in version 1.2
|
9
|
+
* Added persisted? header and a generator
|
5
10
|
|
6
11
|
# Version 1.2
|
7
12
|
|
data/MIT-LICENSE
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright (c) 2009
|
1
|
+
Copyright (c) 2009-2012 Plataformatec http://plataformatec.com.br/
|
2
2
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining
|
4
4
|
a copy of this software and associated documentation files (the
|
data/README.md
ADDED
@@ -0,0 +1,226 @@
|
|
1
|
+
## MailForm
|
2
|
+
[![Build Status](https://secure.travis-ci.org/plataformatec/mail_form.png)](http://travis-ci.org/plataformatec/mail_form)
|
3
|
+
|
4
|
+
### Rails 3
|
5
|
+
|
6
|
+
This gem was built on top of `ActiveModel` to showcase how you can pull in validations, naming
|
7
|
+
and `i18n` from Rails to your models without the need to implement it all by yourself.
|
8
|
+
|
9
|
+
In other words, this README refers to the **MailForm** gem to be used in Rails 3. For instructions
|
10
|
+
on how to use MailForm in Rails 2.x, please check the v1.0 branch:
|
11
|
+
|
12
|
+
http://github.com/plataformatec/mail_form/tree/v1.0
|
13
|
+
|
14
|
+
### Description
|
15
|
+
|
16
|
+
**MailForm** allows you to send an e-mail straight from a form. For instance,
|
17
|
+
if you want to make a contact form just the following lines are needed (including the e-mail):
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
class ContactForm < MailForm::Base
|
21
|
+
attribute :name, :validate => true
|
22
|
+
attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
|
23
|
+
attribute :file, :attachment => true
|
24
|
+
|
25
|
+
attribute :message
|
26
|
+
attribute :nickname, :captcha => true
|
27
|
+
|
28
|
+
# Declare the e-mail headers. It accepts anything the mail method
|
29
|
+
# in ActionMailer accepts.
|
30
|
+
def headers
|
31
|
+
{
|
32
|
+
:subject => "My Contact Form",
|
33
|
+
:to => "your.email@your.domain.com",
|
34
|
+
:from => %("#{name}" <#{email}>)
|
35
|
+
}
|
36
|
+
end
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
Then you start a console with `rails console` and type:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
>> c = ContactForm.new(:name => 'José', :email => 'jose@email.com', :message => 'Cool!')
|
44
|
+
>> c.deliver
|
45
|
+
```
|
46
|
+
|
47
|
+
Check your inbox and the e-mail will be there, with the sent fields (assuming that
|
48
|
+
you configured your mailer delivery method properly).
|
49
|
+
|
50
|
+
### MailForm::Base
|
51
|
+
|
52
|
+
When you inherit from `MailForm::Base`, it pulls down a set of stuff from `ActiveModel`,
|
53
|
+
as `ActiveModel::Validation`, `ActiveModel::Translation` and `ActiveModel::Naming`.
|
54
|
+
|
55
|
+
This bring `I18n`, error messages, validations and attributes handling like in
|
56
|
+
`ActiveRecord` to **MailForm**, so **MailForm** can be used in your controllers and form builders without extra tweaks. This also means that instead of the following:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
|
60
|
+
```
|
61
|
+
|
62
|
+
You could actually do this:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
attribute :email
|
66
|
+
validates_format_of :email, :with => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
|
67
|
+
```
|
68
|
+
|
69
|
+
Choose the one which pleases you the most. For more information on the API, please
|
70
|
+
continue reading below.
|
71
|
+
|
72
|
+
### Playing together ORMs
|
73
|
+
|
74
|
+
**MailForm** plays nice with ORMs as well. You just need to include `MailForm::Delivery`
|
75
|
+
in your model and declare which attributes should be sent:
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
class User < ActiveRecord::Base
|
79
|
+
include MailForm::Delivery
|
80
|
+
|
81
|
+
append :remote_ip, :user_agent, :session
|
82
|
+
attributes :name, :email, :created_at
|
83
|
+
|
84
|
+
def headers
|
85
|
+
{
|
86
|
+
:to => "your.email@your.domain.com",
|
87
|
+
:subject => "User created an account"
|
88
|
+
}
|
89
|
+
end
|
90
|
+
end
|
91
|
+
```
|
92
|
+
|
93
|
+
The delivery will be triggered in an `after_create` hook.
|
94
|
+
|
95
|
+
## Installation
|
96
|
+
|
97
|
+
Install **MailForm** is very easy. Just edit your Gemfile adding the following:
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
gem 'mail_form'
|
101
|
+
```
|
102
|
+
Then run `bundle install` to install **MailForm**.
|
103
|
+
|
104
|
+
If you want it as plugin, just do:
|
105
|
+
|
106
|
+
`script/plugin install git://github.com/plataformatec/mail_form.git`
|
107
|
+
|
108
|
+
## API Overview
|
109
|
+
|
110
|
+
### attributes(*attributes)
|
111
|
+
|
112
|
+
Declare your form attributes. All attributes declared here will be appended
|
113
|
+
to the e-mail, except the ones :captcha is true.
|
114
|
+
|
115
|
+
Options:
|
116
|
+
|
117
|
+
* :validate - A hook to validates_*_of. When true is given, validates the
|
118
|
+
presence of the attribute. When a regexp, validates format. When array,
|
119
|
+
validates the inclusion of the attribute in the array.
|
120
|
+
|
121
|
+
Whenever :validate is given, the presence is automatically checked. Give
|
122
|
+
:allow_blank => true to override.
|
123
|
+
|
124
|
+
Finally, when :validate is a symbol, the method given as symbol will be
|
125
|
+
called. Then you can add validations as you do in ActiveRecord (errors.add).
|
126
|
+
|
127
|
+
* :attachment - When given, expects a file to be sent and attaches
|
128
|
+
it to the e-mail. Don't forget to set your form to multitype.
|
129
|
+
|
130
|
+
* :captcha - When true, validates the attributes must be blank.
|
131
|
+
This is a simple way to avoid spam and the input should be hidden with CSS.
|
132
|
+
|
133
|
+
Examples:
|
134
|
+
|
135
|
+
```ruby
|
136
|
+
class ContactForm < MailForm::Base
|
137
|
+
attributes :name, :validate => true
|
138
|
+
attributes :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
|
139
|
+
attributes :type, :validate => ["General", "Interface bug"]
|
140
|
+
attributes :message
|
141
|
+
attributes :screenshot, :attachment => true, :validate => :interface_bug?
|
142
|
+
attributes :nickname, :captcha => true
|
143
|
+
|
144
|
+
def interface_bug?
|
145
|
+
if type == 'Interface bug' && screenshot.nil?
|
146
|
+
self.errors.add(:screenshot, "can't be blank on interface bugs")
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
c = ContactForm.new(:nickname => 'not_blank', :email => 'your@email.com', :name => 'José')
|
152
|
+
c.valid? #=> true
|
153
|
+
c.spam? #=> true (raises an error in development, to remember you to hide it)
|
154
|
+
c.deliver #=> false (just delivers if is not a spam and is valid, raises an error in development)
|
155
|
+
|
156
|
+
c = ContactForm.new(:email => 'invalid')
|
157
|
+
c.valid? #=> false
|
158
|
+
c.errors.inspect #=> { :name => :blank, :email => :invalid }
|
159
|
+
c.errors.full_messages #=> [ "Name can't be blank", "Email is invalid" ]
|
160
|
+
|
161
|
+
c = ContactForm.new(:name => 'José', :email => 'your@email.com')
|
162
|
+
c.deliver
|
163
|
+
```
|
164
|
+
|
165
|
+
### append(*methods)
|
166
|
+
|
167
|
+
**MailForm** also makes easy to append request information from client to the sent
|
168
|
+
mail. You just have to do:
|
169
|
+
|
170
|
+
```ruby
|
171
|
+
class ContactForm < MailForm::Base
|
172
|
+
append :remote_ip, :user_agent, :session
|
173
|
+
# ...
|
174
|
+
end
|
175
|
+
```
|
176
|
+
|
177
|
+
And in your controller:
|
178
|
+
|
179
|
+
```ruby
|
180
|
+
@contact_form = ContactForm.new(params[:contact_form])
|
181
|
+
@contact_form.request = request
|
182
|
+
```
|
183
|
+
|
184
|
+
The remote ip, user agent and session will be sent in the e-mail in a
|
185
|
+
request information session. You can give to append any method that the
|
186
|
+
request object responds to.
|
187
|
+
|
188
|
+
## I18n
|
189
|
+
|
190
|
+
I18n in **MailForm** works like in ActiveRecord, so all models, attributes and messages
|
191
|
+
can be used with localized. Below is an I18n file example file:
|
192
|
+
|
193
|
+
```ruby
|
194
|
+
mail_form:
|
195
|
+
models:
|
196
|
+
contact_form: "Your site contact form"
|
197
|
+
attributes:
|
198
|
+
contact_form:
|
199
|
+
email: "E-mail"
|
200
|
+
telephone: "Telephone number"
|
201
|
+
message: "Sent message"
|
202
|
+
request:
|
203
|
+
title: "Technical information about the user"
|
204
|
+
remote_ip: "IP Address"
|
205
|
+
user_agent: "Browser"
|
206
|
+
```
|
207
|
+
|
208
|
+
## Custom e-mail template
|
209
|
+
|
210
|
+
To customize the e-mail template that is used create a file called contact.erb in app/views/mail_form.
|
211
|
+
Take a look at lib/mail_form/views/mail_form/contact.erb in this repo to see how the default template works.
|
212
|
+
|
213
|
+
## Maintainers
|
214
|
+
|
215
|
+
* José Valim - http://github.com/josevalim
|
216
|
+
* Carlos Antonio - http://github.com/carlosantoniodasilva
|
217
|
+
|
218
|
+
## Contributors
|
219
|
+
|
220
|
+
* Andrew Timberlake - http://github.com/andrewtimberlake
|
221
|
+
|
222
|
+
## Bugs and Feedback
|
223
|
+
|
224
|
+
If you discover any bug, please use github issues tracker.
|
225
|
+
|
226
|
+
Copyright (c) 2009-2012 Plataformatec http://plataformatec.com.br/
|
data/lib/mail_form.rb
CHANGED
data/lib/mail_form/delivery.rb
CHANGED
@@ -36,10 +36,10 @@ module MailForm
|
|
36
36
|
# * :validate - A hook to validates_*_of. When true is given, validates the
|
37
37
|
# presence of the attribute. When a regexp, validates format. When array,
|
38
38
|
# validates the inclusion of the attribute in the array.
|
39
|
-
#
|
39
|
+
#
|
40
40
|
# Whenever :validate is given, the presence is automatically checked. Give
|
41
41
|
# :allow_blank => true to override.
|
42
|
-
#
|
42
|
+
#
|
43
43
|
# Finally, when :validate is a symbol, the method given as symbol will be
|
44
44
|
# called. Then you can add validations as you do in ActiveRecord (errors.add).
|
45
45
|
#
|
@@ -68,7 +68,10 @@ module MailForm
|
|
68
68
|
#
|
69
69
|
def attribute(*accessors)
|
70
70
|
options = accessors.extract_options!
|
71
|
-
|
71
|
+
|
72
|
+
# TODO: make this not depend on column_names
|
73
|
+
columns_methods = self.respond_to?(:column_names) ? column_names.map(&:to_sym) : []
|
74
|
+
attr_accessor(*(accessors - instance_methods.map(&:to_sym) - columns_methods))
|
72
75
|
|
73
76
|
if options[:attachment]
|
74
77
|
self.mail_attachments += accessors
|
@@ -147,5 +150,13 @@ module MailForm
|
|
147
150
|
def deliver!
|
148
151
|
MailForm::Notifier.contact(self).deliver
|
149
152
|
end
|
153
|
+
|
154
|
+
# Returns a hash of attributes, according to the attributes existent in
|
155
|
+
# self.class.mail_attributes.
|
156
|
+
def mail_form_attributes
|
157
|
+
self.class.mail_attributes.each_with_object({}) do |attr, hash|
|
158
|
+
hash[attr.to_s] = send(attr)
|
159
|
+
end
|
160
|
+
end
|
150
161
|
end
|
151
|
-
end
|
162
|
+
end
|
data/lib/mail_form/shim.rb
CHANGED
@@ -25,19 +25,10 @@ module MailForm
|
|
25
25
|
# Initialize assigning the parameters given as hash.
|
26
26
|
def initialize(params={})
|
27
27
|
params.each_pair do |attr, value|
|
28
|
-
|
28
|
+
send("#{attr}=", value) if respond_to?("#{attr}=", true)
|
29
29
|
end unless params.blank?
|
30
30
|
end
|
31
31
|
|
32
|
-
# Returns a hash of attributes, according to the attributes existent in
|
33
|
-
# self.class.mail_attributes.
|
34
|
-
def attributes
|
35
|
-
self.class.mail_attributes.inject({}) do |hash, attr|
|
36
|
-
hash[attr.to_s] = send(attr)
|
37
|
-
hash
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
32
|
# Always return true so when using form_for, the default method will be post.
|
42
33
|
def new_record?
|
43
34
|
true
|
@@ -62,4 +53,4 @@ module MailForm
|
|
62
53
|
end
|
63
54
|
alias :save :deliver
|
64
55
|
end
|
65
|
-
end
|
56
|
+
end
|
data/lib/mail_form/version.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
<h4 style="text-decoration:underline"><%= message.subject %></h4>
|
2
2
|
|
3
|
-
<% @resource.
|
3
|
+
<% @resource.mail_form_attributes.each do |attribute, value|
|
4
4
|
next if value.blank? %>
|
5
5
|
|
6
6
|
<p><b><%= @resource.class.human_attribute_name(attribute) %>:</b>
|
data/test/mail_form_test.rb
CHANGED
@@ -30,85 +30,85 @@ class MailFormNotifierTest < ActiveSupport::TestCase
|
|
30
30
|
|
31
31
|
def test_body_contains_subject
|
32
32
|
@form.deliver
|
33
|
-
assert_match
|
33
|
+
assert_match %r[Contact form], first_delivery.body.to_s
|
34
34
|
end
|
35
35
|
|
36
36
|
def test_body_contains_attributes_values
|
37
37
|
@form.deliver
|
38
|
-
assert_match
|
39
|
-
assert_match
|
40
|
-
assert_match
|
38
|
+
assert_match %r[José], first_delivery.body.to_s
|
39
|
+
assert_match %r[my.email@my.domain.com], first_delivery.body.to_s
|
40
|
+
assert_match %r[Cool], first_delivery.body.to_s
|
41
41
|
end
|
42
42
|
|
43
43
|
def test_body_contains_attributes_names
|
44
44
|
@form.deliver
|
45
|
-
assert_match
|
46
|
-
assert_match
|
47
|
-
assert_match
|
45
|
+
assert_match %r[Name:], first_delivery.body.to_s
|
46
|
+
assert_match %r[Email:], first_delivery.body.to_s
|
47
|
+
assert_match %r[Message:], first_delivery.body.to_s
|
48
48
|
end
|
49
49
|
|
50
50
|
def test_body_contains_localized_attributes_names
|
51
51
|
I18n.backend.store_translations(:en, :mail_form => { :attributes => { :contact_form => { :message => 'Sent message' } } })
|
52
52
|
@form.deliver
|
53
|
-
assert_match
|
54
|
-
assert_no_match
|
53
|
+
assert_match %r[Sent message:], first_delivery.body.to_s
|
54
|
+
assert_no_match %r[Message:], first_delivery.body.to_s
|
55
55
|
end
|
56
56
|
|
57
57
|
def test_body_mail_format_messages_with_break_line
|
58
58
|
@form.deliver
|
59
|
-
assert_no_match
|
59
|
+
assert_no_match %r[<p>Cool], first_delivery.body.to_s
|
60
60
|
|
61
61
|
@advanced.deliver
|
62
|
-
assert_match
|
62
|
+
assert_match %r[<p>Cool], last_delivery.body.to_s
|
63
63
|
end
|
64
64
|
|
65
65
|
def test_body_mail_format_dates_with_i18n
|
66
66
|
@form.deliver
|
67
|
-
assert_no_match
|
67
|
+
assert_no_match %r[I18n.l(Date.today)], first_delivery.body.to_s
|
68
68
|
end
|
69
69
|
|
70
70
|
def test_body_does_not_append_request_if_append_is_not_called
|
71
71
|
@form.deliver
|
72
|
-
assert_no_match
|
72
|
+
assert_no_match %r[Request information], first_delivery.body.to_s
|
73
73
|
end
|
74
74
|
|
75
75
|
def test_body_does_append_request_if_append_is_called
|
76
76
|
@advanced.deliver
|
77
|
-
assert_match
|
77
|
+
assert_match %r[Request information], last_delivery.body.to_s
|
78
78
|
end
|
79
79
|
|
80
80
|
def test_request_title_is_localized
|
81
81
|
I18n.backend.store_translations(:en, :mail_form => { :request => { :title => 'Information about the request' } })
|
82
82
|
@advanced.deliver
|
83
|
-
assert_no_match
|
84
|
-
assert_match
|
83
|
+
assert_no_match %r[Request information], last_delivery.body.to_s
|
84
|
+
assert_match %r[Information about the request], last_delivery.body.to_s
|
85
85
|
end
|
86
86
|
|
87
87
|
def test_request_info_attributes_are_printed
|
88
88
|
@advanced.deliver
|
89
|
-
assert_match
|
90
|
-
assert_match
|
89
|
+
assert_match %r[Remote ip], last_delivery.body.to_s
|
90
|
+
assert_match %r[User agent], last_delivery.body.to_s
|
91
91
|
end
|
92
92
|
|
93
93
|
def test_request_info_attributes_are_localized
|
94
94
|
I18n.backend.store_translations(:en, :mail_form => { :request => { :remote_ip => 'IP Address' } })
|
95
95
|
@advanced.deliver
|
96
|
-
assert_match
|
97
|
-
assert_no_match
|
96
|
+
assert_match %r[IP Address], last_delivery.body.to_s
|
97
|
+
assert_no_match %r[Remote ip], last_delivery.body.to_s
|
98
98
|
end
|
99
99
|
|
100
100
|
def test_request_info_values_are_printed
|
101
101
|
@advanced.deliver
|
102
|
-
assert_match
|
103
|
-
assert_match
|
102
|
+
assert_match %r[0\.0\.0\.0], last_delivery.body.to_s
|
103
|
+
assert_match %r[Rails Testing], last_delivery.body.to_s
|
104
104
|
end
|
105
105
|
|
106
106
|
def test_request_info_hashes_are_print_inside_lis
|
107
107
|
@request.session = { :my => :session, :user => "data" }
|
108
108
|
@advanced.deliver
|
109
|
-
assert_match
|
110
|
-
assert_match
|
111
|
-
assert_match
|
109
|
+
assert_match %r[<ul], last_delivery.body.to_s
|
110
|
+
assert_match %r[<li>my: :session<\/li>], last_delivery.body.to_s
|
111
|
+
assert_match %r[<li>user: "data"<\/li>], last_delivery.body.to_s
|
112
112
|
end
|
113
113
|
|
114
114
|
def test_error_is_raised_when_append_is_given_but_no_request_is_given
|
@@ -125,7 +125,7 @@ class MailFormNotifierTest < ActiveSupport::TestCase
|
|
125
125
|
|
126
126
|
def test_form_with_file_does_not_output_attachment_as_attribute
|
127
127
|
@with_file.deliver
|
128
|
-
assert_no_match
|
128
|
+
assert_no_match %r[File:], first_delivery.body.to_s
|
129
129
|
end
|
130
130
|
|
131
131
|
protected
|
@@ -137,8 +137,8 @@ class MailFormNotifierTest < ActiveSupport::TestCase
|
|
137
137
|
def last_delivery
|
138
138
|
ActionMailer::Base.deliveries.last
|
139
139
|
end
|
140
|
-
|
140
|
+
|
141
141
|
def teardown
|
142
142
|
I18n.reload!
|
143
143
|
end
|
144
|
-
end
|
144
|
+
end
|
data/test/resource_test.rb
CHANGED
@@ -61,9 +61,9 @@ class MailFormBaseTest < ActiveSupport::TestCase
|
|
61
61
|
|
62
62
|
def test_symbols_given_to_validate_are_called
|
63
63
|
form = ContactForm.new
|
64
|
-
assert form.
|
64
|
+
assert !form.callback_run?
|
65
65
|
form.valid?
|
66
|
-
assert form.
|
66
|
+
assert form.callback_run?
|
67
67
|
end
|
68
68
|
|
69
69
|
def test_deliver_is_false_when_is_a_spam
|
data/test/test_file.txt
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
A test file
|
data/test/test_helper.rb
CHANGED
@@ -26,19 +26,22 @@ class ContactForm < MailForm::Base
|
|
26
26
|
|
27
27
|
attributes :created_at, :message, :validate => :callback
|
28
28
|
|
29
|
-
before_deliver :set_created_at
|
30
|
-
|
31
29
|
def headers
|
32
30
|
{ :to => 'my.email@my.domain.com' }
|
33
31
|
end
|
34
32
|
|
35
|
-
def
|
36
|
-
|
33
|
+
def initialize(*)
|
34
|
+
super
|
35
|
+
@_callback_run = false
|
37
36
|
end
|
38
37
|
|
39
38
|
def callback
|
40
39
|
@_callback_run = true
|
41
40
|
end
|
41
|
+
|
42
|
+
def callback_run?
|
43
|
+
@_callback_run
|
44
|
+
end
|
42
45
|
end
|
43
46
|
|
44
47
|
class AdvancedForm < ContactForm
|
@@ -0,0 +1 @@
|
|
1
|
+
Hello from my custom template!
|
metadata
CHANGED
@@ -1,77 +1,85 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: mail_form
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
|
6
|
-
- 1
|
7
|
-
- 3
|
8
|
-
- 0
|
9
|
-
version: 1.3.0
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 1.4.0
|
10
6
|
platform: ruby
|
11
|
-
authors:
|
12
|
-
-
|
13
|
-
-
|
7
|
+
authors:
|
8
|
+
- José Valim
|
9
|
+
- Carlos Antônio
|
14
10
|
autorequire:
|
15
11
|
bindir: bin
|
16
12
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
13
|
+
date: 2012-12-13 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
version_requirements: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ~>
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '3.0'
|
21
|
+
none: false
|
22
|
+
name: actionmailer
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
requirement: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.0'
|
30
|
+
none: false
|
31
|
+
description: ! '{Send e-mail straight from forms in Rails with I18n, validations,
|
32
|
+
attachments and request information.'
|
23
33
|
email: contact@plataformatec.com.br
|
24
34
|
executables: []
|
25
|
-
|
26
35
|
extensions: []
|
27
|
-
|
28
|
-
|
29
|
-
- README.rdoc
|
30
|
-
files:
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
31
38
|
- CHANGELOG
|
32
39
|
- MIT-LICENSE
|
33
|
-
- README.
|
34
|
-
- Rakefile
|
40
|
+
- README.md
|
35
41
|
- lib/generators/rails/mail_form_generator.rb
|
36
42
|
- lib/generators/rails/templates/model.rb
|
37
|
-
- lib/mail_form.rb
|
38
43
|
- lib/mail_form/base.rb
|
39
44
|
- lib/mail_form/delivery.rb
|
40
45
|
- lib/mail_form/notifier.rb
|
41
46
|
- lib/mail_form/shim.rb
|
42
47
|
- lib/mail_form/version.rb
|
43
48
|
- lib/mail_form/views/mail_form/contact.erb
|
44
|
-
|
45
|
-
|
49
|
+
- lib/mail_form.rb
|
50
|
+
- test/mail_form_test.rb
|
51
|
+
- test/resource_test.rb
|
52
|
+
- test/test_file.txt
|
53
|
+
- test/test_helper.rb
|
54
|
+
- test/views/mail_form/custom_template.erb
|
55
|
+
homepage: https://github.com/plataformatec/mail_form
|
46
56
|
licenses: []
|
47
|
-
|
48
57
|
post_install_message:
|
49
|
-
rdoc_options:
|
50
|
-
|
51
|
-
require_paths:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
52
60
|
- lib
|
53
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
-
requirements:
|
55
|
-
- -
|
56
|
-
- !ruby/object:Gem::Version
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
- 0
|
66
|
-
version: "0"
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
none: false
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ! '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
none: false
|
67
73
|
requirements: []
|
68
|
-
|
69
|
-
|
70
|
-
rubygems_version: 1.3.6
|
74
|
+
rubyforge_project: mail_form
|
75
|
+
rubygems_version: 1.8.23
|
71
76
|
signing_key:
|
72
77
|
specification_version: 3
|
73
|
-
summary: Send e-mail straight from forms in Rails with I18n, validations, attachments
|
74
|
-
|
78
|
+
summary: Send e-mail straight from forms in Rails with I18n, validations, attachments
|
79
|
+
and request information.
|
80
|
+
test_files:
|
75
81
|
- test/mail_form_test.rb
|
76
82
|
- test/resource_test.rb
|
83
|
+
- test/test_file.txt
|
77
84
|
- test/test_helper.rb
|
85
|
+
- test/views/mail_form/custom_template.erb
|
data/README.rdoc
DELETED
@@ -1,199 +0,0 @@
|
|
1
|
-
== MailForm
|
2
|
-
|
3
|
-
=== Rails 3
|
4
|
-
|
5
|
-
This gem was built on top of ActiveModel to showcase how you can pull in validations, naming
|
6
|
-
and i18n from Rails to your models without the need to implement it all by yourself.
|
7
|
-
|
8
|
-
In other words, this README refers to the MailForm gem to be used in Rails 3. For instructions
|
9
|
-
on how to use MailForm in Rails 2.x, please check the v1.0 branch:
|
10
|
-
|
11
|
-
http://github.com/plataformatec/mail_form/tree/v1.0
|
12
|
-
|
13
|
-
=== Description
|
14
|
-
|
15
|
-
MailForm allows you to send an e-mail straight from a form. For instance,
|
16
|
-
if you want to make a contact form just the following lines are needed (including the e-mail):
|
17
|
-
|
18
|
-
class ContactForm < MailForm::Base
|
19
|
-
attribute :name, :validate => true
|
20
|
-
attribute :email, :validate => /[^@]+@[^\.]+\.[\w\.\-]+/
|
21
|
-
attribute :file, :attachment => true
|
22
|
-
|
23
|
-
attribute :message
|
24
|
-
attribute :nickname, :captcha => true
|
25
|
-
|
26
|
-
# Declare the e-mail headers. It accepts anything the mail method
|
27
|
-
# in ActionMailer accepts.
|
28
|
-
def headers
|
29
|
-
{
|
30
|
-
:subject => "My Contact Form",
|
31
|
-
:to => "your.email@your.domain.com",
|
32
|
-
:from => %("#{name}" <#{email}>)
|
33
|
-
}
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
Then you start a script/console and type:
|
38
|
-
|
39
|
-
c = ContactForm.new(:name => 'José', :email => 'jose@email.com', :message => 'Cool!')
|
40
|
-
c.create
|
41
|
-
|
42
|
-
Check your inbox and the e-mail will be there, with the sent fields (assuming that
|
43
|
-
you configured your mailer delivery method properly).
|
44
|
-
|
45
|
-
=== MailForm::Base
|
46
|
-
|
47
|
-
When you inherit from MailForm::Base, it pulls down a set of stuff from ActiveModel,
|
48
|
-
as ActiveModel::Validation, ActiveModel::Translation and ActiveModel::Naming.
|
49
|
-
|
50
|
-
This bring I18n, error messages, validations and attributes handling like in
|
51
|
-
ActiveRecord to MailForm, so MailForm can be used in your controllers and form builders without extra tweaks. This also means that instead of the following:
|
52
|
-
|
53
|
-
attribute :email, :validate => /[^@]+@[^\.]+\.[\w\.\-]+/
|
54
|
-
|
55
|
-
You could actually do this:
|
56
|
-
|
57
|
-
attribute :email
|
58
|
-
validates_format_of :email, :with => /[^@]+@[^\.]+\.[\w\.\-]+/
|
59
|
-
|
60
|
-
Choose the one which pleases you the most. For more information on the API, please
|
61
|
-
continue reading below.
|
62
|
-
|
63
|
-
=== Playing together ORMs
|
64
|
-
|
65
|
-
MailForm plays nice with ORMs as well. You just need to include MailForm::Delivery
|
66
|
-
in your model and declare which attributes should be sent:
|
67
|
-
|
68
|
-
class User < ActiveRecord::Base
|
69
|
-
include MailForm::Delivery
|
70
|
-
|
71
|
-
append :remote_ip, :user_agent, :session
|
72
|
-
attributes :name, :email, :created_at
|
73
|
-
|
74
|
-
def headers
|
75
|
-
{
|
76
|
-
:to => "your.email@your.domain.com",
|
77
|
-
:subject => "User created an account"
|
78
|
-
}
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
The delivery will be triggered in an after_create hook.
|
83
|
-
|
84
|
-
== Installation
|
85
|
-
|
86
|
-
Install MailForm is very easy. It is stored in Gemcutter, so just run the following:
|
87
|
-
|
88
|
-
sudo gem install mail_form
|
89
|
-
|
90
|
-
If you want it as plugin, just do:
|
91
|
-
|
92
|
-
script/plugin install git://github.com/plataformatec/mail_form.git
|
93
|
-
|
94
|
-
== API Overview
|
95
|
-
|
96
|
-
=== attributes(*attributes)
|
97
|
-
|
98
|
-
Declare your form attributes. All attributes declared here will be appended
|
99
|
-
to the e-mail, except the ones :captcha is true.
|
100
|
-
|
101
|
-
Options:
|
102
|
-
|
103
|
-
* :validate - A hook to validates_*_of. When true is given, validates the
|
104
|
-
presence of the attribute. When a regexp, validates format. When array,
|
105
|
-
validates the inclusion of the attribute in the array.
|
106
|
-
|
107
|
-
Whenever :validate is given, the presence is automatically checked. Give
|
108
|
-
:allow_blank => true to override.
|
109
|
-
|
110
|
-
Finally, when :validate is a symbol, the method given as symbol will be
|
111
|
-
called. Then you can add validations as you do in ActiveRecord (errors.add).
|
112
|
-
|
113
|
-
* :attachment - When given, expects a file to be sent and attaches
|
114
|
-
it to the e-mail. Don't forget to set your form to multitype.
|
115
|
-
|
116
|
-
* :captcha - When true, validates the attributes must be blank.
|
117
|
-
This is a simple way to avoid spam and the input should be hidden with CSS.
|
118
|
-
|
119
|
-
Examples:
|
120
|
-
|
121
|
-
class ContactForm < MailForm::Base
|
122
|
-
attributes :name, :validate => true
|
123
|
-
attributes :email, :validate => /[^@]+@[^\.]+\.[\w\.\-]+/
|
124
|
-
attributes :type, :validate => ["General", "Interface bug"]
|
125
|
-
attributes :message
|
126
|
-
attributes :screenshot, :attachment => true, :validate => :interface_bug?
|
127
|
-
attributes :nickname, :captcha => true
|
128
|
-
|
129
|
-
def interface_bug?
|
130
|
-
if type == 'Interface bug' && screenshot.nil?
|
131
|
-
self.errors.add(:screenshot, "can't be blank on interface bugs")
|
132
|
-
end
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
c = ContactForm.new(:nickname => 'not_blank', :email => 'your@email.com', :name => 'José')
|
137
|
-
c.valid? #=> true
|
138
|
-
c.spam? #=> true (raises an error in development, to remember you to hide it)
|
139
|
-
c.deliver #=> false (just delivers if is not a spam and is valid)
|
140
|
-
|
141
|
-
c = ContactForm.new(:email => 'invalid')
|
142
|
-
c.valid? #=> false
|
143
|
-
c.errors.inspect #=> { :name => :blank, :email => :invalid }
|
144
|
-
c.errors.full_messages #=> [ "Name can't be blank", "Email is invalid" ]
|
145
|
-
|
146
|
-
c = ContactForm.new(:name => 'José', :email => 'your@email.com')
|
147
|
-
c.deliver #=> true
|
148
|
-
|
149
|
-
=== append(*methods)
|
150
|
-
|
151
|
-
MailForm also makes easy to append request information from client to the sent
|
152
|
-
mail. You just have to do:
|
153
|
-
|
154
|
-
class ContactForm < MailForm::Base
|
155
|
-
append :remote_ip, :user_agent, :session
|
156
|
-
# ...
|
157
|
-
end
|
158
|
-
|
159
|
-
And in your controller:
|
160
|
-
|
161
|
-
@contact_form = ContactForm.new(params[:contact_form])
|
162
|
-
@contact_form.request = request
|
163
|
-
|
164
|
-
The remote ip, user agent and session will be sent in the e-mail in a
|
165
|
-
request information session. You can give to append any method that the
|
166
|
-
request object responds to.
|
167
|
-
|
168
|
-
== I18n
|
169
|
-
|
170
|
-
I18n in MailForm works like in ActiveRecord, so all models, attributes and messages
|
171
|
-
can be used with localized. Below is an I18n file example file:
|
172
|
-
|
173
|
-
mail_form:
|
174
|
-
models:
|
175
|
-
contact_form: "Your site contact form"
|
176
|
-
attributes:
|
177
|
-
contact_form:
|
178
|
-
email: "E-mail"
|
179
|
-
telephone: "Telephone number"
|
180
|
-
message: "Sent message"
|
181
|
-
request:
|
182
|
-
title: "Technical information about the user"
|
183
|
-
remote_ip: "IP Address"
|
184
|
-
user_agent: "Browser"
|
185
|
-
|
186
|
-
== Maintainers
|
187
|
-
|
188
|
-
* José Valim - http://github.com/josevalim
|
189
|
-
* Carlos Antonio - http://github.com/carlosantoniodasilva
|
190
|
-
|
191
|
-
== Contributors
|
192
|
-
|
193
|
-
* Andrew Timberlake - http://github.com/andrewtimberlake
|
194
|
-
|
195
|
-
== Bugs and Feedback
|
196
|
-
|
197
|
-
If you discover any bug, please use github issues tracker.
|
198
|
-
|
199
|
-
Copyright (c) 2010 Plataforma Tec http://blog.plataformatec.com.br/
|
data/Rakefile
DELETED
@@ -1,41 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
|
3
|
-
require 'rake'
|
4
|
-
require 'rake/testtask'
|
5
|
-
require 'rake/rdoctask'
|
6
|
-
require File.join(File.dirname(__FILE__), "lib", "mail_form", "version")
|
7
|
-
|
8
|
-
desc 'Run tests for MailForm.'
|
9
|
-
Rake::TestTask.new(:test) do |t|
|
10
|
-
t.libs << 'test'
|
11
|
-
t.pattern = 'test/**/*_test.rb'
|
12
|
-
t.verbose = true
|
13
|
-
end
|
14
|
-
|
15
|
-
desc 'Generate documentation for MailForm.'
|
16
|
-
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
-
rdoc.rdoc_dir = 'rdoc'
|
18
|
-
rdoc.title = 'MailForm'
|
19
|
-
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
-
rdoc.rdoc_files.include('README')
|
21
|
-
rdoc.rdoc_files.include('MIT-LICENSE')
|
22
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
|
-
end
|
24
|
-
|
25
|
-
begin
|
26
|
-
require 'jeweler'
|
27
|
-
Jeweler::Tasks.new do |s|
|
28
|
-
s.name = "mail_form"
|
29
|
-
s.version = MailForm::VERSION
|
30
|
-
s.summary = "Send e-mail straight from forms in Rails with I18n, validations, attachments and request information."
|
31
|
-
s.email = "contact@plataformatec.com.br"
|
32
|
-
s.homepage = "http://github.com/plataformatec/mail_form"
|
33
|
-
s.description = "Send e-mail straight from forms in Rails with I18n, validations, attachments and request information."
|
34
|
-
s.authors = ['José Valim', 'Carlos Antônio']
|
35
|
-
s.files = FileList["[A-Z]*", "{lib,views}/**/*"]
|
36
|
-
end
|
37
|
-
|
38
|
-
Jeweler::GemcutterTasks.new
|
39
|
-
rescue LoadError
|
40
|
-
puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install jeweler"
|
41
|
-
end
|