padrino-helpers 0.9.28 → 0.9.29

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -196,12 +196,13 @@ passing javascript information from a js template to a javascript function.
196
196
  There is also an alias for escape_html called <tt>h</tt> for even easier usage within templates.
197
197
 
198
198
  Format helpers also includes a number of useful text manipulation functions such as <tt>simple_format</tt>,
199
- <tt>pluralize</tt>, <tt>word_wrap</tt>, and <tt>truncate</tt>.
199
+ <tt>pluralize</tt>, <tt>word_wrap</tt>, <tt>truncate</tt> and <tt>truncate_words</tt>.
200
200
 
201
201
  simple_format("hello\nworld") # => "<p>hello<br/>world</p>"
202
202
  pluralize(2, 'person') => '2 people'
203
203
  word_wrap('Once upon a time', :line_width => 8) => "Once upon\na time"
204
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..."
205
206
 
206
207
  These helpers can be invoked from any route or view within your application.
207
208
 
@@ -75,6 +75,22 @@ module Padrino
75
75
  (chars.length > options[:length] ? chars[0...len] + options[:omission] : text).to_s
76
76
  end
77
77
  end
78
+
79
+ ##
80
+ # Truncates words of a given text after a given :length if number of words in text is more than :length (defaults to 30).
81
+ # The last words will be replaced with the :omission (defaults to "…") for a total number of words not exceeding :length.
82
+ #
83
+ # ==== Examples
84
+ #
85
+ # truncate_words("Once upon a time in a world far far away", :length => 8) => "Once upon a time in a world far..."
86
+ #
87
+ def truncate_words(text, options={})
88
+ options.reverse_merge!(:length => 30, :omission => "...")
89
+ if text
90
+ words = text.split()
91
+ words[0..(options[:length]-1)].join(' ') + (words.length > options[:length] ? options[:omission] : '')
92
+ end
93
+ end
78
94
 
79
95
  ##
80
96
  # Wraps the text into lines no longer than line_width width.
@@ -109,20 +109,35 @@ class TestFormatHelpers < Test::Unit::TestCase
109
109
  end
110
110
  end
111
111
 
112
+ context 'for #truncate_words method' do
113
+ should "support default truncation" do
114
+ actual_text = truncate_words("Long before books were made, people told stories. They told them to one another and to the children as they sat before the fire. Many of these stories were about interesting people, but most of them were about the ways of fairies and giants.")
115
+ assert_equal "Long before books were made, people told stories. They told them to one another and to the children as they sat before the fire. Many of these stories were about...", actual_text
116
+ end
117
+ should "support specifying length" do
118
+ actual_text = truncate_words("Once upon a time in a world far far away", :length => 8)
119
+ assert_equal "Once upon a time in a world far...", actual_text
120
+ end
121
+ should "support specifying omission text" do
122
+ actual_text = truncate_words("And they found that many people were sleeping better.", :length => 4, :omission => "(clipped)")
123
+ assert_equal "And they found that(clipped)", actual_text
124
+ end
125
+ end
126
+
112
127
  context 'for #h and #h! method' do
113
128
  should "escape the simple html" do
114
- assert_equal '&lt;h1&gt;hello&lt;/h1&gt;', h('<h1>hello</h1>')
115
- assert_equal '&lt;h1&gt;hello&lt;/h1&gt;', escape_html('<h1>hello</h1>')
129
+ assert_equal '&lt;h1&gt;hello&lt;&#x2F;h1&gt;', h('<h1>hello</h1>')
130
+ assert_equal '&lt;h1&gt;hello&lt;&#x2F;h1&gt;', escape_html('<h1>hello</h1>')
116
131
  end
117
132
  should "escape all brackets, quotes and ampersands" do
118
- assert_equal '&lt;h1&gt;&lt;&gt;&quot;&amp;demo&amp;&quot;&lt;&gt;&lt;/h1&gt;', h('<h1><>"&demo&"<></h1>')
133
+ assert_equal '&lt;h1&gt;&lt;&gt;&quot;&amp;demo&amp;&quot;&lt;&gt;&lt;&#x2F;h1&gt;', h('<h1><>"&demo&"<></h1>')
119
134
  end
120
135
  should "return default text if text is empty" do
121
136
  assert_equal 'default', h!("", "default")
122
137
  assert_equal '&nbsp;', h!("")
123
138
  end
124
139
  should "return text escaped if not empty" do
125
- assert_equal '&lt;h1&gt;hello&lt;/h1&gt;', h!('<h1>hello</h1>')
140
+ assert_equal '&lt;h1&gt;hello&lt;&#x2F;h1&gt;', h!('<h1>hello</h1>')
126
141
  end
127
142
  end
128
143
 
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: padrino-helpers
3
3
  version: !ruby/object:Gem::Version
4
- hash: 3
5
4
  prerelease:
6
- segments:
7
- - 0
8
- - 9
9
- - 28
10
- version: 0.9.28
5
+ version: 0.9.29
11
6
  platform: ruby
12
7
  authors:
13
8
  - Padrino Team
@@ -18,7 +13,7 @@ autorequire:
18
13
  bindir: bin
19
14
  cert_chain: []
20
15
 
21
- date: 2011-05-11 00:00:00 Z
16
+ date: 2011-05-23 00:00:00 Z
22
17
  dependencies:
23
18
  - !ruby/object:Gem::Dependency
24
19
  name: padrino-core
@@ -28,12 +23,7 @@ dependencies:
28
23
  requirements:
29
24
  - - "="
30
25
  - !ruby/object:Gem::Version
31
- hash: 3
32
- segments:
33
- - 0
34
- - 9
35
- - 28
36
- version: 0.9.28
26
+ version: 0.9.29
37
27
  type: :runtime
38
28
  version_requirements: *id001
39
29
  - !ruby/object:Gem::Dependency
@@ -44,11 +34,6 @@ dependencies:
44
34
  requirements:
45
35
  - - ">="
46
36
  - !ruby/object:Gem::Version
47
- hash: 13
48
- segments:
49
- - 0
50
- - 4
51
- - 1
52
37
  version: 0.4.1
53
38
  type: :runtime
54
39
  version_requirements: *id002
@@ -169,20 +154,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
169
154
  requirements:
170
155
  - - ">="
171
156
  - !ruby/object:Gem::Version
172
- hash: 3
173
- segments:
174
- - 0
175
157
  version: "0"
176
158
  required_rubygems_version: !ruby/object:Gem::Requirement
177
159
  none: false
178
160
  requirements:
179
161
  - - ">="
180
162
  - !ruby/object:Gem::Version
181
- hash: 23
182
- segments:
183
- - 1
184
- - 3
185
- - 6
186
163
  version: 1.3.6
187
164
  requirements: []
188
165