rails-timeago 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -21,6 +21,8 @@ To use bundled jQuery Timeago plugin add this require statement to your applicat
21
21
 
22
22
  //=require rails-timeago
23
23
 
24
+ This will also convert all matching time tags on page load.
25
+
24
26
  ## Usage
25
27
 
26
28
  Use the timeago_tag helper like any other regular tag helper:
@@ -31,22 +33,45 @@ Use the timeago_tag helper like any other regular tag helper:
31
33
  ### Available options:
32
34
 
33
35
  * nojs
34
- Add time ago in words as time tag content instead of absolute time. (default: false)
36
+
37
+ Add time ago in words as time tag content instead of absolute time.
38
+ (default: false)
35
39
 
36
40
  * date_only
37
- Only print date as tag content instead of full time. (default: true)
41
+
42
+ Only print date as tag content instead of full time.
43
+ (default: true)
38
44
 
39
45
  * format
40
- A time format for localize method used to format static time. (default: default)
46
+
47
+ A time format for localize method used to format static time.
48
+ (default: default)
41
49
 
42
50
  * limit
43
- Set a limit for time ago tags. All dates before given limit will not be converted. (default: 4.days.ago)
51
+
52
+ Set a limit for time ago tags. All dates before given limit will not be converted.
53
+ (default: 4.days.ago)
44
54
 
45
55
  * force
46
- Force time ago tag ignoring limit option. (default: false)
56
+
57
+ Force time ago tag ignoring limit option.
58
+ (default: false)
59
+
60
+ * default
61
+
62
+ String that will be returned if time is nil.
63
+ (default: '-')
47
64
 
48
65
  All other options will be given as options to the time tag helper.
49
66
 
67
+ The above options can be assigned globally as defaults using
68
+
69
+ Rails::Timeago.default_options :limit => proc { 20.days.ago }, :nojs => true
70
+
71
+ A global limit should always be given as a block that will be evaluated each time
72
+ the rails timeago_tag helper is called. That avoids the limit becoming smaller the
73
+ longer the application runs.
74
+
50
75
  ## License
51
76
 
52
77
  [MIT License](http://www.opensource.org/licenses/mit-license.php)
@@ -4,17 +4,20 @@ module Rails
4
4
  # Create a time tag usable for jQuery timeago plugin.
5
5
  #
6
6
  # timeago_tag Time.zone.now
7
- # => ""
7
+ # => "<time datetime="2012-03-10T12:07:07+01:00" title="Sat, 10 Mar 2012 12:07:07 +0100" data-time-ago="2012-03-10T12:07:07+01:00">2012-03-10</time>"
8
8
  #
9
9
  # Available options:
10
10
  # [:+nojs+]
11
11
  # Add time ago in words as time tag content instead of absolute time.
12
+ # (default: false)
12
13
  #
13
14
  # [:+date_only+]
14
15
  # Only print date as tag content instead of full time.
16
+ # (default: true)
15
17
  #
16
18
  # [:+format+]
17
19
  # A time format for localize method used to format static time.
20
+ # (default: :default)
18
21
  #
19
22
  # [:+limit+]
20
23
  # Set a limit for time ago tags. All dates before given limit will not be converted.
@@ -22,28 +25,28 @@ module Rails
22
25
  #
23
26
  # [:+force+]
24
27
  # Force time ago tag ignoring limit option.
28
+ # (default: false)
29
+ #
30
+ # [:+default+]
31
+ # String that will be returned if time is nil.
32
+ # (default: '-')
25
33
  #
26
34
  # All other options will be given as options to tag helper.
27
35
  #
28
36
  def timeago_tag(time, html_options = {})
29
- time_options = {
30
- :nojs => false,
31
- :force => false,
32
- :format => :default,
33
- :limit => 4.days.ago,
34
- :date_only => true
35
- }
37
+ time_options = Rails::Timeago.default_options
36
38
 
37
- time_options.merge! html_options.extract!(*time_options.keys.select{|k| html_options.include?(k)})
39
+ time_options = time_options.merge html_options.extract!(*time_options.keys.select{|k| html_options.include?(k)})
38
40
  html_options.merge! :title => I18n.l(time, :format => time_options[:format])
39
41
 
40
- if time_options[:force] or time_options[:limit].nil? or time_options[:limit] < time
41
- html_options.reverse_merge!('data-time-ago' => time.iso8601)
42
+ return time_options[:default] if time.nil?
42
43
 
43
- time_tag time, timeago_tag_content(time, time_options), html_options
44
- else
45
- time_tag time, timeago_tag_content(time, time_options), html_options
44
+ time_options[:limit] = time_options[:limit].call if time_options[:limit].is_a?(Proc)
45
+
46
+ if time_options[:force] or time_options[:limit].nil? or time_options[:limit] < time
47
+ html_options.merge!('data-time-ago' => time.iso8601)
46
48
  end
49
+ time_tag time, timeago_tag_content(time, time_options), html_options
47
50
  end
48
51
 
49
52
  def timeago_tag_content(time, time_options = {}) # :nodoc:
@@ -53,5 +56,50 @@ module Rails
53
56
  I18n.l time, :format => time_options[:format]
54
57
  end
55
58
  end
59
+
60
+ # Read or write global rails-timeago default options. If no options are given
61
+ # the current defaults will be returned.
62
+ #
63
+ # Available options:
64
+ # [:+nojs+]
65
+ # Add time ago in words as time tag content instead of absolute time.
66
+ # (default: false)
67
+ #
68
+ # [:+date_only+]
69
+ # Only print date as tag content instead of full time.
70
+ # (default: true)
71
+ #
72
+ # [:+format+]
73
+ # A time format for localize method used to format static time.
74
+ # (default: :default)
75
+ #
76
+ # [:+limit+]
77
+ # Set a limit for time ago tags. All dates before given limit will not be converted.
78
+ # Global limit should be given as a block to reevaluate limit each time timeago_tag is called.
79
+ # (default: proc { 4.days.ago })
80
+ #
81
+ # [:+force+]
82
+ # Force time ago tag ignoring limit option.
83
+ # (default: false)
84
+ #
85
+ # [:+default+]
86
+ # String that will be returned if time is nil.
87
+ # (default: '-')
88
+ #
89
+ def self.default_options(opts = nil)
90
+ @defaults ||= {
91
+ :nojs => false,
92
+ :force => false,
93
+ :format => :default,
94
+ :limit => proc { 4.days.ago },
95
+ :date_only => true,
96
+ :default => '-'
97
+ }
98
+ if opts
99
+ @defaults.merge! opts.extract!(*@defaults.keys.select{|k| opts.include?(k)})
100
+ else
101
+ @defaults
102
+ end
103
+ end
56
104
  end
57
105
  end
@@ -2,7 +2,7 @@ module Rails
2
2
  module Timeago
3
3
  module VERSION
4
4
  MAJOR = 1
5
- MINOR = 0
5
+ MINOR = 1
6
6
  PATCH = 0
7
7
 
8
8
  def self.to_s
data/spec/timeago_spec.rb CHANGED
@@ -61,4 +61,24 @@ describe Rails::Timeago::Helper do
61
61
  it 'should pass html option to tag helper' do
62
62
  @stub.timeago_tag(Time.now, :myattr => 'abc').should =~ /<time.*myattr="abc".*>.*<\/time>/
63
63
  end
64
+
65
+ it "should allow to set global options" do
66
+ Rails::Timeago.default_options :format => :short, :limit => proc { 8.days.ago }
67
+ time = 7.days.ago
68
+
69
+ @stub.timeago_tag(time).
70
+ should include(">#{I18n.l time.to_date, :format => :short}<")
71
+ @stub.timeago_tag(time).
72
+ should =~ /<time.*data-time-ago=".*".*>.*<\/time>/
73
+ end
74
+
75
+ it "should allow to override global options" do
76
+ Rails::Timeago.default_options :format => :short, :limit => proc { 8.days.ago }
77
+ time = 7.days.ago
78
+
79
+ @stub.timeago_tag(time, :format => :long).
80
+ should include(">#{I18n.l time.to_date, :format => :long}<")
81
+ @stub.timeago_tag(time, :limit => 4.days.ago).
82
+ should_not =~ /<time.*data-time-ago=".*".*>.*<\/time>/
83
+ end
64
84
  end
@@ -1,18 +1,18 @@
1
1
  /**
2
- * Timeago is a jQuery plugin that makes it easy to support automatically
3
- * updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago").
4
- *
5
- * @name timeago
6
- * @version 0.10.0
7
- * @requires jQuery v1.2.3+
8
- * @author Ryan McGeary
9
- * @license MIT License - http://www.opensource.org/licenses/mit-license.php
10
- *
11
- * For usage and examples, visit:
12
- * http://timeago.yarp.com/
13
- *
14
- * Copyright (c) 2008-2011, Ryan McGeary (ryanonjavascript -[at]- mcgeary [*dot*] org)
15
- */
2
+ * Timeago is a jQuery plugin that makes it easy to support automatically
3
+ * updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago").
4
+ *
5
+ * @name timeago
6
+ * @version 0.11
7
+ * @requires jQuery v1.2.3+
8
+ * @author Ryan McGeary
9
+ * @license MIT License - http://www.opensource.org/licenses/mit-license.php
10
+ *
11
+ * For usage and examples, visit:
12
+ * http://timeago.yarp.com/
13
+ *
14
+ * Copyright (c) 2008-2011, Ryan McGeary (ryanonjavascript -[at]- mcgeary [*dot*] org)
15
+ */
16
16
  (function($) {
17
17
  $.timeago = function(timestamp) {
18
18
  if (timestamp instanceof Date) {
@@ -45,6 +45,7 @@
45
45
  months: "%d months",
46
46
  year: "about a year",
47
47
  years: "%d years",
48
+ wordSeparator: " ",
48
49
  numbers: []
49
50
  }
50
51
  },
@@ -76,14 +77,14 @@
76
77
  minutes < 45 && substitute($l.minutes, Math.round(minutes)) ||
77
78
  minutes < 90 && substitute($l.hour, 1) ||
78
79
  hours < 24 && substitute($l.hours, Math.round(hours)) ||
79
- hours < 48 && substitute($l.day, 1) ||
80
- days < 30 && substitute($l.days, Math.floor(days)) ||
81
- days < 60 && substitute($l.month, 1) ||
82
- days < 365 && substitute($l.months, Math.floor(days / 30)) ||
83
- years < 2 && substitute($l.year, 1) ||
84
- substitute($l.years, Math.floor(years));
80
+ hours < 42 && substitute($l.day, 1) ||
81
+ days < 30 && substitute($l.days, Math.round(days)) ||
82
+ days < 45 && substitute($l.month, 1) ||
83
+ days < 365 && substitute($l.months, Math.round(days / 30)) ||
84
+ years < 1.5 && substitute($l.year, 1) ||
85
+ substitute($l.years, Math.round(years));
85
86
 
86
- return $.trim([prefix, words, suffix].join(" "));
87
+ return $.trim([prefix, words, suffix].join($l.wordSeparator));
87
88
  },
88
89
  parse: function(iso8601) {
89
90
  var s = $.trim(iso8601);
@@ -143,4 +144,4 @@
143
144
  // fix for IE6 suckage
144
145
  document.createElement("abbr");
145
146
  document.createElement("time");
146
- }(jQuery));
147
+ }(jQuery));
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-timeago
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-22 00:00:00.000000000 Z
12
+ date: 2012-03-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &7622620 !ruby/object:Gem::Requirement
16
+ requirement: &15835140 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.1'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *7622620
24
+ version_requirements: *15835140
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: actionpack
27
- requirement: &7621840 !ruby/object:Gem::Requirement
27
+ requirement: &15834280 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '3.1'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *7621840
35
+ version_requirements: *15834280
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &7621400 !ruby/object:Gem::Requirement
38
+ requirement: &15833840 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *7621400
46
+ version_requirements: *15833840
47
47
  description: jQuery Timeago helper for Rails 3
48
48
  email:
49
49
  - jan.graichen@altimos.de