padrino-helpers 0.2.0 → 0.2.1
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.
- data/README.rdoc +22 -0
- data/VERSION +1 -1
- data/lib/padrino-helpers/asset_tag_helpers.rb +6 -4
- data/lib/padrino-helpers/format_helpers.rb +48 -0
- data/lib/padrino-helpers/tag_helpers.rb +2 -2
- data/padrino-helpers.gemspec +2 -2
- data/test/test_asset_tag_helpers.rb +10 -0
- data/test/test_format_helpers.rb +62 -0
- data/test/test_tag_helpers.rb +4 -0
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -389,8 +389,30 @@ passing javascript information from a js template to a javascript function.
|
|
389
389
|
|
390
390
|
There is also an alias for escape_html called <tt>h</tt> for even easier usage within templates.
|
391
391
|
|
392
|
+
Format helpers also includes a number of useful text manipulation functions such as <tt>simple_format</tt>,
|
393
|
+
<tt>pluralize</tt>, <tt>word_wrap</tt>, and <tt>truncate</tt>.
|
394
|
+
|
395
|
+
simple_format("hello\nworld") # => "<p>hello<br/>world</p>"
|
396
|
+
pluralize(2, 'person') => '2 people'
|
397
|
+
word_wrap('Once upon a time', :line_width => 8) => "Once upon\na time"
|
398
|
+
truncate("Once upon a time in a world far far away", :length => 8) => "Once upon..."
|
399
|
+
|
400
|
+
These helpers can be invoked from any route or view within your application.
|
401
|
+
|
392
402
|
The list of defined helpers in the 'format helpers' category:
|
393
403
|
|
404
|
+
* <tt>simple_format(text, html_options)</tt>
|
405
|
+
* Returns text transformed into HTML using simple formatting rules.
|
406
|
+
* <tt>simple_format("hello\nworld")</tt> => "<p>hello<br/>world</p>"
|
407
|
+
* <tt>pluralize(count, singular, plural = nil)</tt>
|
408
|
+
* Attempts to pluralize the singular word unless count is 1.
|
409
|
+
* <tt>pluralize(2, 'person')</tt> => '2 people'
|
410
|
+
* <tt>word_wrap(text, *args)</tt>
|
411
|
+
* Wraps the text into lines no longer than line_width width.
|
412
|
+
* <tt>word_wrap('Once upon a time', :line_width => 8)</tt> => "Once upon\na time"
|
413
|
+
* <tt>truncate(text, *args)</tt>
|
414
|
+
* Truncates a given text after a given :length if text is longer than :length (defaults to 30).
|
415
|
+
* <tt>truncate("Once upon a time in a world far far away", :length => 8)</tt> => "Once upon..."
|
394
416
|
* <tt>escape_html</tt> (alias <tt>h</tt> and <tt>h!</tt>)
|
395
417
|
* (from RackUtils) Escape ampersands, brackets and quotes to their HTML/XML entities.
|
396
418
|
* <tt>relative_time_ago(date)</tt>
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.1
|
@@ -84,8 +84,9 @@ module Padrino
|
|
84
84
|
def javascript_path(source)
|
85
85
|
return source if source =~ /^http/
|
86
86
|
source.gsub!(/\.js$/, '')
|
87
|
-
|
88
|
-
result_path
|
87
|
+
source_name = source; source_name << ".js" unless source =~ /\.js\w{2,4}$/
|
88
|
+
result_path = source_name if source =~ %r{^/} # absolute path
|
89
|
+
result_path ||= uri_root_path("javascripts", source_name)
|
89
90
|
stamp = File.exist?(result_path) ? File.mtime(result_path) : Time.now.to_i
|
90
91
|
"#{result_path}?#{stamp}"
|
91
92
|
end
|
@@ -94,8 +95,9 @@ module Padrino
|
|
94
95
|
def stylesheet_path(source)
|
95
96
|
return source if source =~ /^http/
|
96
97
|
source.gsub!(/\.css$/, '')
|
97
|
-
|
98
|
-
result_path
|
98
|
+
source_name = source; source_name << ".css" unless source =~ /\.css$/
|
99
|
+
result_path = source_name if source =~ %r{^/} # absolute path
|
100
|
+
result_path ||= uri_root_path("stylesheets", source_name)
|
99
101
|
stamp = File.exist?(result_path) ? File.mtime(result_path) : Time.now.to_i
|
100
102
|
"#{result_path}?#{stamp}"
|
101
103
|
end
|
@@ -16,6 +16,54 @@ module Padrino
|
|
16
16
|
h text
|
17
17
|
end
|
18
18
|
|
19
|
+
# Returns text transformed into HTML using simple formatting rules. Two or more consecutive newlines(\n\n) are considered
|
20
|
+
# as a paragraph and wrapped in <p> tags. One newline (\n) is considered as a linebreak and a <br /> tag is appended.
|
21
|
+
# This method does not remove the newlines from the text.
|
22
|
+
# simple_format("hello\nworld") # => "<p>hello<br/>world</p>"
|
23
|
+
def simple_format(text, html_options={})
|
24
|
+
start_tag = tag('p', html_options.merge(:open => true))
|
25
|
+
text = text.to_s.dup
|
26
|
+
text.gsub!(/\r\n?/, "\n") # \r\n and \r -> \n
|
27
|
+
text.gsub!(/\n\n+/, "</p>\n\n#{start_tag}") # 2+ newline -> paragraph
|
28
|
+
text.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline -> br
|
29
|
+
text.insert 0, start_tag
|
30
|
+
text << "</p>"
|
31
|
+
end
|
32
|
+
|
33
|
+
# Attempts to pluralize the singular word unless count is 1. If plural is supplied, it will use that when count is > 1,
|
34
|
+
# otherwise it will use the Inflector to determine the plural form
|
35
|
+
# pluralize(2, 'person') => '2 people'
|
36
|
+
def pluralize(count, singular, plural = nil)
|
37
|
+
"#{count || 0} " + ((count == 1 || count == '1') ? singular : (plural || singular.pluralize))
|
38
|
+
end
|
39
|
+
|
40
|
+
# Truncates a given text after a given :length if text is longer than :length (defaults to 30).
|
41
|
+
# The last characters will be replaced with the :omission (defaults to "…") for a total length not exceeding :length.
|
42
|
+
# truncate("Once upon a time in a world far far away", :length => 8) => "Once upon..."
|
43
|
+
def truncate(text, *args)
|
44
|
+
options = args.extract_options!
|
45
|
+
options.reverse_merge!(:length => 30, :omission => "...")
|
46
|
+
if text
|
47
|
+
len = options[:length] - options[:omission].length
|
48
|
+
chars = text
|
49
|
+
(chars.length > options[:length] ? chars[0...len] + options[:omission] : text).to_s
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# Wraps the text into lines no longer than line_width width.
|
54
|
+
# This method breaks on the first whitespace character that does not exceed line_width (which is 80 by default).
|
55
|
+
# word_wrap('Once upon a time', :line_width => 8) => "Once upon\na time"
|
56
|
+
def word_wrap(text, *args)
|
57
|
+
options = args.extract_options!
|
58
|
+
unless args.blank?
|
59
|
+
options[:line_width] = args[0] || 80
|
60
|
+
end
|
61
|
+
options.reverse_merge!(:line_width => 80)
|
62
|
+
|
63
|
+
text.split("\n").collect do |line|
|
64
|
+
line.length > options[:line_width] ? line.gsub(/(.{1,#{options[:line_width]}})(\s+|$)/, "\\1\n").strip : line
|
65
|
+
end * "\n"
|
66
|
+
end
|
19
67
|
|
20
68
|
# Smart time helper which returns relative text representing times for recent dates
|
21
69
|
# and absolutes for dates that are far removed from the current date
|
@@ -24,11 +24,11 @@ module Padrino
|
|
24
24
|
# tag(:br, :style => 'clear:both')
|
25
25
|
# tag(:p, :content => "hello", :class => 'large')
|
26
26
|
def tag(name, options={})
|
27
|
-
content = options.delete(:content)
|
27
|
+
content, open_tag = options.delete(:content), options.delete(:open)
|
28
28
|
identity_tag_attributes.each { |attr| options[attr] = attr.to_s if options[attr] }
|
29
29
|
html_attrs = options.collect { |a, v| v.blank? ? nil : "#{a}=\"#{v}\"" }.compact.join(" ")
|
30
30
|
base_tag = (html_attrs.present? ? "<#{name} #{html_attrs}" : "<#{name}")
|
31
|
-
base_tag << (content ? ">#{content}</#{name}>" : " />")
|
31
|
+
base_tag << (open_tag ? ">" : (content ? ">#{content}</#{name}>" : " />"))
|
32
32
|
end
|
33
33
|
|
34
34
|
protected
|
data/padrino-helpers.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{padrino-helpers}
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Padrino Team", "Nathan Esquenazi", "Davide D'Agostino", "Arthur Chiu"]
|
12
|
-
s.date = %q{2009-11-
|
12
|
+
s.date = %q{2009-11-30}
|
13
13
|
s.description = %q{Tag helpers, asset helpers, form helpers, form builders and many more helpers for padrino}
|
14
14
|
s.email = %q{nesquena@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -147,6 +147,16 @@ class TestAssetTagHelpers < Test::Unit::TestCase
|
|
147
147
|
actual_html = javascript_include_tag('example/demo/application')
|
148
148
|
assert_has_tag('script', :src => "/javascripts/example/demo/application.js?#{time.to_i}", :type => "text/javascript") { actual_html }
|
149
149
|
end
|
150
|
+
should "display javascript item for path containing js" do
|
151
|
+
time = stop_time_for_test
|
152
|
+
actual_html = javascript_include_tag 'test/jquery.json'
|
153
|
+
assert_has_tag('script', :src => "/javascripts/test/jquery.json?#{time.to_i}", :type => "text/javascript") { actual_html }
|
154
|
+
end
|
155
|
+
should "display javascript item for path containing period" do
|
156
|
+
time = stop_time_for_test
|
157
|
+
actual_html = javascript_include_tag 'test/jquery.min'
|
158
|
+
assert_has_tag('script', :src => "/javascripts/test/jquery.min.js?#{time.to_i}", :type => "text/javascript") { actual_html }
|
159
|
+
end
|
150
160
|
should "display javascript item with absolute path" do
|
151
161
|
time = stop_time_for_test
|
152
162
|
actual_html = javascript_include_tag('/js/application')
|
data/test/test_format_helpers.rb
CHANGED
@@ -8,6 +8,68 @@ class TestFormatHelpers < Test::Unit::TestCase
|
|
8
8
|
|
9
9
|
include Padrino::Helpers::FormatHelpers
|
10
10
|
|
11
|
+
context 'for #simple_format method' do
|
12
|
+
should "format simple text into html format" do
|
13
|
+
actual_text = simple_format("Here is some basic text...\n...with a line break.")
|
14
|
+
assert_equal "<p>Here is some basic text...\n<br />...with a line break.</p>", actual_text
|
15
|
+
end
|
16
|
+
|
17
|
+
should "format more text into html format" do
|
18
|
+
actual_text = simple_format("We want to put a paragraph...\n\n...right there.")
|
19
|
+
assert_equal "<p>We want to put a paragraph...</p>\n\n<p>...right there.</p>", actual_text
|
20
|
+
end
|
21
|
+
|
22
|
+
should "support defining a class for the paragraphs" do
|
23
|
+
actual_text = simple_format("Look ma! A class!", :class => 'description')
|
24
|
+
assert_equal "<p class=\"description\">Look ma! A class!</p>", actual_text
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'for #pluralize method' do
|
29
|
+
should "return singular count for 1 item collections" do
|
30
|
+
actual_text = pluralize(1, 'person')
|
31
|
+
assert_equal '1 person', actual_text
|
32
|
+
end
|
33
|
+
should "return plural count for empty collections" do
|
34
|
+
actual_text = pluralize(0, 'person')
|
35
|
+
assert_equal '0 people', actual_text
|
36
|
+
end
|
37
|
+
should "return plural count for many collections" do
|
38
|
+
actual_text = pluralize(2, 'person')
|
39
|
+
assert_equal '2 people', actual_text
|
40
|
+
end
|
41
|
+
should "return pluralized word specified as argument" do
|
42
|
+
actual_text = pluralize(3, 'person', 'users')
|
43
|
+
assert_equal '3 users', actual_text
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'for #word_wrap method' do
|
48
|
+
should "return proper formatting for 8 max width" do
|
49
|
+
actual_text = word_wrap('Once upon a time', :line_width => 8)
|
50
|
+
assert_equal "Once\nupon a\ntime", actual_text
|
51
|
+
end
|
52
|
+
should "return proper formatting for 1 max width" do
|
53
|
+
actual_text = word_wrap('Once upon a time', :line_width => 1)
|
54
|
+
assert_equal "Once\nupon\na\ntime", actual_text
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context 'for #truncate method' do
|
59
|
+
should "support default truncation" do
|
60
|
+
actual_text = truncate("Once upon a time in a world far far away")
|
61
|
+
assert_equal "Once upon a time in a world...", actual_text
|
62
|
+
end
|
63
|
+
should "support specifying length" do
|
64
|
+
actual_text = truncate("Once upon a time in a world far far away", :length => 14)
|
65
|
+
assert_equal "Once upon a...", actual_text
|
66
|
+
end
|
67
|
+
should "support specifying omission text" do
|
68
|
+
actual_text = truncate("And they found that many people were sleeping better.", :length => 25, :omission => "(clipped)")
|
69
|
+
assert_equal "And they found t(clipped)", actual_text
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
11
73
|
context 'for #h and #h! method' do
|
12
74
|
should "escape the simple html" do
|
13
75
|
assert_equal '<h1>hello</h1>', h('<h1>hello</h1>')
|
data/test/test_tag_helpers.rb
CHANGED
@@ -25,6 +25,10 @@ class TestTagHelpers < Test::Unit::TestCase
|
|
25
25
|
actual_html = tag(:p, :content => "Demo", :class => 'large', :id => 'intro')
|
26
26
|
assert_has_tag('p#intro.large', :content => "Demo") { actual_html }
|
27
27
|
end
|
28
|
+
should "support open tags" do
|
29
|
+
actual_html = tag(:p, :class => 'demo', :open => true)
|
30
|
+
assert_equal "<p class=\"demo\">", actual_html
|
31
|
+
end
|
28
32
|
end
|
29
33
|
|
30
34
|
context 'for #content_tag method' do
|
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.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Padrino Team
|
@@ -12,7 +12,7 @@ autorequire:
|
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
14
|
|
15
|
-
date: 2009-11-
|
15
|
+
date: 2009-11-30 00:00:00 -08:00
|
16
16
|
default_executable:
|
17
17
|
dependencies:
|
18
18
|
- !ruby/object:Gem::Dependency
|