mail_form 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +105 -51
- data/Rakefile +1 -0
- data/lib/mail_form.rb +48 -32
- data/lib/mail_form/base.rb +6 -124
- data/lib/mail_form/delivery.rb +239 -0
- data/lib/mail_form/shim.rb +50 -0
- data/lib/mail_form/version.rb +1 -1
- data/lib/views/mail_form/contact.erb +38 -0
- data/test/mail_form_test.rb +190 -0
- data/test/{base_test.rb → resource_test.rb} +31 -7
- data/test/test_helper.rb +25 -8
- metadata +7 -9
- data/lib/mail_form/dsl.rb +0 -171
- data/lib/mail_form/errors.rb +0 -59
- data/lib/mail_form/notifier.rb +0 -48
- data/test/errors_test.rb +0 -85
- data/test/notifier_test.rb +0 -183
- data/views/mail_form/notifier/contact.erb +0 -30
data/test/errors_test.rb
DELETED
@@ -1,85 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/test_helper'
|
2
|
-
|
3
|
-
class MailFormErrorsTest < ActiveSupport::TestCase
|
4
|
-
|
5
|
-
def test_errors_respond_to_some_hash_methods
|
6
|
-
assert ContactForm.new.errors.respond_to?(:each)
|
7
|
-
assert ContactForm.new.errors.respond_to?(:each_pair)
|
8
|
-
assert ContactForm.new.errors.respond_to?(:size)
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_count_is_an_alias_to_size
|
12
|
-
errors = ContactForm.new.errors
|
13
|
-
assert_equal errors.size, errors.count
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_on_returns_the_message_in_the_given_attribute
|
17
|
-
form = ContactForm.new(:email => 'not_valid')
|
18
|
-
form.valid?
|
19
|
-
assert_equal "can't be blank", form.errors.on(:name)
|
20
|
-
assert_equal "is invalid", form.errors.on(:email)
|
21
|
-
assert_equal nil, form.errors.on(:message)
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_on_returns_a_default_localized_message_in_the_given_attribute
|
25
|
-
I18n.backend.store_translations(:en, :mail_form => { :messages => { :invalid => 'is not valid', :blank => 'should be filled' } })
|
26
|
-
|
27
|
-
form = ContactForm.new(:email => 'not_valid')
|
28
|
-
form.valid?
|
29
|
-
|
30
|
-
assert_equal "should be filled", form.errors.on(:name)
|
31
|
-
assert_equal "is not valid", form.errors.on(:email)
|
32
|
-
assert_equal nil, form.errors.on(:message)
|
33
|
-
end
|
34
|
-
|
35
|
-
def test_on_returns_an_attribute_localized_message_in_the_given_attribute
|
36
|
-
I18n.backend.store_translations(:en, :mail_form => { :messages => { :email => 'fill in the email', :name => 'fill in the name' } })
|
37
|
-
|
38
|
-
form = ContactForm.new(:email => 'not_valid')
|
39
|
-
form.valid?
|
40
|
-
|
41
|
-
assert_equal "fill in the name", form.errors.on(:name)
|
42
|
-
assert_equal "fill in the email", form.errors.on(:email)
|
43
|
-
assert_equal nil, form.errors.on(:message)
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_array_like_option_acts_as_an_alias_for_on
|
47
|
-
form = ContactForm.new(:email => 'not_valid')
|
48
|
-
form.valid?
|
49
|
-
assert_equal "can't be blank", form.errors[:name]
|
50
|
-
assert_equal form.errors.on(:name), form.errors[:name]
|
51
|
-
assert_equal "is invalid", form.errors[:email]
|
52
|
-
assert_equal form.errors.on(:email), form.errors[:email]
|
53
|
-
assert_equal nil, form.errors[:message]
|
54
|
-
end
|
55
|
-
|
56
|
-
def test_get_returns_the_real_value_in_the_given_attribute
|
57
|
-
form = ContactForm.new(:email => 'not_valid')
|
58
|
-
form.valid?
|
59
|
-
assert_equal :blank, form.errors.get(:name)
|
60
|
-
assert_equal :invalid, form.errors.get(:email)
|
61
|
-
assert_equal nil, form.errors.get(:message)
|
62
|
-
end
|
63
|
-
|
64
|
-
def test_full_messages
|
65
|
-
form = ContactForm.new(:email => 'not_valid')
|
66
|
-
form.valid?
|
67
|
-
|
68
|
-
assert form.errors.full_messages.include?("Name can't be blank")
|
69
|
-
assert form.errors.full_messages.include?("Email is invalid")
|
70
|
-
end
|
71
|
-
|
72
|
-
def test_full_localized_messages
|
73
|
-
I18n.backend.store_translations(:en, :mail_form => { :messages => { :email => 'is not valid', :blank => 'should be filled' }, :attributes => { :email => 'E-mail' } })
|
74
|
-
|
75
|
-
form = ContactForm.new(:email => 'not_valid')
|
76
|
-
form.valid?
|
77
|
-
|
78
|
-
assert form.errors.full_messages.include?("Name should be filled")
|
79
|
-
assert form.errors.full_messages.include?("E-mail is not valid")
|
80
|
-
end
|
81
|
-
|
82
|
-
def teardown
|
83
|
-
I18n.reload!
|
84
|
-
end
|
85
|
-
end
|
data/test/notifier_test.rb
DELETED
@@ -1,183 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/test_helper'
|
2
|
-
|
3
|
-
class MailFormNotifierTest < ActiveSupport::TestCase
|
4
|
-
|
5
|
-
def setup
|
6
|
-
@form = ContactForm.new(:name => 'José', :email => 'my.email@my.domain.com', :message => 'Cool')
|
7
|
-
|
8
|
-
@request = ActionController::TestRequest.new
|
9
|
-
@valid_attributes = { :name => 'José', :email => 'my.email@my.domain.com', :message => "Cool\nno?" }
|
10
|
-
@advanced = AdvancedForm.new(@valid_attributes, @request)
|
11
|
-
|
12
|
-
test_file = ActionController::TestUploadedFile.new(File.join(File.dirname(__FILE__), 'test_file.txt'))
|
13
|
-
@with_file = FileForm.new(:name => 'José', :email => 'my.email@my.domain.com', :message => "Cool", :file => test_file)
|
14
|
-
|
15
|
-
@template = TemplateForm.new(@valid_attributes)
|
16
|
-
|
17
|
-
ActionMailer::Base.deliveries = []
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_email_is_sent
|
21
|
-
@form.deliver
|
22
|
-
assert_equal 1, ActionMailer::Base.deliveries.size
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_subject_defaults_to_class_human_name
|
26
|
-
@form.deliver
|
27
|
-
assert_equal 'Contact form', ActionMailer::Base.deliveries.first.subject
|
28
|
-
end
|
29
|
-
|
30
|
-
def test_subject_is_a_string
|
31
|
-
@advanced.deliver
|
32
|
-
assert_equal 'My Advanced Form', ActionMailer::Base.deliveries.first.subject
|
33
|
-
end
|
34
|
-
|
35
|
-
def test_sender_defaults_to_form_email
|
36
|
-
@form.deliver
|
37
|
-
assert_equal ['my.email@my.domain.com'], ActionMailer::Base.deliveries.first.from
|
38
|
-
end
|
39
|
-
|
40
|
-
def test_error_is_raised_when_recipients_is_nil
|
41
|
-
assert_raise ScriptError do
|
42
|
-
NullRecipient.new.deliver
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_recipients_is_a_string
|
47
|
-
@form.deliver
|
48
|
-
assert_equal ['my.email@my.domain.com'], ActionMailer::Base.deliveries.first.to
|
49
|
-
end
|
50
|
-
|
51
|
-
def test_recipients_is_an_array
|
52
|
-
@advanced.deliver
|
53
|
-
assert_equal ['my.first@email.com', 'my.second@email.com'], ActionMailer::Base.deliveries.first.to
|
54
|
-
end
|
55
|
-
|
56
|
-
def test_recipients_is_a_symbold
|
57
|
-
@with_file.deliver
|
58
|
-
assert_equal ['contact_file@my.domain.com'], ActionMailer::Base.deliveries.first.to
|
59
|
-
end
|
60
|
-
|
61
|
-
def test_headers_is_a_hash
|
62
|
-
@advanced.deliver
|
63
|
-
assert_equal '<mypath>', ActionMailer::Base.deliveries.first.header['return-path'].to_s
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_body_contains_subject
|
67
|
-
@form.deliver
|
68
|
-
assert_match /Contact form/, ActionMailer::Base.deliveries.first.body
|
69
|
-
end
|
70
|
-
|
71
|
-
def test_body_contains_attributes_values
|
72
|
-
@form.deliver
|
73
|
-
assert_match /José/, ActionMailer::Base.deliveries.first.body
|
74
|
-
assert_match /my.email@my.domain.com/, ActionMailer::Base.deliveries.first.body
|
75
|
-
assert_match /Cool/, ActionMailer::Base.deliveries.first.body
|
76
|
-
end
|
77
|
-
|
78
|
-
def test_body_contains_attributes_names
|
79
|
-
@form.deliver
|
80
|
-
assert_match /Name:/, ActionMailer::Base.deliveries.first.body
|
81
|
-
assert_match /Email:/, ActionMailer::Base.deliveries.first.body
|
82
|
-
assert_match /Message:/, ActionMailer::Base.deliveries.first.body
|
83
|
-
end
|
84
|
-
|
85
|
-
def test_body_contains_localized_attributes_names
|
86
|
-
I18n.backend.store_translations(:en, :mail_form => { :attributes => { :message => 'Sent message' } })
|
87
|
-
@form.deliver
|
88
|
-
assert_match /Sent message:/, ActionMailer::Base.deliveries.first.body
|
89
|
-
assert_no_match /Message:/, ActionMailer::Base.deliveries.first.body
|
90
|
-
end
|
91
|
-
|
92
|
-
def test_body_mail_format_messages_with_break_line
|
93
|
-
@form.deliver
|
94
|
-
assert_no_match /<p>Cool/, ActionMailer::Base.deliveries.first.body
|
95
|
-
|
96
|
-
@advanced.deliver
|
97
|
-
assert_match /<p>Cool/, ActionMailer::Base.deliveries.last.body
|
98
|
-
end
|
99
|
-
|
100
|
-
def test_body_does_not_append_request_if_append_is_not_called
|
101
|
-
@form.deliver
|
102
|
-
assert_no_match /Request information/, ActionMailer::Base.deliveries.first.body
|
103
|
-
end
|
104
|
-
|
105
|
-
def test_body_does_append_request_if_append_is_called
|
106
|
-
@advanced.deliver
|
107
|
-
assert_match /Request information/, ActionMailer::Base.deliveries.last.body
|
108
|
-
end
|
109
|
-
|
110
|
-
def test_request_title_is_localized
|
111
|
-
I18n.backend.store_translations(:en, :mail_form => { :request => { :title => 'Information about the request' } })
|
112
|
-
@advanced.deliver
|
113
|
-
assert_no_match /Request information/, ActionMailer::Base.deliveries.last.body
|
114
|
-
assert_match /Information about the request/, ActionMailer::Base.deliveries.last.body
|
115
|
-
end
|
116
|
-
|
117
|
-
def test_request_info_attributes_are_printed
|
118
|
-
@advanced.deliver
|
119
|
-
assert_match /Remote ip/, ActionMailer::Base.deliveries.last.body
|
120
|
-
assert_match /User agent/, ActionMailer::Base.deliveries.last.body
|
121
|
-
end
|
122
|
-
|
123
|
-
def test_request_info_attributes_are_localized
|
124
|
-
I18n.backend.store_translations(:en, :mail_form => { :request => { :remote_ip => 'IP Address' } })
|
125
|
-
@advanced.deliver
|
126
|
-
assert_match /IP Address/, ActionMailer::Base.deliveries.last.body
|
127
|
-
assert_no_match /Remote ip/, ActionMailer::Base.deliveries.last.body
|
128
|
-
end
|
129
|
-
|
130
|
-
def test_request_info_values_are_printed
|
131
|
-
@advanced.deliver
|
132
|
-
assert_match /0\.0\.0\.0/, ActionMailer::Base.deliveries.last.body
|
133
|
-
assert_match /Rails Testing/, ActionMailer::Base.deliveries.last.body
|
134
|
-
end
|
135
|
-
|
136
|
-
def test_request_info_hashes_are_print_inside_lis
|
137
|
-
@request.session = { :my => :session, :user => "data" }
|
138
|
-
@advanced.deliver
|
139
|
-
assert_match /<li>my: :session<\/li>/, ActionMailer::Base.deliveries.last.body
|
140
|
-
assert_match /<li>user: "data"<\/li>/, ActionMailer::Base.deliveries.last.body
|
141
|
-
end
|
142
|
-
|
143
|
-
def test_error_is_raised_when_append_is_given_but_no_request_is_given
|
144
|
-
assert_raise ScriptError do
|
145
|
-
@advanced.request = nil
|
146
|
-
@advanced.deliver
|
147
|
-
end
|
148
|
-
end
|
149
|
-
|
150
|
-
def test_form_with_file_includes_an_attachment
|
151
|
-
@with_file.deliver
|
152
|
-
|
153
|
-
#For some reason I need to encode the mail before the attachments array returns values
|
154
|
-
ActionMailer::Base.deliveries.first.to_s
|
155
|
-
assert_equal 1, ActionMailer::Base.deliveries.first.attachments.size
|
156
|
-
end
|
157
|
-
|
158
|
-
def test_form_with_file_does_not_output_attachment_as_attribute
|
159
|
-
@with_file.deliver
|
160
|
-
assert_no_match /File/, ActionMailer::Base.deliveries.first.body
|
161
|
-
end
|
162
|
-
|
163
|
-
def test_form_with_customized_template_raise_missing_template_if_not_found
|
164
|
-
assert_raise ActionView::MissingTemplate do
|
165
|
-
@template.deliver
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
def test_form_with_customized_template_render_correct_template
|
170
|
-
begin
|
171
|
-
default_template_root = MailForm::Notifier.template_root
|
172
|
-
MailForm::Notifier.template_root = File.join(File.dirname(__FILE__), 'views')
|
173
|
-
@template.deliver
|
174
|
-
assert_match 'Hello from my cystom template!', ActionMailer::Base.deliveries.last.body
|
175
|
-
ensure
|
176
|
-
MailForm::Notifier.template_root = default_template_root
|
177
|
-
end
|
178
|
-
end
|
179
|
-
|
180
|
-
def teardown
|
181
|
-
I18n.reload!
|
182
|
-
end
|
183
|
-
end
|
@@ -1,30 +0,0 @@
|
|
1
|
-
<h4 style="text-decoration:underline"><%=h @subject %></h4>
|
2
|
-
|
3
|
-
<% @form.class.form_attributes.each do |attribute|
|
4
|
-
value = @form.send(attribute)
|
5
|
-
next if value.blank? %>
|
6
|
-
|
7
|
-
<p><b><%= @form.class.human_attribute_name(attribute) %>:</b>
|
8
|
-
<%= value.include?("\n") ? simple_format(h(value)) : h(value) %></p>
|
9
|
-
<% end %>
|
10
|
-
|
11
|
-
<% unless @form.class.form_appendable.blank? %>
|
12
|
-
<br /><h4 style="text-decoration:underline"><%= I18n.t :title, :scope => [ :mail_form, :request ], :default => 'Request information' %></h4>
|
13
|
-
|
14
|
-
<% @form.class.form_appendable.each do |attribute|
|
15
|
-
value = @form.request.send(attribute)
|
16
|
-
|
17
|
-
value = if value.is_a?(Hash) && !value.empty?
|
18
|
-
content_tag(:ul, value.to_a.map{|k,v| content_tag(:li, h("#{k}: #{v.inspect}")) }.join("\n"), :style => "list-style:none;")
|
19
|
-
elsif value.is_a?(String)
|
20
|
-
h(value)
|
21
|
-
else
|
22
|
-
h(value.inspect)
|
23
|
-
end
|
24
|
-
%>
|
25
|
-
|
26
|
-
<p><b><%= I18n.t attribute, :scope => [ :mail_form, :request ], :default => attribute.to_s.humanize %>:</b>
|
27
|
-
<%= value.include?("\n") ? simple_format(value) : value %></p>
|
28
|
-
<% end %>
|
29
|
-
<br />
|
30
|
-
<% end %>
|