padrino-helpers 0.9.10 → 0.9.11

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,52 +1,5 @@
1
- require 'rubygems'
2
- require 'rake'
3
-
4
- require File.expand_path("../../padrino-core/lib/padrino-core/version.rb", __FILE__)
5
-
6
- begin
7
- require 'jeweler'
8
- Jeweler::Tasks.new do |gem|
9
- gem.name = "padrino-helpers"
10
- gem.summary = "Helpers for padrino"
11
- gem.description = "Tag helpers, asset helpers, form helpers, form builders and many more helpers for padrino"
12
- gem.email = "padrinorb@gmail.com"
13
- gem.homepage = "http://github.com/padrino/padrino-framework/tree/master/padrino-helpers"
14
- gem.authors = ["Padrino Team", "Nathan Esquenazi", "Davide D'Agostino", "Arthur Chiu"]
15
- gem.rubyforge_project = 'padrino-helpers'
16
- gem.version = Padrino.version
17
- gem.add_runtime_dependency "padrino-core", "= #{Padrino.version}"
18
- gem.add_development_dependency "haml", ">= 2.2.1"
19
- gem.add_development_dependency "shoulda", ">= 2.10.3"
20
- gem.add_development_dependency "mocha", ">= 0.9.7"
21
- gem.add_development_dependency "rack-test", ">= 0.5.0"
22
- gem.add_development_dependency "webrat", ">= 0.5.1"
23
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
24
- end
25
- Jeweler::GemcutterTasks.new
26
- Jeweler::RubyforgeTasks.new { |r| r.doc_task = :none }
27
- rescue LoadError
28
- puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
29
- end
1
+ # coding:utf-8
2
+ RAKE_ROOT = __FILE__
30
3
 
31
- require 'rake/testtask'
32
- Rake::TestTask.new(:test) do |test|
33
- test.libs << 'test'
34
- test.pattern = 'test/**/test_*.rb'
35
- test.verbose = true
36
- end
37
-
38
- begin
39
- require 'rcov/rcovtask'
40
- Rcov::RcovTask.new do |rcov|
41
- rcov.libs << 'test'
42
- rcov.pattern = 'test/**/test_*.rb'
43
- rcov.verbose = true
44
- rcov.rcov_opts << ['--exclude /Gems/1.8/gems/,/tmp/,padrino-admin,padrino-cache,padrino-core,padrino-gen,padrino-mailer']
45
- end
46
- rescue LoadError
47
- task :rcov do
48
- abort "RCov is not available. In order to run rcov, you must: sudo gem install relevance-rcov"
49
- end
50
- end
51
-
52
- task :default => :test
4
+ require 'rubygems'
5
+ require File.expand_path(File.dirname(__FILE__) + '/../gem_rake_helper')
@@ -1,9 +1,8 @@
1
- require 'padrino-core/support_lite'
2
- require 'active_support/core_ext/float/rounding'
3
- require 'active_support/option_merger'
1
+ require 'padrino-core/support_lite' unless defined?(SupportLite)
4
2
  require 'cgi'
3
+ require 'i18n'
5
4
 
6
- Dir[File.dirname(__FILE__) + '/padrino-helpers/**/*.rb'].each {|file| require file }
5
+ FileSet.glob_require('padrino-helpers/**/*.rb', __FILE__)
7
6
 
8
7
  # Load our locales
9
8
  I18n.load_path += Dir["#{File.dirname(__FILE__)}/padrino-helpers/locale/*.yml"]
@@ -28,16 +27,19 @@ module Padrino
28
27
  #
29
28
  # for Padrino::Application
30
29
  #
31
- def self.registered(app)
32
- app.set :default_builder, 'StandardFormBuilder'
33
- app.helpers Padrino::Helpers::OutputHelpers
34
- app.helpers Padrino::Helpers::TagHelpers
35
- app.helpers Padrino::Helpers::AssetTagHelpers
36
- app.helpers Padrino::Helpers::FormHelpers
37
- app.helpers Padrino::Helpers::FormatHelpers
38
- app.helpers Padrino::Helpers::RenderHelpers
39
- app.helpers Padrino::Helpers::NumberHelpers
40
- app.helpers Padrino::Helpers::TranslationHelpers
30
+ class << self
31
+ def registered(app)
32
+ app.set :default_builder, 'StandardFormBuilder'
33
+ app.helpers Padrino::Helpers::OutputHelpers
34
+ app.helpers Padrino::Helpers::TagHelpers
35
+ app.helpers Padrino::Helpers::AssetTagHelpers
36
+ app.helpers Padrino::Helpers::FormHelpers
37
+ app.helpers Padrino::Helpers::FormatHelpers
38
+ app.helpers Padrino::Helpers::RenderHelpers
39
+ app.helpers Padrino::Helpers::NumberHelpers
40
+ app.helpers Padrino::Helpers::TranslationHelpers
41
+ end
42
+ alias :included :registered
41
43
  end
42
44
  end # Helpers
43
45
  end # Padrino
@@ -20,19 +20,19 @@ module Padrino
20
20
  #
21
21
  # ==== Examples
22
22
  #
23
- # link_to 'click me', '/dashboard', :class => 'linky'
24
- # link_to 'click me', '/dashboard', :if => @foo.present?
25
- # link_to 'click me', '/dashboard', :unless => @foo.empty?
26
- # link_to 'click me', '/dashboard', :unless => :current
27
- # link_to('/dashboard', :class => 'blocky') do ... end
23
+ # link_to('click me', '/dashboard', :class => 'linky')
24
+ # link_to('click me', '/dashboard', :remote => true)
25
+ # link_to('click me', '/dashboard', :method => :delete)
26
+ # link_to('click me', :class => 'blocky') do ... end
28
27
  #
29
28
  # Note that you can pass :+if+ or :+unless+ conditions, but if you provide :current as
30
29
  # condition padrino return true/false if the request.path_info match the given url
31
30
  #
32
31
  def link_to(*args, &block)
33
32
  options = args.extract_options!
33
+ options = parse_js_attributes(options) # parses remote, method and confirm options
34
34
  anchor = "##{CGI.escape options.delete(:anchor).to_s}" if options[:anchor]
35
- options["data-remote"] = "true" if options.delete(:remote)
35
+
36
36
  if block_given?
37
37
  url = args[0] ? args[0] + anchor.to_s : anchor || 'javascript:void(0);'
38
38
  options.reverse_merge!(:href => url)
@@ -246,7 +246,7 @@ module Padrino
246
246
  # Returns the timestamp mtime for an asset
247
247
  #
248
248
  def asset_timestamp(file_path)
249
- return nil if file_path =~ /\?/
249
+ return nil if file_path =~ /\?/ || (self.class.respond_to?(:asset_stamp) && !self.class.asset_stamp)
250
250
  public_path = Padrino.root("public", file_path) if Padrino.respond_to?(:root)
251
251
  stamp = Time.now.to_i unless public_path && File.exist?(public_path)
252
252
  stamp ||= File.mtime(public_path).to_i
@@ -254,7 +254,7 @@ module Padrino
254
254
  end
255
255
 
256
256
  ##
257
- # Parse link_to options for give correct conditions
257
+ # Parses link_to options for given correct conditions
258
258
  #
259
259
  def parse_conditions(url, options)
260
260
  if options.has_key?(:if)
@@ -266,6 +266,23 @@ module Padrino
266
266
  true
267
267
  end
268
268
  end
269
+
270
+ ##
271
+ # Parses link_to options for given js declarations (remote, method, confirm)
272
+ # Not destructive on options; returns updated options
273
+ #
274
+ def parse_js_attributes(options)
275
+ options = options.dup
276
+ options["data-remote"] = "true" if options.delete(:remote)
277
+ if link_confirm = options.delete(:confirm)
278
+ options["data-confirm"] = link_confirm
279
+ end
280
+ if link_method = options.delete(:method)
281
+ options["data-method"] = link_method
282
+ options["rel"] = "nofollow"
283
+ end
284
+ options
285
+ end
269
286
  end # AssetTagHelpers
270
287
  end # Helpers
271
- end # Padrino
288
+ end # Padrino
@@ -39,10 +39,11 @@ module Padrino
39
39
  #
40
40
  def form_tag(url, options={}, &block)
41
41
  desired_method = options[:method]
42
- options.delete(:method) if options[:method].to_s !~ /get|post/i
42
+ data_method = options.delete(:method) if options[:method].to_s !~ /get|post/i
43
43
  options.reverse_merge!(:method => 'post', :action => url)
44
44
  options[:enctype] = "multipart/form-data" if options.delete(:multipart)
45
45
  options["data-remote"] = "true" if options.delete(:remote)
46
+ options["data-method"] = data_method if data_method
46
47
  inner_form_html = hidden_form_method_field(desired_method) + capture_html(&block)
47
48
  concat_content content_tag('form', inner_form_html, options)
48
49
  end
@@ -130,7 +131,7 @@ module Padrino
130
131
  error_messages = objects.map { |object|
131
132
  object_name = options[:object_name].to_s.underscore.gsub('_', ' ')
132
133
  object.errors.map { |f, msg|
133
- field = I18n.t(f, :default => object_name.humanize, :scope => [:models, object_name, :attributes])
134
+ field = I18n.t(f, :default => f.to_s.humanize, :scope => [:models, object_name, :attributes])
134
135
  content_tag(:li, "%s %s" % [field, msg])
135
136
  }
136
137
  }.join
@@ -190,7 +191,7 @@ module Padrino
190
191
  # label_tag :username, :class => 'long-label' do ... end
191
192
  #
192
193
  def label_tag(name, options={}, &block)
193
- options.reverse_merge!(:caption => "#{name.to_s.titleize}: ", :for => name)
194
+ options.reverse_merge!(:caption => "#{name.to_s.humanize}: ", :for => name)
194
195
  caption_text = options.delete(:caption)
195
196
  caption_text << "* " if options.delete(:required)
196
197
  if block_given? # label with inner content
@@ -227,24 +227,15 @@ module Padrino
227
227
  distance_of_time_in_words(from_time, Time.now, include_seconds)
228
228
  end
229
229
 
230
- ##
231
- # Remove unsafe chars from our javascript
232
- #
233
- def escape_javascript(html_content)
234
- return '' unless html_content
235
- javascript_mapping = { '\\' => '\\\\', '</' => '<\/', "\r\n" => '\n', "\n" => '\n' }
236
- javascript_mapping.merge("\r" => '\n', '"' => '\\"', "'" => "\\'")
237
- escaped_string = html_content.gsub(/(\\|<\/|\r\n|[\n\r"'])/) { javascript_mapping[$1] }
238
- escaped_string
239
- end
240
-
241
230
  ##
242
231
  # Used in xxxx.js.erb files to escape html so that it can be passed to javascript from Padrino
243
232
  #
244
233
  # js_escape_html("<h1>Hey</h1>")
245
234
  #
246
235
  def js_escape_html(html_content)
247
- %Q["#{escape_javascript(html_content)}"]
236
+ return '' unless html_content
237
+ javascript_mapping = { '\\' => '\\\\', '</' => '<\/', "\r\n" => '\n', "\n" => '\n', "\r" => '\n', '"' => '\\"', "'" => "\\'" }
238
+ html_content.gsub(/(\\|<\/|\r\n|[\n\r"'])/) { javascript_mapping[$1] }
248
239
  end
249
240
  end # FormatHelpers
250
241
  end # Helpers
@@ -0,0 +1,103 @@
1
+ en:
2
+ number:
3
+ # Used in number_with_delimiter()
4
+ # These are also the defaults for 'currency', 'percentage', 'precision', and 'human'
5
+ format:
6
+ # Sets the separator between the units, for more precision (e.g. 1.0 / 2.0 == 0.5)
7
+ separator: ","
8
+ # Delimets thousands (e.g. 1,000,000 is a million) (always in groups of three)
9
+ delimiter: " "
10
+ # Number of decimals, behind the separator (the number 1 with a precision of 2 gives: 1.00)
11
+ precision: 3
12
+
13
+ # Used in number_to_currency()
14
+ currency:
15
+ format:
16
+ # Where is the currency sign? %u is the currency unit, %n the number (default: $5.00)
17
+ format: "%n %u"
18
+ unit: "Kč"
19
+ # These three are to override number.format and are optional
20
+ # separator: ","
21
+ # delimiter: " "
22
+ precision: 2
23
+
24
+ # Used in number_to_percentage()
25
+ percentage:
26
+ format:
27
+ # These three are to override number.format and are optional
28
+ # separator:
29
+ delimiter: ""
30
+ # precision:
31
+
32
+ # Used in number_to_precision()
33
+ precision:
34
+ format:
35
+ # These three are to override number.format and are optional
36
+ # separator:
37
+ delimiter: ""
38
+ # precision:
39
+
40
+ # Used in number_to_human_size()
41
+ human:
42
+ format:
43
+ # These three are to override number.format and are optional
44
+ # separator:
45
+ delimiter: ""
46
+ precision: 1
47
+ storage_units:
48
+ # Storage units output formatting.
49
+ # %u is the storage unit, %n is the number (default: 2 MB)
50
+ format: "%n %u"
51
+ units:
52
+ byte:
53
+ one: "Byte"
54
+ other: "Bytes"
55
+ kb: "KB"
56
+ mb: "MB"
57
+ gb: "GB"
58
+ tb: "TB"
59
+
60
+ # Used in distance_of_time_in_words(), distance_of_time_in_words_to_now(), time_ago_in_words()
61
+ datetime:
62
+ distance_in_words:
63
+ half_a_minute: "půl minutou"
64
+ less_than_x_seconds:
65
+ one: "asi před sekundou"
66
+ other: "asi před {{count}} sekundami"
67
+ x_seconds:
68
+ one: "sekundou"
69
+ other: "{{count}} sekundami"
70
+ less_than_x_minutes:
71
+ one: "před necelou minutou"
72
+ other: "před ani ne {{count}} minutami"
73
+ x_minutes:
74
+ one: "minutou"
75
+ other: "{{count}} minutami"
76
+ about_x_hours:
77
+ one: "asi hodinou"
78
+ other: "asi {{count}} hodinami"
79
+ x_days:
80
+ one: "24 hodinami"
81
+ other: "{{count}} dny"
82
+ about_x_months:
83
+ one: "asi měsícem"
84
+ other: "asi {{count}} měsíci"
85
+ x_months:
86
+ one: "měsícem"
87
+ other: "{{count}} měsíci"
88
+ about_x_years:
89
+ one: "asi rokem"
90
+ other: "asi {{count}} roky"
91
+ over_x_years:
92
+ one: "více než před rokem"
93
+ other: "více než {{count}} roky"
94
+ almost_x_years:
95
+ one: "skoro rokem"
96
+ other: "almost {{count}} roků"
97
+ models:
98
+ errors:
99
+ template:
100
+ header:
101
+ one: "Při ukládání {{model}} došlo k chybě a nebylo jej možné uložit"
102
+ other: "Při ukládání {{model}} došlo ke {{count}} chybám a nebylo možné jej uložit"
103
+ body: "Následující pole obsahují chybně vyplněné údaje:"
@@ -44,37 +44,37 @@ da:
44
44
  half_a_minute: "et halvt minut"
45
45
  less_than_x_seconds:
46
46
  one: "mindre end et sekund"
47
- other: "mindre end {{count}} sekunder"
47
+ other: "mindre end %{count} sekunder"
48
48
  x_seconds:
49
49
  one: "et sekund"
50
- other: "{{count}} sekunder"
50
+ other: "%{count} sekunder"
51
51
  less_than_x_minutes:
52
52
  one: "mindre end et minut"
53
- other: "mindre end {{count}} minutter"
53
+ other: "mindre end %{count} minutter"
54
54
  x_minutes:
55
55
  one: "et minut"
56
- other: "{{count}} minutter"
56
+ other: "%{count} minutter"
57
57
  about_x_hours:
58
58
  one: "cirka en time"
59
- other: "cirka {{count}} timer"
59
+ other: "cirka %{count} timer"
60
60
  x_days:
61
61
  one: "en dag"
62
- other: "{{count}} dage"
62
+ other: "%{count} dage"
63
63
  about_x_months:
64
64
  one: "cirka en måned"
65
- other: "cirka {{count}} måneder"
65
+ other: "cirka %{count} måneder"
66
66
  x_months:
67
67
  one: "en måned"
68
- other: "{{count}} måneder"
68
+ other: "%{count} måneder"
69
69
  about_x_years:
70
70
  one: "cirka et år"
71
- other: "cirka {{count}} år"
71
+ other: "cirka %{count} år"
72
72
  over_x_years:
73
73
  one: "mere end et år"
74
- other: "mere end {{count}} år"
74
+ other: "mere end %{count} år"
75
75
  almost_x_years:
76
76
  one: "næsten et år"
77
- other: "næsten {{count}} years"
77
+ other: "næsten %{count} years"
78
78
  prompts:
79
79
  second: "Sekund"
80
80
  minute: "Minut"
@@ -86,6 +86,6 @@ da:
86
86
  errors:
87
87
  template:
88
88
  header:
89
- one: "En fejl forhindrede {{model}} i at blive gemt"
90
- other: "{{count}} fejl forhindrede denne {{model}} i at blive gemt"
89
+ one: "En fejl forhindrede %{model} i at blive gemt"
90
+ other: "%{count} fejl forhindrede denne %{model} i at blive gemt"
91
91
  body: "Der var problemer med følgende felter:"
@@ -40,39 +40,39 @@ de:
40
40
  less_than_x_seconds:
41
41
  zero: 'weniger als 1 Sekunde'
42
42
  one: 'weniger als 1 Sekunde'
43
- other: 'weniger als {{count}} Sekunden'
43
+ other: 'weniger als %{count} Sekunden'
44
44
  x_seconds:
45
45
  one: '1 Sekunde'
46
- other: '{{count}} Sekunden'
46
+ other: '%{count} Sekunden'
47
47
  less_than_x_minutes:
48
48
  zero: 'weniger als 1 Minute'
49
- one: 'weniger als eine Minute'
50
- other: 'weniger als {{count}} Minuten'
49
+ one: 'weniger als 1 Minute'
50
+ other: 'weniger als %{count} Minuten'
51
51
  x_minutes:
52
52
  one: '1 Minute'
53
- other: '{{count}} Minuten'
53
+ other: '%{count} Minuten'
54
54
  about_x_hours:
55
55
  one: 'etwa 1 Stunde'
56
- other: 'etwa {{count}} Stunden'
56
+ other: 'etwa %{count} Stunden'
57
57
  x_days:
58
58
  one: '1 Tag'
59
- other: '{{count}} Tage'
59
+ other: '%{count} Tage'
60
60
  about_x_months:
61
61
  one: 'etwa 1 Monat'
62
- other: 'etwa {{count}} Monate'
62
+ other: 'etwa %{count} Monate'
63
63
  x_months:
64
64
  one: '1 Monat'
65
- other: '{{count}} Monate'
65
+ other: '%{count} Monate'
66
66
  about_x_years:
67
67
  one: 'etwa 1 Jahr'
68
- other: 'etwa {{count}} Jahre'
68
+ other: 'etwa %{count} Jahre'
69
69
  over_x_years:
70
70
  one: 'mehr als 1 Jahr'
71
- other: 'mehr als {{count}} Jahre'
71
+ other: 'mehr als %{count} Jahre'
72
72
  models:
73
73
  errors:
74
74
  template:
75
75
  header:
76
- one: "1 Fehler verhindert, dass Objekt {{model}} gesichert werden kann"
77
- other: "{{count}} Fehler verhindern, dass Objekt {{model}} gesichert werden kann"
78
- body: "Es existieren Probleme mit den follgenden Feldern:"
76
+ one: "1 Fehler verhindert, dass Objekt %{model} gesichert werden kann"
77
+ other: "%{count} Fehler verhindern, dass Objekt %{model} gesichert werden kann"
78
+ body: "Es existieren Probleme mit den folgenden Feldern:"
@@ -63,41 +63,41 @@ en:
63
63
  half_a_minute: "half a minute"
64
64
  less_than_x_seconds:
65
65
  one: "less than 1 second"
66
- other: "less than {{count}} seconds"
66
+ other: "less than %{count} seconds"
67
67
  x_seconds:
68
68
  one: "1 second"
69
- other: "{{count}} seconds"
69
+ other: "%{count} seconds"
70
70
  less_than_x_minutes:
71
71
  one: "less than a minute"
72
- other: "less than {{count}} minutes"
72
+ other: "less than %{count} minutes"
73
73
  x_minutes:
74
74
  one: "1 minute"
75
- other: "{{count}} minutes"
75
+ other: "%{count} minutes"
76
76
  about_x_hours:
77
77
  one: "about 1 hour"
78
- other: "about {{count}} hours"
78
+ other: "about %{count} hours"
79
79
  x_days:
80
80
  one: "1 day"
81
- other: "{{count}} days"
81
+ other: "%{count} days"
82
82
  about_x_months:
83
83
  one: "about 1 month"
84
- other: "about {{count}} months"
84
+ other: "about %{count} months"
85
85
  x_months:
86
86
  one: "1 month"
87
- other: "{{count}} months"
87
+ other: "%{count} months"
88
88
  about_x_years:
89
89
  one: "about 1 year"
90
- other: "about {{count}} years"
90
+ other: "about %{count} years"
91
91
  over_x_years:
92
92
  one: "over 1 year"
93
- other: "over {{count}} years"
93
+ other: "over %{count} years"
94
94
  almost_x_years:
95
95
  one: "almost 1 year"
96
- other: "almost {{count}} years"
96
+ other: "almost %{count} years"
97
97
  models:
98
98
  errors:
99
99
  template:
100
100
  header:
101
- one: "1 error prohibited this {{model}} from being saved"
102
- other: "{{count}} errors prohibited this {{model}} from being saved"
101
+ one: "1 error prohibited this %{model} from being saved"
102
+ other: "%{count} errors prohibited this %{model} from being saved"
103
103
  body: "There were problems with the following fields:"