rails-timeago 1.2.0.rc3 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -74,14 +74,12 @@ longer the application runs.
74
74
 
75
75
  ## I18n
76
76
 
77
- **Note:** Available since version *1.2.0.rc1*. I18n features are still untested.
78
-
79
77
  **rails-timeago** provides additional localization features and includes locale
80
78
  files for the following locales taken from [jQuery Timeago](https://github.com/rmm5t/jquery-timeago).
81
79
 
82
80
  > ar, bg, bs, ca, cy, cz, da, de, el, en, es, fa, fi, fr,
83
81
  > he, hr, hu, hy, id, it, ja, ko, nl, no, pl, pt, ro, ru,
84
- > sv, tr, uk, zh-CN, zh-TW
82
+ > sv, tr, uk, uz, zh-CN, zh-TW
85
83
 
86
84
  **rails-timeago** will automatically include the locale file for your current
87
85
  locale if it is available. You only have to include the following method into
@@ -97,10 +95,12 @@ list to allow precompilation. By default all locale files will be included but
97
95
  you can specify a list by adding a initializer similar to:
98
96
 
99
97
  ```ruby
100
- Rails::Timeago.locales = [:en, :de]
98
+ Rails::Timeago.locales = [:en, :de, "zh-CN", :sjn]
101
99
  ```
102
100
 
103
- This will only add English and German locale files to precompiled assets.
101
+ This will only add English, German, Chinese and Sindarin locale files to
102
+ precompiled assets. *rails-timeago* will also only use these locales for
103
+ locale file lookup for `timeago_script_tag`.
104
104
  To add your own locales add a file in JavaScript assets directory named
105
105
  `locales/jquery.timeago.<locale>.js` and add your locale to `Rails::Timeago.locales`.
106
106
 
@@ -56,58 +56,12 @@ module Rails
56
56
  end
57
57
 
58
58
  # Return a JavaScript tag to include jQuery timeago
59
- # locale file for current locale.
60
- def timeago_script_tag
61
- if ::Rails::Timeago.has_locale_file(I18n.locale) and I18n.locale != :en
62
- return javascript_include_tag 'locales/jquery.timeago.' + I18n.locale.to_s + '.js'
63
- end
59
+ # locale file for current or given locale.
60
+ def timeago_script_tag(locale = nil)
61
+ locale = ::Rails::Timeago.lookup_locale locale
62
+ return javascript_include_tag 'locales/' + ::Rails::Timeago.locale_file_name(locale) if locale != "en"
64
63
  ''
65
64
  end
66
65
  end
67
-
68
- # Read or write global rails-timeago default options. If no options are given
69
- # the current defaults will be returned.
70
- #
71
- # Available options:
72
- # [:+nojs+]
73
- # Add time ago in words as time tag content instead of absolute time.
74
- # (default: false)
75
- #
76
- # [:+date_only+]
77
- # Only print date as tag content instead of full time.
78
- # (default: true)
79
- #
80
- # [:+format+]
81
- # A time format for localize method used to format static time.
82
- # (default: :default)
83
- #
84
- # [:+limit+]
85
- # Set a limit for time ago tags. All dates before given limit will not be converted.
86
- # Global limit should be given as a block to reevaluate limit each time timeago_tag is called.
87
- # (default: proc { 4.days.ago })
88
- #
89
- # [:+force+]
90
- # Force time ago tag ignoring limit option.
91
- # (default: false)
92
- #
93
- # [:+default+]
94
- # String that will be returned if time is nil.
95
- # (default: '-')
96
- #
97
- def self.default_options(opts = nil)
98
- @defaults ||= {
99
- :nojs => false,
100
- :force => false,
101
- :format => :default,
102
- :limit => proc { 4.days.ago },
103
- :date_only => true,
104
- :default => '-'
105
- }
106
- if opts
107
- @defaults.merge! opts.extract!(*@defaults.keys.select{|k| opts.include?(k)})
108
- else
109
- @defaults
110
- end
111
- end
112
66
  end
113
67
  end
@@ -2,9 +2,9 @@ module Rails
2
2
  module Timeago
3
3
  module VERSION
4
4
  MAJOR = 1
5
- MINOR = 2
5
+ MINOR = 3
6
6
  PATCH = 0
7
- STAGE = 'rc3'
7
+ STAGE = nil
8
8
 
9
9
  def self.to_s
10
10
  [MAJOR, MINOR, PATCH, STAGE].reject(&:nil?).join '.'
data/lib/rails-timeago.rb CHANGED
@@ -38,27 +38,104 @@ module Rails
38
38
  end
39
39
  end
40
40
 
41
+ # Read or write global rails-timeago default options. If no options are given
42
+ # the current defaults will be returned.
43
+ #
44
+ # Available options:
45
+ # [:+nojs+]
46
+ # Add time ago in words as time tag content instead of absolute time.
47
+ # (default: false)
48
+ #
49
+ # [:+date_only+]
50
+ # Only print date as tag content instead of full time.
51
+ # (default: true)
52
+ #
53
+ # [:+format+]
54
+ # A time format for localize method used to format static time.
55
+ # (default: :default)
56
+ #
57
+ # [:+limit+]
58
+ # Set a limit for time ago tags. All dates before given limit will not be converted.
59
+ # Global limit should be given as a block to reevaluate limit each time timeago_tag is called.
60
+ # (default: proc { 4.days.ago })
61
+ #
62
+ # [:+force+]
63
+ # Force time ago tag ignoring limit option.
64
+ # (default: false)
65
+ #
66
+ # [:+default+]
67
+ # String that will be returned if time is nil.
68
+ # (default: '-')
69
+ #
70
+ def self.default_options(opts = nil)
71
+ @defaults ||= {
72
+ :nojs => false,
73
+ :force => false,
74
+ :format => :default,
75
+ :limit => proc { 4.days.ago },
76
+ :date_only => true,
77
+ :default => '-'
78
+ }
79
+ if opts
80
+ @defaults.merge! opts.extract!(*@defaults.keys.select{|k| opts.include?(k)})
81
+ else
82
+ @defaults
83
+ end
84
+ end
85
+
41
86
  def self.locale_path
42
87
  File.dirname(__FILE__) + '/../vendor/assets/javascripts/locales/'
43
88
  end
44
89
 
45
90
  def self.locale_file(locale)
46
- locale_path + 'jquery.timeago.' + locale.to_s + '.js'
91
+ locale_path + locale_file_name(locale)
92
+ end
93
+
94
+ def self.locale_file_name(locale)
95
+ 'jquery.timeago.' + locale + '.js'
47
96
  end
48
97
 
49
98
  def self.has_locale_file(locale)
50
- File.exist? locale_file(I18n.locale)
99
+ File.exist? locale_file(locale)
100
+ end
101
+
102
+ # Look up a timeago locale. If no locale is given I18n's
103
+ # default locale will be used. Lookup follows the given
104
+ # order:
105
+ # 1) ll-CC (language and country)
106
+ # 2) ll (only language)
107
+ # 3) I18n default locale
108
+ # 4) "en"
109
+ def self.lookup_locale(locale = nil)
110
+ locale = I18n.locale.to_s unless locale
111
+
112
+ if locale =~ /^(\w+)(\-(\w+))?$/
113
+ lang = $1.downcase
114
+ if $3
115
+ ctry = $3.upcase
116
+ return "#{lang}-#{ctry}" if has_locale "#{lang}-#{ctry}"
117
+ end
118
+ return lang if has_locale lang
119
+ end
120
+
121
+ return I18n.default_locale.to_s if has_locale I18n.default_locale.to_s
122
+ "en"
123
+ end
124
+
125
+ def self.has_locale(locale)
126
+ return locales.include? locale if locales.any?
127
+ return has_locale_file locale
51
128
  end
52
129
 
53
130
  def self.locales
54
- @locales
131
+ @locales ||= []
55
132
  end
56
133
 
57
134
  def self.locales=(*attrs)
58
135
  if attrs[0].kind_of?(Array)
59
- @locales = attrs[0].map(&:to_sym)
136
+ @locales = attrs[0].map(&:to_s)
60
137
  else
61
- @locales = attrs.map(&:to_sym)
138
+ @locales = attrs.map(&:to_s)
62
139
  end
63
140
  end
64
141
  end
data/spec/support/stub.rb CHANGED
@@ -11,4 +11,8 @@ class TimeagoStub
11
11
  def time_ago_in_words(time)
12
12
  "%time_ago_in_words%"
13
13
  end
14
+
15
+ def javascript_include_tag(path)
16
+ "<script src=\"#{path}\"></script>"
17
+ end
14
18
  end
@@ -0,0 +1,143 @@
1
+
2
+ require File.dirname(__FILE__) + '/../spec_helper'
3
+
4
+ describe Rails::Timeago::Helper do
5
+ before { @stub = TimeagoStub.new }
6
+
7
+ context "#timeago_tag" do
8
+ it 'should create a time tag' do
9
+ @stub.timeago_tag(Time.now).should =~ /<time.*>.*<\/time>/
10
+ end
11
+
12
+ it 'should have title attribute' do
13
+ @stub.timeago_tag(Time.now).should =~ /<time.*title=".*".*>.*<\/time>/
14
+ end
15
+
16
+ it 'should have data-time-ago attribute' do
17
+ @stub.timeago_tag(Time.now).should =~ /<time.*data-time-ago=".*".*>.*<\/time>/
18
+ end
19
+
20
+ it 'should not have data-time-ago attribute for times before limit' do
21
+ @stub.timeago_tag(5.days.ago).should_not =~ /<time.*data-time-ago=".*".*>.*<\/time>/
22
+ end
23
+
24
+ it 'should have data-time-ago attribute for times after given limit' do
25
+ @stub.timeago_tag(5.days.ago, :limit => 6.days.ago).
26
+ should =~ /<time.*data-time-ago=".*".*>.*<\/time>/
27
+ end
28
+
29
+ it 'should have not data-time-ago attribute for times before given limit' do
30
+ @stub.timeago_tag(6.days.ago, :limit => 5.days.ago).
31
+ should_not =~ /<time.*data-time-ago=".*".*>.*<\/time>/
32
+ end
33
+
34
+ it 'should have data-time-ago attribute for times before limit if forced' do
35
+ @stub.timeago_tag(6.days.ago, :force => true).
36
+ should =~ /<time.*data-time-ago=".*".*>.*<\/time>/
37
+ end
38
+
39
+ it 'should have localized date as content' do
40
+ time = 3.days.ago
41
+ @stub.timeago_tag(time).should include(">#{I18n.l time.to_date}<")
42
+ end
43
+
44
+ it 'should have localized time as content if date_only is false' do
45
+ time = 3.days.ago
46
+ @stub.timeago_tag(time, :date_only => false).should include(">#{I18n.l time}<")
47
+ end
48
+
49
+ it 'should have time ago in words as content if nojs is true' do
50
+ time = 3.days.ago
51
+ @stub.timeago_tag(time, :nojs => true).should =~ /<time.*>%time_ago_in_words%<\/time>/
52
+ end
53
+
54
+ it 'should pass format option to localize method' do
55
+ time = 3.days.ago
56
+ @stub.timeago_tag(time, :format => :short).
57
+ should include(">#{I18n.l time.to_date, :format => :short}<")
58
+ end
59
+
60
+ it 'should pass html option to tag helper' do
61
+ @stub.timeago_tag(Time.now, :myattr => 'abc').should =~ /<time.*myattr="abc".*>.*<\/time>/
62
+ end
63
+
64
+ it "should allow to set global options" do
65
+ Rails::Timeago.default_options :format => :short, :limit => proc { 8.days.ago }
66
+ time = 7.days.ago
67
+
68
+ @stub.timeago_tag(time).
69
+ should include(">#{I18n.l time.to_date, :format => :short}<")
70
+ @stub.timeago_tag(time).
71
+ should =~ /<time.*data-time-ago=".*".*>.*<\/time>/
72
+ end
73
+
74
+ it "should allow to override global options" do
75
+ Rails::Timeago.default_options :format => :short, :limit => proc { 8.days.ago }
76
+ time = 7.days.ago
77
+
78
+ @stub.timeago_tag(time, :format => :long).
79
+ should include(">#{I18n.l time.to_date, :format => :long}<")
80
+ @stub.timeago_tag(time, :limit => 4.days.ago).
81
+ should_not =~ /<time.*data-time-ago=".*".*>.*<\/time>/
82
+ end
83
+
84
+ it "should return default string if time is nil" do
85
+ @stub.timeago_tag(nil).should == '-'
86
+ end
87
+ end
88
+
89
+ context "#timeago_script_tag" do
90
+ it "should not return anything if locale is 'en'" do
91
+ I18n.locale = "en"
92
+ @stub.timeago_script_tag.should == ""
93
+ end
94
+
95
+ it "should return javascript tag with locale file" do
96
+ I18n.locale = "de"
97
+ @stub.timeago_script_tag.should == '<script src="locales/jquery.timeago.de.js"></script>'
98
+ end
99
+
100
+ it "should return javascript tag with full locale file if available" do
101
+ I18n.locale = "zh-CN"
102
+ @stub.timeago_script_tag.should == '<script src="locales/jquery.timeago.zh-CN.js"></script>'
103
+ end
104
+
105
+ it "should return javascript tag with lang locale file if available" do
106
+ I18n.locale = "nl-NL"
107
+ @stub.timeago_script_tag.should == '<script src="locales/jquery.timeago.nl.js"></script>'
108
+ end
109
+
110
+ it "should return javascript tag with formatted locale file" do
111
+ I18n.locale = "zh-cn"
112
+ @stub.timeago_script_tag.should == '<script src="locales/jquery.timeago.zh-CN.js"></script>'
113
+ end
114
+
115
+ context "with global locale configuration" do
116
+ before { Rails::Timeago.locales = [:de, :en, "zh-CN", "tlh", "nl"] }
117
+ after { Rails::Timeago.locales = [] }
118
+
119
+ it "should include full locale if available" do
120
+ I18n.locale = "zh-CN"
121
+ @stub.timeago_script_tag.should == '<script src="locales/jquery.timeago.zh-CN.js"></script>'
122
+ end
123
+
124
+ it "should include lang locale if available" do
125
+ I18n.locale = "nl-NL"
126
+ @stub.timeago_script_tag.should == '<script src="locales/jquery.timeago.nl.js"></script>'
127
+ end
128
+
129
+ it "should include default locale for not specified locales" do
130
+ I18n.locale = :ar
131
+ I18n.default_locale = :de
132
+ @stub.timeago_script_tag.should == '<script src="locales/jquery.timeago.de.js"></script>'
133
+
134
+ I18n.default_locale = :en
135
+ end
136
+
137
+ it "should find added locales that does not have a locale file in gem" do
138
+ I18n.locale = "tlh"
139
+ @stub.timeago_script_tag.should == '<script src="locales/jquery.timeago.tlh.js"></script>'
140
+ end
141
+ end
142
+ end
143
+ end
data/spec/timeago_spec.rb CHANGED
@@ -1,86 +1,66 @@
1
1
 
2
2
  require File.dirname(__FILE__) + '/spec_helper'
3
3
 
4
- describe Rails::Timeago::Helper do
5
- before { @stub = TimeagoStub.new }
6
-
7
- it 'should create a time tag' do
8
- @stub.timeago_tag(Time.now).should =~ /<time.*>.*<\/time>/
9
- end
10
-
11
- it 'should have title attribute' do
12
- @stub.timeago_tag(Time.now).should =~ /<time.*title=".*".*>.*<\/time>/
13
- end
14
-
15
- it 'should have data-time-ago attribute' do
16
- @stub.timeago_tag(Time.now).should =~ /<time.*data-time-ago=".*".*>.*<\/time>/
17
- end
18
-
19
- it 'should not have data-time-ago attribute for times before limit' do
20
- @stub.timeago_tag(5.days.ago).should_not =~ /<time.*data-time-ago=".*".*>.*<\/time>/
21
- end
22
-
23
- it 'should have data-time-ago attribute for times after given limit' do
24
- @stub.timeago_tag(5.days.ago, :limit => 6.days.ago).
25
- should =~ /<time.*data-time-ago=".*".*>.*<\/time>/
26
- end
27
-
28
- it 'should have not data-time-ago attribute for times before given limit' do
29
- @stub.timeago_tag(6.days.ago, :limit => 5.days.ago).
30
- should_not =~ /<time.*data-time-ago=".*".*>.*<\/time>/
31
- end
32
-
33
- it 'should have data-time-ago attribute for times before limit if forced' do
34
- @stub.timeago_tag(6.days.ago, :force => true).
35
- should =~ /<time.*data-time-ago=".*".*>.*<\/time>/
36
- end
37
-
38
- it 'should have localized date as content' do
39
- time = 3.days.ago
40
- @stub.timeago_tag(time).should include(">#{I18n.l time.to_date}<")
41
- end
42
-
43
- it 'should have localized time as content if date_only is false' do
44
- time = 3.days.ago
45
- @stub.timeago_tag(time, :date_only => false).should include(">#{I18n.l time}<")
46
- end
47
-
48
- it 'should have time ago in words as content if nojs is true' do
49
- time = 3.days.ago
50
- @stub.timeago_tag(time, :nojs => true).should =~ /<time.*>%time_ago_in_words%<\/time>/
51
- end
52
-
53
- it 'should pass format option to localize method' do
54
- time = 3.days.ago
55
- @stub.timeago_tag(time, :format => :short).
56
- should include(">#{I18n.l time.to_date, :format => :short}<")
57
- end
58
-
59
- it 'should pass html option to tag helper' do
60
- @stub.timeago_tag(Time.now, :myattr => 'abc').should =~ /<time.*myattr="abc".*>.*<\/time>/
61
- end
62
-
63
- it "should allow to set global options" do
64
- Rails::Timeago.default_options :format => :short, :limit => proc { 8.days.ago }
65
- time = 7.days.ago
66
-
67
- @stub.timeago_tag(time).
68
- should include(">#{I18n.l time.to_date, :format => :short}<")
69
- @stub.timeago_tag(time).
70
- should =~ /<time.*data-time-ago=".*".*>.*<\/time>/
71
- end
72
-
73
- it "should allow to override global options" do
74
- Rails::Timeago.default_options :format => :short, :limit => proc { 8.days.ago }
75
- time = 7.days.ago
76
-
77
- @stub.timeago_tag(time, :format => :long).
78
- should include(">#{I18n.l time.to_date, :format => :long}<")
79
- @stub.timeago_tag(time, :limit => 4.days.ago).
80
- should_not =~ /<time.*data-time-ago=".*".*>.*<\/time>/
81
- end
82
-
83
- it "should return default string if time is nil" do
84
- @stub.timeago_tag(nil).should == '-'
4
+ describe Rails::Timeago do
5
+ context "#lookup_locale" do
6
+ it "should return full locale if available" do
7
+ Rails::Timeago.lookup_locale("zh-CN").should == "zh-CN"
8
+ end
9
+
10
+ it "should return formatted locale" do
11
+ Rails::Timeago.lookup_locale("zh-cn").should == "zh-CN"
12
+ end
13
+
14
+ it "should return lang locale if available" do
15
+ Rails::Timeago.lookup_locale("nl-NL").should == "nl"
16
+ end
17
+
18
+ it "should return locale if available" do
19
+ Rails::Timeago.lookup_locale("de").should == "de"
20
+ end
21
+
22
+ it "should lookup default locale if nothing given" do
23
+ I18n.locale = "ar"
24
+
25
+ Rails::Timeago.lookup_locale.should == "ar"
26
+ end
27
+
28
+ it "should return english locale if locale does not
29
+ match and default locale is not available" do
30
+
31
+ I18n.locale = "sjn"
32
+ Rails::Timeago.lookup_locale("tlh").should == "en"
33
+ end
34
+
35
+ context "with global locale configuration" do
36
+ before { Rails::Timeago.locales = [:de, :en, "zh-CN", "tlh"] }
37
+ after { Rails::Timeago.locales = [] }
38
+
39
+ it "should return full locale if available" do
40
+ Rails::Timeago.lookup_locale("zh-CN").should == "zh-CN"
41
+ end
42
+
43
+ it "should return lang locale if available" do
44
+ Rails::Timeago.lookup_locale("en-US").should == "en"
45
+ end
46
+
47
+ it "should return use default locale if available" do
48
+ I18n.locale = "en"
49
+ Rails::Timeago.lookup_locale.should == "en"
50
+ end
51
+
52
+ it "should return default locale for not specified locales" do
53
+ I18n.locale = "ar"
54
+ I18n.default_locale = :de
55
+ Rails::Timeago.lookup_locale.should == "de"
56
+
57
+ I18n.default_locale = :en
58
+ end
59
+
60
+ it "should find added locales that does not have a locale file in gem" do
61
+ I18n.locale = "tlh"
62
+ Rails::Timeago.lookup_locale.should == "tlh"
63
+ end
64
+ end
85
65
  end
86
66
  end
metadata CHANGED
@@ -1,19 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-timeago
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0.rc3
5
- prerelease: 6
4
+ version: 1.3.0
5
+ prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jan Graichen
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-10 00:00:00.000000000 Z
12
+ date: 2012-05-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &4970440 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '3.1'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *4970440
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '3.1'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: actionpack
27
- requirement: &4969820 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: '3.1'
33
38
  type: :runtime
34
39
  prerelease: false
35
- version_requirements: *4969820
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '3.1'
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: rspec
38
- requirement: &4969300 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ! '>='
@@ -43,7 +53,12 @@ dependencies:
43
53
  version: '0'
44
54
  type: :development
45
55
  prerelease: false
46
- version_requirements: *4969300
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
47
62
  description: jQuery Timeago helper for Rails 3
48
63
  email:
49
64
  - jan.graichen@altimos.de
@@ -63,6 +78,7 @@ files:
63
78
  - rails-timeago.gemspec
64
79
  - spec/spec_helper.rb
65
80
  - spec/support/stub.rb
81
+ - spec/timeago/helper_spec.rb
66
82
  - spec/timeago_spec.rb
67
83
  - vendor/assets/javascripts/jquery.timeago.js
68
84
  - vendor/assets/javascripts/locales/jquery.timeago.ar.js
@@ -113,12 +129,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
113
129
  required_rubygems_version: !ruby/object:Gem::Requirement
114
130
  none: false
115
131
  requirements:
116
- - - ! '>'
132
+ - - ! '>='
117
133
  - !ruby/object:Gem::Version
118
- version: 1.3.1
134
+ version: '0'
119
135
  requirements: []
120
136
  rubyforge_project:
121
- rubygems_version: 1.8.15
137
+ rubygems_version: 1.8.24
122
138
  signing_key:
123
139
  specification_version: 3
124
140
  summary: A Rails Helper to create time tags usable for jQuery Timeago plugin