padrino-helpers 0.16.0.pre4 → 0.16.0

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 (76) hide show
  1. checksums.yaml +4 -4
  2. data/README.rdoc +29 -29
  3. data/Rakefile +1 -1
  4. data/lib/padrino/rendering/erb_template.rb +7 -7
  5. data/lib/padrino/rendering/erubi_template.rb +4 -4
  6. data/lib/padrino/rendering/erubis_template.rb +4 -4
  7. data/lib/padrino/rendering/haml_template.rb +1 -1
  8. data/lib/padrino/rendering/hamlit_template.rb +4 -4
  9. data/lib/padrino/rendering/slim_template.rb +5 -5
  10. data/lib/padrino/rendering.rb +54 -45
  11. data/lib/padrino/safe_buffer.rb +31 -36
  12. data/lib/padrino-helpers/asset_tag_helpers.rb +48 -48
  13. data/lib/padrino-helpers/form_builder/abstract_form_builder.rb +74 -70
  14. data/lib/padrino-helpers/form_builder/standard_form_builder.rb +20 -20
  15. data/lib/padrino-helpers/form_helpers/errors.rb +14 -14
  16. data/lib/padrino-helpers/form_helpers/options.rb +25 -24
  17. data/lib/padrino-helpers/form_helpers/security.rb +5 -5
  18. data/lib/padrino-helpers/form_helpers.rb +133 -133
  19. data/lib/padrino-helpers/format_helpers.rb +61 -63
  20. data/lib/padrino-helpers/number_helpers.rb +82 -78
  21. data/lib/padrino-helpers/output_helpers/abstract_handler.rb +5 -3
  22. data/lib/padrino-helpers/output_helpers/erb_handler.rb +2 -2
  23. data/lib/padrino-helpers/output_helpers/haml_handler.rb +1 -1
  24. data/lib/padrino-helpers/output_helpers.rb +16 -17
  25. data/lib/padrino-helpers/render_helpers.rb +28 -27
  26. data/lib/padrino-helpers/tag_helpers.rb +62 -58
  27. data/lib/padrino-helpers/translation_helpers.rb +2 -2
  28. data/lib/padrino-helpers.rb +1 -2
  29. data/padrino-helpers.gemspec +17 -17
  30. data/test/fixtures/apps/render.rb +2 -2
  31. data/test/fixtures/markup_app/app.rb +9 -8
  32. data/test/fixtures/markup_app/views/button_to.erb +3 -3
  33. data/test/fixtures/markup_app/views/button_to.haml +2 -2
  34. data/test/fixtures/markup_app/views/button_to.slim +3 -3
  35. data/test/fixtures/markup_app/views/content_for.erb +4 -4
  36. data/test/fixtures/markup_app/views/content_for.haml +4 -4
  37. data/test/fixtures/markup_app/views/content_for.slim +2 -2
  38. data/test/fixtures/markup_app/views/content_tag.erb +3 -3
  39. data/test/fixtures/markup_app/views/content_tag.haml +3 -3
  40. data/test/fixtures/markup_app/views/content_tag.slim +3 -3
  41. data/test/fixtures/markup_app/views/fields_for.erb +2 -2
  42. data/test/fixtures/markup_app/views/fields_for.haml +2 -2
  43. data/test/fixtures/markup_app/views/fields_for.slim +2 -2
  44. data/test/fixtures/markup_app/views/form_for.erb +41 -41
  45. data/test/fixtures/markup_app/views/form_for.haml +41 -41
  46. data/test/fixtures/markup_app/views/form_for.slim +41 -42
  47. data/test/fixtures/markup_app/views/form_tag.erb +47 -47
  48. data/test/fixtures/markup_app/views/form_tag.haml +48 -48
  49. data/test/fixtures/markup_app/views/form_tag.slim +49 -49
  50. data/test/fixtures/markup_app/views/link_to.erb +2 -2
  51. data/test/fixtures/markup_app/views/link_to.haml +2 -2
  52. data/test/fixtures/markup_app/views/link_to.slim +2 -2
  53. data/test/fixtures/markup_app/views/meta_tag.erb +2 -2
  54. data/test/fixtures/markup_app/views/meta_tag.haml +2 -2
  55. data/test/fixtures/markup_app/views/meta_tag.slim +2 -2
  56. data/test/fixtures/markup_app/views/simple_partial.erb +1 -1
  57. data/test/fixtures/markup_app/views/simple_partial.haml +1 -1
  58. data/test/fixtures/markup_app/views/simple_partial.slim +1 -1
  59. data/test/fixtures/render_app/app.rb +13 -12
  60. data/test/fixtures/render_app/views/_unsafe.html.builder +1 -1
  61. data/test/fixtures/render_app/views/_unsafe_object.html.builder +1 -1
  62. data/test/fixtures/render_app/views/explicit_engine.haml +3 -3
  63. data/test/helper.rb +19 -19
  64. data/test/test_asset_tag_helpers.rb +133 -133
  65. data/test/test_form_builder.rb +629 -628
  66. data/test/test_form_helpers.rb +610 -610
  67. data/test/test_format_helpers.rb +66 -63
  68. data/test/test_helpers.rb +2 -2
  69. data/test/test_locale.rb +13 -11
  70. data/test/test_number_helpers.rb +65 -65
  71. data/test/test_output_helpers.rb +76 -76
  72. data/test/test_render_helpers.rb +101 -101
  73. data/test/test_rendering.rb +329 -326
  74. data/test/test_rendering_extensions.rb +4 -4
  75. data/test/test_tag_helpers.rb +55 -55
  76. metadata +6 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 05e9b76c66a9aaf28ad9829d6f9abbbb4c85cfd58b554d04cf17fd22fd49f085
4
- data.tar.gz: 2492033eb92a224e723700667afede77d65070ec52dd5957f310549d51133925
3
+ metadata.gz: 5c443286ea7c2e047d727b3b844dcf35bfe75997fde2b2310485ab1c39003c4d
4
+ data.tar.gz: eb4829b196a0b7c9d2a32e8aeaad09b91d7479af9e23c26bf6e2adcf4fa266dc
5
5
  SHA512:
6
- metadata.gz: 6c64dcd929e68215ac8e81b33e82e846dbeab8d14d6ede875a50b41505ec65e365ff153e49f07c9733e7f82691ea949c80526ee8e43937bf3eea0b6e6eb0ce8c
7
- data.tar.gz: 8baf0b35927e0b0fd9fc5da5de3ee6360266c63ed313928e0282266370d74d596e42493263e41ede78793a03efbda2ca7b9e752e3f77cda993f2f61553fa1260
6
+ metadata.gz: 50ba74a40763da8b07ee23455b22366d7776be020b7b96694f666f17120f67fe503ac08dda6e328baa69d7a2d64c08baea0534333d05f299901b43fe91a6a826
7
+ data.tar.gz: 9a6177f039e74e912974025842326f7ac96268efdc441ecc46fe3be3795d0d8a4ad098f7ebacb850a360622be644d9fd19144b3d29255a715b71d3566edd5f71
data/README.rdoc CHANGED
@@ -63,13 +63,13 @@ are three major functions for this category: <tt>tag</tt>, <tt>content_tag</tt>
63
63
  The tag and content_tag are for building arbitrary html tags with a name and specified options. If
64
64
  the tag contains 'content' within then <tt>content_tag</tt> is used. For example:
65
65
 
66
- tag(:br, :style => 'clear:both') => <br style="clear:both" />
67
- content_tag(:p, "demo", :class => 'light') => <p class="light">demo</p>
66
+ tag(:br, style: 'clear:both') => <br style="clear:both" />
67
+ content_tag(:p, "demo", class: 'light') => <p class="light">demo</p>
68
68
 
69
69
  The input_tag is used to build tags that are related to accepting input from the user:
70
70
 
71
- input_tag :text, :class => "demo" => <input type='text' class='demo' />
72
- input_tag :password, :value => "secret", :class => "demo"
71
+ input_tag :text, class: "demo" => <input type='text' class='demo' />
72
+ input_tag :password, value: "secret", class: "demo"
73
73
 
74
74
  Note that all of these accept html options and result in returning a string containing html tags.
75
75
 
@@ -90,9 +90,9 @@ simple view template:
90
90
  %body
91
91
  ...
92
92
  = flash_tag :notice
93
- %p= link_to 'Blog', '/blog', :class => 'example'
94
- %p Mail me at #{mail_to 'fake@faker.com', "Fake Email Link", :cc => "test@demo.com"}
95
- %p= image_tag 'padrino.png', :width => '35', :class => 'logo'
93
+ %p= link_to 'Blog', '/blog', class: 'example'
94
+ %p Mail me at #{mail_to 'fake@faker.com', "Fake Email Link", cc: "test@demo.com"}
95
+ %p= image_tag 'padrino.png', width: '35', class: 'logo'
96
96
 
97
97
  For more information on using asset helpers, check out the guide for
98
98
  {Padrino Helpers}[http://padrinorb.com/guides/application-helpers/asset-helpers/].
@@ -102,21 +102,21 @@ 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
107
  = field_set_tag do
108
108
  %p
109
- = label_tag :username, :class => 'first'
110
- = text_field_tag :username, :value => params[:username]
109
+ = label_tag :username, class: 'first'
110
+ = text_field_tag :username, value: params[:username]
111
111
  %p
112
- = label_tag :password, :class => 'first'
113
- = password_field_tag :password, :value => params[:password]
112
+ = label_tag :password, class: 'first'
113
+ = password_field_tag :password, value: params[:password]
114
114
  %p
115
115
  = label_tag :strategy
116
- = select_tag :strategy, :options => ['delete', 'destroy'], :selected => 'delete'
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,10 +129,10 @@ 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
- = f.label :username, :caption => "Nickname"
135
+ = f.label :username, caption: "Nickname"
136
136
  = f.text_field :username
137
137
  %p
138
138
  = f.label :email
@@ -141,17 +141,17 @@ A form_for using these basic fields might look like:
141
141
  = f.label :password
142
142
  = f.password_field :password
143
143
  %p
144
- = f.label :is_admin, :caption => "Admin User?"
144
+ = f.label :is_admin, caption: 'Admin User?'
145
145
  = f.check_box :is_admin
146
146
  %p
147
- = f.label :color, :caption => "Favorite Color?"
148
- = f.select :color, :options => ['red', 'black']
147
+ = f.label :color, caption: 'Favorite Color?'
148
+ = f.select :color, options: ['red', 'black']
149
149
  %p
150
150
  = fields_for @user.location do |location|
151
151
  = location.text_field :street
152
152
  = location.text_field :city
153
153
  %p
154
- = f.submit "Create", :class => 'button'
154
+ = f.submit 'Create', class: 'button'
155
155
 
156
156
  Forms can also accept nested attributes using `fields_for` within the form builder in recent releases. Check out the guide for {Padrino Helpers}[http://padrinorb.com/guides/application-helpers/form-builders/] to learn more about nested forms.
157
157
 
@@ -159,14 +159,14 @@ 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
- = f.text_field_block :name, :caption => "Full name"
164
+ = f.text_field_block :name, caption: "Full name"
165
165
  = f.text_field_block :email
166
166
  = f.check_box_block :remember_me
167
- = f.select_block :fav_color, :options => ['red', 'blue']
167
+ = f.select_block :fav_color, options: ['red', 'blue']
168
168
  = f.password_field_block :password
169
- = f.submit_block "Create", :class => 'button'
169
+ = f.submit_block "Create", class: 'button'
170
170
 
171
171
  and would generate this html (with each input contained in a paragraph and containing a label):
172
172
 
@@ -200,9 +200,9 @@ Format helpers also includes a number of useful text manipulation functions such
200
200
 
201
201
  simple_format("hello\nworld") # => "<p>hello<br/>world</p>"
202
202
  pluralize(2, 'person') => '2 people'
203
- word_wrap('Once upon a time', :line_width => 8) => "Once upon\na time"
204
- truncate("Once upon a time in a world far far away", :length => 8) => "Once upon..."
205
- truncate_words("Once upon a time in a world far far away", :length => 4) => "Once upon a time..."
203
+ word_wrap('Once upon a time', line_width: 8) => "Once upon\na time"
204
+ truncate("Once upon a time in a world far far away", length: 8) => "Once upon..."
205
+ truncate_words("Once upon a time in a world far far away", length: 4) => "Once upon a time..."
206
206
 
207
207
  These helpers can be invoked from any route or view within your application.
208
208
 
@@ -228,8 +228,8 @@ There is also a method which renders the first view matching the path and remove
228
228
 
229
229
  Finally, we have the all-important partials support for rendering mini-templates onto a page:
230
230
 
231
- partial 'photo/_item', :object => @photo, :locals => { :foo => 'bar' }
232
- partial 'photo/_item', :collection => @photos
231
+ partial 'photo/_item', object: @photo, locals: { foo: 'bar' }
232
+ partial 'photo/_item', collection: @photos
233
233
 
234
234
  For more information on using the render and partial helpers, check out the guide for
235
235
  {Padrino Helpers}[http://padrinorb.com/guides/application-helpers/render-helpers/].
data/Rakefile CHANGED
@@ -1 +1 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../gem_rake_helper')
1
+ require_relative '../gem_rake_helper'
@@ -3,9 +3,9 @@ module Padrino
3
3
  class SafeERB < ::ERB
4
4
  class Compiler < ::ERB::Compiler
5
5
  def add_insert_cmd(out, content)
6
- out.push("@__in_ruby_literal = true")
6
+ out.push('@__in_ruby_literal = true')
7
7
  super
8
- out.push("@__in_ruby_literal = false")
8
+ out.push('@__in_ruby_literal = false')
9
9
  end
10
10
  end
11
11
 
@@ -25,7 +25,7 @@ module Padrino
25
25
  def render(*args)
26
26
  app = args.first
27
27
  app_class = app.class
28
- @is_padrino_app = (defined?(Padrino::Application) && app.kind_of?(Padrino::Application)) ||
28
+ @is_padrino_app = (defined?(Padrino::Application) && app.is_a?(Padrino::Application)) ||
29
29
  (app_class.respond_to?(:erb) && app_class.erb[:safe_buffer])
30
30
  super
31
31
  end
@@ -33,14 +33,14 @@ module Padrino
33
33
  if ERB.instance_method(:initialize).parameters.assoc(:key) # Ruby 2.6+
34
34
  def prepare
35
35
  @outvar = options[:outvar] || self.class.default_output_variable
36
- options[:trim] = '<>' if !(options[:trim] == false) && (options[:trim].nil? || options[:trim] == true)
36
+ options[:trim] = '<>' if options[:trim] != false && (options[:trim].nil? || options[:trim] == true)
37
37
 
38
38
  @engine = SafeERB.new(data, trim_mode: options[:trim], eoutvar: @outvar)
39
39
  end
40
40
  else
41
41
  def prepare
42
42
  @outvar = options[:outvar] || self.class.default_output_variable
43
- options[:trim] = '<>' if !(options[:trim] == false) && (options[:trim].nil? || options[:trim] == true)
43
+ options[:trim] = '<>' if options[:trim] != false && (options[:trim].nil? || options[:trim] == true)
44
44
 
45
45
  @engine = SafeERB.new(data, options[:safe], options[:trim], @outvar)
46
46
  end
@@ -58,6 +58,6 @@ end
58
58
  Tilt.prefer(Padrino::Rendering::ERBTemplate, :erb)
59
59
 
60
60
  Padrino::Rendering.engine_configurations[:erb] = {
61
- :safe_buffer => true,
62
- :outvar => '@_out_buf',
61
+ safe_buffer: true,
62
+ outvar: '@_out_buf'
63
63
  }
@@ -7,7 +7,7 @@ module Padrino
7
7
  end
8
8
 
9
9
  def add_expression_result_escaped(code)
10
- @src << " #{bufvar}.safe_concat (" << code << ");"
10
+ @src << " #{bufvar}.safe_concat (" << code << ');'
11
11
  end
12
12
 
13
13
  def add_text(text)
@@ -31,7 +31,7 @@ end
31
31
  Tilt.prefer(Padrino::Rendering::ErubiTemplate, :erb)
32
32
 
33
33
  Padrino::Rendering.engine_configurations[:erb] = {
34
- :bufval => "SafeBuffer.new",
35
- :bufvar => "@_out_buf",
36
- :engine_class => Padrino::Rendering::SafeErubi
34
+ bufval: 'SafeBuffer.new',
35
+ bufvar: '@_out_buf',
36
+ engine_class: Padrino::Rendering::SafeErubi
37
37
  }
@@ -13,7 +13,7 @@ module Padrino
13
13
  def add_stmt(src, code)
14
14
  code = code.sub('end', 'nil;end') if code =~ /\A\s*end\s*\Z/
15
15
  src << code
16
- src << ';' unless code[-1] == ?\n
16
+ src << ';' unless code[-1] == "\n"
17
17
  end
18
18
 
19
19
  def add_expr_escaped(src, code)
@@ -42,7 +42,7 @@ module Padrino
42
42
  def render(*args)
43
43
  app = args.first
44
44
  app_class = app.class
45
- @is_padrino_app = (defined?(Padrino::Application) && app.kind_of?(Padrino::Application)) ||
45
+ @is_padrino_app = (defined?(Padrino::Application) && app.is_a?(Padrino::Application)) ||
46
46
  (app_class.respond_to?(:erb) && app_class.erb[:engine_class] == Padrino::Rendering::SafeEruby)
47
47
  super
48
48
  end
@@ -62,6 +62,6 @@ end
62
62
  Tilt.prefer(Padrino::Rendering::ErubisTemplate, :erb)
63
63
 
64
64
  Padrino::Rendering.engine_configurations[:erb] = {
65
- :engine_class => Padrino::Rendering::SafeEruby,
66
- :outvar => '@_out_buf',
65
+ engine_class: Padrino::Rendering::SafeEruby,
66
+ outvar: '@_out_buf'
67
67
  }
@@ -22,5 +22,5 @@ end
22
22
  Tilt.prefer(Padrino::Rendering::HamlTemplate, :haml)
23
23
 
24
24
  Padrino::Rendering.engine_configurations[:haml] = {
25
- :escape_html => true,
25
+ escape_html: true
26
26
  }
@@ -1,7 +1,7 @@
1
1
  module Padrino
2
2
  module Rendering
3
3
  class HamlitOutputBuffer < Temple::Generators::StringBuffer
4
- define_options :buffer_class => 'SafeBuffer'
4
+ define_options buffer_class: 'SafeBuffer'
5
5
 
6
6
  def call(exp)
7
7
  [preamble, compile(exp), postamble].flatten.compact.join('; '.freeze)
@@ -29,7 +29,7 @@ end
29
29
  Tilt.prefer(Padrino::Rendering::HamlitTemplate, :haml)
30
30
 
31
31
  Padrino::Rendering.engine_configurations[:haml] = {
32
- :generator => Padrino::Rendering::HamlitOutputBuffer,
33
- :buffer => "@_out_buf",
34
- :use_html_safe => true,
32
+ generator: Padrino::Rendering::HamlitOutputBuffer,
33
+ buffer: '@_out_buf',
34
+ use_html_safe: true
35
35
  }
@@ -1,7 +1,7 @@
1
1
  module Padrino
2
2
  module Rendering
3
3
  class SlimOutputBuffer < Temple::Generators::StringBuffer
4
- define_options :buffer_class => 'SafeBuffer'
4
+ define_options buffer_class: 'SafeBuffer'
5
5
 
6
6
  def call(exp)
7
7
  [preamble, compile(exp), postamble].flatten.compact.join('; '.freeze)
@@ -29,8 +29,8 @@ end
29
29
  Tilt.prefer(Padrino::Rendering::SlimTemplate, :slim)
30
30
 
31
31
  Padrino::Rendering.engine_configurations[:slim] = {
32
- :generator => Padrino::Rendering::SlimOutputBuffer,
33
- :buffer => "@_out_buf",
34
- :use_html_safe => true,
35
- :disable_capture => true,
32
+ generator: Padrino::Rendering::SlimOutputBuffer,
33
+ buffer: '@_out_buf',
34
+ use_html_safe: true,
35
+ disable_capture: true
36
36
  }
@@ -31,19 +31,21 @@ module Padrino
31
31
  # @example
32
32
  # Padrino::Rendering::IGNORE_FILE_PATTERN << /~$/
33
33
  #
34
- IGNORE_FILE_PATTERN = [
35
- /~$/ # This is for Gedit
36
- ] unless defined?(IGNORE_FILE_PATTERN)
34
+ unless defined?(IGNORE_FILE_PATTERN)
35
+ IGNORE_FILE_PATTERN = [
36
+ /~$/ # This is for Gedit
37
+ ]
38
+ end
37
39
 
38
40
  ##
39
41
  # Defines common content-type alias mappings.
40
42
  #
41
- CONTENT_TYPE_ALIASES = { :htm => :html }
43
+ CONTENT_TYPE_ALIASES = { htm: :html }
42
44
 
43
45
  ##
44
46
  # Default options used in the resolve_template-method.
45
47
  #
46
- DEFAULT_RENDERING_OPTIONS = { :strict_format => false, :raise_exceptions => true } unless defined?(DEFAULT_RENDERING_OPTIONS)
48
+ DEFAULT_RENDERING_OPTIONS = { strict_format: false, raise_exceptions: true } unless defined?(DEFAULT_RENDERING_OPTIONS)
47
49
 
48
50
  class << self
49
51
  ##
@@ -58,7 +60,7 @@ module Padrino
58
60
  def registered(app)
59
61
  if defined?(Padrino::Application) && app == Padrino::Application
60
62
  # this fail can be removed later when jRuby is not bugged and MRI19 is dropped
61
- fail 'Please, do not use `register` on Padrino::Application object, use `.dup` or subclassing'
63
+ raise 'Please, do not use `register` on Padrino::Application object, use `.dup` or subclassing'
62
64
  end
63
65
  included(app)
64
66
  engine_configurations.each do |engine, configs|
@@ -94,7 +96,7 @@ module Padrino
94
96
  #
95
97
  # @yield []
96
98
  #
97
- def layout(name=:layout, &block)
99
+ def layout(name = :layout, &block)
98
100
  return super(name, &block) if block_given?
99
101
  @layout = name
100
102
  end
@@ -107,7 +109,7 @@ module Padrino
107
109
  # @param [String, nil] layouts_path
108
110
  # The directory where the layouts are located. Defaults to #views.
109
111
  #
110
- def fetch_layout_path(given_layout, layouts_path=views)
112
+ def fetch_layout_path(given_layout, layouts_path = views)
111
113
  layout_name = (given_layout || @layout || :application).to_s
112
114
  cache_layout_path(layout_name) do
113
115
  if Pathname.new(layout_name).absolute? && Dir["#{layout_name}.*"].any? || Dir["#{layouts_path}/#{layout_name}.*"].any?
@@ -120,7 +122,7 @@ module Padrino
120
122
 
121
123
  def cache_layout_path(name)
122
124
  @_cached_layout ||= {}
123
- if !reload_templates? && path = @_cached_layout[name]
125
+ if !reload_templates? && (path = @_cached_layout[name])
124
126
  path
125
127
  else
126
128
  @_cached_layout[name] = yield(name)
@@ -131,7 +133,7 @@ module Padrino
131
133
  began_at = Time.now
132
134
  @_cached_templates ||= {}
133
135
  logging = defined?(settings) && settings.logging? && defined?(logger)
134
- if !reload_templates? && path = @_cached_templates[options]
136
+ if !reload_templates? && (path = @_cached_templates[options])
135
137
  logger.debug :cached, began_at, path[0] if logging
136
138
  else
137
139
  path = @_cached_templates[options] = yield(name)
@@ -169,7 +171,7 @@ module Padrino
169
171
  #
170
172
  # # => set directly the Content-Type to 'text/html'
171
173
  #
172
- def content_type(type=nil, params={})
174
+ def content_type(type = nil, params = {})
173
175
  if type
174
176
  super(type, params)
175
177
  @_content_type = type
@@ -179,19 +181,19 @@ module Padrino
179
181
 
180
182
  private
181
183
 
182
- def render_like_sinatra(engine, data, options={}, locals={}, &block)
184
+ def render_like_sinatra(engine, data, options = {}, locals = {}, &block)
183
185
  # merge app-level options
184
186
  engine_options = settings.respond_to?(engine) ? settings.send(engine) : {}
185
187
  options = engine_options.merge(options)
186
188
 
187
189
  # extract generic options
188
190
  locals = options.delete(:locals) || locals || {}
189
- views = options.delete(:views) || settings.views || "./views"
191
+ views = options.delete(:views) || settings.views || './views'
190
192
  layout = options[:layout]
191
193
  layout = false if layout.nil? && options.include?(:layout)
192
194
  eat_errors = layout.nil?
193
- layout = engine_options[:layout] if layout.nil? or (layout == true && engine_options[:layout] != false)
194
- layout = @default_layout if layout.nil? or layout == true
195
+ layout = engine_options[:layout] if layout.nil? || (layout == true && engine_options[:layout] != false)
196
+ layout = @default_layout if layout.nil? || (layout == true)
195
197
  layout_options = options.delete(:layout_options) || {}
196
198
  content_type = options.delete(:default_content_type)
197
199
  content_type = options.delete(:content_type) || content_type
@@ -215,7 +217,7 @@ module Padrino
215
217
  # render layout
216
218
  if layout
217
219
  layout_engine_options = settings.respond_to?(layout_engine) ? settings.send(layout_engine).dup : {}
218
- options = layout_engine_options.update(:views => views, :layout => false, :eat_errors => eat_errors, :scope => scope).update(layout_options)
220
+ options = layout_engine_options.update(views: views, layout: false, eat_errors: eat_errors, scope: scope).update(layout_options)
219
221
  catch(:layout_missing) { return render_like_sinatra(layout_engine, layout, options, locals) { output } }
220
222
  end
221
223
 
@@ -230,16 +232,16 @@ module Padrino
230
232
  # * Use render 'path/to/my/template' (without symbols)
231
233
  # * Use render 'path/to/my/template' (with engine lookup)
232
234
  # * Use render 'path/to/template.haml' (with explicit engine lookup)
233
- # * Use render 'path/to/template', :layout => false
234
- # * Use render 'path/to/template', :layout => false, :engine => 'haml'
235
+ # * Use render 'path/to/template', layout: false
236
+ # * Use render 'path/to/template', layout: false, engine: 'haml'
235
237
  #
236
- def render(engine, data=nil, options={}, locals={}, &block)
238
+ def render(engine, data = nil, options = {}, locals = {}, &block)
237
239
  # If engine is nil, ignore engine parameter and shift up all arguments
238
- # render nil, "index", { :layout => true }, { :localvar => "foo" }
240
+ # render nil, 'index', { layout: true }, { localvar: "foo" }
239
241
  engine, data, options = data, options, locals if engine.nil? && data
240
242
 
241
243
  # Data is a hash of options when no engine isn't explicit
242
- # render "index", { :layout => true }, { :localvar => "foo" }
244
+ # render 'index', { layout: true }, { localvar: "foo" }
243
245
  # Data is options, and options is locals in this case
244
246
  data, options, locals = nil, data, options if data.is_a?(Hash)
245
247
 
@@ -251,7 +253,7 @@ module Padrino
251
253
 
252
254
  # Cleanup the template.
253
255
  @current_engine, engine_was = engine, @current_engine
254
- @_out_buf, buf_was = SafeBuffer.new, @_out_buf
256
+ @_out_buf, buf_was = SafeBuffer.new, @_out_buf
255
257
 
256
258
  # Pass arguments to Sinatra render method.
257
259
  render_like_sinatra(engine, data, with_layout(options), locals, &block)
@@ -283,24 +285,24 @@ module Padrino
283
285
  # The template could not be found.
284
286
  #
285
287
  # @example
286
- # get "/foo", :provides => [:html, :js] do; render 'path/to/foo'; end
288
+ # get "/foo", provides: [:html, :js] do; render 'path/to/foo'; end
287
289
  # # If you request "/foo.js" with I18n.locale == :ru => [:"/path/to/foo.ru.js", :erb]
288
290
  # # If you request "/foo" with I18n.locale == :de => [:"/path/to/foo.de.haml", :haml]
289
291
  #
290
- def resolve_template(template_path, options={})
292
+ def resolve_template(template_path, options = {})
291
293
  template_path = template_path.to_s
292
294
  controller_key = respond_to?(:request) && request.respond_to?(:controller) && request.controller
293
295
  rendering_options = [template_path, content_type || :html, locale]
294
296
 
295
297
  settings.cache_template_path(["#{controller_key}/#{template_path}", rendering_options[1], rendering_options[2]]) do
296
298
  options = DEFAULT_RENDERING_OPTIONS.merge(options)
297
- view_path = options[:views] || settings.views || "./views"
299
+ view_path = options[:views] || settings.views || './views'
298
300
 
299
301
  template_candidates = glob_templates(view_path, template_path)
300
302
  selected_template = select_template(template_candidates, *rendering_options)
301
303
  selected_template ||= template_candidates.first unless options[:strict_format]
302
304
 
303
- fail TemplateNotFound, "Template '#{template_path}' not found in '#{view_path}'" if !selected_template && options[:raise_exceptions]
305
+ raise TemplateNotFound, "Template '#{template_path}' not found in '#{view_path}'" if !selected_template && options[:raise_exceptions]
304
306
  selected_template
305
307
  end
306
308
  end
@@ -312,8 +314,8 @@ module Padrino
312
314
  I18n.locale if defined?(I18n)
313
315
  end
314
316
 
315
- def resolve_layout(layout, options={})
316
- layouts_path = options[:layout_options] && options[:layout_options][:views] || options[:views] || settings.views || "./views"
317
+ def resolve_layout(layout, options = {})
318
+ layouts_path = options[:layout_options] && options[:layout_options][:views] || options[:views] || settings.views || './views'
317
319
  template_path = settings.fetch_layout_path(layout, layouts_path)
318
320
  rendering_options = [template_path, content_type || :html, locale]
319
321
 
@@ -321,7 +323,7 @@ module Padrino
321
323
  template_candidates = glob_templates(layouts_path, template_path)
322
324
  selected_template = select_template(template_candidates, *rendering_options)
323
325
 
324
- fail TemplateNotFound, "Layout '#{template_path}' not found in '#{layouts_path}'" if !selected_template && layout
326
+ raise TemplateNotFound, "Layout '#{template_path}' not found in '#{layouts_path}'" if !selected_template && layout
325
327
  selected_template
326
328
  end
327
329
  end
@@ -332,15 +334,15 @@ module Padrino
332
334
  return options if layout == false
333
335
 
334
336
  layout = @layout if !layout || layout == true
335
- return options if settings.templates.has_key?(:layout) && !layout
337
+ return options if settings.templates.key?(:layout) && !layout
336
338
 
337
- if layout.kind_of?(String) && Pathname.new(layout).absolute?
339
+ if layout.is_a?(String) && Pathname.new(layout).absolute?
338
340
  layout_path, _, layout = layout.rpartition('/')
339
341
  options[:layout_options] ||= {}
340
342
  options[:layout_options][:views] ||= layout_path
341
343
  end
342
344
  layout, layout_engine = resolve_layout(layout, options)
343
- options.update(:layout => layout, :layout_engine => layout_engine)
345
+ options.update(layout: layout, layout_engine: layout_engine)
344
346
  end
345
347
 
346
348
  def glob_templates(views_path, template_path)
@@ -349,28 +351,29 @@ module Padrino
349
351
  if respond_to?(:request) && request.respond_to?(:controller) && request.controller && Pathname.new(template_path).relative?
350
352
  parts << "{,#{request.controller}}"
351
353
  end
352
- parts << template_path.chomp(File.extname(template_path)) + '.*'
353
- Dir.glob(File.join(parts)).inject([]) do |all,file|
354
- next all if IGNORE_FILE_PATTERN.any?{ |pattern| file.to_s =~ pattern }
354
+ parts << "#{template_path.chomp(File.extname(template_path))}.*"
355
+ Dir.glob(File.join(parts)).inject([]) do |all, file|
356
+ next all if IGNORE_FILE_PATTERN.any? { |pattern| file.to_s =~ pattern }
355
357
  all << path_and_engine(file, views_path)
356
358
  end
357
359
  end
358
360
 
359
361
  def select_template(templates, template_path, content_type, _locale)
360
362
  symbol = content_type_symbol(content_type)
361
- simple_content_type = [:html, :plain].include?(symbol)
363
+ simple_content_type = %i[html plain].include?(symbol)
362
364
  target_path, target_engine = path_and_engine(template_path)
363
365
 
364
- templates.find{ |file,_| file.to_s == "#{target_path}.#{locale}.#{symbol}" } ||
365
- templates.find{ |file,_| file.to_s == "#{target_path}.#{locale}" && simple_content_type } ||
366
- templates.find{ |file,engine| engine == target_engine || File.extname(file.to_s) == ".#{target_engine}" } ||
367
- templates.find{ |file,_| file.to_s == "#{target_path}.#{symbol}" } ||
368
- templates.find{ |file,_| file.to_s == "#{target_path}" && simple_content_type }
366
+ # FIXME: We can probable iterate just 1 time over the templates
367
+ templates.find { |file, _| file.to_s == "#{target_path}.#{locale}.#{symbol}" } ||
368
+ templates.find { |file, _| file.to_s == "#{target_path}.#{locale}" && simple_content_type } ||
369
+ templates.find { |file, engine| engine == target_engine || File.extname(file.to_s) == ".#{target_engine}" } ||
370
+ templates.find { |file, _| file.to_s == "#{target_path}.#{symbol}" } ||
371
+ templates.find { |file, _| file.to_s == target_path.to_s && simple_content_type }
369
372
  end
370
373
 
371
- def path_and_engine(path, relative=nil)
374
+ def path_and_engine(path, relative = nil)
372
375
  extname = File.extname(path)
373
- engine = (extname[1..-1]||'none').to_sym
376
+ engine = (extname[1..] || 'none').to_sym
374
377
  path = path.chomp(extname)
375
378
  path.insert(0, '/') unless Pathname.new(path).absolute?
376
379
  path = path.squeeze('/').sub(relative, '') if relative
@@ -382,15 +385,16 @@ module Padrino
382
385
  return nil unless engine == :erb
383
386
  require 'erb'
384
387
  rescue LoadError
388
+ # do nothing since erb is not available
385
389
  else
386
390
  require 'padrino/rendering/erb_template'
387
391
  settings.set :erb, Padrino::Rendering.engine_configurations[:erb]
388
392
  end
389
393
 
390
394
  def content_type_symbol(type)
391
- if defined?(::Rack::Mime::MIME_TYPES) && type.kind_of?(String) &&
395
+ if defined?(::Rack::Mime::MIME_TYPES) && type.is_a?(String) &&
392
396
  ::Rack::Mime::MIME_TYPES.key(type)
393
- type = ::Rack::Mime::MIME_TYPES.key(type).sub(/\./,'').to_sym
397
+ type = ::Rack::Mime::MIME_TYPES.key(type).sub(/\./, '').to_sym
394
398
  end
395
399
  CONTENT_TYPE_ALIASES[type] || type
396
400
  end
@@ -404,6 +408,7 @@ unless defined? Padrino::Rendering::HamlTemplate
404
408
  require 'haml/helpers/xss_mods'
405
409
  require 'haml/helpers/action_view_extensions'
406
410
  rescue LoadError
411
+ # do nothing since haml is not available
407
412
  else
408
413
  require 'padrino/rendering/haml_template'
409
414
  end
@@ -413,6 +418,7 @@ unless defined? Padrino::Rendering::HamlitTemplate
413
418
  begin
414
419
  require 'hamlit'
415
420
  rescue LoadError
421
+ # do nothing since hamlit is not available
416
422
  else
417
423
  require 'padrino/rendering/hamlit_template'
418
424
  end
@@ -422,6 +428,7 @@ unless defined? Padrino::Rendering::ErubisTemplate
422
428
  begin
423
429
  require 'erubis'
424
430
  rescue LoadError
431
+ # do nothing since erubis is not available
425
432
  else
426
433
  require 'padrino/rendering/erubis_template'
427
434
  end
@@ -431,6 +438,7 @@ unless defined? Padrino::Rendering::SlimTemplate
431
438
  begin
432
439
  require 'slim'
433
440
  rescue LoadError
441
+ # do nothing since slim is not available
434
442
  else
435
443
  require 'padrino/rendering/slim_template'
436
444
  end
@@ -440,6 +448,7 @@ unless defined? Padrino::Rendering::ErubiTemplate
440
448
  begin
441
449
  require 'erubi'
442
450
  rescue LoadError
451
+ # do nothing since erubi is not available
443
452
  else
444
453
  require 'padrino/rendering/erubi_template'
445
454
  end