padrino-helpers 0.10.2 → 0.10.3

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.
Files changed (34) hide show
  1. data/.document +3 -3
  2. data/.yardopts +1 -0
  3. data/{LICENSE → LICENSE.txt} +0 -0
  4. data/README.rdoc +3 -3
  5. data/lib/padrino-helpers.rb +21 -14
  6. data/lib/padrino-helpers/asset_tag_helpers.rb +163 -34
  7. data/lib/padrino-helpers/form_builder/abstract_form_builder.rb +1 -1
  8. data/lib/padrino-helpers/form_helpers.rb +287 -134
  9. data/lib/padrino-helpers/format_helpers.rb +135 -17
  10. data/lib/padrino-helpers/locale/lv.yml +103 -0
  11. data/lib/padrino-helpers/locale/zh_cn.yml +1 -1
  12. data/lib/padrino-helpers/number_helpers.rb +73 -59
  13. data/lib/padrino-helpers/output_helpers.rb +59 -15
  14. data/lib/padrino-helpers/output_helpers/abstract_handler.rb +15 -20
  15. data/lib/padrino-helpers/output_helpers/erb_handler.rb +12 -15
  16. data/lib/padrino-helpers/output_helpers/haml_handler.rb +10 -14
  17. data/lib/padrino-helpers/output_helpers/slim_handler.rb +10 -14
  18. data/lib/padrino-helpers/render_helpers.rb +23 -7
  19. data/lib/padrino-helpers/tag_helpers.rb +41 -16
  20. data/lib/padrino-helpers/translation_helpers.rb +14 -0
  21. data/test/fixtures/markup_app/views/content_for.erb +3 -0
  22. data/test/fixtures/markup_app/views/content_for.haml +3 -0
  23. data/test/fixtures/markup_app/views/content_for.slim +3 -0
  24. data/test/helper.rb +3 -15
  25. data/test/test_asset_tag_helpers.rb +1 -1
  26. data/test/test_form_builder.rb +3 -5
  27. data/test/test_form_helpers.rb +1 -1
  28. data/test/test_format_helpers.rb +1 -1
  29. data/test/test_locale.rb +1 -1
  30. data/test/test_number_helpers.rb +1 -1
  31. data/test/test_output_helpers.rb +22 -2
  32. data/test/test_render_helpers.rb +1 -1
  33. data/test/test_tag_helpers.rb +1 -1
  34. metadata +10 -8
@@ -7,8 +7,8 @@ module Padrino
7
7
  end
8
8
 
9
9
  ##
10
- # Module used to detect in vanilla sinatra apps the current engine
11
- #
10
+ # Module used to detect the current engine in vanilla sinatra apps.
11
+ # @private
12
12
  module SinatraCurrentEngine
13
13
  attr_reader :current_engine
14
14
 
@@ -21,12 +21,20 @@ module Padrino
21
21
  end
22
22
 
23
23
  ##
24
- # Captures the html from a block of template code for any available handler
24
+ # Captures the html from a block of template code for any available handler.
25
+ #
26
+ # @param [Object] *args
27
+ # Objects yield to the captured block
28
+ # @param [Proc] &block
29
+ # Template code to capture as html
25
30
  #
26
- # ==== Examples
31
+ # @return [String] Captured html resulting from the block
27
32
  #
33
+ # @example
28
34
  # capture_html(&block) => "...html..."
35
+ # capture_html(object_for_block, &block) => "...html..."
29
36
  #
37
+ # @api semipublic
30
38
  def capture_html(*args, &block)
31
39
  handler = find_proper_handler
32
40
  captured_html = ""
@@ -40,12 +48,15 @@ module Padrino
40
48
  alias :capture :capture_html
41
49
 
42
50
  ##
43
- # Outputs the given text to the templates buffer directly
51
+ # Outputs the given text to the templates buffer directly.
44
52
  #
45
- # ==== Examples
53
+ # @param [String] text
54
+ # Text to concatenate to the buffer.
46
55
  #
56
+ # @example
47
57
  # concat_content("This will be output to the template buffer")
48
58
  #
59
+ # @api semipublic
49
60
  def concat_content(text="")
50
61
  handler = find_proper_handler
51
62
  if handler && handler.is_type?
@@ -58,12 +69,17 @@ module Padrino
58
69
 
59
70
  ##
60
71
  # Returns true if the block is from a supported template type; false otherwise.
61
- # Used to determine if html should be returned or concatenated to the view
72
+ # Used to determine if html should be returned or concatenated to the view.
62
73
  #
63
- # ==== Examples
74
+ # @param [Block] block
75
+ # Determine if this block is a view template.
64
76
  #
65
- # block_is_template?(block)
77
+ # @example
78
+ # block_is_template?(block) => true
66
79
  #
80
+ # @return [Boolean] True if the block is a template; false otherwise.
81
+ #
82
+ # @api semipublic
67
83
  def block_is_template?(block)
68
84
  handler = find_proper_handler
69
85
  block && handler && handler.block_is_type?(block)
@@ -73,27 +89,57 @@ module Padrino
73
89
  # Capture a block or text of content to be rendered at a later time.
74
90
  # Your blocks can also receive values, which are passed to them by <tt>yield_content</tt>
75
91
  #
76
- # ==== Examples
92
+ # @overload content_for(key, content)
93
+ # @param [Symbol] key Name of your key for the content yield.
94
+ # @param [String] content Text to be stored for this key.
95
+ # @overload content_for(key, &block)
96
+ # @param [Symbol] key Name of your key for the content yield.
97
+ # @param [Proc] block Block to be stored as content for this key.
77
98
  #
99
+ # @example
78
100
  # content_for(:name) { ...content... }
79
101
  # content_for(:name) { |name| ...content... }
80
102
  # content_for(:name, "I'm Jeff")
81
103
  #
104
+ # @api public
82
105
  def content_for(key, content = nil, &block)
83
106
  content_blocks[key.to_sym] << (block_given? ? block : Proc.new { content })
84
107
  end
85
108
 
109
+ ##
110
+ # Is there a content block for a given key?
111
+ #
112
+ # @param [Symbol] key
113
+ # Name of content to yield
114
+ #
115
+ # @return [TrueClass,FalseClass] Result html for the given +key+
116
+ #
117
+ # @example
118
+ # content_for? :header => true
119
+ #
120
+ # @api public
121
+ def content_for?(key)
122
+ content_blocks[key.to_sym].present?
123
+ end
124
+
86
125
  ##
87
126
  # Render the captured content blocks for a given key.
88
127
  # You can also pass values to the content blocks by passing them
89
128
  # as arguments after the key.
90
129
  #
91
- # ==== Examples
130
+ # @param [Symbol] key
131
+ # Name of content to yield
132
+ # @param *args
133
+ # Values to pass to the content block
92
134
  #
135
+ # @return [String] Result html for the given +key+
136
+ #
137
+ # @example
93
138
  # yield_content :include
94
139
  # yield_content :head, "param1", "param2"
95
140
  # yield_content(:title) || "My page title"
96
141
  #
142
+ # @api public
97
143
  def yield_content(key, *args)
98
144
  blocks = content_blocks[key.to_sym]
99
145
  return nil if blocks.empty?
@@ -104,8 +150,7 @@ module Padrino
104
150
  ##
105
151
  # Retrieves content_blocks stored by content_for or within yield_content
106
152
  #
107
- # ==== Examples
108
- #
153
+ # @example
109
154
  # content_blocks[:name] => ['...', '...']
110
155
  #
111
156
  def content_blocks
@@ -116,8 +161,7 @@ module Padrino
116
161
  # Retrieves the template handler for the given output context.
117
162
  # Can handle any output related to capturing or concating in a given template.
118
163
  #
119
- # ==== Examples
120
- #
164
+ # @example
121
165
  # find_proper_handler => <OutputHelpers::HamlHandler>
122
166
  #
123
167
  def find_proper_handler
@@ -4,10 +4,10 @@ module Padrino
4
4
  ##
5
5
  # Returns the list of all available template handlers
6
6
  #
7
- # ==== Examples
8
- #
7
+ # @example
9
8
  # OutputHelpers.handlers => [<OutputHelpers::HamlHandler>, <OutputHelpers::ErbHandler>]
10
9
  #
10
+ # @private
11
11
  def self.handlers
12
12
  @_template_handlers ||= []
13
13
  end
@@ -15,14 +15,15 @@ module Padrino
15
15
  ##
16
16
  # Registers a new handler as available to the output helpers
17
17
  #
18
- # ==== Examples
19
- #
18
+ # @example
20
19
  # OutputHelpers.register(OutputHelpers::HamlHandler)
21
20
  #
21
+ # @private
22
22
  def self.register(handler)
23
23
  handlers << handler
24
24
  end
25
25
 
26
+ # @abstract Extend this to create a template handler
26
27
  class AbstractHandler
27
28
  attr_reader :template
28
29
 
@@ -33,9 +34,8 @@ module Padrino
33
34
  ##
34
35
  # Returns extension of the template
35
36
  #
36
- # ==== Examples
37
- #
38
- # @handler.template_extension => "erb"
37
+ # @example
38
+ # @handler.template_extension => "erb"
39
39
  #
40
40
  def template_extension
41
41
  caller.find { |c| c =~ /\/views\// }[/\.([\w]*?)\:/, 1] rescue nil
@@ -46,8 +46,7 @@ module Padrino
46
46
  ##
47
47
  # Returns an array of engines used for the template
48
48
  #
49
- # ==== Examples
50
- #
49
+ # @example
51
50
  # @handler.engines => [:erb, :erubis]
52
51
  #
53
52
  def engines
@@ -57,9 +56,8 @@ module Padrino
57
56
  ##
58
57
  # Returns true if the current template type is same as this handlers; false otherwise.
59
58
  #
60
- # ==== Examples
61
- #
62
- # @handler.is_type? => true
59
+ # @example
60
+ # @handler.is_type? => true
63
61
  #
64
62
  def is_type?
65
63
  # Implemented in subclass
@@ -68,9 +66,8 @@ module Padrino
68
66
  ##
69
67
  # Returns true if the block given is of the handler's template type; false otherwise.
70
68
  #
71
- # ==== Examples
72
- #
73
- # @handler.block_is_type?(block) => true
69
+ # @example
70
+ # @handler.block_is_type?(block) => true
74
71
  #
75
72
  def block_is_type?(block)
76
73
  # Implemented in subclass
@@ -79,9 +76,8 @@ module Padrino
79
76
  ##
80
77
  # Captures the html from a block of template code for this handler
81
78
  #
82
- # ==== Examples
83
- #
84
- # @handler.capture_from_template(&block) => "...html..."
79
+ # @example
80
+ # @handler.capture_from_template(&block) => "...html..."
85
81
  #
86
82
  def capture_from_template(*args, &block)
87
83
  # Implemented in subclass
@@ -90,8 +86,7 @@ module Padrino
90
86
  ##
91
87
  # Outputs the given text to the templates buffer directly
92
88
  #
93
- # ==== Examples
94
- #
89
+ # @example
95
90
  # @handler.concat_to_template("This will be output to the template buffer")
96
91
  #
97
92
  def concat_to_template(text="")
@@ -12,9 +12,8 @@ module Padrino
12
12
  ##
13
13
  # Returns true if the current template type is same as this handlers; false otherwise.
14
14
  #
15
- # ==== Examples
16
- #
17
- # @handler.is_type? => true
15
+ # @example
16
+ # @handler.is_type? => true
18
17
  #
19
18
  def is_type?
20
19
  !self.output_buffer.nil?
@@ -22,9 +21,8 @@ module Padrino
22
21
 
23
22
  # Captures the html from a block of template code for this handler
24
23
  #
25
- # ==== Examples
26
- #
27
- # @handler.capture_from_template(&block) => "...html..."
24
+ # @example
25
+ # @handler.capture_from_template(&block) => "...html..."
28
26
  #
29
27
  def capture_from_template(*args, &block)
30
28
  self.output_buffer, _buf_was = "", self.output_buffer
@@ -37,8 +35,7 @@ module Padrino
37
35
  ##
38
36
  # Outputs the given text to the templates buffer directly
39
37
  #
40
- # ==== Examples
41
- #
38
+ # @example
42
39
  # @handler.concat_to_template("This will be output to the template buffer")
43
40
  #
44
41
  def concat_to_template(text="")
@@ -49,9 +46,8 @@ module Padrino
49
46
  ##
50
47
  # Returns true if the block given is of the handler's template type; false otherwise.
51
48
  #
52
- # ==== Examples
53
- #
54
- # @handler.block_is_type?(block) => true
49
+ # @example
50
+ # @handler.block_is_type?(block) => true
55
51
  #
56
52
  def block_is_type?(block)
57
53
  is_type? || (block && eval('defined?(__in_erb_template)', block.binding))
@@ -60,8 +56,7 @@ module Padrino
60
56
  ##
61
57
  # Returns an array of engines used for the template
62
58
  #
63
- # ==== Examples
64
- #
59
+ # @example
65
60
  # @handler.engines => [:erb, :erubis]
66
61
  #
67
62
  def engines
@@ -69,11 +64,13 @@ module Padrino
69
64
  end
70
65
 
71
66
  protected
67
+
72
68
  def output_buffer=(val)
73
69
  template.instance_variable_set(:@_out_buf, val)
74
70
  end
75
- end # ErbHandler
76
- OutputHelpers.register(ErbHandler)
71
+ end # ErbHandler
72
+
73
+ OutputHelpers.register(ErbHandler)
77
74
  end # OutputHelpers
78
75
  end # Helpers
79
76
  end # Padrino
@@ -5,9 +5,8 @@ module Padrino
5
5
  ##
6
6
  # Returns true if the current template type is same as this handlers; false otherwise.
7
7
  #
8
- # ==== Examples
9
- #
10
- # @handler.is_type? => true
8
+ # @example
9
+ # @handler.is_type? => true
11
10
  #
12
11
  def is_type?
13
12
  template.respond_to?(:is_haml?) && template.is_haml?
@@ -16,9 +15,8 @@ module Padrino
16
15
  ##
17
16
  # Returns true if the block given is of the handler's template type; false otherwise.
18
17
  #
19
- # ==== Examples
20
- #
21
- # @handler.block_is_type?(block) => true
18
+ # @example
19
+ # @handler.block_is_type?(block) => true
22
20
  #
23
21
  def block_is_type?(block)
24
22
  template.block_is_haml?(block)
@@ -26,9 +24,8 @@ module Padrino
26
24
 
27
25
  # Captures the html from a block of template code for this handler
28
26
  #
29
- # ==== Examples
30
- #
31
- # @handler.capture_from_template(&block) => "...html..."
27
+ # @example
28
+ # @handler.capture_from_template(&block) => "...html..."
32
29
  #
33
30
  def capture_from_template(*args, &block)
34
31
  eval("_hamlout ||= @haml_buffer", block.binding) # this is for rbx
@@ -38,8 +35,7 @@ module Padrino
38
35
  ##
39
36
  # Outputs the given text to the templates buffer directly
40
37
  #
41
- # ==== Examples
42
- #
38
+ # @example
43
39
  # @handler.concat_to_template("This will be output to the template buffer")
44
40
  #
45
41
  def concat_to_template(text="")
@@ -50,14 +46,14 @@ module Padrino
50
46
  ##
51
47
  # Returns an array of engines used for the template
52
48
  #
53
- # ==== Examples
54
- #
55
- # @handler.engines => [:erb, :erubis]
49
+ # @example
50
+ # @handler.engines => [:haml]
56
51
  #
57
52
  def engines
58
53
  @_engines ||= [:haml]
59
54
  end
60
55
  end # HamlHandler
56
+
61
57
  OutputHelpers.register(HamlHandler)
62
58
  end # OutputHelpers
63
59
  end # Helpers
@@ -15,9 +15,8 @@ module Padrino
15
15
  ##
16
16
  # Returns true if the current template type is same as this handlers; false otherwise.
17
17
  #
18
- # ==== Examples
19
- #
20
- # @handler.is_type? => true
18
+ # @example
19
+ # @handler.is_type? => true
21
20
  #
22
21
  def is_type?
23
22
  !self.output_buffer.nil?
@@ -25,9 +24,8 @@ module Padrino
25
24
 
26
25
  # Captures the html from a block of template code for this handler
27
26
  #
28
- # ==== Examples
29
- #
30
- # @handler.capture_from_template(&block) => "...html..."
27
+ # @example
28
+ # @handler.capture_from_template(&block) => "...html..."
31
29
  #
32
30
  def capture_from_template(*args, &block)
33
31
  self.output_buffer, _buf_was = "", self.output_buffer
@@ -40,8 +38,7 @@ module Padrino
40
38
  ##
41
39
  # Outputs the given text to the templates buffer directly
42
40
  #
43
- # ==== Examples
44
- #
41
+ # @example
45
42
  # @handler.concat_to_template("This will be output to the template buffer")
46
43
  #
47
44
  def concat_to_template(text="")
@@ -52,9 +49,8 @@ module Padrino
52
49
  ##
53
50
  # Returns true if the block given is of the handler's template type; false otherwise.
54
51
  #
55
- # ==== Examples
56
- #
57
- # @handler.block_is_type?(block) => true
52
+ # @example
53
+ # @handler.block_is_type?(block) => true
58
54
  #
59
55
  def block_is_type?(block)
60
56
  is_type? || (block && eval('defined? __in_erb_template', block.binding))
@@ -63,8 +59,7 @@ module Padrino
63
59
  ##
64
60
  # Returns an array of engines used for the template
65
61
  #
66
- # ==== Examples
67
- #
62
+ # @example
68
63
  # @handler.engines => [:erb, :erubis]
69
64
  #
70
65
  def engines
@@ -75,7 +70,8 @@ module Padrino
75
70
  def output_buffer=(val)
76
71
  template.instance_variable_set(:@_out_buf, val)
77
72
  end
78
- end # ErbHandler
73
+ end # SlimHandler
74
+
79
75
  OutputHelpers.register(SlimHandler)
80
76
  end # OutputHelpers
81
77
  end # Helpers
@@ -2,23 +2,39 @@ module Padrino
2
2
  module Helpers
3
3
  module RenderHelpers
4
4
  ##
5
- # Partials implementation which includes collections support
5
+ # Render a partials with collections support
6
6
  #
7
- # ==== Examples
7
+ # @param [String] template
8
+ # Relative path to partial template.
9
+ # @param [Hash] options
10
+ # Options hash for rendering options.
11
+ # @option options [Object] :object
12
+ # Object rendered in partial.
13
+ # @option options [Array<Object>] :collection
14
+ # Partial is rendered for each object in this collection.
15
+ # @option options [Hash] :locals ({})
16
+ # Local variables accessible in the partial.
17
+ # @option options [Symbol] :engine
18
+ # Explicit rendering engine to use for this partial
8
19
  #
20
+ # @return [String] The html generated from this partial.
21
+ #
22
+ # @example
9
23
  # partial 'photo/item', :object => @photo
10
24
  # partial 'photo/item', :collection => @photos
11
25
  # partial 'photo/item', :locals => { :foo => :bar }
12
26
  # partial 'photo/item', :engine => :erb
13
27
  #
28
+ # @note If using this from Sinatra, pass explicit +:engine+ option
29
+ #
30
+ # @api public
14
31
  def partial(template, options={})
15
- logger.debug "PARTIAL: #{template} called" if defined?(logger)
16
32
  options.reverse_merge!(:locals => {}, :layout => false)
17
- path = template.to_s.split(File::SEPARATOR)
18
- object_name = path[-1].to_sym
19
- path[-1] = "_#{path[-1]}"
33
+ path = template.to_s.split(File::SEPARATOR)
34
+ object_name = path[-1].to_sym
35
+ path[-1] = "_#{path[-1]}"
20
36
  explicit_engine = options.delete(:engine)
21
- template_path = File.join(path).to_sym
37
+ template_path = File.join(path).to_sym
22
38
  raise 'Partial collection specified but is nil' if options.has_key?(:collection) && options[:collection].nil?
23
39
  if collection = options.delete(:collection)
24
40
  options.delete(:object)