fancy_date_time 0.0.1.alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +18 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +111 -0
- data/Rakefile +2 -0
- data/app/config/locales/en.yml +14 -0
- data/fancy_date_time.gemspec +20 -0
- data/lib/fancy_date_time.rb +5 -0
- data/lib/fancy_date_time/railtie.rb +16 -0
- data/lib/fancy_date_time/version.rb +3 -0
- data/lib/fancy_date_time/view_helpers.rb +35 -0
- data/spec/fancy_date_spec.rb +51 -0
- data/spec/locales/en.yml +17 -0
- data/spec/locales/ru.yml +4 -0
- data/spec/spec_helper.rb +7 -0
- metadata +88 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Alexander Zaytsev
|
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,111 @@
|
|
1
|
+
# fancy_date_time
|
2
|
+
|
3
|
+
`fancy_date_time` provides your Rails app with view helpers for better time/date formatting.
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
time = Time.parse '2012-08-28 17:20:54'
|
7
|
+
|
8
|
+
fancy_time(time) # 5:20 pm
|
9
|
+
fancy_time(time - 1.day) # Yesterday
|
10
|
+
fancy_time(time - 2.days) # Aug 26
|
11
|
+
fancy_time(time - 1.year) # Aug 28, 2011
|
12
|
+
```
|
13
|
+
|
14
|
+
`fancy_date_time` provides you with 4 "states" for your Time/Date objects: `today`, `yesterday`, `current_year` and `previous_years`.
|
15
|
+
|
16
|
+
The defaults are:
|
17
|
+
|
18
|
+
`today` displays time only: `5:20 pm`
|
19
|
+
|
20
|
+
`yesterday` displays: `Yesterday`
|
21
|
+
|
22
|
+
`current_year` displays the month and the day: `Aug 26`
|
23
|
+
|
24
|
+
`previous_years` displays the month, the day and the year: `Aug 28, 2011`
|
25
|
+
|
26
|
+
You can customize the formats in the locale file, see the Usage section below.
|
27
|
+
|
28
|
+
## Installation
|
29
|
+
|
30
|
+
Add this line to your application's Gemfile:
|
31
|
+
|
32
|
+
gem 'fancy_date_time'
|
33
|
+
|
34
|
+
And then execute:
|
35
|
+
|
36
|
+
$ bundle
|
37
|
+
|
38
|
+
Or install it yourself as:
|
39
|
+
|
40
|
+
$ gem install fancy_date_time
|
41
|
+
|
42
|
+
## Usage
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
time = Time.parse '2012-08-28 17:20:54'
|
46
|
+
|
47
|
+
fancy_time(time) # 5:20 pm
|
48
|
+
fancy_time(time - 1.day) # Yesterday
|
49
|
+
fancy_time(time - 2.days) # Aug 26
|
50
|
+
fancy_time(time - 1.year) # Aug 28, 2011
|
51
|
+
|
52
|
+
# it works with dates, too
|
53
|
+
date = Date.parse '2012-08-28'
|
54
|
+
|
55
|
+
fancy_date(date) # Today
|
56
|
+
fancy_date(date - 1.year) # Aug 28, 2011
|
57
|
+
```
|
58
|
+
|
59
|
+
### Customizing format
|
60
|
+
|
61
|
+
You can customize the format by changing the locale file. The defaults are:
|
62
|
+
```ruby
|
63
|
+
# config/locales/en.yml
|
64
|
+
en:
|
65
|
+
date:
|
66
|
+
formats:
|
67
|
+
today: "Today"
|
68
|
+
yesterday: "Yesterday"
|
69
|
+
current_year: "%h %e"
|
70
|
+
previous_years: "%h %e, %Y"
|
71
|
+
|
72
|
+
time:
|
73
|
+
formats:
|
74
|
+
today: "%l:%M %p"
|
75
|
+
yesterday: "Yesterday"
|
76
|
+
current_year: "%h %e"
|
77
|
+
previous_years: "%h %e, %Y"
|
78
|
+
|
79
|
+
```
|
80
|
+
|
81
|
+
### Namespaces
|
82
|
+
|
83
|
+
If you have several models and want each of them to have a different format (for example, for a `Post` you want to display 'Today' if it was posted today, but for a `Comment` you want to display something like '5:20 pm'), you can use namespaces.
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
# config/locales/en.yml
|
87
|
+
en:
|
88
|
+
posts:
|
89
|
+
time:
|
90
|
+
formats:
|
91
|
+
today: "Today"
|
92
|
+
|
93
|
+
comments:
|
94
|
+
time:
|
95
|
+
formats:
|
96
|
+
today: "%l:%M %p"
|
97
|
+
```
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
time = Time.parse '2012-08-28 17:20:54'
|
101
|
+
|
102
|
+
fancy_time(time, :posts) # Today
|
103
|
+
fancy_time(time, :comments) # 5:20 pm
|
104
|
+
```
|
105
|
+
## Contributing
|
106
|
+
|
107
|
+
1. Fork it
|
108
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
109
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
110
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
111
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/fancy_date_time/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Alexander Zaytsev"]
|
6
|
+
gem.email = ["alexander.f.zaitsev@gmail.com"]
|
7
|
+
gem.description = %q{View helper to make your Dates and Times fancy}
|
8
|
+
gem.summary = %q{Display dates and times for humans, not robots!}
|
9
|
+
gem.homepage = "http://github.com/AlexanderZaytsev/fancy_date_time"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "fancy_date_time"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = FancyDateTime::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency "activesupport"
|
19
|
+
gem.add_development_dependency "rspec"
|
20
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'fancy_date_time/view_helpers'
|
2
|
+
|
3
|
+
|
4
|
+
module FancyDateTime
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
|
7
|
+
initializer 'rails-i18n' do |app|
|
8
|
+
I18n.load_path << Dir[File.join(File.expand_path(File.dirname(__FILE__) + '/../../app/config/locales'), '*.yml')]
|
9
|
+
I18n.load_path.flatten!
|
10
|
+
end
|
11
|
+
|
12
|
+
initializer 'fancy_date_time.view_helpers' do
|
13
|
+
ActionView::Base.send :include, ViewHelpers
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module FancyDateTime
|
2
|
+
module ViewHelpers
|
3
|
+
|
4
|
+
def fancy_date(object, namespace = nil)
|
5
|
+
format_name = format_name_for_object(object)
|
6
|
+
|
7
|
+
format = namespace ? format_for_namespace(namespace, object, format_name) : format_name
|
8
|
+
|
9
|
+
I18n.l(object, format: format).strip
|
10
|
+
end
|
11
|
+
|
12
|
+
alias_method :fancy_time, :fancy_date
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def format_for_namespace(namespace, object, format_name)
|
17
|
+
object_type = object.respond_to?(:sec) ? 'time' : 'date'
|
18
|
+
I18n.t("#{namespace}.#{object_type}.formats.#{format_name}")
|
19
|
+
end
|
20
|
+
|
21
|
+
def format_name_for_object(object)
|
22
|
+
date = object.to_date
|
23
|
+
if date == Date.today
|
24
|
+
format = :today
|
25
|
+
elsif date == Date.yesterday
|
26
|
+
format = :yesterday
|
27
|
+
elsif date.year == Date.today.year
|
28
|
+
format = :current_year
|
29
|
+
elsif date.year != Date.today.year
|
30
|
+
format = :previous_years
|
31
|
+
end
|
32
|
+
format
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
3
|
+
|
4
|
+
include FancyDateTime::ViewHelpers
|
5
|
+
|
6
|
+
describe FancyDateTime::ViewHelpers do
|
7
|
+
|
8
|
+
before do
|
9
|
+
@now = Time.parse '2012-08-28 17:20:54'
|
10
|
+
@today = Date.parse '2012-08-28'
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#fancy_date' do
|
14
|
+
it 'displays date in the correct format' do
|
15
|
+
fancy_date(@today).should == "Today"
|
16
|
+
fancy_date(@today - 2.days).should == "Aug 26"
|
17
|
+
fancy_date(@today - 1.year).should == "Aug 28, 2011"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '#fancy_time' do
|
22
|
+
it 'displays time in the correct format' do
|
23
|
+
fancy_time(@now).should == "5:20 PM"
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'works with namespaces' do
|
27
|
+
fancy_time(@now, :comments).should == "17:20"
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'works with different locales' do
|
31
|
+
I18n.locale = :ru
|
32
|
+
fancy_time(@now).should == "17:20:54"
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
describe '#format_name_for_object' do
|
38
|
+
it 'returns the valid format for Time' do
|
39
|
+
format_name_for_object(@now).should == :today
|
40
|
+
format_name_for_object(@now - 1.day).should == :yesterday
|
41
|
+
format_name_for_object(@now - 2.days).should == :current_year
|
42
|
+
format_name_for_object(@now - 1.year).should == :previous_years
|
43
|
+
end
|
44
|
+
it 'returns the valid format for Date' do
|
45
|
+
format_name_for_object(@today).should == :today
|
46
|
+
format_name_for_object(@today - 1.day).should == :yesterday
|
47
|
+
format_name_for_object(@today - 2.days).should == :current_year
|
48
|
+
format_name_for_object(@today - 1.year).should == :previous_years
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/spec/locales/en.yml
ADDED
data/spec/locales/ru.yml
ADDED
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fancy_date_time
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1.alpha
|
5
|
+
prerelease: 6
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Alexander Zaytsev
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: &2160660940 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2160660940
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &2160657720 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2160657720
|
36
|
+
description: View helper to make your Dates and Times fancy
|
37
|
+
email:
|
38
|
+
- alexander.f.zaitsev@gmail.com
|
39
|
+
executables: []
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files: []
|
42
|
+
files:
|
43
|
+
- .gitignore
|
44
|
+
- .rspec
|
45
|
+
- Gemfile
|
46
|
+
- LICENSE
|
47
|
+
- README.md
|
48
|
+
- Rakefile
|
49
|
+
- app/config/locales/en.yml
|
50
|
+
- fancy_date_time.gemspec
|
51
|
+
- lib/.DS_Store
|
52
|
+
- lib/fancy_date_time.rb
|
53
|
+
- lib/fancy_date_time/railtie.rb
|
54
|
+
- lib/fancy_date_time/version.rb
|
55
|
+
- lib/fancy_date_time/view_helpers.rb
|
56
|
+
- spec/fancy_date_spec.rb
|
57
|
+
- spec/locales/en.yml
|
58
|
+
- spec/locales/ru.yml
|
59
|
+
- spec/spec_helper.rb
|
60
|
+
homepage: http://github.com/AlexanderZaytsev/fancy_date_time
|
61
|
+
licenses: []
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ! '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>'
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 1.3.1
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 1.8.15
|
81
|
+
signing_key:
|
82
|
+
specification_version: 3
|
83
|
+
summary: Display dates and times for humans, not robots!
|
84
|
+
test_files:
|
85
|
+
- spec/fancy_date_spec.rb
|
86
|
+
- spec/locales/en.yml
|
87
|
+
- spec/locales/ru.yml
|
88
|
+
- spec/spec_helper.rb
|