File: README.md

package info (click to toggle)
ruby-rails-timeago 2.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 524 kB
  • sloc: javascript: 2,394; ruby: 376; makefile: 10
file content (128 lines) | stat: -rw-r--r-- 3,974 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# rails-timeago

[![Gem Version](https://img.shields.io/gem/v/rails-timeago?logo=ruby)](https://rubygems.org/gems/rails-timeago)
[![Build Status](https://img.shields.io/travis/jgraichen/rails-timeago/master?logo=travis)](https://travis-ci.org/jgraichen/rails-timeago)

**rails-timeago** provides a timeago_tag helper to create time tags usable for
[jQuery Timeago](https://github.com/rmm5t/jquery-timeago) plugin.

## Installation

Add this line to your application's `Gemfile`:

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

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install rails-timeago

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:

```erb
<%= 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

```ruby
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:

```erb
<%= 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`:

```erb
<%= timeago_script_tag %>
```

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:

```js
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:

```js
//= 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](https://github.com/rmm5t/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:

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

## License

[MIT License](http://www.opensource.org/licenses/mit-license.php)

Copyright (c) 2014, Jan Graichen