padrino-helpers 0.8.5 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -429,15 +429,15 @@ This plugin also has support for useful additions such as partials (with support
429
429
 
430
430
  Using render plugin helpers is extremely simple. If you want to render an erb template in your view path:
431
431
 
432
- erb_template 'path/to/my/template'
432
+ render :erb, 'path/to/erb/template'
433
433
 
434
434
  or using haml templates works just as well:
435
435
 
436
- haml_template 'path/to/haml/template'
436
+ render :haml, 'path/to/haml/template'
437
437
 
438
438
  There is also a method which renders the first view matching the path and removes the need to define an engine:
439
439
 
440
- render_template 'path/to/any/template'
440
+ render 'path/to/any/template'
441
441
 
442
442
  It is worth noting these are mostly for convenience. With nested view file paths in Sinatra, this becomes tiresome:
443
443
 
@@ -457,16 +457,10 @@ This works as you would expect and also supports the collection counter inside t
457
457
 
458
458
  The list of defined helpers in the 'render helpers' category:
459
459
 
460
- * <tt>erb_template(template_path, options={})</tt>
461
- * Renders a erb template based on the given path
462
- * <tt>erb_template 'users/new'</tt>
463
- * <tt>haml_template(template_path, options={})</tt>
464
- * Renders a haml template based on the given path
465
- * <tt>haml_template 'users/new'</tt>
466
- * <tt>render_template(template_path, options={})</tt>
467
- * Renders the first detected template based on the given path
468
- * <tt>render_template 'users/new'</tt>
469
- * <tt>render_template 'users/index', :template_engine => 'haml'</tt>
460
+ * <tt>render(engine, data, options, locals)</tt>
461
+ * Renders the specified template with the given options
462
+ * <tt>render ‘user/new'</tt>
463
+ * <tt>render :erb, ‘users/new’, :layout => false</tt>
470
464
  * <tt>partial(template, *args)</tt>
471
465
  * Renders the html related to the partial template for object or collection
472
466
  * <tt>partial 'photo/_item', :object => @photo, :locals => { :foo => 'bar' }</tt>
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.8.5
1
+ 0.9.0
@@ -37,6 +37,7 @@ module Padrino
37
37
  app.helpers Padrino::Helpers::FormatHelpers
38
38
  app.helpers Padrino::Helpers::RenderHelpers
39
39
  app.helpers Padrino::Helpers::NumberHelpers
40
+ app.helpers Padrino::Helpers::TranslationHelpers
40
41
  end
41
42
  end # Helpers
42
43
  end # Padrino
@@ -5,13 +5,13 @@ module Padrino
5
5
  # Creates a div to display the flash of given type if it exists
6
6
  #
7
7
  # ==== Examples
8
- #
9
- # flash_tag(:notice, :class => 'flash', :id => 'flash-notice')
8
+ # # => <div class="notice">flash-notice</div>
9
+ # flash_tag(:notice, :id => 'flash-notice')
10
10
  #
11
11
  def flash_tag(kind, options={})
12
12
  flash_text = flash[kind]
13
13
  return '' if flash_text.blank?
14
- options.reverse_merge!(:class => 'flash')
14
+ options.reverse_merge!(:class => kind)
15
15
  content_tag(:div, flash_text, options)
16
16
  end
17
17
 
@@ -37,7 +37,7 @@ module Padrino
37
37
  url = args[0] ? args[0] + anchor.to_s : anchor || 'javascript:void(0);'
38
38
  options.reverse_merge!(:href => url)
39
39
  link_content = capture_html(&block)
40
- return name unless parse_conditions(url, options)
40
+ return '' unless parse_conditions(url, options)
41
41
  result_link = content_tag(:a, link_content, options)
42
42
  block_is_template?(block) ? concat_content(result_link) : result_link
43
43
  else
@@ -48,6 +48,31 @@ module Padrino
48
48
  end
49
49
  end
50
50
 
51
+ ##
52
+ # Creates a form containing a single button that submits to the url.
53
+ #
54
+ # ==== Examples
55
+ #
56
+ # # Generates:
57
+ # # <form class="form" action="/admin/accounts/destroy/2" method="post">
58
+ # # <input type="hidden" value="delete" name="_method" />
59
+ # # <input type="submit" value="Delete" />
60
+ # # </form>
61
+ # button_to 'Delete', url(:accounts_destroy, :id => account), :method => :delete, :class => :form
62
+ #
63
+ def button_to(*args, &block)
64
+ name, url = args[0], args[1]
65
+ options = args.extract_options!
66
+ desired_method = options[:method]
67
+ options.delete(:method) if options[:method].to_s !~ /get|post/i
68
+ options.reverse_merge!(:method => 'post', :action => url)
69
+ options[:enctype] = "multipart/form-data" if options.delete(:multipart)
70
+ options["data-remote"] = "true" if options.delete(:remote)
71
+ inner_form_html = hidden_form_method_field(desired_method)
72
+ inner_form_html += block_given? ? capture_html(&block) : submit_tag(name)
73
+ content_tag('form', inner_form_html, options)
74
+ end
75
+
51
76
  ##
52
77
  # Creates a mail link element with given name and caption
53
78
  #
@@ -180,6 +205,18 @@ module Padrino
180
205
  stamp = File.exist?(public_path) ? File.mtime(public_path).to_i : Time.now.to_i
181
206
  "#{result_path}?#{stamp}"
182
207
  end
208
+
209
+ ##
210
+ # Generates a favicon link.
211
+ # example:
212
+ # favicon_tag 'images/favicon.png'
213
+ # or override some options
214
+ # favicon_tag 'images/favicon.png', :type => 'image/ico'
215
+ def favicon_tag(source,options={})
216
+ type = File.extname(source).gsub('.','')
217
+ options = options.dup.reverse_merge!(:href => source, :rel => 'icon', :type => "image/#{type}")
218
+ tag(:link, options)
219
+ end
183
220
 
184
221
  private
185
222
  ##
@@ -17,6 +17,11 @@ module Padrino
17
17
  @template.error_messages_for(*params)
18
18
  end
19
19
 
20
+ # f.error_message_on(field)
21
+ def error_message_on(field, options={})
22
+ @template.error_message_on(object_name, field, options)
23
+ end
24
+
20
25
  # f.label :username, :caption => "Nickname"
21
26
  def label(field, options={})
22
27
  options.reverse_merge!(:caption => "#{field_human_name(field)}: ")
@@ -31,26 +36,30 @@ module Padrino
31
36
 
32
37
  # f.text_field :username, :value => "(blank)", :id => 'username'
33
38
  def text_field(field, options={})
34
- options.reverse_merge!(:value => field_value(field), :id => field_id(field), :class => field_error(field, options))
39
+ options.reverse_merge!(:value => field_value(field), :id => field_id(field))
40
+ options.merge!(:class => field_error(field, options))
35
41
  @template.text_field_tag field_name(field), options
36
42
  end
37
43
 
38
44
  # f.text_area :summary, :value => "(enter summary)", :id => 'summary'
39
45
  def text_area(field, options={})
40
- options.reverse_merge!(:value => field_value(field), :id => field_id(field), :class => field_error(field, options))
46
+ options.reverse_merge!(:value => field_value(field), :id => field_id(field))
47
+ options.merge!(:class => field_error(field, options))
41
48
  @template.text_area_tag field_name(field), options
42
49
  end
43
50
 
44
51
  # f.password_field :password, :id => 'password'
45
52
  def password_field(field, options={})
46
- options.reverse_merge!(:value => field_value(field), :id => field_id(field), :class => field_error(field, options))
53
+ options.reverse_merge!(:value => field_value(field), :id => field_id(field))
54
+ options.merge!(:class => field_error(field, options))
47
55
  @template.password_field_tag field_name(field), options
48
56
  end
49
57
 
50
58
  # f.select :color, :options => ['red', 'green'], :include_blank => true
51
59
  # f.select :color, :collection => @colors, :fields => [:name, :id]
52
60
  def select(field, options={})
53
- options.reverse_merge!(:id => field_id(field), :selected => field_value(field), :class => field_error(field, options))
61
+ options.reverse_merge!(:id => field_id(field), :selected => field_value(field))
62
+ options.merge!(:class => field_error(field, options))
54
63
  @template.select_tag field_name(field), options
55
64
  end
56
65
 
@@ -72,7 +81,8 @@ module Padrino
72
81
 
73
82
  # f.file_field :photo, :class => 'avatar'
74
83
  def file_field(field, options={})
75
- options.reverse_merge!(:id => field_id(field), :class => field_error(field, options))
84
+ options.reverse_merge!(:id => field_id(field))
85
+ options.merge!(:class => field_error(field, options))
76
86
  @template.file_field_tag field_name(field), options
77
87
  end
78
88
 
@@ -110,9 +120,12 @@ module Padrino
110
120
  @object && @object.respond_to?(field) ? @object.send(field) : ""
111
121
  end
112
122
 
123
+ # Add a :invalid css class to the field if it contain an error
113
124
  def field_error(field, options)
114
- if @object && @object.respond_to?(:errors) && @object.errors.respond_to?(:on) && @object.errors.on(field)
115
- ["x-form-invalid", options[:class]].compact.join(" ")
125
+ if @object && @object.respond_to?(:errors) && @object.errors.respond_to?(:[]) && @object.errors[field]
126
+ [options[:class], :invalid].flatten.compact.join(" ")
127
+ else
128
+ options[:class]
116
129
  end
117
130
  end
118
131
 
@@ -38,13 +38,30 @@ module Padrino
38
38
  # form_tag '/register' do ... end
39
39
  #
40
40
  def form_tag(url, options={}, &block)
41
+ desired_method = options[:method]
42
+ options.delete(:method) if options[:method].to_s !~ /get|post/i
41
43
  options.reverse_merge!(:method => 'post', :action => url)
42
44
  options[:enctype] = "multipart/form-data" if options.delete(:multipart)
43
45
  options["data-remote"] = "true" if options.delete(:remote)
44
- inner_form_html = hidden_form_method_field(options[:method]) + capture_html(&block)
46
+ inner_form_html = hidden_form_method_field(desired_method) + capture_html(&block)
45
47
  concat_content content_tag('form', inner_form_html, options)
46
48
  end
47
49
 
50
+ ##
51
+ # Returns the hidden method field for 'put' and 'delete' forms
52
+ # Only 'get' and 'post' are allowed within browsers;
53
+ # 'put' and 'delete' are just specified using hidden fields with form action still 'put'.
54
+ #
55
+ # ==== Examples
56
+ #
57
+ # # Generate: <input name="_method" value="delete" />
58
+ # hidden_form_method_field('delete')
59
+ #
60
+ def hidden_form_method_field(desired_method)
61
+ return '' if desired_method.blank? || desired_method.to_s =~ /get|post/i
62
+ hidden_field_tag(:_method, :value => desired_method)
63
+ end
64
+
48
65
  ##
49
66
  # Constructs a field_set to group fields with given options
50
67
  #
@@ -124,6 +141,38 @@ module Padrino
124
141
  end
125
142
  end
126
143
 
144
+ ##
145
+ # Returns a string containing the error message attached to the +method+ on the +object+ if one exists.
146
+ #
147
+ # ==== Options
148
+ #
149
+ # :tag:: The tag that enclose your error. (Default 'div')
150
+ # :prepend:: Text to add before error.
151
+ # :append:: Text to add after error.
152
+ #
153
+ # ==== Examples
154
+ #
155
+ # # => <span class="error">can't be blank</div>
156
+ # error_message_on :post, :title
157
+ #
158
+ # # => <div class="custom" style="border:1px solid red">can't be blank</div>
159
+ # error_message_on :post, :title, :tag => :id, :class => :custom, :style => "border:1px solid red"
160
+ #
161
+ # # => <div class="error">This title can't be blank (or it won't work)</div>
162
+ # error_message_on :post, :title, :prepend => "This title", :append => "(or it won't work)"
163
+ #
164
+ def error_message_on(object, field, options={})
165
+ object = instance_variable_get("@#{object}")
166
+ if object && object.respond_to?(:errors) && object.errors.respond_to?(:[]) && object.errors[field]
167
+ options.reverse_merge!(:tag => :span, :class => :error)
168
+ tag = options.delete(:tag)
169
+ error = [options.delete(:prepend), Array(object.errors[field]).first, options.delete(:append)].compact.join(" ")
170
+ content_tag(tag, error, options)
171
+ else
172
+ ''
173
+ end
174
+ end
175
+
127
176
  ##
128
177
  # Constructs a label tag from the given options
129
178
  #
@@ -303,23 +352,6 @@ module Padrino
303
352
  end
304
353
  end
305
354
 
306
- ##
307
- # Returns the hidden method field for 'put' and 'delete' forms
308
- # Only 'get' and 'post' are allowed within browsers;
309
- # 'put' and 'delete' are just specified using hidden fields with form action still 'put'.
310
- #
311
- # ==== Examples
312
- #
313
- # # Generate: <input name="_method" value="delete" />
314
- # hidden_form_method_field('delete')
315
- #
316
- def hidden_form_method_field(desired_method)
317
- return '' if (desired_method.to_s =~ /get|post/)
318
- original_method = desired_method.to_s.dup
319
- desired_method.to_s.replace('post')
320
- hidden_field_tag(:_method, :value => original_method)
321
- end
322
-
323
355
  private
324
356
  ##
325
357
  # Returns the FormBuilder class to use based on all available setting sources
@@ -49,16 +49,17 @@ module Padrino
49
49
  end
50
50
 
51
51
  ##
52
- # Capture a block of content to be rendered at a later time.
52
+ # Capture a block or text of content to be rendered at a later time.
53
53
  # Your blocks can also receive values, which are passed to them by <tt>yield_content</tt>
54
54
  #
55
55
  # ==== Examples
56
56
  #
57
57
  # content_for(:name) { ...content... }
58
58
  # content_for(:name) { |name| ...content... }
59
+ # content_for(:name, "I'm Jeff")
59
60
  #
60
- def content_for(key, &block)
61
- content_blocks[key.to_sym] << block
61
+ def content_for(key, content = nil, &block)
62
+ content_blocks[key.to_sym] << (block_given? ? block : Proc.new { content })
62
63
  end
63
64
 
64
65
  ##
@@ -70,9 +71,12 @@ module Padrino
70
71
  #
71
72
  # yield_content :include
72
73
  # yield_content :head, "param1", "param2"
74
+ # yield_content(:title) || "My page title"
73
75
  #
74
76
  def yield_content(key, *args)
75
- content_blocks[key.to_sym].map { |content|
77
+ blocks = content_blocks[key.to_sym]
78
+ return nil if blocks.empty?
79
+ blocks.map { |content|
76
80
  capture_html(*args, &content)
77
81
  }.join
78
82
  end
@@ -0,0 +1,21 @@
1
+ module Padrino
2
+ module Helpers
3
+ module TranslationHelpers
4
+ ##
5
+ # Delegates to I18n.translate with no additional functionality.
6
+ #
7
+ def translate(*args)
8
+ I18n.translate(*args)
9
+ end
10
+ alias :t :translate
11
+
12
+ ##
13
+ # Delegates to I18n.localize with no additional functionality.
14
+ #
15
+ def localize(*args)
16
+ I18n.localize(*args)
17
+ end
18
+ alias :l :localize
19
+ end # TranslationHelpers
20
+ end # Helpers
21
+ end # Padrino
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{padrino-helpers}
8
- s.version = "0.8.5"
8
+ s.version = "0.9.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Padrino Team", "Nathan Esquenazi", "Davide D'Agostino", "Arthur Chiu"]
12
- s.date = %q{2010-02-18}
12
+ s.date = %q{2010-02-23}
13
13
  s.description = %q{Tag helpers, asset helpers, form helpers, form builders and many more helpers for padrino}
14
14
  s.email = %q{padrinorb@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -35,6 +35,7 @@ Gem::Specification.new do |s|
35
35
  "lib/padrino-helpers/output_helpers.rb",
36
36
  "lib/padrino-helpers/render_helpers.rb",
37
37
  "lib/padrino-helpers/tag_helpers.rb",
38
+ "lib/padrino-helpers/translation_helpers.rb",
38
39
  "padrino-helpers.gemspec",
39
40
  "test/fixtures/markup_app/app.rb",
40
41
  "test/fixtures/markup_app/views/capture_concat.erb",
@@ -83,14 +84,14 @@ Gem::Specification.new do |s|
83
84
  s.specification_version = 3
84
85
 
85
86
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
86
- s.add_runtime_dependency(%q<padrino-core>, ["= 0.8.5"])
87
+ s.add_runtime_dependency(%q<padrino-core>, ["= 0.9.0"])
87
88
  s.add_development_dependency(%q<haml>, [">= 2.2.1"])
88
89
  s.add_development_dependency(%q<shoulda>, [">= 2.10.3"])
89
90
  s.add_development_dependency(%q<mocha>, [">= 0.9.7"])
90
91
  s.add_development_dependency(%q<rack-test>, [">= 0.5.0"])
91
92
  s.add_development_dependency(%q<webrat>, [">= 0.5.1"])
92
93
  else
93
- s.add_dependency(%q<padrino-core>, ["= 0.8.5"])
94
+ s.add_dependency(%q<padrino-core>, ["= 0.9.0"])
94
95
  s.add_dependency(%q<haml>, [">= 2.2.1"])
95
96
  s.add_dependency(%q<shoulda>, [">= 2.10.3"])
96
97
  s.add_dependency(%q<mocha>, [">= 0.9.7"])
@@ -98,7 +99,7 @@ Gem::Specification.new do |s|
98
99
  s.add_dependency(%q<webrat>, [">= 0.5.1"])
99
100
  end
100
101
  else
101
- s.add_dependency(%q<padrino-core>, ["= 0.8.5"])
102
+ s.add_dependency(%q<padrino-core>, ["= 0.9.0"])
102
103
  s.add_dependency(%q<haml>, [">= 2.2.1"])
103
104
  s.add_dependency(%q<shoulda>, [">= 2.10.3"])
104
105
  s.add_dependency(%q<mocha>, [">= 0.9.7"])
@@ -55,7 +55,7 @@ end
55
55
 
56
56
  class Errors < Array
57
57
  def initialize; self << [:fake, :second, :third]; end
58
- def on(fake); true if fake == :email; end
58
+ def [](fake); true if fake == :email; end
59
59
  def full_messages
60
60
  ["This is a fake error", "This is a second fake error", "This is a third fake error"]
61
61
  end
@@ -14,7 +14,7 @@ class TestAssetTagHelpers < Test::Unit::TestCase
14
14
 
15
15
  context 'for #flash_tag method' do
16
16
  should "display flash with no given attributes" do
17
- assert_has_tag('div.flash', :content => "Demo notice") { flash_tag(:notice) }
17
+ assert_has_tag('div.notice', :content => "Demo notice") { flash_tag(:notice) }
18
18
  end
19
19
  should "display flash with given attributes" do
20
20
  actual_html = flash_tag(:notice, :class => 'notice', :id => 'notice-area')
@@ -202,4 +202,24 @@ class TestAssetTagHelpers < Test::Unit::TestCase
202
202
  assert_has_tag('script', :src => "http://google.com/lib.js") { actual_html }
203
203
  end
204
204
  end
205
+
206
+ context "for #favicon_tag method" do
207
+ should "display favicon" do
208
+ time = stop_time_for_test
209
+ actual_html = favicon_tag('images/favicon.png')
210
+ assert_has_tag('link', :rel => 'icon', :type => 'image/png', :href => 'images/favicon.png') { actual_html }
211
+ end
212
+ should "match type with file ext" do
213
+ time = stop_time_for_test
214
+ actual_html = favicon_tag('images/favicon.ico')
215
+ assert_has_tag('link', :rel => 'icon', :type => 'image/ico', :href => 'images/favicon.ico') { actual_html }
216
+ end
217
+ should "allow option overrides" do
218
+ time = stop_time_for_test
219
+ actual_html = favicon_tag('images/favicon.png', :type => 'image/ico')
220
+ assert_has_tag('link', :rel => 'icon', :type => 'image/ico', :href => 'images/favicon.png') { actual_html }
221
+ end
222
+
223
+ end
224
+
205
225
  end
@@ -9,7 +9,7 @@ class TestFormBuilder < Test::Unit::TestCase
9
9
  end
10
10
 
11
11
  def setup
12
- error_stub = stub(:full_messages => ["1", "2"], :count => 2)
12
+ error_stub = stub(:full_messages => ["1", "2"], :count => 2, :[] => [])
13
13
  role_types = [stub(:name => 'Admin', :id => 1), stub(:name => 'Moderate', :id => 2), stub(:name => 'Limited', :id => 3)]
14
14
  @user = stub(:errors => error_stub, :class => 'User', :first_name => "Joe", :session_id => 54)
15
15
  @user.stubs(:role_types => role_types, :role => "1")
@@ -143,7 +143,7 @@ class TestFormBuilder < Test::Unit::TestCase
143
143
  assert_have_selector '#demo div.field-errors ul li', :content => "This is a fake error"
144
144
  assert_have_selector '#demo2 div.field-errors h2', :content => "custom MarkupUser cannot be saved!"
145
145
  assert_have_selector '#demo2 div.field-errors ul li', :content => "This is a fake error"
146
- assert_have_selector '#demo input', :name => 'markup_user[email]', :class => 'x-form-invalid'
146
+ assert_have_selector '#demo input', :name => 'markup_user[email]', :class => 'invalid'
147
147
  end
148
148
 
149
149
  should "display correct form in erb" do
@@ -152,7 +152,7 @@ class TestFormBuilder < Test::Unit::TestCase
152
152
  assert_have_selector '#demo div.field-errors ul li', :content => "This is a fake error"
153
153
  assert_have_selector '#demo2 div.field-errors h2', :content => "custom MarkupUser cannot be saved!"
154
154
  assert_have_selector '#demo2 div.field-errors ul li', :content => "This is a fake error"
155
- assert_have_selector '#demo input', :name => 'markup_user[email]', :class => 'x-form-invalid'
155
+ assert_have_selector '#demo input', :name => 'markup_user[email]', :class => 'invalid'
156
156
  end
157
157
  end
158
158
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: padrino-helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.5
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Padrino Team
@@ -12,7 +12,7 @@ autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
14
 
15
- date: 2010-02-18 00:00:00 +01:00
15
+ date: 2010-02-23 00:00:00 -08:00
16
16
  default_executable:
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
@@ -23,7 +23,7 @@ dependencies:
23
23
  requirements:
24
24
  - - "="
25
25
  - !ruby/object:Gem::Version
26
- version: 0.8.5
26
+ version: 0.9.0
27
27
  version:
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: haml
@@ -103,6 +103,7 @@ files:
103
103
  - lib/padrino-helpers/output_helpers.rb
104
104
  - lib/padrino-helpers/render_helpers.rb
105
105
  - lib/padrino-helpers/tag_helpers.rb
106
+ - lib/padrino-helpers/translation_helpers.rb
106
107
  - padrino-helpers.gemspec
107
108
  - test/fixtures/markup_app/app.rb
108
109
  - test/fixtures/markup_app/views/capture_concat.erb