mail_form 1.8.0 → 1.8.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fafb31d6fcba8c9d1412a17b15aa225963a0af0c27514218e33f7dc27bad71dd
4
- data.tar.gz: 3d780e1e5eaa74e3f6a17a3b1bae23edb1ba8c860fff72a94be084da0e76a648
3
+ metadata.gz: 4b953cb8f6f9b66bc2f50c2859dc7e0893bc303e886f843199fa20b84078343b
4
+ data.tar.gz: cbf5ada685154e561f5274d5dec52155cfcaa07cdf29d32033a4d35002e8275e
5
5
  SHA512:
6
- metadata.gz: 2578aee1b96544f3da889237bc00969a994913f5b59581e69940fc3c28c29b38a72283c57c0959d9cf238855a90ec9a40e0c8d2093efa2a32f129b4a03b7e5d8
7
- data.tar.gz: 5d119f7f61d22081be3a7c0c9356fce328823148dc841172180496d832635d8c21064497b5e6bcd3db0c58531cd2cfe5afcc2aee8d44a92b4afdc2235baefaaf
6
+ metadata.gz: dc1d9999ba9c420d17c325b2d8c0e5c813397bca394fe9ed0e97c12c1533df534be8b8df4617bc3519336f3f5644ee094e8c384e2250fac5ca1c2518bad8db54
7
+ data.tar.gz: aae1d4ced488183e71150fc2c428a326ee752714f3aded441ec27eab53526ce3befb5f43328b76c41fe565d19e5f66cf6e422d8b7f7482cae871c5060dcfa5c6
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  ## MailForm
2
2
 
3
3
  [![Gem Version](https://fury-badge.herokuapp.com/rb/mail_form.svg)](http://badge.fury.io/rb/mail_form)
4
- [![Build Status](https://api.travis-ci.org/plataformatec/mail_form.svg?branch=master)](http://travis-ci.org/plataformatec/mail_form)
5
- [![Code Climate](https://codeclimate.com/github/plataformatec/mail_form.svg)](https://codeclimate.com/github/plataformatec/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
7
  ### Rails 5
8
8
 
@@ -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, :validate => true
23
- attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
24
- attribute :file, :attachment => true
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, :captcha => true
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
- :subject => "My Contact Form",
34
- :to => "your.email@your.domain.com",
35
- :from => %("#{name}" <#{email}>)
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(:name => 'José', :email => 'jose@email.com', :message => 'Cool!')
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, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
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, :with => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
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
- :to => "your.email@your.domain.com",
88
- :subject => "User created an account"
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
- If you want it as plugin, just do:
104
+ Then run `bundle install` to install **MailForm**.
106
105
 
107
- `script/plugin install git://github.com/plataformatec/mail_form.git`
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
- * :validate - A hook to validates_*_of. When true is given, validates the
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 :validate is given, the presence is automatically checked. Give
123
- :allow_blank => true to override.
122
+ Whenever `:validate` is given, the presence is automatically checked. Give
123
+ `allow_blank: true` to override.
124
124
 
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 ActiveRecord (errors.add).
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
- * :attachment - When given, expects a file to be sent and attaches
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
- * :captcha - When true, validates the attributes must be blank.
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, :validate => true
139
- attributes :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
140
- attributes :type, :validate => ["General", "Interface bug"]
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, :attachment => true, :validate => :interface_bug?
143
- attributes :nickname, :captcha => true
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(: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)
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(:email => 'invalid')
157
+ c = ContactForm.new(email: 'invalid')
158
158
  c.valid? #=> false
159
- c.errors.inspect #=> { :name => :blank, :email => :invalid }
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(:name => 'José', :email => 'your@email.com')
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
 
@@ -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, :type => :array, :default => [], :banner => "field:type field:type"
8
+ argument :attributes, type: :array, default: [], banner: "field:type field:type"
9
9
 
10
10
  check_class_collision
11
11
 
@@ -4,6 +4,6 @@ class <%= class_name %> < MailForm::Base
4
4
  <% end -%>
5
5
 
6
6
  def headers
7
- { :to => "PLEASE-CHANGE-ME@example.org" }
7
+ { to: "PLEASE-CHANGE-ME@example.org" }
8
8
  end
9
- end
9
+ end
@@ -15,16 +15,11 @@ module MailForm
15
15
  class_attribute :mail_appendable
16
16
  self.mail_appendable = []
17
17
 
18
- # not_spam? simply returns false, but when used as a callback, we have to throw(:abort)
19
- # to halt the callback chain, beginning with Rails 5.1.
20
- if ActionPack.respond_to?(:version) && ActionPack.version >= Gem::Version.new('5.1')
18
+ if respond_to?(:before_deliver) && respond_to?(:after_deliver)
21
19
  before_deliver :check_not_spam
22
20
  after_deliver :deliver!
23
- elsif respond_to?(:before_deliver) && respond_to?(:after_deliver)
24
- before_deliver :not_spam?
25
- after_deliver :deliver!
26
21
  else # For ActiveRecord compatibility
27
- before_create :not_spam?
22
+ before_create :check_not_spam
28
23
  after_create :deliver!
29
24
  alias :deliver :save
30
25
  end
@@ -43,7 +38,7 @@ module MailForm
43
38
  # validates the inclusion of the attribute in the array.
44
39
  #
45
40
  # Whenever :validate is given, the presence is automatically checked. Give
46
- # :allow_blank => true to override.
41
+ # allow_blank: true to override.
47
42
  #
48
43
  # Finally, when :validate is a symbol, the method given as symbol will be
49
44
  # called. Then you can add validations as you do in ActiveRecord (errors.add).
@@ -57,12 +52,12 @@ module MailForm
57
52
  # == Examples
58
53
  #
59
54
  # class ContactForm < MailForm
60
- # attributes :name, :validate => true
61
- # attributes :email, :validate => /^([^@]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
62
- # attributes :type, :validate => ["General", "Interface bug"]
55
+ # attributes :name, validate: true
56
+ # attributes :email, validate: /\A[^@\s]+@[^@\s]+\z/i
57
+ # attributes :type, validate: ["General", "Interface bug"]
63
58
  # attributes :message
64
- # attributes :screenshot, :attachment => true, :validate => :interface_bug?
65
- # attributes :nickname, :captcha => true
59
+ # attributes :screenshot, attachment: true, validate: :interface_bug?
60
+ # attributes :nickname, captcha: true
66
61
  #
67
62
  # def interface_bug?
68
63
  # if type == 'Interface bug' && screenshot.nil?
@@ -95,11 +90,11 @@ module MailForm
95
90
  validate validation
96
91
  break
97
92
  when Regexp
98
- validates_format_of accessor, :with => validation, :allow_blank => true
93
+ validates_format_of accessor, with: validation, allow_blank: true
99
94
  when Array
100
- validates_inclusion_of accessor, :in => validation, :allow_blank => true
95
+ validates_inclusion_of accessor, in: validation, allow_blank: true
101
96
  when Range
102
- validates_length_of accessor, :within => validation, :allow_blank => true
97
+ validates_length_of accessor, within: validation, allow_blank: true
103
98
  end
104
99
 
105
100
  validates_presence_of accessor unless options[:allow_blank] == true
@@ -154,6 +149,7 @@ module MailForm
154
149
  def check_not_spam
155
150
  throw(:abort) if spam?
156
151
  end
152
+ private :check_not_spam
157
153
 
158
154
  # Deliver the resource without running any validation.
159
155
  def deliver!
@@ -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?
@@ -1,3 +1,3 @@
1
1
  module MailForm
2
- VERSION = "1.8.0".freeze
2
+ VERSION = "1.8.1".freeze
3
3
  end
@@ -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, :scope => [ :mail_form, :request ], :default => 'Request information' %></h4>
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), :style => "list-style:none;")
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, :scope => [ :mail_form, :request ], :default => attribute.to_s.humanize %>:</b>
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
@@ -5,7 +5,7 @@ require 'test_helper'
5
5
  class MailFormNotifierTest < ActiveSupport::TestCase
6
6
 
7
7
  def setup
8
- @form = ContactForm.new(:name => 'José', :email => 'my.email@my.domain.com', :message => 'Cool')
8
+ @form = ContactForm.new(name: 'José', email: 'my.email@my.domain.com', message: 'Cool')
9
9
 
10
10
  @request = if ActionPack.respond_to?(:version) && ActionPack.version >= Gem::Version.new('5.1')
11
11
  ActionController::TestRequest.create(Class.new) # Rails 5.1
@@ -14,12 +14,12 @@ class MailFormNotifierTest < ActiveSupport::TestCase
14
14
  else
15
15
  ActionController::TestRequest.new
16
16
  end
17
- @valid_attributes = { :name => 'José', :email => 'my.email@my.domain.com', :message => "Cool\nno?" }
17
+ @valid_attributes = { name: 'José', email: 'my.email@my.domain.com', message: "Cool\nno?" }
18
18
  @advanced = AdvancedForm.new(@valid_attributes)
19
19
  @advanced.request = @request
20
20
 
21
21
  test_file = Rack::Test::UploadedFile.new(File.join(File.dirname(__FILE__), 'test_file.txt'))
22
- @with_file = FileForm.new(:name => 'José', :email => 'my.email@my.domain.com', :message => "Cool", :file => test_file)
22
+ @with_file = FileForm.new(name: 'José', email: 'my.email@my.domain.com', message: "Cool", file: test_file)
23
23
 
24
24
  ActionMailer::Base.deliveries = []
25
25
  end
@@ -54,7 +54,7 @@ class MailFormNotifierTest < ActiveSupport::TestCase
54
54
  end
55
55
 
56
56
  def test_body_contains_localized_attributes_names
57
- I18n.backend.store_translations(:en, :mail_form => { :attributes => { :contact_form => { :message => 'Sent message' } } })
57
+ I18n.backend.store_translations(:en, mail_form: { attributes: { contact_form: { message: 'Sent message' } } })
58
58
  @form.deliver
59
59
  assert_match %r[Sent message:], first_delivery.body.to_s
60
60
  assert_no_match %r[Message:], first_delivery.body.to_s
@@ -84,7 +84,7 @@ class MailFormNotifierTest < ActiveSupport::TestCase
84
84
  end
85
85
 
86
86
  def test_request_title_is_localized
87
- I18n.backend.store_translations(:en, :mail_form => { :request => { :title => 'Information about the request' } })
87
+ I18n.backend.store_translations(:en, mail_form: { request: { title: 'Information about the request' } })
88
88
  @advanced.deliver
89
89
  assert_no_match %r[Request information], last_delivery.body.to_s
90
90
  assert_match %r[Information about the request], last_delivery.body.to_s
@@ -97,7 +97,7 @@ class MailFormNotifierTest < ActiveSupport::TestCase
97
97
  end
98
98
 
99
99
  def test_request_info_attributes_are_localized
100
- I18n.backend.store_translations(:en, :mail_form => { :request => { :remote_ip => 'IP Address' } })
100
+ I18n.backend.store_translations(:en, mail_form: { request: { remote_ip: 'IP Address' } })
101
101
  @advanced.deliver
102
102
  assert_match %r[IP Address], last_delivery.body.to_s
103
103
  assert_no_match %r[Remote ip], last_delivery.body.to_s
@@ -110,7 +110,7 @@ class MailFormNotifierTest < ActiveSupport::TestCase
110
110
  end
111
111
 
112
112
  def test_request_info_hashes_are_print_inside_lists
113
- @request.session = { :my => :session, :user => "data" }
113
+ @request.session = { my: :session, user: "data" }
114
114
  @advanced.deliver
115
115
  assert_match %r[<ul], last_delivery.body.to_s
116
116
  assert_match %r[<li>my: :session<\/li>], last_delivery.body.to_s
@@ -17,13 +17,13 @@ class MailFormBaseTest < ActiveSupport::TestCase
17
17
  end
18
18
 
19
19
  def test_initialize_with_options
20
- form = ContactForm.new(:name => 'José', :email => 'jose@my.email.com')
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(:nickname => 'not_blank')
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(:name => 'Jose', :email => 'not_valid')
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(:name => 'Jose', :email => 'is.valid@email.com')
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(:name => 'Jose', :email => 'is.valid@email.com', :nickname => 'not_blank')
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(:name => 'Jose', :email => 'is.com')
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(:name => 'Jose', :email => 'is.valid@email.com')
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, :mail_form => { :models => { :contact_form => 'Formulário de contato' } })
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, :mail_form => { :attributes => { :contact_form => { :message => 'Mensagem' } } })
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(:email => 'not_valid', :category => "invalid")
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, :mail_form => { :errors => { :models => { :contact_form =>
120
- { :attributes => { :email => { :invalid => 'fill in the email' },
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(:email => 'not_valid')
125
+ form = ContactForm.new(email: 'not_valid')
126
126
  form.valid?
127
127
 
128
128
  assert_equal ["fill in the name"], form.errors[:name]
@@ -26,15 +26,15 @@ module Rails
26
26
  end
27
27
 
28
28
  class ContactForm < MailForm::Base
29
- attribute :name, :validate => true
30
- attribute :email, :validate => /[^@]+@[^\.]+\.[\w\.\-]+/
31
- attribute :category, :validate => ["Interface bug", "General"], :allow_blank => true
32
- attribute :nickname, :captcha => true
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, :validate => :callback
34
+ attributes :created_at, :message, validate: :callback
35
35
 
36
36
  def headers
37
- { :to => 'my.email@my.domain.com' }
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
- { :to => [ 'my.first@email.com', 'my.second@email.com' ],
59
- :subject => "My Advanced Form",
60
- :from => %{"#{name}" <#{email}>},
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, :attachment => true, :validate => true
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
- { :to => to }
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.8.0
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: 2019-08-20 00:00:00.000000000 Z
12
+ date: 2020-09-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: actionmailer
@@ -57,6 +57,7 @@ files:
57
57
  - lib/mail_form/shim.rb
58
58
  - lib/mail_form/version.rb
59
59
  - lib/mail_form/views/mail_form/contact.erb
60
+ - test/active_record_test.rb
60
61
  - test/mail_form_test.rb
61
62
  - test/resource_test.rb
62
63
  - test/test_file.txt
@@ -66,7 +67,7 @@ homepage: https://github.com/plataformatec/mail_form
66
67
  licenses:
67
68
  - MIT
68
69
  metadata: {}
69
- post_install_message:
70
+ post_install_message:
70
71
  rdoc_options: []
71
72
  require_paths:
72
73
  - lib
@@ -81,14 +82,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
82
  - !ruby/object:Gem::Version
82
83
  version: '0'
83
84
  requirements: []
84
- rubyforge_project: mail_form
85
- rubygems_version: 2.7.6
86
- signing_key:
85
+ rubygems_version: 3.0.3
86
+ signing_key:
87
87
  specification_version: 4
88
88
  summary: Send e-mail straight from forms in Rails with I18n, validations, attachments
89
89
  and request information.
90
90
  test_files:
91
91
  - test/mail_form_test.rb
92
+ - test/active_record_test.rb
92
93
  - test/resource_test.rb
93
94
  - test/test_file.txt
94
95
  - test/test_helper.rb