rails-timeago 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rails-timeago.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Jan Graichen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # rails-timeago
2
+
3
+ *rails-timeago* provides a timeago_tag helper to create time tags usable for
4
+ [jQuery Timeago](https://github.com/rmm5t/jquery-timeago) plugin.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'rails-timeago'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install rails-timeago
19
+
20
+ To use bundled jQuery Timeago plugin add this require statement to your application.js file:
21
+
22
+ //=require rails-timeago
23
+
24
+ ## Usage
25
+
26
+ Use the timeago_tag helper like any other regular tag helper:
27
+
28
+ <%= timeago_tag Time.zone.now, :nojs => true, :limit => 10.days.ago %>
29
+
30
+
31
+ ### Available options:
32
+
33
+ * nojs
34
+ Add time ago in words as time tag content instead of absolute time. (default: false)
35
+
36
+ * date_only
37
+ Only print date as tag content instead of full time. (default: true)
38
+
39
+ * format
40
+ A time format for localize method used to format static time. (default: default)
41
+
42
+ * limit
43
+ Set a limit for time ago tags. All dates before given limit will not be converted. (default: 4.days.ago)
44
+
45
+ * force
46
+ Force time ago tag ignoring limit option. (default: false)
47
+
48
+ All other options will be given as options to the time tag helper.
49
+
50
+ ## License
51
+
52
+ [MIT License](http://www.opensource.org/licenses/mit-license.php)
53
+
54
+ Copyright (c) 2012, Jan Graichen
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,8 @@
1
+ //
2
+ // jQuery Timeago bootstrap for rails-timeago helper
3
+ //
4
+ //= require jquery.timeago
5
+
6
+ jQuery(document).ready(function() {
7
+ $('time[data-time-ago]').timeago();
8
+ });
@@ -0,0 +1,18 @@
1
+ require "rails-timeago/version"
2
+ require "rails-timeago/helper"
3
+
4
+ module Rails
5
+ module Timeago
6
+ class Engine < ::Rails::Engine # :nodoc:
7
+ initializer 'rails-timeago' do |app|
8
+ ActiveSupport.on_load(:action_controller) do
9
+ include Rails::Timeago::Helper
10
+ end
11
+
12
+ ActiveSupport.on_load(:action_view) do
13
+ include Rails::Timeago::Helper
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,57 @@
1
+ module Rails
2
+ module Timeago
3
+ module Helper
4
+ # Create a time tag usable for jQuery timeago plugin.
5
+ #
6
+ # timeago_tag Time.zone.now
7
+ # => ""
8
+ #
9
+ # Available options:
10
+ # [:+nojs+]
11
+ # Add time ago in words as time tag content instead of absolute time.
12
+ #
13
+ # [:+date_only+]
14
+ # Only print date as tag content instead of full time.
15
+ #
16
+ # [:+format+]
17
+ # A time format for localize method used to format static time.
18
+ #
19
+ # [:+limit+]
20
+ # Set a limit for time ago tags. All dates before given limit will not be converted.
21
+ # (default: 4.days.ago)
22
+ #
23
+ # [:+force+]
24
+ # Force time ago tag ignoring limit option.
25
+ #
26
+ # All other options will be given as options to tag helper.
27
+ #
28
+ 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
+ }
36
+
37
+ time_options.merge! html_options.extract!(*time_options.keys.select{|k| html_options.include?(k)})
38
+ html_options.merge! :title => I18n.l(time, :format => time_options[:format])
39
+
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
+
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
46
+ end
47
+ end
48
+
49
+ def timeago_tag_content(time, time_options = {}) # :nodoc:
50
+ time = time.to_date if time_options[:date_only]
51
+ return time_ago_in_words(time) if time_options[:nojs]
52
+
53
+ I18n.l time, :format => time_options[:format]
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,13 @@
1
+ module Rails
2
+ module Timeago
3
+ module VERSION
4
+ MAJOR = 1
5
+ MINOR = 0
6
+ PATCH = 0
7
+
8
+ def self.to_s
9
+ [MAJOR, MINOR, PATCH].join '.'
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/rails-timeago/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Jan Graichen"]
6
+ gem.email = ["jan.graichen@altimos.de"]
7
+ gem.description = %q{jQuery Timeago helper for Rails 3}
8
+ gem.summary = %q{A Rails Helper to create time tags usable for jQuery Timeago plugin}
9
+ gem.homepage = "https://github.com/jgraichen/rails-timeago"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "rails-timeago"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Rails::Timeago::VERSION
17
+
18
+ gem.add_dependency "activesupport", ">= 3.1"
19
+ gem.add_dependency "actionpack", ">= 3.1"
20
+ gem.add_development_dependency "rspec"
21
+ end
@@ -0,0 +1,6 @@
1
+
2
+ require 'active_support/core_ext'
3
+
4
+ RSpec.configure do |config|
5
+ config.mock_with :rspec
6
+ end
@@ -0,0 +1,12 @@
1
+ class TimeagoStub
2
+ include Rails::Timeago::Helper
3
+
4
+ def time_tag(time, content, options = {})
5
+ options = options.map { |k,v| "#{k}=\"#{v}\""}
6
+ "<time datetime=\"#{time.iso8601}\" #{options.join ' '}>#{content}</time>"
7
+ end
8
+
9
+ def time_ago_in_words(time)
10
+ "%time_ago_in_words%"
11
+ end
12
+ end
@@ -0,0 +1,64 @@
1
+
2
+ require File.dirname(__FILE__) + '/spec_helper'
3
+ require File.dirname(__FILE__) + '/../lib/rails-timeago/helper.rb'
4
+ require File.dirname(__FILE__) + '/support/stub.rb'
5
+
6
+ describe Rails::Timeago::Helper do
7
+ before { @stub = TimeagoStub.new }
8
+
9
+ it 'should create a time tag' do
10
+ @stub.timeago_tag(Time.now).should =~ /<time.*>.*<\/time>/
11
+ end
12
+
13
+ it 'should have title attribute' do
14
+ @stub.timeago_tag(Time.now).should =~ /<time.*title=".*".*>.*<\/time>/
15
+ end
16
+
17
+ it 'should have data-time-ago attribute' do
18
+ @stub.timeago_tag(Time.now).should =~ /<time.*data-time-ago=".*".*>.*<\/time>/
19
+ end
20
+
21
+ it 'should not have data-time-ago attribute for times before limit' do
22
+ @stub.timeago_tag(5.days.ago).should_not =~ /<time.*data-time-ago=".*".*>.*<\/time>/
23
+ end
24
+
25
+ it 'should have data-time-ago attribute for times after given limit' do
26
+ @stub.timeago_tag(5.days.ago, :limit => 6.days.ago).
27
+ should =~ /<time.*data-time-ago=".*".*>.*<\/time>/
28
+ end
29
+
30
+ it 'should have not data-time-ago attribute for times before given limit' do
31
+ @stub.timeago_tag(6.days.ago, :limit => 5.days.ago).
32
+ should_not =~ /<time.*data-time-ago=".*".*>.*<\/time>/
33
+ end
34
+
35
+ it 'should have data-time-ago attribute for times before limit if forced' do
36
+ @stub.timeago_tag(6.days.ago, :force => true).
37
+ should =~ /<time.*data-time-ago=".*".*>.*<\/time>/
38
+ end
39
+
40
+ it 'should have localized date as content' do
41
+ time = 3.days.ago
42
+ @stub.timeago_tag(time).should include(">#{I18n.l time.to_date}<")
43
+ end
44
+
45
+ it 'should have localized time as content date_only is false' do
46
+ time = 3.days.ago
47
+ @stub.timeago_tag(time, :date_only => false).should include(">#{I18n.l time}<")
48
+ end
49
+
50
+ it 'should have time ago in words as content if nojs is true' do
51
+ time = 3.days.ago
52
+ @stub.timeago_tag(time, :nojs => true).should =~ /<time.*>%time_ago_in_words%<\/time>/
53
+ end
54
+
55
+ it 'should pass format option to localize method' do
56
+ time = 3.days.ago
57
+ @stub.timeago_tag(time, :format => :short).
58
+ should include(">#{I18n.l time.to_date, :format => :short}<")
59
+ end
60
+
61
+ it 'should pass html option to tag helper' do
62
+ @stub.timeago_tag(Time.now, :myattr => 'abc').should =~ /<time.*myattr="abc".*>.*<\/time>/
63
+ end
64
+ end
@@ -0,0 +1,146 @@
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
+ */
16
+ (function($) {
17
+ $.timeago = function(timestamp) {
18
+ if (timestamp instanceof Date) {
19
+ return inWords(timestamp);
20
+ } else if (typeof timestamp === "string") {
21
+ return inWords($.timeago.parse(timestamp));
22
+ } else {
23
+ return inWords($.timeago.datetime(timestamp));
24
+ }
25
+ };
26
+ var $t = $.timeago;
27
+
28
+ $.extend($.timeago, {
29
+ settings: {
30
+ refreshMillis: 60000,
31
+ allowFuture: false,
32
+ strings: {
33
+ prefixAgo: null,
34
+ prefixFromNow: null,
35
+ suffixAgo: "ago",
36
+ suffixFromNow: "from now",
37
+ seconds: "less than a minute",
38
+ minute: "about a minute",
39
+ minutes: "%d minutes",
40
+ hour: "about an hour",
41
+ hours: "about %d hours",
42
+ day: "a day",
43
+ days: "%d days",
44
+ month: "about a month",
45
+ months: "%d months",
46
+ year: "about a year",
47
+ years: "%d years",
48
+ numbers: []
49
+ }
50
+ },
51
+ inWords: function(distanceMillis) {
52
+ var $l = this.settings.strings;
53
+ var prefix = $l.prefixAgo;
54
+ var suffix = $l.suffixAgo;
55
+ if (this.settings.allowFuture) {
56
+ if (distanceMillis < 0) {
57
+ prefix = $l.prefixFromNow;
58
+ suffix = $l.suffixFromNow;
59
+ }
60
+ }
61
+
62
+ var seconds = Math.abs(distanceMillis) / 1000;
63
+ var minutes = seconds / 60;
64
+ var hours = minutes / 60;
65
+ var days = hours / 24;
66
+ var years = days / 365;
67
+
68
+ function substitute(stringOrFunction, number) {
69
+ var string = $.isFunction(stringOrFunction) ? stringOrFunction(number, distanceMillis) : stringOrFunction;
70
+ var value = ($l.numbers && $l.numbers[number]) || number;
71
+ return string.replace(/%d/i, value);
72
+ }
73
+
74
+ var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) ||
75
+ seconds < 90 && substitute($l.minute, 1) ||
76
+ minutes < 45 && substitute($l.minutes, Math.round(minutes)) ||
77
+ minutes < 90 && substitute($l.hour, 1) ||
78
+ 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));
85
+
86
+ return $.trim([prefix, words, suffix].join(" "));
87
+ },
88
+ parse: function(iso8601) {
89
+ var s = $.trim(iso8601);
90
+ s = s.replace(/\.\d\d\d+/,""); // remove milliseconds
91
+ s = s.replace(/-/,"/").replace(/-/,"/");
92
+ s = s.replace(/T/," ").replace(/Z/," UTC");
93
+ s = s.replace(/([\+\-]\d\d)\:?(\d\d)/," $1$2"); // -04:00 -> -0400
94
+ return new Date(s);
95
+ },
96
+ datetime: function(elem) {
97
+ // jQuery's `is()` doesn't play well with HTML5 in IE
98
+ var isTime = $(elem).get(0).tagName.toLowerCase() === "time"; // $(elem).is("time");
99
+ var iso8601 = isTime ? $(elem).attr("datetime") : $(elem).attr("title");
100
+ return $t.parse(iso8601);
101
+ }
102
+ });
103
+
104
+ $.fn.timeago = function() {
105
+ var self = this;
106
+ self.each(refresh);
107
+
108
+ var $s = $t.settings;
109
+ if ($s.refreshMillis > 0) {
110
+ setInterval(function() { self.each(refresh); }, $s.refreshMillis);
111
+ }
112
+ return self;
113
+ };
114
+
115
+ function refresh() {
116
+ var data = prepareData(this);
117
+ if (!isNaN(data.datetime)) {
118
+ $(this).text(inWords(data.datetime));
119
+ }
120
+ return this;
121
+ }
122
+
123
+ function prepareData(element) {
124
+ element = $(element);
125
+ if (!element.data("timeago")) {
126
+ element.data("timeago", { datetime: $t.datetime(element) });
127
+ var text = $.trim(element.text());
128
+ if (text.length > 0) {
129
+ element.attr("title", text);
130
+ }
131
+ }
132
+ return element.data("timeago");
133
+ }
134
+
135
+ function inWords(date) {
136
+ return $t.inWords(distance(date));
137
+ }
138
+
139
+ function distance(date) {
140
+ return (new Date().getTime() - date.getTime());
141
+ }
142
+
143
+ // fix for IE6 suckage
144
+ document.createElement("abbr");
145
+ document.createElement("time");
146
+ }(jQuery));
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-timeago
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jan Graichen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: &7622620 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3.1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *7622620
25
+ - !ruby/object:Gem::Dependency
26
+ name: actionpack
27
+ requirement: &7621840 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '3.1'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *7621840
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &7621400 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *7621400
47
+ description: jQuery Timeago helper for Rails 3
48
+ email:
49
+ - jan.graichen@altimos.de
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE
57
+ - README.md
58
+ - Rakefile
59
+ - lib/assets/javascripts/rails-timeago.js
60
+ - lib/rails-timeago.rb
61
+ - lib/rails-timeago/helper.rb
62
+ - lib/rails-timeago/version.rb
63
+ - rails-timeago.gemspec
64
+ - spec/spec_helper.rb
65
+ - spec/support/stub.rb
66
+ - spec/timeago_spec.rb
67
+ - vendor/assets/javascripts/jquery.timeago.js
68
+ homepage: https://github.com/jgraichen/rails-timeago
69
+ licenses: []
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 1.8.15
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: A Rails Helper to create time tags usable for jQuery Timeago plugin
92
+ test_files: []