• Stars
    star
    212
  • Rank 186,122 (Top 4 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 13 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Rails helper for time tags that can be used with the jQuery Timeago plugin.

rails-timeago

Gem Version Build Status

rails-timeago provides a timeago_tag helper to create time tags usable for jQuery Timeago plugin.

Installation

Add this line to your Gemfile:

gem 'rails-timeago', '~> 2.0'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rails-timeago

Use bundled JavaScript with Sprockets

Note: The bundled JavaScript can only be used with sprockets. Rails-webpacker cannot load scripts bundled with the gem. If you use rails-webpacker you need to install, load, and setup jquery-timeago on your own.

To use bundled jQuery Timeago plugin add this require statement to your application.js file:

//= require rails-timeago

This will also convert all matching time tags on page load.

Use the following to also include all available locale files:

//= require rails-timeago-all

If using a recent Rails with rails-ujs, jQuery might not be present anymore. You need to add jquery-rails to your Gemfile and load it, e.g.:

//= require jquery
//= require rails-timeago
//= require rails-timeago-all

Usage

Use the timeago_tag helper like any other regular tag helper:

<%= timeago_tag Time.zone.now limit: 10.days.ago %>

Available options:

date_only Only print date as tag content instead of full time. (default: true)

format A time format for localize method used to format static time. (default: default)

limit Set a limit for time ago tags. All dates before given limit will not be converted. (default: 4.days.ago)

force Force time ago tag ignoring limit option. (default: false)

default String that will be returned if time is nil. (default: '-')

title A string or block that will be used to create a title attribute for timeago tags. It set to nil or false no title attribute will be set. (default: proc { |time, options| I18n.l time, format: options[:format] })

All other options will be given as options to the time tag helper. The above options can be assigned globally as defaults using

Rails::Timeago.default_options limit: proc { 20.days.ago }

A global limit should always be given as a block that will be evaluated each time the rails timeago_tag helper is called. That avoids the limit becoming smaller the longer the application runs.

I18n

rails-timeago 2 ships with a modified version of jQuery timeago that allows to include all locale files at once and set the locale via an option or per element via the lang attribute:

<%= timeago_tag Time.zone.now, lang: :de %>

The following snippet will print a script tag that set the jQuery timeago locale according to your I18n.locale:

<%= timeago_script_tag %>

Arguments are passed to Rails' javascript_tag helper, e.g. to assign a CSP nonce: timeago_script_tag(nonce: true).

Just insert it in your application layout's html head. If you use another I18n framework for JavaScript you can also directly set jQuery.timeago.settings.lang. For example:

jQuery.timeago.settings.lang = $('html').attr('lang')

Do not forget to require the needed locale files by either require rails-timeago-all in your application.js file or require specific locale files:

//= require locales/jquery.timeago.de.js
//= require locales/jquery.timeago.ru.js

Note: English is included in jQuery timeago library, but can be easily override by include an own file that defines jQuery.timeago.settings.strings["en"]. See a locale file for more details.

rails-timeago includes locale files for the following locales taken from jQuery Timeago.

Your customized jQuery locale files must be changed to work with rails-timeago 2. Instead of defining your locale strings as jQuery.timeago.settings.strings you need to define them like this:

jQuery.timeago.settings.strings["en"] = {
    ...
}

License

MIT License

Copyright (c) 2014, Jan Graichen

More Repositories

1

redmine_dashboard

This redmine plugin adds an issue dashboard that supports drag and drop for issues and support various filters and groups.
Ruby
421
star
2

salt-tower

A salt external pillar for replacing salt pillars
Python
23
star
3

msgr

A Rails-like Messaging Framework.
Ruby
22
star
4

paginate-responder

A Rails pagination responder with link header support.
Ruby
21
star
5

sprockets-standalone

Rack task library for using Sprockets standalone.
Ruby
14
star
6

telegraf-ruby

Send events from Ruby to a Telegraf agent
Ruby
13
star
7

omniauth-kerberos

OmniAuth strategy for kerberos authentication.
Ruby
13
star
8

acfs

API client for services
Ruby
13
star
9

debian-isso

lightweight web-based commenting system
10
star
10

restify

Restify is a experimental, parallel and pipelined Hypermedia API client
Ruby
8
star
11

rails-rfc6570

Pragmatical access to your Rails routes as RFC6570 URI templates.
Ruby
7
star
12

rack-link_headers

Easy Link header management for rack responses.
Ruby
6
star
13

gurke

Gurke is an experimental, alternative cucumber runner.
Ruby
6
star
14

geminabox-rake

Geminabox rake task for building and releasing gems.
Ruby
6
star
15

toque

Toque - The Cap of a Chef. Combines the power of Capistrano and Chef Solo.
Ruby
5
star
16

rake-release

Customized gem release rake task set
Ruby
4
star
17

uuid4

UUID support library.
Ruby
3
star
18

os-image-updater

Keep cloud images for OpenStack up-to-date
Go
3
star
19

rubypath

Path library incorporating File, Dir, Pathname, IO methods as well as a virtual mock filesystem.
Ruby
3
star
20

molecule

Collection of React/SASS UI molecules to assemble higher level components.
CoffeeScript
2
star
21

systemd-failure-notify

Shell
2
star
22

docker-plugin-cinder

A Docker volume plugin to use OpenStack Cinder as container volumes
Go
2
star
23

decorate-responder

A Rails responder to decorate resources.
Ruby
2
star
24

loco45

1
star
25

mnemosyne-web

An in-house monitoring platform (experimental)
Python
1
star
26

ajutil

A Java Utility Library
Java
1
star
27

thunderbird

Custom Thunderbird user chrome stylesheets
CSS
1
star
28

stic

Opinionated extensible static site generator.
Ruby
1
star
29

attribute_ext

AttributeExt provides additional access control for rails model attributes.
Ruby
1
star
30

rails-assets-manifest

Load all assets from a manifest (e.g. from webpack) including SRI support
Ruby
1
star
31

shields

Yes it's called shield now.
1
star
32

omniauth-internal

OmniAuth strategy for simple model authentication.
Ruby
1
star
33

salt-template

Shared salt templates and functions to serialize data and text
Python
1
star
34

multi_process

Some experiments to handle multiple processes running parallel and their output.
Ruby
1
star
35

debian-blobmoji

Minimal debian package for blobmoji font
1
star
36

vmbuilder

Fork of https://launchpad.net/vmbuilder & https://code.launchpad.net/~malte.swart/vmbuilder/vmbuilder with more debianess
Python
1
star
37

proc-value

Take Smalltalks `#value` to Ruby. Dead easy transparent block usage.
Ruby
1
star
38

JSweeper

A small feature-less Minesweeper written in Java.
Java
1
star
39

munin-rubygems

Munin plugin to monitor rubygems downloads for gems and/or profiles.
Ruby
1
star
40

rack-remote

Small request intercepting rack middleware to invoke remote calls over HTTP.
Ruby
1
star
41

salt-pki

Salt toolkit to manage X.509 PKI keys and certificates and customizable remote signing.
Python
1
star
42

JSnipper

JSnipper is a small screen snipping tool to capture and annotate parts of your screen.
Java
1
star
43

rstime

A small utility to export one or multiple GNOME hamster applet databases into tsv format.
Rust
1
star
44

opt

An option parsing library. Optional.
Ruby
1
star
45

salt-acme

Python
1
star