sinatra_more 0.2.5 → 0.2.7
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +24 -9
- data/VERSION +1 -1
- data/lib/sinatra_more/markup_plugin/format_helpers.rb +28 -5
- data/sinatra_more.gemspec +1 -1
- data/test/markup_plugin/test_format_helpers.rb +41 -8
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -46,10 +46,12 @@ of tools, helpers and components that will make Sinatra suitable for more comple
|
|
46
46
|
Here is a small list of what sinatra_more contains:
|
47
47
|
|
48
48
|
* Generic view and tag helpers (<tt>tag</tt>, <tt>content_tag</tt>, <tt>input_tag</tt>, ...)
|
49
|
-
* Asset helpers (<tt>link_to</tt>, <tt>image_tag</tt>, <tt>javascript_include_tag</tt>, ...)
|
50
|
-
*
|
49
|
+
* Asset tag helpers (<tt>link_to</tt>, <tt>image_tag</tt>, <tt>javascript_include_tag</tt>, ...)
|
50
|
+
* Full form helpers and builders support (<tt>form_tag</tt>, <tt>form_for</tt>, <tt>field_set_tag</tt>, <tt>text_field</tt>, ...)
|
51
|
+
* Generally useful formatting extensions (<tt>relative_time_ago</tt>, <tt>js_escape_html</tt>, <tt>sanitize_html</tt>)
|
51
52
|
* Plug and play setup for the excellent Warden authentication system
|
52
|
-
*
|
53
|
+
* Robust 'mailer' support for sinatra (akin to <tt>ActionMailer</tt> but simpler and powered by <tt>pony</tt>) (COMING SOON)
|
54
|
+
* Code generators for creating a new sinatra application (COMING SOON)
|
53
55
|
|
54
56
|
Keep in mind, the user will be able to pull in these components seperately and leave out those that are not required.
|
55
57
|
|
@@ -333,7 +335,14 @@ I hope to create or merge in an even better 'default' form_builder in the near f
|
|
333
335
|
* (from RackUtils) Escape ampersands, brackets and quotes to their HTML/XML entities.
|
334
336
|
* <tt>relative_time_ago(date)</tt>
|
335
337
|
* Returns relative time in words referencing the given date
|
336
|
-
* <tt>relative_time_ago(2.days.ago)</tt> => "2 days
|
338
|
+
* <tt>relative_time_ago(2.days.ago)</tt> => "2 days"
|
339
|
+
* <tt>relative_time_ago(5.minutes.ago)</tt> => "5 minutes"
|
340
|
+
* <tt>relative_time_ago(2800.days.ago)</tt> => "over 7 years"
|
341
|
+
* <tt>time_in_words(date)</tt>
|
342
|
+
* Returns relative time in the past or future using appropriate date format
|
343
|
+
* <tt>time_in_words(2.days.ago)</tt> => "2 days ago"
|
344
|
+
* <tt>time_in_words(100.days.ago)</tt> => "Tuesday, July 21"
|
345
|
+
* <tt>time_in_words(1.day.from_now)</tt> => "tomorrow"
|
337
346
|
* <tt>escape_javascript(html_content)</tt>
|
338
347
|
* Escapes html to allow passing information to javascript. Used for passing data inside an ajax .js.erb template
|
339
348
|
* <tt>escape_javascript("<h1>Hey</h1>")</tt>
|
@@ -417,10 +426,16 @@ name then you need to specify that as follows:
|
|
417
426
|
|
418
427
|
SinatraMore::WardenPlugin::PasswordStrategy.user_class = CustomUser
|
419
428
|
|
420
|
-
|
429
|
+
In addition, the strategy used expects that you have an <tt>authenticate</tt> method with the specific signature below:
|
421
430
|
|
422
|
-
|
431
|
+
class CustomUser
|
432
|
+
# ...
|
423
433
|
# Returns user record if user and password match; otherwise return false
|
434
|
+
def authenticate(username, password)
|
435
|
+
user = User.find(username)
|
436
|
+
user.has_password?(password) ? user : false
|
437
|
+
end
|
438
|
+
# ...
|
424
439
|
end
|
425
440
|
|
426
441
|
Using this plugin you also do need to define your own routes for managing warden sessions. An example is below:
|
@@ -445,12 +460,12 @@ Using this plugin you also do need to define your own routes for managing warden
|
|
445
460
|
redirect '/session/login'
|
446
461
|
end
|
447
462
|
|
448
|
-
I was made aware of other sinatra/warden
|
463
|
+
I was made aware of other sinatra/warden plugins which work very similarly to the system I outline for this plugin.
|
449
464
|
Most notably is the sinatra_warden plugin by Justin Smestad (http://github.com/jsmestad/sinatra_warden)
|
450
465
|
Eventually I plan to vendor that gem or just remove support for this piece of my plugin completely.
|
451
466
|
|
452
|
-
If anybody has any thoughts on this
|
453
|
-
access to a plug-and-play warden solution directly from this gem.
|
467
|
+
If anybody has any thoughts on this or the warden integration in general, please let me know.
|
468
|
+
Nonetheless, there is a certain convenience for me having access to a plug-and-play warden solution directly from this gem.
|
454
469
|
|
455
470
|
== Acknowledgements
|
456
471
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.7
|
@@ -2,10 +2,11 @@ module SinatraMore
|
|
2
2
|
module FormatHelpers
|
3
3
|
|
4
4
|
# Returns escaped text to protect against malicious content
|
5
|
-
def
|
5
|
+
def escape_html(text)
|
6
6
|
Rack::Utils.escape_html(text)
|
7
7
|
end
|
8
|
-
alias escape_html
|
8
|
+
alias h escape_html
|
9
|
+
alias sanitize_html escape_html
|
9
10
|
|
10
11
|
# Returns escaped text to protect against malicious content
|
11
12
|
# Returns blank if the text is empty
|
@@ -14,9 +15,11 @@ module SinatraMore
|
|
14
15
|
h text
|
15
16
|
end
|
16
17
|
|
17
|
-
|
18
|
-
#
|
19
|
-
|
18
|
+
|
19
|
+
# Smart time helper which returns relative text representing times for recent dates
|
20
|
+
# and absolutes for dates that are far removed from the current date
|
21
|
+
# time_in_words(10.days.ago) => '10 days ago'
|
22
|
+
def time_in_words(date)
|
20
23
|
date = date.to_date
|
21
24
|
date = Date.parse(date, true) unless /Date.*/ =~ date.class.to_s
|
22
25
|
days = (date - Date.today).to_i
|
@@ -31,6 +34,25 @@ module SinatraMore
|
|
31
34
|
return date.strftime('%A, %B %e') if days.abs < 182
|
32
35
|
return date.strftime('%A, %B %e, %Y')
|
33
36
|
end
|
37
|
+
alias time_ago time_in_words
|
38
|
+
|
39
|
+
# Returns relative time in words referencing the given date
|
40
|
+
# relative_time_ago(Time.now) => 'about a minute ago'
|
41
|
+
def relative_time_ago(from_time)
|
42
|
+
distance_in_minutes = (((Time.now - from_time.to_time).abs)/60).round
|
43
|
+
case distance_in_minutes
|
44
|
+
when 0..1 then 'about a minute'
|
45
|
+
when 2..44 then "#{distance_in_minutes} minutes"
|
46
|
+
when 45..89 then 'about 1 hour'
|
47
|
+
when 90..1439 then "about #{(distance_in_minutes.to_f / 60.0).round} hours"
|
48
|
+
when 1440..2879 then '1 day'
|
49
|
+
when 2880..43199 then "#{(distance_in_minutes / 1440).round} days"
|
50
|
+
when 43200..86399 then 'about 1 month'
|
51
|
+
when 86400..525599 then "#{(distance_in_minutes / 43200).round} months"
|
52
|
+
when 525600..1051199 then 'about 1 year'
|
53
|
+
else "over #{(distance_in_minutes / 525600).round} years"
|
54
|
+
end
|
55
|
+
end
|
34
56
|
|
35
57
|
# Used in xxxx.js.erb files to escape html so that it can be passed to javascript from sinatra
|
36
58
|
# escape_javascript("<h1>Hey</h1>")
|
@@ -43,6 +65,7 @@ module SinatraMore
|
|
43
65
|
end
|
44
66
|
|
45
67
|
alias js_escape escape_javascript
|
68
|
+
alias js_escape_html escape_javascript
|
46
69
|
alias escape_for_javascript escape_javascript
|
47
70
|
|
48
71
|
end
|
data/sinatra_more.gemspec
CHANGED
@@ -25,27 +25,60 @@ class TestFormatHelpers < Test::Unit::TestCase
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
context 'for #
|
28
|
+
context 'for #time_in_words method' do
|
29
29
|
should "display today" do
|
30
|
-
assert_equal 'today',
|
30
|
+
assert_equal 'today', time_in_words(Time.now)
|
31
31
|
end
|
32
32
|
should "display yesterday" do
|
33
|
-
assert_equal 'yesterday',
|
33
|
+
assert_equal 'yesterday', time_in_words(1.day.ago)
|
34
34
|
end
|
35
35
|
should "display tomorrow" do
|
36
|
-
assert_equal 'tomorrow',
|
36
|
+
assert_equal 'tomorrow', time_in_words(1.day.from_now)
|
37
37
|
end
|
38
38
|
should "return future number of days" do
|
39
|
-
assert_equal 'in 4 days',
|
39
|
+
assert_equal 'in 4 days', time_in_words(4.days.from_now)
|
40
40
|
end
|
41
41
|
should "return past days ago" do
|
42
|
-
assert_equal '4 days ago',
|
42
|
+
assert_equal '4 days ago', time_in_words(4.days.ago)
|
43
43
|
end
|
44
44
|
should "return formatted archived date" do
|
45
|
-
assert_equal 100.days.ago.strftime('%A, %B %e'),
|
45
|
+
assert_equal 100.days.ago.strftime('%A, %B %e'), time_in_words(100.days.ago)
|
46
46
|
end
|
47
47
|
should "return formatted archived year date" do
|
48
|
-
assert_equal 500.days.ago.strftime('%A, %B %e, %Y'),
|
48
|
+
assert_equal 500.days.ago.strftime('%A, %B %e, %Y'), time_in_words(500.days.ago)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'for #relative_time_ago method' do
|
53
|
+
should 'display now as a minute ago' do
|
54
|
+
assert_equal 'about a minute', relative_time_ago(1.minute.ago)
|
55
|
+
end
|
56
|
+
should "display a few minutes ago" do
|
57
|
+
assert_equal '4 minutes', relative_time_ago(4.minute.ago)
|
58
|
+
end
|
59
|
+
should "display an hour ago" do
|
60
|
+
assert_equal 'about 1 hour', relative_time_ago(1.hour.ago + 5.minutes.ago.sec)
|
61
|
+
end
|
62
|
+
should "display a few hours ago" do
|
63
|
+
assert_equal 'about 3 hours', relative_time_ago(3.hour.ago + 5.minutes.ago.sec)
|
64
|
+
end
|
65
|
+
should "display a day ago" do
|
66
|
+
assert_equal '1 day', relative_time_ago(1.day.ago)
|
67
|
+
end
|
68
|
+
should "display a few days ago" do
|
69
|
+
assert_equal '5 days', relative_time_ago(5.days.ago - 5.minutes.ago.sec)
|
70
|
+
end
|
71
|
+
should "display a month ago" do
|
72
|
+
assert_equal 'about 1 month', relative_time_ago(32.days.ago + 5.minutes.ago.sec)
|
73
|
+
end
|
74
|
+
should "display a few months ago" do
|
75
|
+
assert_equal '6 months', relative_time_ago(180.days.ago - 5.minutes.ago.sec)
|
76
|
+
end
|
77
|
+
should "display a year ago" do
|
78
|
+
assert_equal 'about 1 year', relative_time_ago(365.days.ago - 5.minutes.ago.sec)
|
79
|
+
end
|
80
|
+
should "display a few years ago" do
|
81
|
+
assert_equal 'over 7 years', relative_time_ago(2800.days.ago - 5.minutes.ago.sec)
|
49
82
|
end
|
50
83
|
end
|
51
84
|
|