padrino-helpers 0.11.4 → 0.12.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/README.rdoc +6 -6
  3. data/lib/padrino-helpers/asset_tag_helpers.rb +26 -33
  4. data/lib/padrino-helpers/form_builder/abstract_form_builder.rb +16 -17
  5. data/lib/padrino-helpers/form_helpers.rb +121 -129
  6. data/lib/padrino-helpers/format_helpers.rb +1 -1
  7. data/lib/padrino-helpers/output_helpers/abstract_handler.rb +25 -60
  8. data/lib/padrino-helpers/output_helpers/erb_handler.rb +7 -58
  9. data/lib/padrino-helpers/output_helpers/haml_handler.rb +5 -43
  10. data/lib/padrino-helpers/output_helpers/slim_handler.rb +5 -65
  11. data/lib/padrino-helpers/output_helpers.rb +26 -13
  12. data/lib/padrino-helpers/render_helpers.rb +21 -18
  13. data/lib/padrino-helpers.rb +1 -0
  14. data/padrino-helpers.gemspec +2 -1
  15. data/test/fixtures/markup_app/app.rb +14 -10
  16. data/test/fixtures/markup_app/views/button_to.haml +2 -2
  17. data/test/fixtures/markup_app/views/capture_concat.haml +3 -3
  18. data/test/fixtures/markup_app/views/capture_concat.slim +3 -3
  19. data/test/fixtures/markup_app/views/content_for.erb +10 -1
  20. data/test/fixtures/markup_app/views/content_for.haml +7 -0
  21. data/test/fixtures/markup_app/views/content_for.slim +8 -1
  22. data/test/fixtures/markup_app/views/content_tag.erb +2 -0
  23. data/test/fixtures/markup_app/views/content_tag.haml +4 -2
  24. data/test/fixtures/markup_app/views/content_tag.slim +3 -1
  25. data/test/fixtures/markup_app/views/fields_for.haml +4 -4
  26. data/test/fixtures/markup_app/views/form_for.haml +3 -3
  27. data/test/fixtures/markup_app/views/form_tag.haml +6 -6
  28. data/test/fixtures/markup_app/views/link_to.haml +1 -1
  29. data/test/fixtures/markup_app/views/simple_partial.slim +1 -1
  30. data/test/fixtures/render_app/app.rb +16 -4
  31. data/test/fixtures/render_app/views/_deep.erb +3 -0
  32. data/test/fixtures/render_app/views/_deep.haml +2 -0
  33. data/test/fixtures/render_app/views/_deep.slim +2 -0
  34. data/test/fixtures/render_app/views/_partial_block_erb.erb +10 -0
  35. data/test/fixtures/render_app/views/_partial_block_haml.haml +7 -0
  36. data/test/fixtures/render_app/views/_partial_block_slim.slim +7 -0
  37. data/test/fixtures/render_app/views/double_capture_erb.erb +2 -2
  38. data/test/fixtures/render_app/views/double_capture_haml.haml +1 -1
  39. data/test/fixtures/render_app/views/double_capture_slim.slim +1 -1
  40. data/test/fixtures/render_app/views/render_block_erb.erb +5 -0
  41. data/test/fixtures/render_app/views/render_block_haml.haml +4 -0
  42. data/test/fixtures/render_app/views/render_block_slim.slim +4 -0
  43. data/test/fixtures/render_app/views/wrong_capture_erb.erb +3 -0
  44. data/test/fixtures/render_app/views/wrong_capture_haml.haml +2 -0
  45. data/test/fixtures/render_app/views/wrong_capture_slim.slim +2 -0
  46. data/test/test_asset_tag_helpers.rb +34 -0
  47. data/test/test_form_builder.rb +10 -0
  48. data/test/test_form_helpers.rb +18 -18
  49. data/test/test_format_helpers.rb +10 -0
  50. data/test/test_output_helpers.rb +25 -22
  51. data/test/test_render_helpers.rb +67 -0
  52. data/test/test_tag_helpers.rb +9 -0
  53. metadata +38 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ef68c8d06903529b8d8f51c05b1290487a392463
4
- data.tar.gz: e7a25b76bffb3892afd7f4a4820e0507670c608a
3
+ metadata.gz: 4f08fb60a16abffb90ddf4fcef2e7a266bc7f892
4
+ data.tar.gz: 768407b42f56ce9536202c5e7acc686be5d91357
5
5
  SHA512:
6
- metadata.gz: 66cda324d865e815ec7bbdd1e93ac7dae958d5ec3db1e7b1f53a409f2aa825c76c58bcab9f632da80c2dce5f48d532a04aa77f9d7c632f72fea4d7955d5b1e19
7
- data.tar.gz: c01aaa4bdb41938c530acb00cc1784490a06b462f1ff315c53514f3915a959bc76214c4a5142213e1ebe01abd5407fc498782663ea811368db9c2a9fc0b61671
6
+ metadata.gz: 0eb4f7c593554199c8700143ec9cf4e8c45ebd3cf6b4c8e7555e04e31d7e6985f54945fded466dd62f4a34b7ad987c01536054ebc34977a7c304a9c562b3b194
7
+ data.tar.gz: 452ddb85c692e2af46f6f806d85bdfd7c65ce9f71d6f648aba0ce92008340cde2ebe9ac40ecfa67eac64e6479e358e4a8f18f086f933b721c26f423e1cbec5fa
data/README.rdoc CHANGED
@@ -102,9 +102,9 @@ For more information on using asset helpers, check out the guide for
102
102
  Form helpers are the 'standard' form tag helpers you would come to expect when building forms. A simple
103
103
  example of constructing a non-object form would be:
104
104
 
105
- - form_tag '/destroy', :class => 'destroy-form', :method => 'delete' do
105
+ = form_tag '/destroy', :class => 'destroy-form', :method => 'delete' do
106
106
  = flash_tag(:notice)
107
- - field_set_tag do
107
+ = field_set_tag do
108
108
  %p
109
109
  = label_tag :username, :class => 'first'
110
110
  = text_field_tag :username, :value => params[:username]
@@ -116,7 +116,7 @@ example of constructing a non-object form would be:
116
116
  = select_tag :strategy, :options => ['delete', 'destroy'], :selected => 'delete'
117
117
  %p
118
118
  = check_box_tag :confirm_delete
119
- - field_set_tag(:class => 'buttons') do
119
+ = field_set_tag(:class => 'buttons') do
120
120
  = submit_tag "Remove"
121
121
 
122
122
  For more information on using form helpers, check out the guide for
@@ -129,7 +129,7 @@ using a simple, intuitive syntax.
129
129
 
130
130
  A form_for using these basic fields might look like:
131
131
 
132
- - form_for @user, '/register', :id => 'register' do |f|
132
+ = form_for @user, '/register', :id => 'register' do |f|
133
133
  = f.error_messages
134
134
  %p
135
135
  = f.label :username, :caption => "Nickname"
@@ -147,7 +147,7 @@ A form_for using these basic fields might look like:
147
147
  = f.label :color, :caption => "Favorite Color?"
148
148
  = f.select :color, :options => ['red', 'black']
149
149
  %p
150
- - fields_for @user.location do |location|
150
+ = fields_for @user.location do |location|
151
151
  = location.text_field :street
152
152
  = location.text_field :city
153
153
  %p
@@ -159,7 +159,7 @@ There is also an additional StandardFormBuilder which builds on the abstract fie
159
159
 
160
160
  A form_for using these standard fields might be:
161
161
 
162
- - form_for @user, '/register', :id => 'register' do |f|
162
+ = form_for @user, '/register', :id => 'register' do |f|
163
163
  = f.error_messages
164
164
  = f.text_field_block :name, :caption => "Full name"
165
165
  = f.text_field_block :email
@@ -4,9 +4,12 @@ module Padrino
4
4
  # Helpers related to producing assets (images, stylesheets, js, etc) within templates.
5
5
  #
6
6
  module AssetTagHelpers
7
- FRAGMENT_HASH = "#".html_safe.freeze
8
7
  APPEND_ASSET_EXTENSIONS = ["js", "css"]
9
8
  ABSOLUTE_URL_PATTERN = %r{^(https?://)}
9
+ ASSET_FOLDERS = {
10
+ :js => 'javascripts',
11
+ :css => 'stylesheets',
12
+ }
10
13
 
11
14
  ##
12
15
  # Creates a div to display the flash of given type if it exists.
@@ -79,34 +82,16 @@ module Padrino
79
82
  options = args.extract_options!
80
83
  fragment = options.delete(:anchor).to_s if options[:anchor]
81
84
  fragment = options.delete(:fragment).to_s if options[:fragment]
82
-
83
- url = ActiveSupport::SafeBuffer.new
84
- if block_given?
85
- if args[0]
86
- url.concat(args[0])
87
- url.concat(FRAGMENT_HASH).concat(fragment) if fragment
88
- else
89
- url.concat(FRAGMENT_HASH)
90
- url.concat(fragment) if fragment
91
- end
92
- options.reverse_merge!(:href => url)
93
- link_content = capture_html(&block)
94
- return '' unless parse_conditions(url, options)
95
- result_link = content_tag(:a, link_content, options)
96
- block_is_template?(block) ? concat_content(result_link) : result_link
85
+ name = block_given? ? '' : args.shift
86
+ if url = args.first
87
+ url << '#' << fragment if fragment
97
88
  else
98
- if args[1]
99
- url.concat(args[1])
100
- url.safe_concat(FRAGMENT_HASH).concat(fragment) if fragment
101
- else
102
- url = FRAGMENT_HASH
103
- url.concat(fragment) if fragment
104
- end
105
- name = args[0]
106
- return name unless parse_conditions(url, options)
107
- options.reverse_merge!(:href => url)
108
- content_tag(:a, name, options)
89
+ url = '#'
90
+ url << fragment if fragment
109
91
  end
92
+ options.reverse_merge!(:href => url)
93
+ return name unless parse_conditions(url, options)
94
+ block_given? ? content_tag(:a, options, &block) : content_tag(:a, name, options)
110
95
  end
111
96
 
112
97
  ##
@@ -312,9 +297,13 @@ module Padrino
312
297
  # # Generates: /images/example.jpg?1269008689
313
298
  # asset_path :images, 'example.jpg'
314
299
  #
315
- def asset_path(kind, source)
300
+ # # Generates: /uploads/file.ext?1269008689
301
+ # asset_path 'uploads/file.ext'
302
+ #
303
+ def asset_path(kind, source = nil)
304
+ kind, source = source, kind if source.nil?
316
305
  source = asset_normalize_extension(kind, URI.escape(source.to_s))
317
- return source if source =~ ABSOLUTE_URL_PATTERN || source =~ /^\// # absolute source
306
+ return source if source =~ ABSOLUTE_URL_PATTERN || source =~ /^\//
318
307
  source = File.join(asset_folder_name(kind), source)
319
308
  timestamp = asset_timestamp(source)
320
309
  result_path = uri_root_path(source)
@@ -322,6 +311,7 @@ module Padrino
322
311
  end
323
312
 
324
313
  private
314
+
325
315
  ##
326
316
  # Returns the URI root of the application with optional paths appended.
327
317
  #
@@ -353,16 +343,19 @@ module Padrino
353
343
  ###
354
344
  # Returns the asset folder given a kind.
355
345
  #
346
+ # Configureable by setting kind_asset_folder.
347
+ #
356
348
  # @example
357
349
  # asset_folder_name(:css) => 'stylesheets'
358
350
  # asset_folder_name(:js) => 'javascripts'
359
351
  # asset_folder_name(:images) => 'images'
352
+ # asset_folder_name(:abrakadabrah) => 'abrakadabrah'
360
353
  #
361
354
  def asset_folder_name(kind)
362
- case kind
363
- when :css then 'stylesheets'
364
- when :js then 'javascripts'
365
- else kind.to_s
355
+ if self.class.respond_to? "#{kind}_asset_folder"
356
+ self.class.send "#{kind}_asset_folder"
357
+ else
358
+ (ASSET_FOLDERS[kind] || kind).to_s
366
359
  end
367
360
  end
368
361
 
@@ -32,51 +32,43 @@ module Padrino
32
32
  end
33
33
 
34
34
  def text_field(field, options={})
35
- options.reverse_merge!(:value => field_value(field), :id => field_id(field))
36
- options.merge!(:class => field_error(field, options))
35
+ merge_default_options!(field, options)
37
36
  @template.text_field_tag field_name(field), options
38
37
  end
39
38
 
40
39
  def number_field(field, options={})
41
- options.reverse_merge!(:value => field_value(field), :id => field_id(field))
42
- options.merge!(:class => field_error(field, options))
40
+ merge_default_options!(field, options)
43
41
  @template.number_field_tag field_name(field), options
44
42
  end
45
43
 
46
44
  def telephone_field(field, options={})
47
- options.reverse_merge!(:value => field_value(field), :id => field_id(field))
48
- options.merge!(:class => field_error(field, options))
45
+ merge_default_options!(field, options)
49
46
  @template.telephone_field_tag field_name(field), options
50
47
  end
51
48
  alias_method :phone_field, :telephone_field
52
49
 
53
50
  def email_field(field, options={})
54
- options.reverse_merge!(:value => field_value(field), :id => field_id(field))
55
- options.merge!(:class => field_error(field, options))
51
+ merge_default_options!(field, options)
56
52
  @template.email_field_tag field_name(field), options
57
53
  end
58
54
 
59
55
  def search_field(field, options={})
60
- options.reverse_merge!(:value => field_value(field), :id => field_id(field))
61
- options.merge!(:class => field_error(field, options))
56
+ merge_default_options!(field, options)
62
57
  @template.search_field_tag field_name(field), options
63
58
  end
64
59
 
65
60
  def url_field(field, options={})
66
- options.reverse_merge!(:value => field_value(field), :id => field_id(field))
67
- options.merge!(:class => field_error(field, options))
61
+ merge_default_options!(field, options)
68
62
  @template.url_field_tag field_name(field), options
69
63
  end
70
64
 
71
65
  def text_area(field, options={})
72
- options.reverse_merge!(:value => field_value(field), :id => field_id(field))
73
- options.merge!(:class => field_error(field, options))
66
+ merge_default_options!(field, options)
74
67
  @template.text_area_tag field_name(field), options
75
68
  end
76
69
 
77
70
  def password_field(field, options={})
78
- options.reverse_merge!(:value => field_value(field), :id => field_id(field))
79
- options.merge!(:class => field_error(field, options))
71
+ merge_default_options!(field, options)
80
72
  @template.password_field_tag field_name(field), options
81
73
  end
82
74
 
@@ -246,6 +238,7 @@ module Padrino
246
238
  # Returns the object's models name.
247
239
  #
248
240
  def object_model_name(explicit_object=object)
241
+ return @options[:as] if root_form? && @options[:as].is_a?(Symbol)
249
242
  explicit_object.is_a?(Symbol) ? explicit_object : explicit_object.class.to_s.underscore.gsub(/\//, '_')
250
243
  end
251
244
 
@@ -280,7 +273,8 @@ module Padrino
280
273
  end
281
274
  variants.inject(''.html_safe) do |html, variant|
282
275
  variant[2] = "#{field_id(field)}_#{variant[1]}"
283
- html << @template.label_tag("#{field_name(field)}[]", :for => variant[2], :caption => "#{yield(variant)} #{variant[0]}")
276
+ caption = yield(variant) << ' ' << variant[0]
277
+ html << @template.label_tag("#{field_name(field)}[]", :for => variant[2], :caption => caption)
284
278
  end
285
279
  end
286
280
 
@@ -311,6 +305,11 @@ module Padrino
311
305
  :attributes_name => "#{@options[:nested][:association]}_attributes"
312
306
  }
313
307
  end
308
+
309
+ def merge_default_options!(field, options)
310
+ options.reverse_merge!(:value => field_value(field), :id => field_id(field))
311
+ options.merge!(:class => field_error(field, options))
312
+ end
314
313
  end
315
314
  end
316
315
  end