josevalim-simple_form 0.2.1 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG CHANGED
@@ -1,6 +1,10 @@
1
- # Version 0.2
1
+ # Version 0.3
2
2
 
3
+ * Added support to symbols on :sender, :subject and :recipients
3
4
  * Added support to symbols on :validate
5
+
6
+ # Version 0.2
7
+
4
8
  * Added support to request objects and append DSL
5
9
  * Added support to :attachments (thanks to @andrewtimberlake)
6
10
 
data/README CHANGED
@@ -1,6 +1,6 @@
1
1
  Simple Form
2
2
  License: MIT
3
- Version: 0.2.1
3
+ Version: 0.3.1
4
4
 
5
5
  You can also read this README in pretty html at the GitHub project Wiki page:
6
6
 
@@ -121,31 +121,40 @@ Examples:
121
121
  # save is an alias to deliver to allow it to work with InheritedResources
122
122
  c.save #=> true
123
123
 
124
- == subject(string=nil, &block)
124
+ == subject(string_or_symbol_or_block)
125
125
 
126
- Declares the subject of the contact email. It can be a string or a proc.
127
- As a proc, it receives a simple form instance. When not specified, it defaults
126
+ Declares the subject of the contact email. It can be a string or a proc or a symbol.
127
+
128
+ When a symbol is given, it will call a method on the form object with the same
129
+ name as the symbol. As a proc, it receives a simple form instance. It defaults
128
130
  to the class human name.
129
131
 
130
132
  subject "My Contact Form"
131
- subject {|c| "Contacted by #{c.name}"}
133
+ subject { |c| "Contacted by #{c.name}" }
134
+
135
+ == sender(string_or_symbol_or_block)
132
136
 
133
- == sender(&block)
137
+ Declares contact email sender. It can be a string or a proc or a symbol.
134
138
 
135
- Declares contact email sender. It can be a string or a proc.
136
- As a proc, it receives a simple form instance. By default is:
139
+ When a symbol is given, it will call a method on the form object with the same
140
+ name as the symbol. As a proc, it receives a simple form instance. By default is:
137
141
 
138
142
  sender{ |c| c.email }
139
143
 
140
- This requires that your SimpleForm object have at least an email attribute.
144
+ This requires that your SimpleForm object have an email attribute.
141
145
 
142
- == headers(hash)
146
+ == recipients(string_or_array_or_symbol_or_block)
143
147
 
144
- Additional headers to your e-mail.
148
+ Who will receive the e-mail. Can be a string or array or a symbol or a proc.
145
149
 
146
- == recipients(string_or_array_or_proc)
150
+ When a symbol is given, it will call a method on the form object with the same
151
+ name as the symbol. As a proc, it receives a simple form instance.
147
152
 
148
- Who will receive the e-mail. Can be a string or array, or a proc that returns one of them.
153
+ Both the proc and the symbol must return a string or an array. By default is nil.
154
+
155
+ == headers(hash)
156
+
157
+ Additional headers to your e-mail.
149
158
 
150
159
  I18n
151
160
  ----
@@ -60,9 +60,11 @@ class SimpleForm
60
60
  end
61
61
  alias :attributes :attribute
62
62
 
63
- # Declares the subject of the contact email. It can be a string or a proc.
64
- # As a proc, it receives a simple form instance. When not specified, it
65
- # defaults to the class human name.
63
+ # Declares contact email sender. It can be a string or a proc or a symbol.
64
+ #
65
+ # When a symbol is given, it will call a method on the form object with
66
+ # the same name as the symbol. As a proc, it receives a simple form
67
+ # instance. By default is the class human name.
66
68
  #
67
69
  # == Examples
68
70
  #
@@ -70,53 +72,62 @@ class SimpleForm
70
72
  # subject "My Contact Form"
71
73
  # end
72
74
  #
73
- def subject(string=nil, &block)
74
- write_inheritable_attribute(:form_subject, string || block)
75
+ def subject(duck=nil, &block)
76
+ write_inheritable_attribute(:form_subject, duck || block)
75
77
  end
76
78
 
77
- # Declares contact email sender. It can be a string or a proc.
78
- # As a proc, it receives a simple form instance. By default is:
79
+ # Declares contact email sender. It can be a string or a proc or a symbol.
80
+ #
81
+ # When a symbol is given, it will call a method on the form object with
82
+ # the same name as the symbol. As a proc, it receives a simple form
83
+ # instance. By default is:
79
84
  #
80
85
  # sender{ |c| c.email }
81
86
  #
82
- # This requires that your SimpleForm object have at least an email attribute.
87
+ # This requires that your SimpleForm object have an email attribute.
83
88
  #
84
89
  # == Examples
85
90
  #
86
91
  # class ContactForm < SimpleForm
87
92
  # # Change sender to include also the name
88
- # sender{|c| %{"#{c.name}" <#{c.email}>} }
93
+ # sender { |c| %{"#{c.name}" <#{c.email}>} }
89
94
  # end
90
95
  #
91
- def sender(string=nil, &block)
92
- write_inheritable_attribute(:form_sender, string || block)
96
+ def sender(duck=nil, &block)
97
+ write_inheritable_attribute(:form_sender, duck || block)
93
98
  end
94
99
  alias :from :sender
95
100
 
96
- # Additional headers to your e-mail.
101
+ # Who will receive the e-mail. Can be a string or array or a symbol or a proc.
102
+ #
103
+ # When a symbol is given, it will call a method on the form object with
104
+ # the same name as the symbol. As a proc, it receives a simple form instance.
105
+ #
106
+ # Both the proc and the symbol must return a string or an array. By default
107
+ # is nil.
97
108
  #
98
109
  # == Examples
99
110
  #
100
111
  # class ContactForm < SimpleForm
101
- # headers { :content_type => 'text/html' }
112
+ # recipients [ "first.manager@domain.com", "second.manager@domain.com" ]
102
113
  # end
103
114
  #
104
- def headers(hash)
105
- write_inheritable_hash(:form_headers, hash)
115
+ def recipients(duck=nil, &block)
116
+ write_inheritable_attribute(:form_recipients, duck || block)
106
117
  end
118
+ alias :to :recipients
107
119
 
108
- # Who will receive the e-mail. Can be a string or array.
120
+ # Additional headers to your e-mail.
109
121
  #
110
122
  # == Examples
111
123
  #
112
124
  # class ContactForm < SimpleForm
113
- # recipients "jose.valim@gmail.com"
125
+ # headers { :content_type => 'text/html' }
114
126
  # end
115
127
  #
116
- def recipients(string_or_array_or_proc)
117
- write_inheritable_attribute(:form_recipients, string_or_array_or_proc)
128
+ def headers(hash)
129
+ write_inheritable_hash(:form_headers, hash)
118
130
  end
119
- alias :to :recipients
120
131
 
121
132
  # Values from request object to be appended to the contact form.
122
133
  # Whenever used, you have to send the request object when initializing the object:
@@ -36,6 +36,8 @@ class SimpleForm
36
36
 
37
37
  if duck.is_a?(Proc)
38
38
  duck.call(form)
39
+ elsif duck.is_a?(Symbol)
40
+ form.send(duck)
39
41
  else
40
42
  duck
41
43
  end
@@ -9,7 +9,7 @@ class SimpleFormNotifierTest < ActiveSupport::TestCase
9
9
  @valid_attributes = { :name => 'José', :email => 'my.email@my.domain.com', :message => "Cool\nno?" }
10
10
  @advanced = AdvancedForm.new(@valid_attributes, @request)
11
11
 
12
- test_file = ActionController::TestUploadedFile.new(File.join(File.dirname(__FILE__), 'test-file.txt'))
12
+ test_file = ActionController::TestUploadedFile.new(File.join(File.dirname(__FILE__), 'test_file.txt'))
13
13
  @with_file = FileForm.new(:name => 'José', :email => 'my.email@my.domain.com', :message => "Cool", :file => test_file)
14
14
 
15
15
  ActionMailer::Base.deliveries = []
@@ -51,6 +51,11 @@ class SimpleFormNotifierTest < ActiveSupport::TestCase
51
51
  assert_equal ['my.first@email.com', 'my.second@email.com'], ActionMailer::Base.deliveries.first.to
52
52
  end
53
53
 
54
+ def test_recipients_is_a_symbold
55
+ @with_file.deliver
56
+ assert_equal ['contact_file@my.domain.com'], ActionMailer::Base.deliveries.first.to
57
+ end
58
+
54
59
  def test_headers_is_a_hash
55
60
  @advanced.deliver
56
61
  assert_equal '<mypath>', ActionMailer::Base.deliveries.first.header['return-path'].to_s
@@ -0,0 +1 @@
1
+ A test file
data/test/test_helper.rb CHANGED
@@ -37,13 +37,22 @@ end
37
37
 
38
38
  class FileForm < ContactForm
39
39
  attribute :file, :attachment => true, :validate => true
40
+ recipients :set_recipient
41
+
42
+ def set_recipient
43
+ if file
44
+ "contact_file@my.domain.com"
45
+ else
46
+ "contact@my.domain.com"
47
+ end
48
+ end
40
49
  end
41
50
 
42
51
  class NullRecipient < SimpleForm
43
52
  sender 'my.email@my.domain.com'
44
53
  end
45
54
 
46
- #Needed to correctly test an uploaded file
55
+ # Needed to correctly test an uploaded file
47
56
  class ActionController::TestUploadedFile
48
57
  def read
49
58
  @tempfile.read
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: josevalim-simple_form
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Jos\xC3\xA9 Valim"
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-15 00:00:00 -07:00
12
+ date: 2009-05-23 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -64,4 +64,5 @@ test_files:
64
64
  - test/base_test.rb
65
65
  - test/errors_test.rb
66
66
  - test/notifier_test.rb
67
+ - test/test_file.txt
67
68
  - test/test_helper.rb