mail_form 1.6.0 → 1.9.0

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
- SHA1:
3
- metadata.gz: 1d303b19e76c996f383cdd3cca96a6d1f5ebbf99
4
- data.tar.gz: 37b37ccd2e0f7d3e534d9985dc79f04cfbc1d4d3
2
+ SHA256:
3
+ metadata.gz: c29692d7133535b027bee364610e17dfa471a119d0970c327879ecf9c68a4196
4
+ data.tar.gz: 3305eaa30e4e23797a8f0f77cb1c8f429c23348be3c03cfdad22d22e744bea76
5
5
  SHA512:
6
- metadata.gz: cef061942f687c6d82678d7258d8360093efc67f8ff93dcd0721ffe788e3ee64f1f8817930b22ef8ab463e1292e96eecf0aab5b10d03d64109c333f9af9eda05
7
- data.tar.gz: aa04d2badd1f88698639de4ab99ccc7ce48d004a044662d29445a3341ee3aea0257a9c58f65affe72a9640b5ce274c8fe8e83bb2c6612ef85586db8cf20ec0b4
6
+ metadata.gz: e9fd29a8f0e1718418d20432be4c79b6b26f4bf006c55dc6648bda4611d317f2908d28c9a9f3c1658a291c91c51cf53735bd197c0797eaf76a1670b0a889378b
7
+ data.tar.gz: b0d94c74629678aa29830abb2b5b15f4ecf47fc9f4d6446e0ec71e3624ade342b4ee1e0a0a9977ee4ef8eba9ab230eac0a078fcafc0c2f647ac04bc507e5837f
@@ -1,4 +1,4 @@
1
- Copyright (c) 2009-2015 Plataformatec http://plataformatec.com.br/
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,14 @@
1
1
  ## MailForm
2
2
 
3
- [![Gem Version](https://fury-badge.herokuapp.com/rb/mail_form.png)](http://badge.fury.io/rb/mail_form)
4
- [![Build Status](https://api.travis-ci.org/plataformatec/mail_form.png?branch=master)](http://travis-ci.org/plataformatec/mail_form)
5
- [![Code Climate](https://codeclimate.com/github/plataformatec/mail_form.png)](https://codeclimate.com/github/plataformatec/mail_form)
3
+ [![Gem Version](https://fury-badge.herokuapp.com/rb/mail_form.svg)](http://badge.fury.io/rb/mail_form)
4
+ [![Code Climate](https://codeclimate.com/github/heartcombo/mail_form.svg)](https://codeclimate.com/github/heartcombo/mail_form)
6
5
 
7
- ### Rails 3
6
+ ### Rails 5
8
7
 
9
8
  This gem was built on top of `ActiveModel` to showcase how you can pull in validations, naming
10
9
  and `i18n` from Rails to your models without the need to implement it all by yourself.
11
10
 
12
- This README refers to the **MailForm** gem to be used in Rails 3.2 or 4+. For instructions
11
+ This README refers to the **MailForm** gem to be used in Rails 5+. For instructions
13
12
  on how to use MailForm in older versions of Rails, please refer to the available branches.
14
13
 
15
14
  ### Description
@@ -19,20 +18,20 @@ if you want to make a contact form just the following lines are needed (includin
19
18
 
20
19
  ```ruby
21
20
  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
21
+ attribute :name, validate: true
22
+ attribute :email, validate: /\A[^@\s]+@[^@\s]+\z/i
23
+ attribute :file, attachment: true
25
24
 
26
25
  attribute :message
27
- attribute :nickname, :captcha => true
26
+ attribute :nickname, captcha: true
28
27
 
29
28
  # Declare the e-mail headers. It accepts anything the mail method
30
29
  # in ActionMailer accepts.
31
30
  def headers
32
31
  {
33
- :subject => "My Contact Form",
34
- :to => "your.email@your.domain.com",
35
- :from => %("#{name}" <#{email}>)
32
+ subject: "My Contact Form",
33
+ to: "your.email@your.domain.com",
34
+ from: %("#{name}" <#{email}>)
36
35
  }
37
36
  end
38
37
  end
@@ -41,7 +40,7 @@ end
41
40
  Then you start a console with `rails console` and type:
42
41
 
43
42
  ```ruby
44
- >> c = ContactForm.new(:name => 'José', :email => 'jose@email.com', :message => 'Cool!')
43
+ >> c = ContactForm.new(name: 'José', email: 'jose@email.com', message: 'Cool!')
45
44
  >> c.deliver
46
45
  ```
47
46
 
@@ -57,14 +56,14 @@ This brings `I18n`, error messages, validations and attributes handling like in
57
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:
58
57
 
59
58
  ```ruby
60
- attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
59
+ attribute :email, validate: /\A[^@\s]+@[^@\s]+\z/i
61
60
  ```
62
61
 
63
62
  You could actually do this:
64
63
 
65
64
  ```ruby
66
65
  attribute :email
67
- validates_format_of :email, :with => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
66
+ validates_format_of :email, with: /\A[^@\s]+@[^@\s]+\z/i
68
67
  ```
69
68
 
70
69
  Choose the one which pleases you the most. For more information on the API, please
@@ -84,8 +83,8 @@ class User < ActiveRecord::Base
84
83
 
85
84
  def headers
86
85
  {
87
- :to => "your.email@your.domain.com",
88
- :subject => "User created an account"
86
+ to: "your.email@your.domain.com",
87
+ subject: "User created an account"
89
88
  }
90
89
  end
91
90
  end
@@ -100,11 +99,11 @@ Install **MailForm** is very easy. Just edit your Gemfile adding the following:
100
99
  ```ruby
101
100
  gem 'mail_form'
102
101
  ```
103
- Then run `bundle install` to install **MailForm**.
104
102
 
105
- If you want it as plugin, just do:
103
+ Then run `bundle install` to install **MailForm**.
106
104
 
107
- `script/plugin install git://github.com/plataformatec/mail_form.git`
105
+ You can run `rails generate mail_form` to view help information on how to generate
106
+ a basic form to get you started.
108
107
 
109
108
  ## API Overview
110
109
 
@@ -115,32 +114,32 @@ to the e-mail, except the ones :captcha is true.
115
114
 
116
115
  Options:
117
116
 
118
- * :validate - A hook to validates_*_of. When true is given, validates the
117
+ * `:validate` - A hook to `validates_*_of`. When `true` is given, validates the
119
118
  presence of the attribute. When a regexp, validates format. When array,
120
119
  validates the inclusion of the attribute in the array.
121
120
 
122
- Whenever :validate is given, the presence is automatically checked. Give
123
- :allow_blank => true to override.
121
+ Whenever `:validate` is given, the presence is automatically checked. Give
122
+ `allow_blank: true` to override.
124
123
 
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).
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 Active Record (`errors.add`).
127
126
 
128
- * :attachment - When given, expects a file to be sent and attaches
127
+ * `:attachment` - When given, expects a file to be sent and attaches
129
128
  it to the e-mail. Don't forget to set your form to multitype.
130
129
 
131
- * :captcha - When true, validates the attributes must be blank.
130
+ * `:captcha` - When true, validates the attributes must be blank.
132
131
  This is a simple way to avoid spam and the input should be hidden with CSS.
133
132
 
134
133
  Examples:
135
134
 
136
135
  ```ruby
137
136
  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"]
137
+ attributes :name, validate: true
138
+ attributes :email, validate: /\A[^@\s]+@[^@\s]+\z/i
139
+ attributes :type, validate: ["General", "Interface bug"]
141
140
  attributes :message
142
- attributes :screenshot, :attachment => true, :validate => :interface_bug?
143
- attributes :nickname, :captcha => true
141
+ attributes :screenshot, attachment: true, validate: :interface_bug?
142
+ attributes :nickname, captcha: true
144
143
 
145
144
  def interface_bug?
146
145
  if type == 'Interface bug' && screenshot.nil?
@@ -149,17 +148,17 @@ class ContactForm < MailForm::Base
149
148
  end
150
149
  end
151
150
 
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)
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)
155
154
  c.deliver #=> false (just delivers if is not a spam and is valid, raises an error in development)
156
155
 
157
- c = ContactForm.new(:email => 'invalid')
156
+ c = ContactForm.new(email: 'invalid')
158
157
  c.valid? #=> false
159
- c.errors.inspect #=> { :name => :blank, :email => :invalid }
158
+ c.errors.inspect #=> { name: :blank, email: :invalid }
160
159
  c.errors.full_messages #=> [ "Name can't be blank", "Email is invalid" ]
161
160
 
162
- c = ContactForm.new(:name => 'José', :email => 'your@email.com')
161
+ c = ContactForm.new(name: 'José', email: 'your@email.com')
163
162
  c.deliver
164
163
  ```
165
164
 
@@ -208,8 +207,8 @@ mail_form:
208
207
 
209
208
  ## Custom e-mail template
210
209
 
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.
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.
213
212
 
214
213
  ## Maintainers
215
214
 
@@ -220,8 +219,15 @@ Take a look at lib/mail_form/views/mail_form/contact.erb in this repo to see how
220
219
 
221
220
  * Andrew Timberlake - http://github.com/andrewtimberlake
222
221
 
222
+ ## Supported Ruby / Rails versions
223
+
224
+ We intend to maintain support for all Ruby / Rails versions that haven't reached end-of-life.
225
+
226
+ For more information about specific versions please check [Ruby](https://www.ruby-lang.org/en/downloads/branches/)
227
+ and [Rails](https://guides.rubyonrails.org/maintenance_policy.html) maintenance policies, and our test matrix.
228
+
223
229
  ## Bugs and Feedback
224
230
 
225
231
  If you discover any bug, please use github issues tracker.
226
232
 
227
- Copyright (c) 2009-2016 Plataformatec http://plataformatec.com.br/
233
+ 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, :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
@@ -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 :not_spam?
19
+ before_deliver :check_not_spam
20
20
  after_deliver :deliver!
21
21
  else # For ActiveRecord compatibility
22
- before_create :not_spam?
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
- # :allow_blank => true to override.
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, :validate => true
56
- # attributes :email, :validate => /^([^@]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
57
- # 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"]
58
58
  # attributes :message
59
- # attributes :screenshot, :attachment => true, :validate => :interface_bug?
60
- # attributes :nickname, :captcha => true
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, :with => validation, :allow_blank => true
93
+ validates_format_of accessor, with: validation, allow_blank: true
94
94
  when Array
95
- validates_inclusion_of accessor, :in => validation, :allow_blank => true
95
+ validates_inclusion_of accessor, in: validation, allow_blank: true
96
96
  when Range
97
- validates_length_of accessor, :within => validation, :allow_blank => true
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)
@@ -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.6.0".freeze
2
+ VERSION = "1.9.0".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,17 +5,21 @@ 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')
9
-
10
- ActionController::TestRequest.respond_to?(:create) ?
11
- @request = ActionController::TestRequest.create :
12
- @request = ActionController::TestRequest.new
13
- @valid_attributes = { :name => 'José', :email => 'my.email@my.domain.com', :message => "Cool\nno?" }
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?" }
14
18
  @advanced = AdvancedForm.new(@valid_attributes)
15
19
  @advanced.request = @request
16
20
 
17
21
  test_file = Rack::Test::UploadedFile.new(File.join(File.dirname(__FILE__), 'test_file.txt'))
18
- @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)
19
23
 
20
24
  ActionMailer::Base.deliveries = []
21
25
  end
@@ -50,7 +54,7 @@ class MailFormNotifierTest < ActiveSupport::TestCase
50
54
  end
51
55
 
52
56
  def test_body_contains_localized_attributes_names
53
- 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' } } })
54
58
  @form.deliver
55
59
  assert_match %r[Sent message:], first_delivery.body.to_s
56
60
  assert_no_match %r[Message:], first_delivery.body.to_s
@@ -80,7 +84,7 @@ class MailFormNotifierTest < ActiveSupport::TestCase
80
84
  end
81
85
 
82
86
  def test_request_title_is_localized
83
- 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' } })
84
88
  @advanced.deliver
85
89
  assert_no_match %r[Request information], last_delivery.body.to_s
86
90
  assert_match %r[Information about the request], last_delivery.body.to_s
@@ -93,7 +97,7 @@ class MailFormNotifierTest < ActiveSupport::TestCase
93
97
  end
94
98
 
95
99
  def test_request_info_attributes_are_localized
96
- 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' } })
97
101
  @advanced.deliver
98
102
  assert_match %r[IP Address], last_delivery.body.to_s
99
103
  assert_no_match %r[Remote ip], last_delivery.body.to_s
@@ -106,7 +110,7 @@ class MailFormNotifierTest < ActiveSupport::TestCase
106
110
  end
107
111
 
108
112
  def test_request_info_hashes_are_print_inside_lists
109
- @request.session = { :my => :session, :user => "data" }
113
+ @request.session = { my: :session, user: "data" }
110
114
  @advanced.deliver
111
115
  assert_match %r[<ul], last_delivery.body.to_s
112
116
  assert_match %r[<li>my: :session<\/li>], last_delivery.body.to_s
@@ -9,7 +9,7 @@ class MailFormBaseTest < ActiveSupport::TestCase
9
9
  end
10
10
 
11
11
  def test_id_is_nil
12
- assert_equal nil, ContactForm.new.id
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(: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.6.0
4
+ version: 1.9.0
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: 2016-10-31 00:00:00.000000000 Z
12
+ date: 2021-01-27 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: '3.2'
21
- - - "<"
22
- - !ruby/object:Gem::Version
23
- version: '5.1'
20
+ version: '5.2'
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: '3.2'
31
- - - "<"
32
- - !ruby/object:Gem::Version
33
- version: '5.1'
27
+ version: '5.2'
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: '3.2'
41
- - - "<"
42
- - !ruby/object:Gem::Version
43
- version: '5.1'
34
+ version: '5.2'
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: '3.2'
51
- - - "<"
52
- - !ruby/object:Gem::Version
53
- version: '5.1'
41
+ version: '5.2'
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,23 +75,22 @@ required_ruby_version: !ruby/object:Gem::Requirement
86
75
  requirements:
87
76
  - - ">="
88
77
  - !ruby/object:Gem::Version
89
- version: '0'
78
+ version: 2.5.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
- rubyforge_project: mail_form
97
- rubygems_version: 2.5.1
98
- signing_key:
85
+ rubygems_version: 3.2.6
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
107
96
  - test/views/mail_form/custom_template.erb
108
- has_rdoc: