mail_form 1.5.1 → 1.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/MIT-LICENSE +1 -1
- data/README.md +41 -41
- data/lib/generators/rails/mail_form_generator.rb +1 -1
- data/lib/generators/rails/templates/model.rb +2 -2
- data/lib/mail_form/delivery.rb +16 -11
- data/lib/mail_form/shim.rb +1 -1
- data/lib/mail_form/version.rb +1 -1
- data/lib/mail_form/views/mail_form/contact.erb +3 -3
- data/test/active_record_test.rb +33 -0
- data/test/mail_form_test.rb +15 -9
- data/test/resource_test.rb +16 -16
- data/test/test_helper.rb +40 -11
- metadata +13 -24
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4b953cb8f6f9b66bc2f50c2859dc7e0893bc303e886f843199fa20b84078343b
|
4
|
+
data.tar.gz: cbf5ada685154e561f5274d5dec52155cfcaa07cdf29d32033a4d35002e8275e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dc1d9999ba9c420d17c325b2d8c0e5c813397bca394fe9ed0e97c12c1533df534be8b8df4617bc3519336f3f5644ee094e8c384e2250fac5ca1c2518bad8db54
|
7
|
+
data.tar.gz: aae1d4ced488183e71150fc2c428a326ee752714f3aded441ec27eab53526ce3befb5f43328b76c41fe565d19e5f66cf6e422d8b7f7482cae871c5060dcfa5c6
|
data/MIT-LICENSE
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright (c) 2009-
|
1
|
+
Copyright (c) 2009-2019 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
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
## MailForm
|
2
2
|
|
3
|
-
[![Gem Version](https://fury-badge.herokuapp.com/rb/mail_form.
|
4
|
-
[![Build Status](https://
|
5
|
-
[![Code Climate](https://codeclimate.com/github/
|
3
|
+
[![Gem Version](https://fury-badge.herokuapp.com/rb/mail_form.svg)](http://badge.fury.io/rb/mail_form)
|
4
|
+
[![Build Status](https://travis-ci.org/heartcombo/mail_form.svg?branch=master)](https://travis-ci.org/heartcombo/mail_form)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/heartcombo/mail_form.svg)](https://codeclimate.com/github/heartcombo/mail_form)
|
6
6
|
|
7
|
-
### Rails
|
7
|
+
### Rails 5
|
8
8
|
|
9
9
|
This gem was built on top of `ActiveModel` to showcase how you can pull in validations, naming
|
10
10
|
and `i18n` from Rails to your models without the need to implement it all by yourself.
|
11
11
|
|
12
|
-
This README refers to the **MailForm** gem to be used in Rails
|
12
|
+
This README refers to the **MailForm** gem to be used in Rails 5+. For instructions
|
13
13
|
on how to use MailForm in older versions of Rails, please refer to the available branches.
|
14
14
|
|
15
15
|
### Description
|
@@ -19,20 +19,20 @@ if you want to make a contact form just the following lines are needed (includin
|
|
19
19
|
|
20
20
|
```ruby
|
21
21
|
class ContactForm < MailForm::Base
|
22
|
-
attribute :name,
|
23
|
-
attribute :email,
|
24
|
-
attribute :file,
|
22
|
+
attribute :name, validate: true
|
23
|
+
attribute :email, validate: /\A[^@\s]+@[^@\s]+\z/i
|
24
|
+
attribute :file, attachment: true
|
25
25
|
|
26
26
|
attribute :message
|
27
|
-
attribute :nickname,
|
27
|
+
attribute :nickname, captcha: true
|
28
28
|
|
29
29
|
# Declare the e-mail headers. It accepts anything the mail method
|
30
30
|
# in ActionMailer accepts.
|
31
31
|
def headers
|
32
32
|
{
|
33
|
-
:
|
34
|
-
:
|
35
|
-
:
|
33
|
+
subject: "My Contact Form",
|
34
|
+
to: "your.email@your.domain.com",
|
35
|
+
from: %("#{name}" <#{email}>)
|
36
36
|
}
|
37
37
|
end
|
38
38
|
end
|
@@ -41,7 +41,7 @@ end
|
|
41
41
|
Then you start a console with `rails console` and type:
|
42
42
|
|
43
43
|
```ruby
|
44
|
-
>> c = ContactForm.new(:
|
44
|
+
>> c = ContactForm.new(name: 'José', email: 'jose@email.com', message: 'Cool!')
|
45
45
|
>> c.deliver
|
46
46
|
```
|
47
47
|
|
@@ -57,14 +57,14 @@ This brings `I18n`, error messages, validations and attributes handling like in
|
|
57
57
|
`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:
|
58
58
|
|
59
59
|
```ruby
|
60
|
-
attribute :email, :
|
60
|
+
attribute :email, validate: /\A[^@\s]+@[^@\s]+\z/i
|
61
61
|
```
|
62
62
|
|
63
63
|
You could actually do this:
|
64
64
|
|
65
65
|
```ruby
|
66
66
|
attribute :email
|
67
|
-
validates_format_of :email, :
|
67
|
+
validates_format_of :email, with: /\A[^@\s]+@[^@\s]+\z/i
|
68
68
|
```
|
69
69
|
|
70
70
|
Choose the one which pleases you the most. For more information on the API, please
|
@@ -84,8 +84,8 @@ class User < ActiveRecord::Base
|
|
84
84
|
|
85
85
|
def headers
|
86
86
|
{
|
87
|
-
:
|
88
|
-
:
|
87
|
+
to: "your.email@your.domain.com",
|
88
|
+
subject: "User created an account"
|
89
89
|
}
|
90
90
|
end
|
91
91
|
end
|
@@ -100,11 +100,11 @@ Install **MailForm** is very easy. Just edit your Gemfile adding the following:
|
|
100
100
|
```ruby
|
101
101
|
gem 'mail_form'
|
102
102
|
```
|
103
|
-
Then run `bundle install` to install **MailForm**.
|
104
103
|
|
105
|
-
|
104
|
+
Then run `bundle install` to install **MailForm**.
|
106
105
|
|
107
|
-
`
|
106
|
+
You can run `rails generate mail_form` to view help information on how to generate
|
107
|
+
a basic form to get you started.
|
108
108
|
|
109
109
|
## API Overview
|
110
110
|
|
@@ -115,32 +115,32 @@ to the e-mail, except the ones :captcha is true.
|
|
115
115
|
|
116
116
|
Options:
|
117
117
|
|
118
|
-
*
|
118
|
+
* `:validate` - A hook to `validates_*_of`. When `true` is given, validates the
|
119
119
|
presence of the attribute. When a regexp, validates format. When array,
|
120
120
|
validates the inclusion of the attribute in the array.
|
121
121
|
|
122
|
-
Whenever
|
123
|
-
:
|
122
|
+
Whenever `:validate` is given, the presence is automatically checked. Give
|
123
|
+
`allow_blank: true` to override.
|
124
124
|
|
125
|
-
Finally, when
|
126
|
-
called. Then you can add validations as you do in
|
125
|
+
Finally, when `:validate` is a symbol, the method given as symbol will be
|
126
|
+
called. Then you can add validations as you do in Active Record (`errors.add`).
|
127
127
|
|
128
|
-
*
|
128
|
+
* `:attachment` - When given, expects a file to be sent and attaches
|
129
129
|
it to the e-mail. Don't forget to set your form to multitype.
|
130
130
|
|
131
|
-
*
|
131
|
+
* `:captcha` - When true, validates the attributes must be blank.
|
132
132
|
This is a simple way to avoid spam and the input should be hidden with CSS.
|
133
133
|
|
134
134
|
Examples:
|
135
135
|
|
136
136
|
```ruby
|
137
137
|
class ContactForm < MailForm::Base
|
138
|
-
attributes :name,
|
139
|
-
attributes :email, :
|
140
|
-
attributes :type,
|
138
|
+
attributes :name, validate: true
|
139
|
+
attributes :email, validate: /\A[^@\s]+@[^@\s]+\z/i
|
140
|
+
attributes :type, validate: ["General", "Interface bug"]
|
141
141
|
attributes :message
|
142
|
-
attributes :screenshot, :
|
143
|
-
attributes :nickname,
|
142
|
+
attributes :screenshot, attachment: true, validate: :interface_bug?
|
143
|
+
attributes :nickname, captcha: true
|
144
144
|
|
145
145
|
def interface_bug?
|
146
146
|
if type == 'Interface bug' && screenshot.nil?
|
@@ -149,17 +149,17 @@ class ContactForm < MailForm::Base
|
|
149
149
|
end
|
150
150
|
end
|
151
151
|
|
152
|
-
c = ContactForm.new(:
|
153
|
-
c.valid?
|
154
|
-
c.spam?
|
152
|
+
c = ContactForm.new(nickname: 'not_blank', email: 'your@email.com', name: 'José')
|
153
|
+
c.valid? #=> true
|
154
|
+
c.spam? #=> true (raises an error in development, to remember you to hide it)
|
155
155
|
c.deliver #=> false (just delivers if is not a spam and is valid, raises an error in development)
|
156
156
|
|
157
|
-
c = ContactForm.new(:
|
157
|
+
c = ContactForm.new(email: 'invalid')
|
158
158
|
c.valid? #=> false
|
159
|
-
c.errors.inspect #=> { :
|
159
|
+
c.errors.inspect #=> { name: :blank, email: :invalid }
|
160
160
|
c.errors.full_messages #=> [ "Name can't be blank", "Email is invalid" ]
|
161
161
|
|
162
|
-
c = ContactForm.new(:
|
162
|
+
c = ContactForm.new(name: 'José', email: 'your@email.com')
|
163
163
|
c.deliver
|
164
164
|
```
|
165
165
|
|
@@ -208,8 +208,8 @@ mail_form:
|
|
208
208
|
|
209
209
|
## Custom e-mail template
|
210
210
|
|
211
|
-
To customize the e-mail template that is used create a file called contact.erb in app/views/mail_form
|
212
|
-
Take a look at lib/mail_form/views/mail_form/contact.erb in this repo to see how the default template works.
|
211
|
+
To customize the e-mail template that is used create a file called `contact.erb` in `app/views/mail_form`.
|
212
|
+
Take a look at `lib/mail_form/views/mail_form/contact.erb` in this repo to see how the default template works.
|
213
213
|
|
214
214
|
## Maintainers
|
215
215
|
|
@@ -224,4 +224,4 @@ Take a look at lib/mail_form/views/mail_form/contact.erb in this repo to see how
|
|
224
224
|
|
225
225
|
If you discover any bug, please use github issues tracker.
|
226
226
|
|
227
|
-
Copyright (c) 2009-
|
227
|
+
Copyright (c) 2009-2019 Plataformatec http://plataformatec.com.br/
|
@@ -5,7 +5,7 @@ module Rails
|
|
5
5
|
@_mail_form_source_root ||= File.expand_path("../templates", __FILE__)
|
6
6
|
end
|
7
7
|
|
8
|
-
argument :attributes, :
|
8
|
+
argument :attributes, type: :array, default: [], banner: "field:type field:type"
|
9
9
|
|
10
10
|
check_class_collision
|
11
11
|
|
data/lib/mail_form/delivery.rb
CHANGED
@@ -16,10 +16,10 @@ module MailForm
|
|
16
16
|
self.mail_appendable = []
|
17
17
|
|
18
18
|
if respond_to?(:before_deliver) && respond_to?(:after_deliver)
|
19
|
-
before_deliver :
|
19
|
+
before_deliver :check_not_spam
|
20
20
|
after_deliver :deliver!
|
21
21
|
else # For ActiveRecord compatibility
|
22
|
-
before_create :
|
22
|
+
before_create :check_not_spam
|
23
23
|
after_create :deliver!
|
24
24
|
alias :deliver :save
|
25
25
|
end
|
@@ -38,7 +38,7 @@ module MailForm
|
|
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).
|
@@ -52,12 +52,12 @@ module MailForm
|
|
52
52
|
# == Examples
|
53
53
|
#
|
54
54
|
# class ContactForm < MailForm
|
55
|
-
# attributes :name,
|
56
|
-
# attributes :email, :
|
57
|
-
# attributes :type,
|
55
|
+
# attributes :name, validate: true
|
56
|
+
# attributes :email, validate: /\A[^@\s]+@[^@\s]+\z/i
|
57
|
+
# attributes :type, validate: ["General", "Interface bug"]
|
58
58
|
# attributes :message
|
59
|
-
# attributes :screenshot, :
|
60
|
-
# attributes :nickname, :
|
59
|
+
# attributes :screenshot, attachment: true, validate: :interface_bug?
|
60
|
+
# attributes :nickname, captcha: true
|
61
61
|
#
|
62
62
|
# def interface_bug?
|
63
63
|
# if type == 'Interface bug' && screenshot.nil?
|
@@ -90,11 +90,11 @@ module MailForm
|
|
90
90
|
validate validation
|
91
91
|
break
|
92
92
|
when Regexp
|
93
|
-
validates_format_of accessor, :
|
93
|
+
validates_format_of accessor, with: validation, allow_blank: true
|
94
94
|
when Array
|
95
|
-
validates_inclusion_of accessor, :
|
95
|
+
validates_inclusion_of accessor, in: validation, allow_blank: true
|
96
96
|
when Range
|
97
|
-
validates_length_of accessor, :
|
97
|
+
validates_length_of accessor, within: validation, allow_blank: true
|
98
98
|
end
|
99
99
|
|
100
100
|
validates_presence_of accessor unless options[:allow_blank] == true
|
@@ -146,6 +146,11 @@ module MailForm
|
|
146
146
|
!spam?
|
147
147
|
end
|
148
148
|
|
149
|
+
def check_not_spam
|
150
|
+
throw(:abort) if spam?
|
151
|
+
end
|
152
|
+
private :check_not_spam
|
153
|
+
|
149
154
|
# Deliver the resource without running any validation.
|
150
155
|
def deliver!
|
151
156
|
mailer = MailForm::Notifier.contact(self)
|
data/lib/mail_form/shim.rb
CHANGED
@@ -23,7 +23,7 @@ module MailForm
|
|
23
23
|
end
|
24
24
|
|
25
25
|
# Initialize assigning the parameters given as hash.
|
26
|
-
def initialize(params={})
|
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?
|
data/lib/mail_form/version.rb
CHANGED
@@ -16,14 +16,14 @@
|
|
16
16
|
<% end %>
|
17
17
|
|
18
18
|
<% unless @resource.class.mail_appendable.blank? %>
|
19
|
-
<br /><h4 style="text-decoration:underline"><%= I18n.t :title, :
|
19
|
+
<br /><h4 style="text-decoration:underline"><%= I18n.t :title, scope: [ :mail_form, :request ], default: 'Request information' %></h4>
|
20
20
|
|
21
21
|
<% @resource.class.mail_appendable.each do |attribute|
|
22
22
|
value = @resource.request.send(attribute)
|
23
23
|
|
24
24
|
value = if value.is_a?(Hash) && !value.empty?
|
25
25
|
list = value.to_a.map{ |k,v| content_tag(:li, h("#{k}: #{v.inspect}")) }.join("\n")
|
26
|
-
content_tag(:ul, raw(list), :
|
26
|
+
content_tag(:ul, raw(list), style: "list-style:none;")
|
27
27
|
elsif value.is_a?(String)
|
28
28
|
value
|
29
29
|
else
|
@@ -31,7 +31,7 @@
|
|
31
31
|
end
|
32
32
|
%>
|
33
33
|
|
34
|
-
<p><b><%= I18n.t attribute, :
|
34
|
+
<p><b><%= I18n.t attribute, scope: [ :mail_form, :request ], default: attribute.to_s.humanize %>:</b>
|
35
35
|
<%= value.include?("\n") ? simple_format(value) : value %></p>
|
36
36
|
<% end %>
|
37
37
|
<br />
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class ActiveRecordTest < ActiveSupport::TestCase
|
6
|
+
def setup
|
7
|
+
ActionMailer::Base.deliveries = []
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_save_is_false_when_is_a_spam
|
11
|
+
form = ActiveRecordForm.new(name: 'Carlos', email: 'is.valid@email.com', nickname: 'not_blank')
|
12
|
+
assert form.valid?
|
13
|
+
assert form.spam?
|
14
|
+
assert !form.save
|
15
|
+
assert_empty ActionMailer::Base.deliveries
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_save_is_false_when_is_invalid
|
19
|
+
form = ActiveRecordForm.new(name: 'Carlos', email: 'is.com')
|
20
|
+
assert form.invalid?
|
21
|
+
assert form.not_spam?
|
22
|
+
assert !form.save
|
23
|
+
assert_empty ActionMailer::Base.deliveries
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_save_is_true_when_is_not_spam_and_valid
|
27
|
+
form = ActiveRecordForm.new(name: 'Carlos', email: 'is.valid@email.com')
|
28
|
+
assert form.valid?
|
29
|
+
assert form.not_spam?
|
30
|
+
assert form.save
|
31
|
+
assert_equal 1, ActionMailer::Base.deliveries.size
|
32
|
+
end
|
33
|
+
end
|
data/test/mail_form_test.rb
CHANGED
@@ -5,15 +5,21 @@ require 'test_helper'
|
|
5
5
|
class MailFormNotifierTest < ActiveSupport::TestCase
|
6
6
|
|
7
7
|
def setup
|
8
|
-
@form = ContactForm.new(:
|
9
|
-
|
10
|
-
@request
|
11
|
-
|
8
|
+
@form = ContactForm.new(name: 'José', email: 'my.email@my.domain.com', message: 'Cool')
|
9
|
+
|
10
|
+
@request = if ActionPack.respond_to?(:version) && ActionPack.version >= Gem::Version.new('5.1')
|
11
|
+
ActionController::TestRequest.create(Class.new) # Rails 5.1
|
12
|
+
elsif ActionPack.respond_to?(:version) && ActionPack.version >= Gem::Version.new('5.0')
|
13
|
+
ActionController::TestRequest.create # Rails 5
|
14
|
+
else
|
15
|
+
ActionController::TestRequest.new
|
16
|
+
end
|
17
|
+
@valid_attributes = { name: 'José', email: 'my.email@my.domain.com', message: "Cool\nno?" }
|
12
18
|
@advanced = AdvancedForm.new(@valid_attributes)
|
13
19
|
@advanced.request = @request
|
14
20
|
|
15
21
|
test_file = Rack::Test::UploadedFile.new(File.join(File.dirname(__FILE__), 'test_file.txt'))
|
16
|
-
@with_file = FileForm.new(:
|
22
|
+
@with_file = FileForm.new(name: 'José', email: 'my.email@my.domain.com', message: "Cool", file: test_file)
|
17
23
|
|
18
24
|
ActionMailer::Base.deliveries = []
|
19
25
|
end
|
@@ -48,7 +54,7 @@ class MailFormNotifierTest < ActiveSupport::TestCase
|
|
48
54
|
end
|
49
55
|
|
50
56
|
def test_body_contains_localized_attributes_names
|
51
|
-
I18n.backend.store_translations(:en, :
|
57
|
+
I18n.backend.store_translations(:en, mail_form: { attributes: { contact_form: { message: 'Sent message' } } })
|
52
58
|
@form.deliver
|
53
59
|
assert_match %r[Sent message:], first_delivery.body.to_s
|
54
60
|
assert_no_match %r[Message:], first_delivery.body.to_s
|
@@ -78,7 +84,7 @@ class MailFormNotifierTest < ActiveSupport::TestCase
|
|
78
84
|
end
|
79
85
|
|
80
86
|
def test_request_title_is_localized
|
81
|
-
I18n.backend.store_translations(:en, :
|
87
|
+
I18n.backend.store_translations(:en, mail_form: { request: { title: 'Information about the request' } })
|
82
88
|
@advanced.deliver
|
83
89
|
assert_no_match %r[Request information], last_delivery.body.to_s
|
84
90
|
assert_match %r[Information about the request], last_delivery.body.to_s
|
@@ -91,7 +97,7 @@ class MailFormNotifierTest < ActiveSupport::TestCase
|
|
91
97
|
end
|
92
98
|
|
93
99
|
def test_request_info_attributes_are_localized
|
94
|
-
I18n.backend.store_translations(:en, :
|
100
|
+
I18n.backend.store_translations(:en, mail_form: { request: { remote_ip: 'IP Address' } })
|
95
101
|
@advanced.deliver
|
96
102
|
assert_match %r[IP Address], last_delivery.body.to_s
|
97
103
|
assert_no_match %r[Remote ip], last_delivery.body.to_s
|
@@ -104,7 +110,7 @@ class MailFormNotifierTest < ActiveSupport::TestCase
|
|
104
110
|
end
|
105
111
|
|
106
112
|
def test_request_info_hashes_are_print_inside_lists
|
107
|
-
@request.session = { :
|
113
|
+
@request.session = { my: :session, user: "data" }
|
108
114
|
@advanced.deliver
|
109
115
|
assert_match %r[<ul], last_delivery.body.to_s
|
110
116
|
assert_match %r[<li>my: :session<\/li>], last_delivery.body.to_s
|
data/test/resource_test.rb
CHANGED
@@ -9,7 +9,7 @@ class MailFormBaseTest < ActiveSupport::TestCase
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def test_id_is_nil
|
12
|
-
|
12
|
+
assert_nil ContactForm.new.id
|
13
13
|
end
|
14
14
|
|
15
15
|
def test_is_always_a_new_record
|
@@ -17,13 +17,13 @@ class MailFormBaseTest < ActiveSupport::TestCase
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def test_initialize_with_options
|
20
|
-
form = ContactForm.new(:
|
20
|
+
form = ContactForm.new(name: 'José', email: 'jose@my.email.com')
|
21
21
|
assert_equal 'José', form.name
|
22
22
|
assert_equal 'jose@my.email.com', form.email
|
23
23
|
end
|
24
24
|
|
25
25
|
def test_spam_is_true_when_captcha_field_is_set
|
26
|
-
form = ContactForm.new(:
|
26
|
+
form = ContactForm.new(nickname: 'not_blank')
|
27
27
|
assert form.spam?
|
28
28
|
assert !form.not_spam?
|
29
29
|
end
|
@@ -45,7 +45,7 @@ class MailFormBaseTest < ActiveSupport::TestCase
|
|
45
45
|
end
|
46
46
|
|
47
47
|
def test_is_not_valid_when_validatable_regexp_does_not_match
|
48
|
-
form = ContactForm.new(:
|
48
|
+
form = ContactForm.new(name: 'Jose', email: 'not_valid')
|
49
49
|
assert !form.valid?
|
50
50
|
assert form.invalid?
|
51
51
|
|
@@ -54,7 +54,7 @@ class MailFormBaseTest < ActiveSupport::TestCase
|
|
54
54
|
end
|
55
55
|
|
56
56
|
def test_is_valid_when_validatable_attributes_are_valid
|
57
|
-
form = ContactForm.new(:
|
57
|
+
form = ContactForm.new(name: 'Jose', email: 'is.valid@email.com')
|
58
58
|
assert form.valid?
|
59
59
|
assert !form.invalid?
|
60
60
|
end
|
@@ -67,21 +67,23 @@ class MailFormBaseTest < ActiveSupport::TestCase
|
|
67
67
|
end
|
68
68
|
|
69
69
|
def test_deliver_is_false_when_is_a_spam
|
70
|
-
form = ContactForm.new(:
|
70
|
+
form = ContactForm.new(name: 'Jose', email: 'is.valid@email.com', nickname: 'not_blank')
|
71
71
|
assert form.valid?
|
72
72
|
assert form.spam?
|
73
73
|
assert !form.deliver
|
74
|
+
assert_empty ActionMailer::Base.deliveries
|
74
75
|
end
|
75
76
|
|
76
77
|
def test_deliver_is_false_when_is_invalid
|
77
|
-
form = ContactForm.new(:
|
78
|
+
form = ContactForm.new(name: 'Jose', email: 'is.com')
|
78
79
|
assert form.invalid?
|
79
80
|
assert form.not_spam?
|
80
81
|
assert !form.deliver
|
82
|
+
assert_empty ActionMailer::Base.deliveries
|
81
83
|
end
|
82
84
|
|
83
85
|
def test_deliver_is_true_when_is_not_spam_and_valid
|
84
|
-
form = ContactForm.new(:
|
86
|
+
form = ContactForm.new(name: 'Jose', email: 'is.valid@email.com')
|
85
87
|
assert form.valid?
|
86
88
|
assert form.not_spam?
|
87
89
|
assert form.deliver
|
@@ -93,7 +95,7 @@ class MailFormBaseTest < ActiveSupport::TestCase
|
|
93
95
|
end
|
94
96
|
|
95
97
|
def test_human_name_can_be_localized
|
96
|
-
I18n.backend.store_translations(:en, :
|
98
|
+
I18n.backend.store_translations(:en, mail_form: { models: { contact_form: 'Formulário de contato' } })
|
97
99
|
assert_equal 'Formulário de contato', ContactForm.model_name.human
|
98
100
|
end
|
99
101
|
|
@@ -102,12 +104,12 @@ class MailFormBaseTest < ActiveSupport::TestCase
|
|
102
104
|
end
|
103
105
|
|
104
106
|
def test_human_attribute_name_can_be_localized
|
105
|
-
I18n.backend.store_translations(:en, :
|
107
|
+
I18n.backend.store_translations(:en, mail_form: { attributes: { contact_form: { message: 'Mensagem' } } })
|
106
108
|
assert_equal 'Mensagem', ContactForm.human_attribute_name(:message)
|
107
109
|
end
|
108
110
|
|
109
111
|
def test_activemodel_linked_errors
|
110
|
-
form = ContactForm.new(:
|
112
|
+
form = ContactForm.new(email: 'not_valid', category: "invalid")
|
111
113
|
form.valid?
|
112
114
|
assert_equal ["can't be blank"], form.errors[:name]
|
113
115
|
assert_equal ["is invalid"], form.errors[:email]
|
@@ -116,13 +118,11 @@ class MailFormBaseTest < ActiveSupport::TestCase
|
|
116
118
|
end
|
117
119
|
|
118
120
|
def test_activemodel_errors_lookups_model_keys
|
119
|
-
I18n.backend.store_translations(:en, :
|
120
|
-
{ :
|
121
|
-
:name => { :blank => 'fill in the name' } }
|
122
|
-
}
|
121
|
+
I18n.backend.store_translations(:en, mail_form: { errors: { models: { contact_form:
|
122
|
+
{ attributes: { email: { invalid: 'fill in the email' }, name: { blank: 'fill in the name' } } }
|
123
123
|
}}})
|
124
124
|
|
125
|
-
form = ContactForm.new(:
|
125
|
+
form = ContactForm.new(email: 'not_valid')
|
126
126
|
form.valid?
|
127
127
|
|
128
128
|
assert_equal ["fill in the name"], form.errors[:name]
|
data/test/test_helper.rb
CHANGED
@@ -26,15 +26,15 @@ module Rails
|
|
26
26
|
end
|
27
27
|
|
28
28
|
class ContactForm < MailForm::Base
|
29
|
-
attribute :name, :
|
30
|
-
attribute :email, :
|
31
|
-
attribute :category, :
|
32
|
-
attribute :nickname, :
|
29
|
+
attribute :name, validate: true
|
30
|
+
attribute :email, validate: /\A[^@\s]+@[^@\s]+\z/i
|
31
|
+
attribute :category, validate: ["Interface bug", "General"], allow_blank: true
|
32
|
+
attribute :nickname, captcha: true
|
33
33
|
|
34
|
-
attributes :created_at, :message, :
|
34
|
+
attributes :created_at, :message, validate: :callback
|
35
35
|
|
36
36
|
def headers
|
37
|
-
{ :
|
37
|
+
{ to: 'my.email@my.domain.com' }
|
38
38
|
end
|
39
39
|
|
40
40
|
def initialize(*)
|
@@ -55,16 +55,16 @@ class AdvancedForm < ContactForm
|
|
55
55
|
append :remote_ip, :user_agent, :session
|
56
56
|
|
57
57
|
def headers
|
58
|
-
{ :
|
59
|
-
:
|
60
|
-
:
|
58
|
+
{ to: [ 'my.first@email.com', 'my.second@email.com' ],
|
59
|
+
subject: "My Advanced Form",
|
60
|
+
from: %{"#{name}" <#{email}>},
|
61
61
|
"return-path" => "mypath"
|
62
62
|
}
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
66
66
|
class FileForm < ContactForm
|
67
|
-
attribute :file, :
|
67
|
+
attribute :file, attachment: true, validate: true
|
68
68
|
|
69
69
|
def headers
|
70
70
|
to = if file
|
@@ -72,7 +72,36 @@ class FileForm < ContactForm
|
|
72
72
|
else
|
73
73
|
"contact@my.domain.com"
|
74
74
|
end
|
75
|
-
{ :
|
75
|
+
{ to: to }
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
class ActiveRecordForm
|
80
|
+
include ActiveModel::Model
|
81
|
+
|
82
|
+
# Simulate Active Record `*_create` callbacks.
|
83
|
+
extend ActiveModel::Callbacks
|
84
|
+
define_model_callbacks :create
|
85
|
+
|
86
|
+
def save
|
87
|
+
if valid?
|
88
|
+
run_callbacks(:create) { true }
|
89
|
+
else
|
90
|
+
false
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
include MailForm::Delivery
|
95
|
+
|
96
|
+
attribute :name, validate: true
|
97
|
+
attribute :email, validate: /\A[^@\s]+@[^@\s]+\z/i
|
98
|
+
attribute :nickname, captcha: true
|
99
|
+
|
100
|
+
def headers
|
101
|
+
{
|
102
|
+
to: "your.email@your.domain.com",
|
103
|
+
subject: "User created an account"
|
104
|
+
}
|
76
105
|
end
|
77
106
|
end
|
78
107
|
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mail_form
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- José Valim
|
8
8
|
- Carlos Antônio
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2020-09-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: actionmailer
|
@@ -17,40 +17,28 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - ">="
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version: '
|
21
|
-
- - "<"
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: '5'
|
20
|
+
version: '5.0'
|
24
21
|
type: :runtime
|
25
22
|
prerelease: false
|
26
23
|
version_requirements: !ruby/object:Gem::Requirement
|
27
24
|
requirements:
|
28
25
|
- - ">="
|
29
26
|
- !ruby/object:Gem::Version
|
30
|
-
version: '
|
31
|
-
- - "<"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '5'
|
27
|
+
version: '5.0'
|
34
28
|
- !ruby/object:Gem::Dependency
|
35
29
|
name: activemodel
|
36
30
|
requirement: !ruby/object:Gem::Requirement
|
37
31
|
requirements:
|
38
32
|
- - ">="
|
39
33
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
41
|
-
- - "<"
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
version: '5'
|
34
|
+
version: '5.0'
|
44
35
|
type: :runtime
|
45
36
|
prerelease: false
|
46
37
|
version_requirements: !ruby/object:Gem::Requirement
|
47
38
|
requirements:
|
48
39
|
- - ">="
|
49
40
|
- !ruby/object:Gem::Version
|
50
|
-
version: '
|
51
|
-
- - "<"
|
52
|
-
- !ruby/object:Gem::Version
|
53
|
-
version: '5'
|
41
|
+
version: '5.0'
|
54
42
|
description: Send e-mail straight from forms in Rails with I18n, validations, attachments
|
55
43
|
and request information.
|
56
44
|
email: contact@plataformatec.com.br
|
@@ -69,6 +57,7 @@ files:
|
|
69
57
|
- lib/mail_form/shim.rb
|
70
58
|
- lib/mail_form/version.rb
|
71
59
|
- lib/mail_form/views/mail_form/contact.erb
|
60
|
+
- test/active_record_test.rb
|
72
61
|
- test/mail_form_test.rb
|
73
62
|
- test/resource_test.rb
|
74
63
|
- test/test_file.txt
|
@@ -78,7 +67,7 @@ homepage: https://github.com/plataformatec/mail_form
|
|
78
67
|
licenses:
|
79
68
|
- MIT
|
80
69
|
metadata: {}
|
81
|
-
post_install_message:
|
70
|
+
post_install_message:
|
82
71
|
rdoc_options: []
|
83
72
|
require_paths:
|
84
73
|
- lib
|
@@ -86,21 +75,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
86
75
|
requirements:
|
87
76
|
- - ">="
|
88
77
|
- !ruby/object:Gem::Version
|
89
|
-
version:
|
78
|
+
version: 2.4.0
|
90
79
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
80
|
requirements:
|
92
81
|
- - ">="
|
93
82
|
- !ruby/object:Gem::Version
|
94
83
|
version: '0'
|
95
84
|
requirements: []
|
96
|
-
|
97
|
-
|
98
|
-
signing_key:
|
85
|
+
rubygems_version: 3.0.3
|
86
|
+
signing_key:
|
99
87
|
specification_version: 4
|
100
88
|
summary: Send e-mail straight from forms in Rails with I18n, validations, attachments
|
101
89
|
and request information.
|
102
90
|
test_files:
|
103
91
|
- test/mail_form_test.rb
|
92
|
+
- test/active_record_test.rb
|
104
93
|
- test/resource_test.rb
|
105
94
|
- test/test_file.txt
|
106
95
|
- test/test_helper.rb
|