Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

First commit.

  • Loading branch information...
commit e38d34e4e8f62d625a39ff7cfb008981ba783902 0 parents
@josevalim josevalim authored
3  CHANGELOG
@@ -0,0 +1,3 @@
+# Version 0.1
+
+* First release
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2009 José Valim
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
143 README
@@ -0,0 +1,143 @@
+Simple Form
+License: MIT
+Version: 0.1
+
+You can also read this README in pretty html at the GitHub project Wiki page:
+
+ http://wiki.github.com/josevalim/simple_form/
+
+Description
+-----------
+
+Simple Form is a ready to go contact form. Just the following lines are needed
+to have a contact form (including the e-mail):
+
+ class ContactForm < SimpleForm
+ subject "My Contact Form"
+ recipients "your.email@your.domain.com"
+ sender{|c| %{"#{c.name}" <#{c.email}>} }
+
+ attribute :name, :validate => true
+ attribute :email, :validate => /[^@]+@[^\.]+\.[\w\.\-]+/
+ attribute :company_name
+ attribute :telephone
+ attribute :message, :validate => true
+ attribute :nickname, :captcha => true
+ end
+
+Then you start a script/console and type:
+
+ c = ContactForm.new(:name => 'José', :email => 'jose@email.com', :message => 'Cool!')
+ c.deliver
+
+Check your inbox and the e-mail will be there, with the sent fields.
+
+SimpleForm is tested and compatible with Rails 2.2.x and Rails 2.3.x. It also
+supports I18n and error messages, just as an ActiveRecord model does.
+
+Installation
+------------
+
+Install Simple Form is very easy. It is stored in GitHub, so just run the following:
+
+ gem sources -a http://gems.github.com
+ sudo gem install josevalim-simple_form
+
+If you want it as plugin, just do:
+
+ script/plugin install git://github.com/josevalim/simple_form.git
+
+API Overview
+------------
+
+== attributes(*attributes)
+
+Declare your form attributes. All attributes declared here will be appended
+to the e-mail, except the ones :captcha is true.
+
+Options:
+
+* :validate - When true, validates the attributes can't be blank.
+ When a regexp is given, check if the attribute matches is not blank and
+ then if it matches the regexp.
+
+* :captcha - When true, validates the attributes must be blank.
+ This is a simple way to avoid spam and the input should be hidden with CSS.
+
+Examples:
+
+ class ContactForm < SimpleForm
+ attributes :name, :validate => true
+ attributes :email, :validate => /[^@]+@[^\.]+\.[\w\.\-]+/
+ attributes :message
+ attributes :nickname, :captcha => true
+ end
+
+ c = ContactForm.new(:nickname => 'not_blank', :email => 'your@email.com', :name => 'José')
+ c.valid? #=> true
+ c.spam? #=> true (raises an error in development, to remember you to hide it)
+ c.deliver #=> false (just delivers if is not a spam and is valid)
+
+ c = ContactForm.new(:email => 'invalid')
+ c.valid? #=> false
+ c.errors.inspect #=> { :name => :blank, :email => :invalid }
+ c.errors.full_messages #=> [ "Name can't be blank", "Email is invalid" ]
+
+ c = ContactForm.new(:name => 'José', :email => 'your@email.com')
+ # save is an alias to deliver to allow it to work with InheritedResources
+ c.save #=> true
+
+== subject(string=nil, &block)
+
+Declares the subject of the contact email. It can be a string or a proc.
+As a proc, it receives a simple form instance. When not specified, it defaults
+to the class human name.
+
+ subject "My Contact Form"
+ subject {|c| "Contacted by #{c.name}"}
+
+== sender(&block)
+
+Declares contact email sender. It can be a string or a proc.
+As a proc, it receives a simple form instance. By default is:
+
+ sender{ |c| c.email }
+
+This requires that your SimpleForm object have at least an email attribute.
+
+== headers(hash)
+
+Additional headers to your e-mail.
+
+== recipients(string_or_array)
+
+Who will receive the e-mail. Can be a string or array.
+
+I18n
+----
+
+All models, attributes and messages in SimpleForm can be used with localized.
+Below is an I18n example file:
+
+ simple_form:
+ models:
+ contact_form: Formulário de contato
+ attributes:
+ name: Nome
+ email: E-mail
+ company_name: Empresa
+ telephone: Telefone
+ message: Mensagem
+ messages:
+ blank: "não pode ficar em branco"
+ invalid: "não é válido"
+ telephone: "deve haver oito dígitos"
+
+Bugs and Feedback
+-----------------
+
+If you discover any bugs, please send an e-mail to jose.valim@gmail.com
+If you just want to give some positive feedback or drop a line, that's fine too!
+
+Copyright (c) 2009 José Valim
+http://josevalim.blogspot.com/
19 Rakefile
@@ -0,0 +1,19 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Run tests for InheritedResources.'
+Rake::TestTask.new(:test) do |t|
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for InheritedResources.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'SimpleForm'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('MIT-LICENSE')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
1  init.rb
@@ -0,0 +1 @@
+File.join(File.dirname(__FILE__), 'lib', 'simple_form')
29 lib/simple_form.rb
@@ -0,0 +1,29 @@
+dir = File.dirname(__FILE__)
+require File.join(dir, 'simple_form', 'base')
+require File.join(dir, 'simple_form', 'dsl')
+require File.join(dir, 'simple_form', 'errors')
+require File.join(dir, 'simple_form', 'notifier')
+
+class SimpleForm
+ extend SimpleForm::DSL
+
+ ACCESSORS = [ :form_attributes, :form_validatable, :form_subject,
+ :form_recipients, :form_sender, :form_captcha, :form_headers ]
+
+ DEFAULT_MESSAGES = { :blank => "can't be blank", :invalid => "is invalid" }
+
+ class_inheritable_reader *ACCESSORS
+ protected *ACCESSORS
+
+ # Configure default values
+ #
+ attribute :captcha => true
+ attribute :validate => true
+
+ headers({})
+ recipients([])
+ sender{|c| c.email }
+ subject{|c| c.class.human_name }
+end
+
+SimpleForm::Notifier.template_root = File.join(dir, '..', 'views')
113 lib/simple_form/base.rb
@@ -0,0 +1,113 @@
+class SimpleForm
+
+ # Initialize assigning the parameters given as hash (just as in ActiveRecord).
+ #
+ def initialize(params={})
+ params.each_pair do |attr, value|
+ self.send(:"#{attr}=", value)
+ end unless params.blank?
+ end
+
+ # In development, raises an error if the captcha field is not blank. This is
+ # is good to remember that the field should be hidden with CSS and shown only
+ # to robots.
+ #
+ # In test and in production, it returns true if aall captcha field are blank,
+ # returns false otherwise.
+ #
+ def spam?
+ form_captcha.each do |field|
+ next if send(field).blank?
+
+ if RAILS_ENV == 'development'
+ raise ScriptError, "The captcha field #{field} was supposed to be blank"
+ else
+ return true
+ end
+ end
+
+ return false
+ end
+
+ def not_spam?
+ !spam?
+ end
+
+ # The form is valid if all elements marked to be validated are not blank
+ # and elements given with a regexp match the regexp.
+ #
+ def valid?
+ return false unless errors.empty?
+
+ form_validatable.each_pair do |field, validation|
+ if send(field).blank?
+ errors.add(field, :blank)
+ next
+ end
+
+ errors.add(field, :invalid) if validation.is_a?(Regexp) && send(field) !~ validation
+ end
+
+ errors.empty?
+ end
+
+ def invalid?
+ !valid?
+ end
+
+ # Always return true so when using form_for, the default method will be post.
+ #
+ def new_record?
+ true
+ end
+
+ # Always return nil so when using form_for, the default method will be post.
+ #
+ def id
+ nil
+ end
+
+ # If is not spam and the form is valid, we send the e-mail and returns true.
+ # Otherwise returns false.
+ #
+ def deliver
+ if self.not_spam? && self.valid?
+ SimpleForm::Notifier.deliver_contact(self)
+ return true
+ else
+ return false
+ end
+ end
+ alias :save :deliver
+
+ # Add a human attribute name interface on top of I18n. If email is received as
+ # attribute, it will look for a translated name on:
+ #
+ # simple_form:
+ # attributes:
+ # email: E-mail
+ #
+ def self.human_attribute_name(attribute, options={})
+ I18n.translate("attributes.#{attribute}", options.merge(:default => attribute.to_s.humanize, :scope => [:simple_form]))
+ end
+
+ # Add a human name interface on top of I18n. If you have a model named
+ # SimpleForm, it will search for the localized name on:
+ #
+ # simple_form:
+ # models:
+ # contact_form: Contact form
+ #
+ def self.human_name(options={})
+ underscored = self.name.demodulize.underscore
+ I18n.translate("models.#{underscored}", options.merge(:default => underscored.humanize, :scope => [:simple_form]))
+ end
+
+ # Return the errors in this form. The object returned as the same API as the
+ # ActiveRecord one.
+ #
+ def errors
+ @errors ||= SimpleForm::Errors.new(self)
+ end
+
+end
105 lib/simple_form/dsl.rb
@@ -0,0 +1,105 @@
+class SimpleForm
+ module DSL
+
+ protected
+
+ # Declare your form attributes. All attributes declared here will be appended
+ # to the e-mail, except the ones captcha is true.
+ #
+ # == Options
+ #
+ # * <tt>:validate</tt> - When true, validates the attributes can't be blank.
+ # When a regexp is given, check if the attribute matches is not blank and
+ # then if it matches the regexp.
+ # * <tt>:captcha</tt> - When true, validates the attributes must be blank
+ # This is a simple way to avoid spam
+ #
+ # == Examples
+ #
+ # class ContactForm < SimpleForm
+ # attributes :name, :validate => true
+ # attributes :email, :validate => /^([^@]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
+ # attributes :message
+ # attributes :nickname, :captcha => true
+ # end
+ #
+ def attribute(*accessors)
+ options = accessors.extract_options!
+
+ attr_accessor *accessors
+
+ if options[:captcha]
+ write_inheritable_array(:form_captcha, accessors)
+ else
+ write_inheritable_array(:form_attributes, accessors)
+ end
+
+ if options[:validate]
+ validations = {}
+ accessors.each{ |a| validations[a] = options[:validate] }
+
+ write_inheritable_hash(:form_validatable, validations)
+ end
+ end
+ alias :attributes :attribute
+
+ # Declares the subject of the contact email. It can be a string or a proc.
+ # As a proc, it receives a simple form instance. When not specified, it
+ # defaults to the class human name.
+ #
+ # == Examples
+ #
+ # class ContactForm < SimpleForm
+ # subject "My Contact Form"
+ # end
+ #
+ def subject(string=nil, &block)
+ write_inheritable_attribute(:form_subject, string || block)
+ end
+
+ # Declares contact email sender. It can be a string or a proc.
+ # As a proc, it receives a simple form instance. By default is:
+ #
+ # sender{ |c| c.email }
+ #
+ # This requires that your SimpleForm object have at least an email attribute.
+ #
+ # == Examples
+ #
+ # class ContactForm < SimpleForm
+ # # Change sender to include also the name
+ # sender{|c| %{"#{c.name}" <#{c.email}>} }
+ # end
+ #
+ def sender(string=nil, &block)
+ write_inheritable_attribute(:form_sender, string || block)
+ end
+ alias :from :sender
+
+ # Additional headers to your e-mail.
+ #
+ # == Examples
+ #
+ # class ContactForm < SimpleForm
+ # headers { :content_type => 'text/html' }
+ # end
+ #
+ def headers(hash)
+ write_inheritable_hash(:form_headers, hash)
+ end
+
+ # Who will receive the e-mail. Can be a string or array.
+ #
+ # == Examples
+ #
+ # class ContactForm < SimpleForm
+ # recipients "jose.valim@gmail.com"
+ # end
+ #
+ def recipients(string_or_array)
+ write_inheritable_array(:form_recipients, [*string_or_array])
+ end
+ alias :to :recipients
+
+ end
+end
57 lib/simple_form/errors.rb
@@ -0,0 +1,57 @@
+# Provides an Errors class similar with ActiveRecord ones.
+#
+# class ContactForm < SimpleForm
+# attributes :name, :validate => true
+# attributes :email, :validate => /^([^@]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
+# attributes :message
+# attributes :nickname, :captcha => true
+# end
+#
+# When validating an attribute name as above, it will search for messages in
+# the following order:
+#
+# simple_form.messages.name
+# simple_form.messages.blank
+#
+# When validating email, it will search for:
+#
+# simple_form.messages.name
+# simple_form.messages.invalid
+#
+# If the message is not available, it will output: "can't be blank" in the first
+# case and "is invalid" in the second.
+#
+class SimpleForm
+ class Errors < Hash
+
+ def initialize(base, *args)
+ @base = base
+ super(*args)
+ end
+
+ alias :add :store
+ alias :count :size
+
+ def on(attribute)
+ attribute = attribute.to_sym
+ return nil unless self[attribute]
+
+ generate_message_for(attribute, self[attribute])
+ end
+
+ def full_messages
+ map do |attribute, message|
+ next if message.nil?
+ attribute = attribute.to_sym
+ "#{@base.class.human_attribute_name(attribute)} #{generate_message_for(attribute, message)}"
+ end.compact.reverse
+ end
+
+ protected
+
+ def generate_message_for(attribute, message)
+ I18n.t(attribute, :default => [ message, DEFAULT_MESSAGES[message] ], :scope => [:simple_form, :messages])
+ end
+
+ end
+end
24 lib/simple_form/notifier.rb
@@ -0,0 +1,24 @@
+# This is the class responsable to send the e-mails.
+#
+class SimpleForm
+ class Notifier < ActionMailer::Base
+ def contact(form)
+ @subject = form.class.form_subject
+ @subject = @subject.call(form) if @subject.is_a?(Proc)
+
+ @from = form.class.form_sender
+ @from = @from.call(form) if @from.is_a?(Proc)
+
+ @recipients = form.class.form_recipients
+
+ raise ScriptError, "You forgot to setup #{form.class.name} recipients" if @recipients.blank?
+
+ @body['form'] = form
+ @body['subject'] = @subject
+
+ @sent_on = Time.now.utc
+ @headers = form.class.form_headers
+ @content_type = 'text/html'
+ end
+ end
+end
103 test/base_test.rb
@@ -0,0 +1,103 @@
+require File.dirname(__FILE__) + '/test_helper'
+
+class SimpleFormBaseTest < ActiveSupport::TestCase
+
+ def setup
+ ActionMailer::Base.deliveries = []
+ end
+
+ def test_id_is_nil
+ assert_equal nil, ContactForm.new.id
+ end
+
+ def test_is_always_a_new_record
+ assert ContactForm.new.new_record?
+ end
+
+ def test_initialize_with_options
+ form = ContactForm.new(:name => 'José', :email => 'jose@my.email.com')
+ assert_equal 'José', form.name
+ assert_equal 'jose@my.email.com', form.email
+ end
+
+ def test_spam_is_true_when_captcha_field_is_set
+ form = ContactForm.new(:nickname => 'not_blank')
+ assert form.spam?
+ assert !form.not_spam?
+ end
+
+ def test_spam_is_false_when_captcha_field_is_not_set
+ form = ContactForm.new
+ assert !form.spam?
+ assert form.not_spam?
+ end
+
+ def test_is_not_valid_when_validatable_attributes_are_blank
+ form = ContactForm.new
+ assert !form.valid?
+ assert form.invalid?
+
+ assert_equal(2, form.errors.count)
+ assert_equal({:email=>:blank, :name=>:blank}, form.errors)
+ end
+
+ def test_is_not_valid_when_validatable_regexp_does_not_match
+ form = ContactForm.new(:name => 'Jose', :email => 'not_valid')
+ assert !form.valid?
+ assert form.invalid?
+
+ assert_equal(1, form.errors.count)
+ assert_equal({:email=>:invalid}, form.errors)
+ end
+
+ def test_is_valid_when_validatable_attributes_are_valid
+ form = ContactForm.new(:name => 'Jose', :email => 'is.valid@email.com')
+ assert form.valid?
+ assert !form.invalid?
+ end
+
+ def test_deliver_is_false_when_is_a_spam
+ form = ContactForm.new(:name => 'Jose', :email => 'is.valid@email.com', :nickname => 'not_blank')
+ assert form.valid?
+ assert form.spam?
+ assert !form.deliver
+ end
+
+ def test_deliver_is_false_when_is_invalid
+ form = ContactForm.new(:name => 'Jose', :email => 'is.com')
+ assert form.invalid?
+ assert form.not_spam?
+ assert !form.deliver
+ end
+
+ def test_deliver_is_true_when_is_not_spam_and_valid
+ form = ContactForm.new(:name => 'Jose', :email => 'is.valid@email.com')
+ assert form.valid?
+ assert form.not_spam?
+ assert form.deliver
+ assert_equal 1, ActionMailer::Base.deliveries.size
+ end
+
+ def test_human_name_returns_a_humanized_name
+ assert_equal 'Contact form', ContactForm.human_name
+ end
+
+ def test_human_name_can_be_localized
+ I18n.backend.store_translations(:en, :simple_form => { :models => { :contact_form => 'Formulário de contato' } })
+ assert_equal 'Formulário de contato', ContactForm.human_name
+ end
+
+ def test_human_attribute_name_returns_a_humanized_attribute
+ assert_equal 'Message', ContactForm.human_attribute_name(:message)
+ end
+
+ def test_human_attribute_name_can_be_localized
+ I18n.backend.store_translations(:en, :simple_form => { :attributes => { :message => 'Mensagem' } })
+ assert_equal 'Mensagem', ContactForm.human_attribute_name(:message)
+ end
+
+ def teardown
+ I18n.reload!
+ end
+
+end
65 test/errors_test.rb
@@ -0,0 +1,65 @@
+require File.dirname(__FILE__) + '/test_helper'
+
+class SimpleFormErrorsTest < ActiveSupport::TestCase
+
+ def test_errors_respond_to_some_hash_methods
+ assert ContactForm.new.errors.respond_to?(:each)
+ assert ContactForm.new.errors.respond_to?(:each_pair)
+ assert ContactForm.new.errors.respond_to?(:size)
+ end
+
+ def test_count_is_an_alias_to_size
+ errors = ContactForm.new.errors
+ assert_equal errors.size, errors.count
+ end
+
+ def test_on_returns_the_message_in_the_given_attribute
+ form = ContactForm.new(:email => 'not_valid')
+ form.valid?
+ assert_equal "can't be blank", form.errors.on(:name)
+ assert_equal "is invalid", form.errors.on(:email)
+ assert_equal nil, form.errors.on(:message)
+ end
+
+ def test_on_returns_a_default_localized_message_in_the_given_attribute
+ I18n.backend.store_translations(:en, :simple_form => { :messages => { :invalid => 'is not valid', :blank => 'should be filled' } })
+
+ form = ContactForm.new(:email => 'not_valid')
+ form.valid?
+
+ assert_equal "should be filled", form.errors.on(:name)
+ assert_equal "is not valid", form.errors.on(:email)
+ assert_equal nil, form.errors.on(:message)
+ end
+
+ def test_on_returns_an_attribute_localized_message_in_the_given_attribute
+ I18n.backend.store_translations(:en, :simple_form => { :messages => { :email => 'fill in the email', :name => 'fill in the name' } })
+
+ form = ContactForm.new(:email => 'not_valid')
+ form.valid?
+
+ assert_equal "fill in the name", form.errors.on(:name)
+ assert_equal "fill in the email", form.errors.on(:email)
+ assert_equal nil, form.errors.on(:message)
+ end
+
+ def test_full_messages
+ form = ContactForm.new(:email => 'not_valid')
+ form.valid?
+
+ assert_equal ["Name can't be blank", "Email is invalid"], form.errors.full_messages
+ end
+
+ def test_full_localized_messages
+ I18n.backend.store_translations(:en, :simple_form => { :messages => { :email => 'is not valid', :blank => 'should be filled' }, :attributes => { :email => 'E-mail' } })
+
+ form = ContactForm.new(:email => 'not_valid')
+ form.valid?
+
+ assert_equal ["Name should be filled", "E-mail is not valid"], form.errors.full_messages
+ end
+
+ def teardown
+ I18n.reload!
+ end
+end
89 test/notifier_test.rb
@@ -0,0 +1,89 @@
+require File.dirname(__FILE__) + '/test_helper'
+
+class SimpleFormNotifierTest < ActiveSupport::TestCase
+
+ def setup
+ @form = ContactForm.new(:name => 'José', :email => 'my.email@my.domain.com', :message => 'Cool')
+ @advanced = AdvancedForm.new(:name => 'José', :email => 'my.email@my.domain.com', :message => "Cool\nno?")
+ ActionMailer::Base.deliveries = []
+ end
+
+ def test_email_is_sent
+ @form.deliver
+ assert_equal 1, ActionMailer::Base.deliveries.size
+ end
+
+ def test_subject_defaults_to_class_human_name
+ @form.deliver
+ assert_equal 'Contact form', ActionMailer::Base.deliveries.first.subject
+ end
+
+ def test_subject_is_a_string
+ @advanced.deliver
+ assert_equal 'My Advanced Form', ActionMailer::Base.deliveries.first.subject
+ end
+
+ def test_sender_defaults_to_form_email
+ @form.deliver
+ assert_equal ['my.email@my.domain.com'], ActionMailer::Base.deliveries.first.from
+ end
+
+ def test_error_is_raised_when_recipients_is_nil
+ assert_raise ScriptError do
+ NullRecipient.new.deliver
+ end
+ end
+
+ def test_recipients_is_a_string
+ @form.deliver
+ assert_equal ['my.email@my.domain.com'], ActionMailer::Base.deliveries.first.to
+ end
+
+ def test_recipients_is_an_array
+ @advanced.deliver
+ assert_equal ['my.email@my.domain.com', 'my.first@email.com', 'my.second@email.com'], ActionMailer::Base.deliveries.first.to
+ end
+
+ def test_headers_is_a_hash
+ @advanced.deliver
+ assert_equal '<mypath>', ActionMailer::Base.deliveries.first.header['return-path'].to_s
+ end
+
+ def test_body_contains_subject
+ @form.deliver
+ assert_match /Contact form/, ActionMailer::Base.deliveries.first.body
+ 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
+ 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
+ end
+
+ def test_body_contains_localized_attributes_names
+ I18n.backend.store_translations(:en, :simple_form => { :attributes => { :email => 'E-mail', :message => 'Sent message' } })
+ @form.deliver
+ assert_match /E\-mail:/, ActionMailer::Base.deliveries.first.body
+ assert_match /Sent message:/, ActionMailer::Base.deliveries.first.body
+ end
+
+ def test_body_simple_format_messages_with_break_line
+ @form.deliver
+ assert_no_match /<p>Cool/, ActionMailer::Base.deliveries.first.body
+
+ @advanced.deliver
+ assert_match /<p>Cool/, ActionMailer::Base.deliveries.last.body
+ end
+
+ def teardown
+ I18n.reload!
+ end
+end
33 test/test_helper.rb
@@ -0,0 +1,33 @@
+require 'test/unit'
+require 'rubygems'
+
+RAILS_ENV = ENV["RAILS_ENV"] = "test"
+
+require 'active_support'
+require 'active_support/test_case'
+require 'action_mailer'
+
+ActionMailer::Base.delivery_method = :test
+
+# Load respond_to before defining ApplicationController
+require File.dirname(__FILE__) + '/../lib/simple_form.rb'
+
+class ContactForm < SimpleForm
+ recipients 'my.email@my.domain.com'
+
+ attribute :name, :validate => true
+ attribute :email, :validate => /[^@]+@[^\.]+\.[\w\.\-]+/
+ attribute :nickname, :captcha => true
+ attributes :tellphone, :message
+end
+
+class AdvancedForm < ContactForm
+ recipients [ 'my.first@email.com', 'my.second@email.com' ]
+ subject 'My Advanced Form'
+ sender{|c| %{"#{c.name}" <#{c.email}>} }
+ headers 'return-path' => 'mypath'
+end
+
+class NullRecipient < SimpleForm
+ sender 'my.email@my.domain.com'
+end
9 views/simple_form/notifier/contact.erb
@@ -0,0 +1,9 @@
+<p><%=h @subject %></p>
+
+<% @form.class.form_attributes.each do |attribute|
+ value = @form.send(attribute)
+ next if value.blank? %>
+
+ <p><b><%= @form.class.human_attribute_name(attribute) %>:</b>
+ <%= value.include?("\n") ? simple_format(h(value)) : h(value) %></p>
+<% end %>
Please sign in to comment.
Something went wrong with that request. Please try again.