Permalink
Browse files

Allow customize template that will be rendered.

  • Loading branch information...
1 parent 89813b1 commit 32883dc92fd7a969655e8a5c33eb529302c8fba4 @carlosantoniodasilva carlosantoniodasilva committed with josevalim Nov 7, 2009
View
@@ -7,8 +7,9 @@
class SimpleForm
extend SimpleForm::DSL
- ACCESSORS = [ :form_attributes, :form_validatable, :form_subject, :form_attachments,
- :form_recipients, :form_sender, :form_captcha, :form_headers, :form_appendable ]
+ ACCESSORS = [ :form_attributes, :form_validatable, :form_subject,
+ :form_attachments, :form_recipients, :form_sender,
+ :form_captcha, :form_headers, :form_template, :form_appendable ]
DEFAULT_MESSAGES = { :blank => "can't be blank", :invalid => "is invalid" }
@@ -26,6 +27,7 @@ class SimpleForm
headers({})
sender {|c| c.email }
subject{|c| c.class.human_name }
+ template 'contact'
end
SimpleForm::Notifier.template_root = File.join(dir, '..', 'views')
View
@@ -129,6 +129,26 @@ def headers(hash)
write_inheritable_hash(:form_headers, hash)
end
+ # Customized template for your e-mail, if you don't want to use default
+ # 'contact' template or need more than one contact form with different
+ # template layouts.
+ #
+ # When a symbol is given, it will call a method on the form object with
+ # the same name as the symbol. As a proc, it receives a simple form
+ # instance. Both method and proc must return a string with the template
+ # name. Defaults to 'contact'.
+ #
+ # == Examples
+ #
+ # class ContactForm < SimpleForm
+ # # look for a template in views/simple_form/notifier/my_template.erb
+ # template 'my_template'
+ # end
+ #
+ def template(new_template)
+ write_inheritable_attribute(:form_template, new_template)
+ end
+
# Values from request object to be appended to the contact form.
# Whenever used, you have to send the request object when initializing the object:
#
@@ -7,6 +7,7 @@ def contact(form)
@from = get_from_class_and_eval(form, :form_sender)
@subject = get_from_class_and_eval(form, :form_subject)
@recipients = get_from_class_and_eval(form, :form_recipients)
+ @template = get_from_class_and_eval(form, :form_template)
raise ScriptError, "You forgot to setup #{form.class.name} recipients" if @recipients.blank?
raise ScriptError, "You set :append values but forgot to give me the request object" if form.request.nil? && !form.class.form_appendable.blank?
View
@@ -12,6 +12,8 @@ def setup
test_file = ActionController::TestUploadedFile.new(File.join(File.dirname(__FILE__), 'test_file.txt'))
@with_file = FileForm.new(:name => 'José', :email => 'my.email@my.domain.com', :message => "Cool", :file => test_file)
+ @template = TemplateForm.new(@valid_attributes)
+
ActionMailer::Base.deliveries = []
end
@@ -158,6 +160,23 @@ def test_form_with_file_does_not_output_attachment_as_attribute
assert_no_match /File/, ActionMailer::Base.deliveries.first.body
end
+ def test_form_with_customized_template_raise_missing_template_if_not_found
+ assert_raise ActionView::MissingTemplate do
+ @template.deliver
+ end
+ end
+
+ def test_form_with_customized_template_render_correct_template
+ begin
+ default_template_root = SimpleForm::Notifier.template_root
+ SimpleForm::Notifier.template_root = File.join(File.dirname(__FILE__), 'views')
+ @template.deliver
+ assert_match 'Hello from my cystom template!', ActionMailer::Base.deliveries.last.body
+ ensure
+ SimpleForm::Notifier.template_root = default_template_root
+ end
+ end
+
def teardown
I18n.reload!
end
View
@@ -52,6 +52,10 @@ class NullRecipient < SimpleForm
sender 'my.email@my.domain.com'
end
+class TemplateForm < ContactForm
+ template 'custom_template'
+end
+
# Needed to correctly test an uploaded file
class ActionController::TestUploadedFile
def read
@@ -0,0 +1 @@
+Hello from my cystom template!

0 comments on commit 32883dc

Please sign in to comment.