padrino-helpers 0.12.5 → 0.12.6

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 040e93999ca90d71fb40e3a334b1d2a504dfbd5c
4
- data.tar.gz: 0fcdcd695f6de6325e60a2905c3e9b4bc3f8dfcf
3
+ metadata.gz: 228ed24dba7a04edaa171094b1a4b80fc755bb69
4
+ data.tar.gz: ccb711977c96a4543ffc80e2151a2575cf2bc89a
5
5
  SHA512:
6
- metadata.gz: eb323c9277dc1a8c80ba5fb7d75bbb38797564fcd4030ca01a727814d0404cd8850bfb700894f979afd3a6c9183d12ac31948605739b005133891e0ba6ebecc6
7
- data.tar.gz: e7cc3cba87861d21228c4b66470467edadcfd0920449b6e3b5837e741388e3c7fa8f403eec8cc08f9bab68926745dc8fcfe2891c0f5636899b8394fc52ccd0f8
6
+ metadata.gz: 69fbd42b69b64106ce3845dc94411a0ec3d67d829085fe8fa5ec4920682ed0ac888703ac91c49179a2fe971a23867d1fdc3f034fe6562ec61d02bb51a83c0525
7
+ data.tar.gz: 3222dff475ea7f212338858a203d2fde7d8f2a44b72ba64235a85aa877317feae0efe88c126937aac4a909396e06accaa2c532fb45823933f4cb5f37b0692f0b
@@ -6,7 +6,6 @@ require 'active_support/core_ext/string/conversions' # to_date
6
6
  require 'active_support/option_merger' # with_options
7
7
  require 'active_support/core_ext/object/with_options' # with_options
8
8
  require 'active_support/inflector' # humanize
9
- require 'active_support/core_ext/hash/except' # Hash#except
10
9
  require 'padrino/rendering'
11
10
 
12
11
  FileSet.glob_require('padrino-helpers/**/*.rb', __FILE__)
@@ -37,7 +37,9 @@ module Padrino
37
37
  def label(field, options={}, &block)
38
38
  options[:id] ||= nil
39
39
  options[:caption] ||= I18n.t("#{model_name}.attributes.#{field}", :count => 1, :default => field.to_s.humanize, :scope => :models) + ': '
40
- @template.label_tag(field_id(field), default_options(field, options), &block)
40
+ defaults = default_options(field, options)
41
+ defaults.delete(:value)
42
+ @template.label_tag(field_id(field), defaults, &block)
41
43
  end
42
44
 
43
45
  def hidden_field(field, options={})
@@ -110,7 +112,9 @@ module Padrino
110
112
 
111
113
  def file_field(field, options={})
112
114
  self.multipart = true
113
- @template.file_field_tag field_name(field), default_options(field, options).except(:value)
115
+ defaults = default_options(field, options)
116
+ defaults.delete(:value)
117
+ @template.file_field_tag field_name(field), defaults
114
118
  end
115
119
 
116
120
  def submit(*args)
@@ -150,6 +154,13 @@ module Padrino
150
154
  [:hidden_field, :text_field, :text_area, :password_field, :file_field, :radio_button, :check_box, :select]
151
155
  end
152
156
 
157
+ ##
158
+ # Returns the human name of the field. Look that use builtin I18n.
159
+ #
160
+ def field_human_name(field)
161
+ I18n.translate("#{object_model_name}.attributes.#{field}", :count => 1, :default => field.to_s.humanize, :scope => :models)
162
+ end
163
+
153
164
  ##
154
165
  # Returns the name for the given field.
155
166
  # field_name(:username) => "user[username]"
@@ -45,7 +45,9 @@ module Padrino
45
45
  instance = builder_instance(object, options)
46
46
  # this can erect instance.multipart flag if the block calls instance.file_field
47
47
  html = capture_html(instance, &block)
48
- options = { :multipart => instance.multipart }.update(options.except(:namespace, :as))
48
+ options = { :multipart => instance.multipart }.update(options)
49
+ options.delete(:as)
50
+ options.delete(:namespace)
49
51
  form_tag(url, options) { html }
50
52
  end
51
53
 
@@ -398,8 +400,7 @@ module Padrino
398
400
  #
399
401
  def text_area_tag(name, options={})
400
402
  inner_html = TagHelpers::NEWLINE + options.delete(:value).to_s
401
- options = { :name => name, :rows => "", :cols => "" }.update(options)
402
- content_tag(:textarea, inner_html, options)
403
+ content_tag(:textarea, inner_html, { :name => name }.update(options))
403
404
  end
404
405
 
405
406
  ##
@@ -34,6 +34,11 @@ module Padrino
34
34
  /~$/ # This is for Gedit
35
35
  ] unless defined?(IGNORE_FILE_PATTERN)
36
36
 
37
+ ##
38
+ # Defines common content-type alias mappings.
39
+ #
40
+ CONTENT_TYPE_ALIASES = { :htm => :html }
41
+
37
42
  ##
38
43
  # Default options used in the resolve_template-method.
39
44
  #
@@ -221,7 +226,7 @@ module Padrino
221
226
  # This means that no engine was explicitly defined
222
227
  data, engine = resolve_template(engine, options) if data.nil?
223
228
 
224
- options[:layout] ||= false unless Rendering.engine_configurations.has_key?(engine)
229
+ ensure_rendering_engine(engine) || (options[:layout] ||= @layout || false)
225
230
 
226
231
  # Cleanup the template.
227
232
  @current_engine, engine_was = engine, @current_engine
@@ -345,13 +350,14 @@ module Padrino
345
350
  end
346
351
 
347
352
  def select_template(templates, template_path, content_type, _locale)
348
- simple_content_type = [:html, :plain].include?(content_type)
353
+ symbol = content_type_symbol(content_type)
354
+ simple_content_type = [:html, :plain].include?(symbol)
349
355
  target_path, target_engine = path_and_engine(template_path)
350
356
 
351
- templates.find{ |file,_| file.to_s == "#{target_path}.#{locale}.#{content_type}" } ||
357
+ templates.find{ |file,_| file.to_s == "#{target_path}.#{locale}.#{symbol}" } ||
352
358
  templates.find{ |file,_| file.to_s == "#{target_path}.#{locale}" && simple_content_type } ||
353
359
  templates.find{ |file,engine| engine == target_engine || File.extname(file.to_s) == ".#{target_engine}" } ||
354
- templates.find{ |file,_| file.to_s == "#{target_path}.#{content_type}" } ||
360
+ templates.find{ |file,_| file.to_s == "#{target_path}.#{symbol}" } ||
355
361
  templates.find{ |file,_| file.to_s == "#{target_path}" && simple_content_type }
356
362
  end
357
363
 
@@ -361,7 +367,24 @@ module Padrino
361
367
  path = path.chomp(extname)
362
368
  path.insert(0, '/') unless Pathname.new(path).absolute?
363
369
  path = path.squeeze('/').sub(relative, '') if relative
364
- [path.to_sym, engine]
370
+ [path.to_sym, engine.to_sym]
371
+ end
372
+
373
+ def ensure_rendering_engine(engine)
374
+ return true if settings.respond_to?(engine)
375
+ return nil unless engine == :erb
376
+ require 'erb'
377
+ rescue LoadError
378
+ else
379
+ require 'padrino/rendering/erb_template'
380
+ settings.set :erb, Padrino::Rendering.engine_configurations[:erb]
381
+ end
382
+
383
+ def content_type_symbol(type)
384
+ if defined?(::Rack::Mime::MIME_TYPES) && type.kind_of?(String)
385
+ type = ::Rack::Mime::MIME_TYPES.key(type).sub(/\./,'').to_sym
386
+ end
387
+ CONTENT_TYPE_ALIASES[type] || type
365
388
  end
366
389
  end
367
390
  end
@@ -395,13 +418,3 @@ unless defined? Padrino::Rendering::SlimTemplate
395
418
  require 'padrino/rendering/slim_template'
396
419
  end
397
420
  end
398
-
399
- if Padrino::Rendering.engine_configurations.empty? && !defined?(Padrino::IGNORE_NO_RENDERING_ENGINE)
400
- warn <<-EOT
401
- WARNING: no supported rendering engine found. To use Padrino::Helpers and
402
- Padrino::Rendering properly you should include `gem 'erubis'`, `gem 'haml'`
403
- or `gem 'slim'` in your Gemfile. If you are confident about using
404
- Padrino::Helpers without Padrino::Rendering, please define constant
405
- `Padrino::IGNORE_NO_RENDERING_ENGINE = true` before `require 'padrino-helpers'`.
406
- EOT
407
- end
@@ -0,0 +1,40 @@
1
+ module Padrino
2
+ module Rendering
3
+ class SafeERB < ::ERB
4
+ def set_eoutvar(compiler, eoutvar = '_erbout')
5
+ compiler.put_cmd = "#{eoutvar}.safe_concat"
6
+ compiler.insert_cmd = "#{eoutvar}.concat"
7
+ compiler.pre_cmd = ["#{eoutvar} = ActiveSupport::SafeBuffer.new"]
8
+ compiler.post_cmd = ["#{eoutvar}.force_encoding(__ENCODING__)"]
9
+ end
10
+ end
11
+
12
+ class ERBTemplate < Tilt::ERBTemplate
13
+ def render(*args)
14
+ app = args.first
15
+ app_class = app.class
16
+ @is_padrino_app = (defined?(Padrino::Application) && app.kind_of?(Padrino::Application)) ||
17
+ (app_class.respond_to?(:erb) && app_class.erb[:safe_buffer])
18
+ super
19
+ end
20
+
21
+ def prepare
22
+ @outvar = options[:outvar] || self.class.default_output_variable
23
+ options[:trim] = '<>' if !(options[:trim] == false) && (options[:trim].nil? || options[:trim] == true)
24
+ @engine = SafeERB.new(data, options[:safe], options[:trim], @outvar)
25
+ end
26
+
27
+ def precompiled_preamble(locals)
28
+ original = super
29
+ return original unless @is_padrino_app
30
+ "__in_erb_template = true\n" << original
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ Tilt.prefer(Padrino::Rendering::ERBTemplate, :erb)
37
+
38
+ Padrino::Rendering.engine_configurations[:erb] = {
39
+ :safe_buffer => true
40
+ }
@@ -68,7 +68,7 @@ class MiniTest::Spec
68
68
  path = "/views/#{name}"
69
69
  path += ".#{options.delete(:locale)}" if options[:locale].present?
70
70
  path += ".#{options[:format]}" if options[:format].present?
71
- path += ".erb" unless options[:format].to_s =~ /erb|slim|haml|rss|atom|builder/
71
+ path += ".erb" unless options[:format].to_s =~ /erb|slim|haml|rss|atom|builder|liquid/
72
72
  path += ".builder" if options[:format].to_s =~ /rss|atom/
73
73
  file = File.dirname(__FILE__) + path
74
74
  File.open(file, 'w') { |io| io.write content }
@@ -1,5 +1,6 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/helper')
2
2
  require 'slim'
3
+ require 'liquid'
3
4
 
4
5
  describe "Rendering" do
5
6
  def setup
@@ -87,6 +88,26 @@ describe "Rendering" do
87
88
  assert_equal "js file", body
88
89
  end
89
90
 
91
+ it 'should set and restore layout in controllers' do
92
+ create_layout :boo, "boo is a <%= yield %>"
93
+ create_layout :moo, "moo is a <%= yield %>"
94
+ create_view :foo, "liquid file", :format => :liquid
95
+ mock_app do
96
+ layout :boo
97
+ controller :moo do
98
+ layout :moo
99
+ get('/liquid') { render :foo }
100
+ end
101
+ controller :boo do
102
+ get('/liquid') { render :foo }
103
+ end
104
+ end
105
+ get "/moo/liquid"
106
+ assert_equal "moo is a liquid file", body
107
+ get "/boo/liquid"
108
+ assert_equal "boo is a liquid file", body
109
+ end
110
+
90
111
  it 'should use correct layout for each format' do
91
112
  create_layout :application, "this is an <%= yield %>"
92
113
  create_layout :application, "document start <%= yield %> end", :format => :xml
@@ -116,6 +137,20 @@ describe "Rendering" do
116
137
  assert_equal "html file", body
117
138
  end
118
139
 
140
+ it 'should find proper templates when content_type is set by string' do
141
+ create_layout :error, "layout<%= yield %>"
142
+ create_view :e404, "404 file"
143
+
144
+ mock_app do
145
+ not_found do
146
+ content_type 'text/html'
147
+ render 'e404', :layout => :error
148
+ end
149
+ end
150
+ get '/missing'
151
+ assert_equal 'layout404 file', body
152
+ end
153
+
119
154
  it 'should not use html file when DEFAULT_RENDERING_OPTIONS[:strict_format] == true' do
120
155
  create_layout :foo, "html file", :format => :html
121
156
 
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.12.5
4
+ version: 0.12.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Padrino Team
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2015-03-04 00:00:00.000000000 Z
14
+ date: 2016-05-09 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: padrino-support
@@ -19,46 +19,46 @@ dependencies:
19
19
  requirements:
20
20
  - - '='
21
21
  - !ruby/object:Gem::Version
22
- version: 0.12.5
22
+ version: 0.12.6
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - '='
28
28
  - !ruby/object:Gem::Version
29
- version: 0.12.5
29
+ version: 0.12.6
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: tilt
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  requirements:
34
- - - ~>
34
+ - - "~>"
35
35
  - !ruby/object:Gem::Version
36
36
  version: 1.4.1
37
37
  type: :runtime
38
38
  prerelease: false
39
39
  version_requirements: !ruby/object:Gem::Requirement
40
40
  requirements:
41
- - - ~>
41
+ - - "~>"
42
42
  - !ruby/object:Gem::Version
43
43
  version: 1.4.1
44
44
  - !ruby/object:Gem::Dependency
45
45
  name: i18n
46
46
  requirement: !ruby/object:Gem::Requirement
47
47
  requirements:
48
- - - ~>
48
+ - - "~>"
49
49
  - !ruby/object:Gem::Version
50
50
  version: '0.6'
51
- - - '>='
51
+ - - ">="
52
52
  - !ruby/object:Gem::Version
53
53
  version: 0.6.7
54
54
  type: :runtime
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  requirements:
58
- - - ~>
58
+ - - "~>"
59
59
  - !ruby/object:Gem::Version
60
60
  version: '0.6'
61
- - - '>='
61
+ - - ">="
62
62
  - !ruby/object:Gem::Version
63
63
  version: 0.6.7
64
64
  description: Tag helpers, asset helpers, form helpers, form builders and many more
@@ -69,9 +69,9 @@ extensions: []
69
69
  extra_rdoc_files:
70
70
  - README.rdoc
71
71
  files:
72
- - .document
73
- - .gitignore
74
- - .yardopts
72
+ - ".document"
73
+ - ".gitignore"
74
+ - ".yardopts"
75
75
  - LICENSE.txt
76
76
  - README.rdoc
77
77
  - Rakefile
@@ -116,6 +116,7 @@ files:
116
116
  - lib/padrino-helpers/tag_helpers.rb
117
117
  - lib/padrino-helpers/translation_helpers.rb
118
118
  - lib/padrino/rendering.rb
119
+ - lib/padrino/rendering/erb_template.rb
119
120
  - lib/padrino/rendering/erubis_template.rb
120
121
  - lib/padrino/rendering/haml_template.rb
121
122
  - lib/padrino/rendering/slim_template.rb
@@ -224,22 +225,22 @@ licenses:
224
225
  metadata: {}
225
226
  post_install_message:
226
227
  rdoc_options:
227
- - --charset=UTF-8
228
+ - "--charset=UTF-8"
228
229
  require_paths:
229
230
  - lib
230
231
  required_ruby_version: !ruby/object:Gem::Requirement
231
232
  requirements:
232
- - - '>='
233
+ - - ">="
233
234
  - !ruby/object:Gem::Version
234
235
  version: '0'
235
236
  required_rubygems_version: !ruby/object:Gem::Requirement
236
237
  requirements:
237
- - - '>='
238
+ - - ">="
238
239
  - !ruby/object:Gem::Version
239
240
  version: 1.3.6
240
241
  requirements: []
241
242
  rubyforge_project: padrino-helpers
242
- rubygems_version: 2.0.6
243
+ rubygems_version: 2.4.8
243
244
  signing_key:
244
245
  specification_version: 4
245
246
  summary: Helpers for padrino