Skip to content
Browse files

Merge branch 'master' of git@github.com:josevalim/simple_form

  • Loading branch information...
2 parents d930b34 + 32883dc commit 65451c8879afdbd2ea281a3c2680744c5f7e6edd @josevalim josevalim committed
View
6 lib/simple_form.rb
@@ -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
20 lib/simple_form/dsl.rb
@@ -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:
#
View
6 lib/simple_form/errors.rb
@@ -31,13 +31,15 @@ def initialize(base, *args)
alias :add :store
alias :count :size
+ alias :get :[]
def on(attribute)
attribute = attribute.to_sym
- return nil unless self[attribute]
+ return nil unless get(attribute)
- generate_message_for(attribute, self[attribute])
+ generate_message_for(attribute, get(attribute))
end
+ alias :[] :on
def full_messages
map do |attribute, message|
View
1 lib/simple_form/notifier.rb
@@ -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
18 test/errors_test.rb
@@ -43,6 +43,24 @@ def test_on_returns_an_attribute_localized_message_in_the_given_attribute
assert_equal nil, form.errors.on(:message)
end
+ def test_array_like_option_acts_as_an_alias_for_on
+ form = ContactForm.new(:email => 'not_valid')
+ form.valid?
+ assert_equal "can't be blank", form.errors[:name]
+ assert_equal form.errors.on(:name), form.errors[:name]
+ assert_equal "is invalid", form.errors[:email]
+ assert_equal form.errors.on(:email), form.errors[:email]
+ assert_equal nil, form.errors[:message]
+ end
+
+ def test_get_returns_the_real_value_in_the_given_attribute
+ form = ContactForm.new(:email => 'not_valid')
+ form.valid?
+ assert_equal :blank, form.errors.get(:name)
+ assert_equal :invalid, form.errors.get(:email)
+ assert_equal nil, form.errors.get(:message)
+ end
+
def test_full_messages
form = ContactForm.new(:email => 'not_valid')
form.valid?
View
19 test/notifier_test.rb
@@ -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
4 test/test_helper.rb
@@ -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
View
1 test/views/simple_form/notifier/custom_template.erb
@@ -0,0 +1 @@
+Hello from my cystom template!

0 comments on commit 65451c8

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