Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Tests running without deprecations.

  • Loading branch information...
commit 35a7cf5858a6f240f112bce6ec527c9913569769 1 parent 0a2cf99
@josevalim josevalim authored
View
1  Rakefile
@@ -5,6 +5,7 @@ require File.join(File.dirname(__FILE__), "lib", "mail_form", "version")
desc 'Run tests for MailForm.'
Rake::TestTask.new(:test) do |t|
+ t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end
View
1  init.rb
@@ -0,0 +1 @@
+require 'mail_form'
View
10 lib/mail_form.rb
@@ -4,9 +4,9 @@ class MailForm < ActionMailer::Base
autoload :Delivery, 'mail_form/delivery'
autoload :Shim, 'mail_form/shim'
- self.template_root = File.expand_path('../views', File.dirname(__FILE__))
+ append_view_path File.expand_path('../views', __FILE__)
@Sutto
Sutto added a note

With the rails 3 beta, just putting mail_form in the Gemfile breaks from this line (namely because when it's required Rails hasn't started etc); I'm happy to submit a patch but I just wanted to check if there was a reason behind this?

@josevalim Owner

A patch would be welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
- def default(resource)
+ def contact(resource)
@from = get_from_class_and_eval(resource, :mail_sender)
@subject = get_from_class_and_eval(resource, :mail_subject)
@recipients = get_from_class_and_eval(resource, :mail_recipients)
@@ -28,11 +28,7 @@ def default(resource)
resource.class.mail_attachments.each do |attribute|
value = resource.send(attribute)
next unless value.respond_to?(:read)
-
- attachment value.content_type.to_s do |att|
- att.filename = value.original_filename
- att.body = value.read
- end
+ attachments[value.original_filename] = value.read
end
end
View
6 lib/mail_form/delivery.rb
@@ -18,7 +18,7 @@ module MailForm::Delivery
headers({})
sender {|c| c.email }
subject{|c| c.class.model_name.human }
- template 'default'
+ template 'contact'
before_create :not_spam?
after_create :deliver!
@@ -218,7 +218,7 @@ def spam?
mail_captcha.each do |field|
next if send(field).blank?
- if RAILS_ENV == 'development'
+ if defined?(Rails) && Rails.env.development?
raise ScriptError, "The captcha field #{field} was supposed to be blank"
else
return true
@@ -234,6 +234,6 @@ def not_spam?
# Deliver the resource without checking any condition.
def deliver!
- MailForm.deliver_default(self)
+ MailForm.contact(self).deliver
end
end
View
0  views/mail_form/default.erb → lib/views/mail_form/contact.erb
File renamed without changes
View
51 mail_form.gemspec
@@ -0,0 +1,51 @@
+# Generated by jeweler
+# DO NOT EDIT THIS FILE DIRECTLY
+# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{mail_form}
+ s.version = "1.1.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Jos\303\251 Valim", "Carlos Ant\303\264nio"]
+ s.date = %q{2010-02-07}
+ s.description = %q{Send e-mail straight from forms in Rails with I18n, validations, attachments and request information.}
+ s.email = %q{contact@plataformatec.com.br}
+ s.extra_rdoc_files = [
+ "README.rdoc"
+ ]
+ s.files = [
+ "CHANGELOG",
+ "MIT-LICENSE",
+ "README.rdoc",
+ "Rakefile",
+ "lib/mail_form.rb",
+ "lib/mail_form/base.rb",
+ "lib/mail_form/delivery.rb",
+ "lib/mail_form/shim.rb",
+ "lib/mail_form/version.rb",
+ "lib/views/mail_form/contact.erb"
+ ]
+ s.homepage = %q{http://github.com/plataformatec/mail_form}
+ s.rdoc_options = ["--charset=UTF-8"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.5}
+ s.summary = %q{Send e-mail straight from forms in Rails with I18n, validations, attachments and request information.}
+ s.test_files = [
+ "test/mail_form_test.rb",
+ "test/resource_test.rb",
+ "test/test_helper.rb"
+ ]
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ else
+ end
+ else
+ end
+end
+
View
97 test/mail_form_test.rb
@@ -1,4 +1,4 @@
-require File.dirname(__FILE__) + '/test_helper'
+require 'test_helper'
class MailFormNotifierTest < ActiveSupport::TestCase
@@ -25,17 +25,17 @@ def test_email_is_sent
def test_subject_defaults_to_class_human_name
@form.deliver
- assert_equal 'Contact form', ActionMailer::Base.deliveries.first.subject
+ assert_equal 'Contact form', first_delivery.subject
end
def test_subject_is_a_string
@advanced.deliver
- assert_equal 'My Advanced Form', ActionMailer::Base.deliveries.first.subject
+ assert_equal 'My Advanced Form', first_delivery.subject
end
def test_sender_defaults_to_form_email
@form.deliver
- assert_equal 'my.email@my.domain.com', ActionMailer::Base.deliveries.first.from
+ assert_equal ['my.email@my.domain.com'], first_delivery.from
end
def test_error_is_raised_when_recipients_is_nil
@@ -46,104 +46,104 @@ def test_error_is_raised_when_recipients_is_nil
def test_recipients_is_a_string
@form.deliver
- assert_equal 'my.email@my.domain.com', ActionMailer::Base.deliveries.first.to
+ assert_equal ['my.email@my.domain.com'], first_delivery.to
end
def test_recipients_is_an_array
@advanced.deliver
- assert_equal ['my.first@email.com', 'my.second@email.com'], ActionMailer::Base.deliveries.first.to
+ assert_equal ['my.first@email.com', 'my.second@email.com'], first_delivery.to
end
def test_recipients_is_a_symbold
@with_file.deliver
- assert_equal ['contact_file@my.domain.com'], ActionMailer::Base.deliveries.first.to
+ assert_equal ['contact_file@my.domain.com'], first_delivery.to
end
def test_headers_is_a_hash
@advanced.deliver
- assert_equal 'mypath', ActionMailer::Base.deliveries.first.header['return-path'].to_s
+ assert_equal 'mypath', first_delivery.header['return-path'].to_s
end
def test_body_contains_subject
@form.deliver
- assert_match /Contact form/, ActionMailer::Base.deliveries.first.body
+ assert_match /Contact form/, first_delivery.body.to_s
end
def test_body_contains_attributes_values
@form.deliver
- assert_match /José/, ActionMailer::Base.deliveries.first.body
- assert_match /my.email@my.domain.com/, ActionMailer::Base.deliveries.first.body
- assert_match /Cool/, ActionMailer::Base.deliveries.first.body
+ assert_match /José/, first_delivery.body.to_s
+ assert_match /my.email@my.domain.com/, first_delivery.body.to_s
+ assert_match /Cool/, first_delivery.body.to_s
end
def test_body_contains_attributes_names
@form.deliver
- assert_match /Name:/, ActionMailer::Base.deliveries.first.body
- assert_match /Email:/, ActionMailer::Base.deliveries.first.body
- assert_match /Message:/, ActionMailer::Base.deliveries.first.body
+ assert_match /Name:/, first_delivery.body.to_s
+ assert_match /Email:/, first_delivery.body.to_s
+ assert_match /Message:/, first_delivery.body.to_s
end
def test_body_contains_localized_attributes_names
I18n.backend.store_translations(:en, :mail_form => { :attributes => { :contact_form => { :message => 'Sent message' } } })
@form.deliver
- assert_match /Sent message:/, ActionMailer::Base.deliveries.first.body
- assert_no_match /Message:/, ActionMailer::Base.deliveries.first.body
+ assert_match /Sent message:/, first_delivery.body.to_s
+ assert_no_match /Message:/, first_delivery.body.to_s
end
def test_body_mail_format_messages_with_break_line
@form.deliver
- assert_no_match /<p>Cool/, ActionMailer::Base.deliveries.first.body
+ assert_no_match /<p>Cool/, first_delivery.body.to_s
@advanced.deliver
- assert_match /<p>Cool/, ActionMailer::Base.deliveries.last.body
+ assert_match /<p>Cool/, last_delivery.body.to_s
end
def test_body_mail_format_dates_with_i18n
@form.deliver
- assert_no_match /I18n.l(Date.today)/, ActionMailer::Base.deliveries.first.body
+ assert_no_match /I18n.l(Date.today)/, first_delivery.body.to_s
end
def test_body_does_not_append_request_if_append_is_not_called
@form.deliver
- assert_no_match /Request information/, ActionMailer::Base.deliveries.first.body
+ assert_no_match /Request information/, first_delivery.body.to_s
end
def test_body_does_append_request_if_append_is_called
@advanced.deliver
- assert_match /Request information/, ActionMailer::Base.deliveries.last.body
+ assert_match /Request information/, last_delivery.body.to_s
end
def test_request_title_is_localized
I18n.backend.store_translations(:en, :mail_form => { :request => { :title => 'Information about the request' } })
@advanced.deliver
- assert_no_match /Request information/, ActionMailer::Base.deliveries.last.body
- assert_match /Information about the request/, ActionMailer::Base.deliveries.last.body
+ assert_no_match /Request information/, last_delivery.body.to_s
+ assert_match /Information about the request/, last_delivery.body.to_s
end
def test_request_info_attributes_are_printed
@advanced.deliver
- assert_match /Remote ip/, ActionMailer::Base.deliveries.last.body
- assert_match /User agent/, ActionMailer::Base.deliveries.last.body
+ assert_match /Remote ip/, last_delivery.body.to_s
+ assert_match /User agent/, last_delivery.body.to_s
end
def test_request_info_attributes_are_localized
I18n.backend.store_translations(:en, :mail_form => { :request => { :remote_ip => 'IP Address' } })
@advanced.deliver
- assert_match /IP Address/, ActionMailer::Base.deliveries.last.body
- assert_no_match /Remote ip/, ActionMailer::Base.deliveries.last.body
+ assert_match /IP Address/, last_delivery.body.to_s
+ assert_no_match /Remote ip/, last_delivery.body.to_s
end
def test_request_info_values_are_printed
@advanced.deliver
- assert_match /0\.0\.0\.0/, ActionMailer::Base.deliveries.last.body
- assert_match /Rails Testing/, ActionMailer::Base.deliveries.last.body
+ assert_match /0\.0\.0\.0/, last_delivery.body.to_s
+ assert_match /Rails Testing/, last_delivery.body.to_s
end
def test_request_info_hashes_are_print_inside_lis
@request.session = { :my => :session, :user => "data" }
@advanced.deliver
- assert_match /<li>my: :session<\/li>/, ActionMailer::Base.deliveries.last.body
- assert_match /<li>user: &quot;data&quot;<\/li>/, ActionMailer::Base.deliveries.last.body
+ assert_match /<li>my: :session<\/li>/, last_delivery.body.to_s
+ assert_match /<li>user: &quot;data&quot;<\/li>/, last_delivery.body.to_s
end
def test_error_is_raised_when_append_is_given_but_no_request_is_given
@@ -155,29 +155,36 @@ def test_error_is_raised_when_append_is_given_but_no_request_is_given
def test_form_with_file_includes_an_attachment
@with_file.deliver
-
- #For some reason I need to encode the mail before the attachments array returns values
- ActionMailer::Base.deliveries.first.to_s
- assert_equal 1, ActionMailer::Base.deliveries.first.attachments.size
+ assert_equal 1, first_delivery.attachments.size
end
def test_form_with_file_does_not_output_attachment_as_attribute
@with_file.deliver
- assert_no_match /File:/, ActionMailer::Base.deliveries.first.body
+ assert_no_match /File:/, first_delivery.body.to_s
end
def test_form_with_customized_template_render_correct_template
begin
- default_template_root = MailForm.template_root
- MailForm.template_root = File.join(File.dirname(__FILE__), 'views')
+ previous_view_path = MailForm.view_paths
+ MailForm.prepend_view_path File.join(File.dirname(__FILE__), 'views')
@template.deliver
- assert_match 'Hello from my cystom template!', ActionMailer::Base.deliveries.last.body
+ assert_match 'Hello from my custom template!', last_delivery.body.to_s
ensure
- MailForm.template_root = default_template_root
+ MailForm.view_paths = previous_view_path
end
end
- def teardown
- I18n.reload!
- end
-end
+ protected
+
+ def first_delivery
+ ActionMailer::Base.deliveries.first
+ end
+
+ def last_delivery
+ ActionMailer::Base.deliveries.last
+ end
+
+ def teardown
+ I18n.reload!
+ end
+end
View
6 test/resource_test.rb
@@ -1,4 +1,4 @@
-require File.dirname(__FILE__) + '/test_helper'
+require 'test_helper'
class MailFormBaseTest < ActiveSupport::TestCase
@@ -114,11 +114,11 @@ def test_activemodel_linked_errors
end
def test_activemodel_errors_lookups_model_keys
- I18n.backend.store_translations(:en, :errors => { :models => { :contact_form =>
+ I18n.backend.store_translations(:en, :mail_form => { :errors => { :models => { :contact_form =>
{ :attributes => { :email => { :invalid => 'fill in the email' },
:name => { :blank => 'fill in the name' } }
}
- }})
+ }}})
form = ContactForm.new(:email => 'not_valid')
form.valid?
View
15 test/test_helper.rb
@@ -1,14 +1,17 @@
-require 'test/unit'
-RAILS_ENV = ENV["RAILS_ENV"] = "test"
+require 'rubygems'
+
+begin
+ require 'test/unit'
+rescue LoadError
+end
-# This should point to a Rails 3 master checkout
-# git://github.com/rails/rails.git
-require File.expand_path(File.dirname(__FILE__) + "/../../rails/vendor/gems/environment")
+RAILS_ENV = ENV["RAILS_ENV"] = "test"
require 'active_support'
require 'active_support/test_case'
-require 'action_mailer'
+require 'action_controller'
require 'action_controller/test_case'
+require 'action_mailer'
ActionMailer::Base.delivery_method = :test
View
2  test/views/mail_form/custom_template.erb
@@ -1 +1 @@
-Hello from my cystom template!
+Hello from my custom template!
@Sutto

With the rails 3 beta, just putting mail_form in the Gemfile breaks from this line (namely because when it's required Rails hasn't started etc); I'm happy to submit a patch but I just wanted to check if there was a reason behind this?

@josevalim

A patch would be welcome!

Please sign in to comment.
Something went wrong with that request. Please try again.