Skip to content
This repository
Browse code

Cache timestamp in pull request partial. Use jQuery.timeago to show d…

…istance in words. #198
  • Loading branch information...
commit 9b1f0297d9d0b9e9f8739c2e072356c9f20afd19 1 parent d8bed26
authored December 12, 2012
1  app/assets/javascripts/application.js
@@ -14,4 +14,5 @@
14 14
 //= require jquery_ujs
15 15
 //= require twitter/bootstrap
16 16
 //= require coderwall
  17
+//= require jquery.timeago
17 18
 //= require_tree .
2  app/assets/javascripts/pull_requests.js.coffee
... ...
@@ -0,0 +1,2 @@
  1
+$ ->
  2
+  $('abbr.timeago').timeago()
5  app/views/pull_requests/_pull_request.html.haml
@@ -8,7 +8,6 @@
8 8
           on
9 9
           = link_to pull_request.repo_name, "https://github.com/#{pull_request.repo_name}", :target => :blank
10 10
           %small
11  
-            = distance_of_time_in_words_to_now pull_request.created_at
12  
-            ago
  11
+            %abbr.timeago{:title => pull_request.created_at.iso8601}= pull_request.created_at
13 12
       %p= truncate pull_request.body, :length => 300
14  
-      %hr
  13
+      %hr
152  vendor/assets/javascripts/jquery.timeago.js
... ...
@@ -0,0 +1,152 @@
  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.11.4
  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-2012, Ryan McGeary (ryan -[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 if (typeof timestamp === "number") {
  23
+      return inWords(new Date(timestamp));
  24
+    } else {
  25
+      return inWords($.timeago.datetime(timestamp));
  26
+    }
  27
+  };
  28
+  var $t = $.timeago;
  29
+
  30
+  $.extend($.timeago, {
  31
+    settings: {
  32
+      refreshMillis: 60000,
  33
+      allowFuture: false,
  34
+      strings: {
  35
+        prefixAgo: null,
  36
+        prefixFromNow: null,
  37
+        suffixAgo: "ago",
  38
+        suffixFromNow: "from now",
  39
+        seconds: "less than a minute",
  40
+        minute: "about a minute",
  41
+        minutes: "%d minutes",
  42
+        hour: "about an hour",
  43
+        hours: "about %d hours",
  44
+        day: "a day",
  45
+        days: "%d days",
  46
+        month: "about a month",
  47
+        months: "%d months",
  48
+        year: "about a year",
  49
+        years: "%d years",
  50
+        wordSeparator: " ",
  51
+        numbers: []
  52
+      }
  53
+    },
  54
+    inWords: function(distanceMillis) {
  55
+      var $l = this.settings.strings;
  56
+      var prefix = $l.prefixAgo;
  57
+      var suffix = $l.suffixAgo;
  58
+      if (this.settings.allowFuture) {
  59
+        if (distanceMillis < 0) {
  60
+          prefix = $l.prefixFromNow;
  61
+          suffix = $l.suffixFromNow;
  62
+        }
  63
+      }
  64
+
  65
+      var seconds = Math.abs(distanceMillis) / 1000;
  66
+      var minutes = seconds / 60;
  67
+      var hours = minutes / 60;
  68
+      var days = hours / 24;
  69
+      var years = days / 365;
  70
+
  71
+      function substitute(stringOrFunction, number) {
  72
+        var string = $.isFunction(stringOrFunction) ? stringOrFunction(number, distanceMillis) : stringOrFunction;
  73
+        var value = ($l.numbers && $l.numbers[number]) || number;
  74
+        return string.replace(/%d/i, value);
  75
+      }
  76
+
  77
+      var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) ||
  78
+        seconds < 90 && substitute($l.minute, 1) ||
  79
+        minutes < 45 && substitute($l.minutes, Math.round(minutes)) ||
  80
+        minutes < 90 && substitute($l.hour, 1) ||
  81
+        hours < 24 && substitute($l.hours, Math.round(hours)) ||
  82
+        hours < 42 && substitute($l.day, 1) ||
  83
+        days < 30 && substitute($l.days, Math.round(days)) ||
  84
+        days < 45 && substitute($l.month, 1) ||
  85
+        days < 365 && substitute($l.months, Math.round(days / 30)) ||
  86
+        years < 1.5 && substitute($l.year, 1) ||
  87
+        substitute($l.years, Math.round(years));
  88
+
  89
+      var separator = $l.wordSeparator === undefined ?  " " : $l.wordSeparator;
  90
+      return $.trim([prefix, words, suffix].join(separator));
  91
+    },
  92
+    parse: function(iso8601) {
  93
+      var s = $.trim(iso8601);
  94
+      s = s.replace(/\.\d+/,""); // remove milliseconds
  95
+      s = s.replace(/-/,"/").replace(/-/,"/");
  96
+      s = s.replace(/T/," ").replace(/Z/," UTC");
  97
+      s = s.replace(/([\+\-]\d\d)\:?(\d\d)/," $1$2"); // -04:00 -> -0400
  98
+      return new Date(s);
  99
+    },
  100
+    datetime: function(elem) {
  101
+      var iso8601 = $t.isTime(elem) ? $(elem).attr("datetime") : $(elem).attr("title");
  102
+      return $t.parse(iso8601);
  103
+    },
  104
+    isTime: function(elem) {
  105
+      // jQuery's `is()` doesn't play well with HTML5 in IE
  106
+      return $(elem).get(0).tagName.toLowerCase() === "time"; // $(elem).is("time");
  107
+    }
  108
+  });
  109
+
  110
+  $.fn.timeago = function() {
  111
+    var self = this;
  112
+    self.each(refresh);
  113
+
  114
+    var $s = $t.settings;
  115
+    if ($s.refreshMillis > 0) {
  116
+      setInterval(function() { self.each(refresh); }, $s.refreshMillis);
  117
+    }
  118
+    return self;
  119
+  };
  120
+
  121
+  function refresh() {
  122
+    var data = prepareData(this);
  123
+    if (!isNaN(data.datetime)) {
  124
+      $(this).text(inWords(data.datetime));
  125
+    }
  126
+    return this;
  127
+  }
  128
+
  129
+  function prepareData(element) {
  130
+    element = $(element);
  131
+    if (!element.data("timeago")) {
  132
+      element.data("timeago", { datetime: $t.datetime(element) });
  133
+      var text = $.trim(element.text());
  134
+      if (text.length > 0 && !($t.isTime(element) && element.attr("title"))) {
  135
+        element.attr("title", text);
  136
+      }
  137
+    }
  138
+    return element.data("timeago");
  139
+  }
  140
+
  141
+  function inWords(date) {
  142
+    return $t.inWords(distance(date));
  143
+  }
  144
+
  145
+  function distance(date) {
  146
+    return (new Date().getTime() - date.getTime());
  147
+  }
  148
+
  149
+  // fix for IE6 suckage
  150
+  document.createElement("abbr");
  151
+  document.createElement("time");
  152
+}(jQuery));

0 notes on commit 9b1f029

Please sign in to comment.
Something went wrong with that request. Please try again.