creative_rails_utilities 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 16788059b4280282619b0f310bb6da722d07f820
4
- data.tar.gz: 6e22528cd792c29dd49f5999e4ea2cc20af0b3eb
3
+ metadata.gz: c11e2d81b41c9250fa934fe3351ff2b91f27d1e1
4
+ data.tar.gz: 10374464c1ed042a4ac75fb91c8cceb3f1d5082f
5
5
  SHA512:
6
- metadata.gz: 38370d59cbabb4955f92864fb297097ac8648a0668b929a59f48dd0886b5c9b13b5fed9a7fbd5a57be784701f29ae63df82811c87476bc0a54ccea4b8c9b6c9c
7
- data.tar.gz: e4a5e8ef2d4f928f8679c277b9390744c0b8325b2542f2309e411b0afb298cd3745f3c1855a8cfd06993ce2cf9e7660378de7de1faf173ac59b94ddb5074b901
6
+ metadata.gz: 0290b96140235311179d75a50df9b9b21691c37c344ec5f70a320ff6257526756696a3a15f1b5dc20794e15e33856cebcad2f03231840440a78a6989d9f2bbb9
7
+ data.tar.gz: 760f094be9e34f0aae9a76bf811a4f077c7dc81b76132df2279673bd4bc9d08b62a7ff2232dec252a9cfdfb6a9b5118980b8f8fd7ef5709b409ee3b63851fb35
data/.rspec CHANGED
@@ -1,2 +1,3 @@
1
1
  --format documentation
2
+ --require spec_helper
2
3
  --color
data/CHANGELOG.md CHANGED
@@ -2,11 +2,16 @@
2
2
  All notable changes to this project will be documented in this file.
3
3
  This project adheres to [Semantic Versioning](http://semver.org/).
4
4
 
5
- ## [Release candidate][0.2.3-alpha.1]
5
+ ## [Release candidate][0.3.1-alpha.1]
6
6
  ### Added
7
7
  -
8
8
 
9
- ## [Release candidate][0.2.2]
9
+ ## [0.3.0] - 2015-12-10
10
+ ### Added
11
+ - ViewHelper module for Rails
12
+ - helper#relative_time_parse(earlier_time, later_time=optional)
13
+
14
+ ## [0.2.3] - 2015-11-20
10
15
  ### Added
11
16
  - Added Integer#safe_part
12
17
  - Documented Date#array_with_pre_churn_limit(limit_day_count)
data/README.md CHANGED
@@ -10,7 +10,68 @@ Intended for Rails > 4.0.0, but will probably also work for older projects with
10
10
 
11
11
  ## Usage
12
12
 
13
- Feel free to read the source under `/lib/creative_rails_utilities/`
13
+ Feel free to read the source under `/lib/creative_rails_utilities/` and peruse the method "it" lines under `/spec`
14
+
15
+ #### View Helpers
16
+ Rails-only (via railtie) view helpers have been added, they are automagically loaded and usable upon gem inclusion.
17
+
18
+ ##### relative_time_parse(earlier_time, later_time=optional)
19
+ turns a timestamp into "time ago" format.
20
+ Examples:
21
+
22
+ ```ruby
23
+ # If used with only one argument, will default the second argument to Time.now
24
+ # at "2015-01-15 12:00".to_datetime
25
+
26
+ relative_time_parse("2015-01-15 12:00".to_datetime)
27
+ #=> {key: "now", count: nil}
28
+ relative_time_parse("2015-01-15 11:59:59".to_datetime)
29
+ #=> {key: "second", count: nil}
30
+ relative_time_parse("2015-01-15 11:59:58".to_datetime)
31
+ #=> {key: "seconds", count: "1"}
32
+ relative_time_parse("2015-01-15 11:59:00".to_datetime)
33
+ #=> {key: "minute", count: nil}
34
+ relative_time_parse("2015-01-15 11:58:00".to_datetime)
35
+ #=> {key: "minutes", count: "2"}
36
+ relative_time_parse("2015-01-15 11:00".to_datetime)
37
+ #=> {key: "hour", count: nil}
38
+ relative_time_parse("2015-01-15 09:00".to_datetime)
39
+ #=> {key: "hours", count: "3"}
40
+ relative_time_parse("2015-01-14 11:00".to_datetime)
41
+ #=> {key: "day", count: nil}
42
+ relative_time_parse("2015-01-10 09:00".to_datetime)
43
+ #=> {key: "days", count: 5}
44
+
45
+ # if used with both arguments, expects the second to be later than the first and will calculate relative time between them
46
+ relative_time_parse("2015-01-01 12:00".to_datetime, "2015-01-01 12:02".to_datetime)
47
+ #=> {key: "minutes", count: "2"}
48
+ ```
49
+
50
+ The keys are intended to be used together with `I18n.t`
51
+ Sample yaml for English
52
+
53
+ ```yml
54
+ en:
55
+ time:
56
+ now: "just now"
57
+ second: "a second ago"
58
+ seconds: "%{count} seconds ago"
59
+ minute: "a minute ago"
60
+ minutes: "%{count} minutes ago"
61
+ hour: "an hour ago"
62
+ hours: "%{count} hours ago"
63
+ day: "a day ago"
64
+ days: "%{count} days ago"
65
+ ```
66
+
67
+ Then you can write a simple helper that specifies localication key location and returns the correct value based on the hash returned by `relative_time_parse`
68
+ ```ruby
69
+ def relative_short_time(t1, t2=nil)
70
+ hash = relative_time_parse(t1, t2)
71
+ count_hash = hash.count.present? ? hash.except(:key): {}
72
+ return I18n.t("time.#{hash[:key]}", count_hash)
73
+ end
74
+ ```
14
75
 
15
76
  ##### Date
16
77
 
@@ -10,5 +10,7 @@ require "creative_rails_utilities/numeric"
10
10
  require "creative_rails_utilities/string"
11
11
  require "creative_rails_utilities/version"
12
12
 
13
+ require 'creative_rails_utilities/railtie' if defined?(Rails)
14
+
13
15
  module CreativeRailsUtilities
14
16
  end
@@ -0,0 +1,10 @@
1
+ require 'creative_rails_utilities/view_helpers'
2
+
3
+ module CreativeRailsUtilities
4
+ class Railtie < Rails::Railtie
5
+ initializer "creative_rails_utilities.view_helpers" do
6
+ ActiveSupport.on_load( :action_view ){ include CreativeRailsUtilities::ViewHelpers }
7
+ # ActionView::Base.send :include, ViewHelpers
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module CreativeRailsUtilities
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -0,0 +1,35 @@
1
+ module CreativeRailsUtilities
2
+ module ViewHelpers
3
+
4
+ def relative_time_parse(earlier_time, later_time=nil)
5
+ later_time ||= Time.now
6
+ difference = (later_time.to_i - earlier_time.to_i).to_i
7
+
8
+ case difference
9
+ when 0
10
+ return {key: "now", count: nil}
11
+ when 1
12
+ return {key: "second", count: nil}
13
+ when 2..59
14
+ return {key: "seconds", count: difference.to_s}
15
+ when 60..119
16
+ return {key: "minute", count: nil}
17
+ when 120..3540
18
+ return {key: "minutes", count: (difference/60).to_i.to_s}
19
+ when 3541..7100
20
+ return {key: "hour", count: nil}
21
+ when 7101..82800
22
+ return {key: "hours", count: ((difference+99)/3600).to_i.to_s}
23
+ when 82801..172000
24
+ return {key: "day", count: nil}
25
+ else
26
+ if difference < 0
27
+ return {key: "now", count: nil}
28
+ else
29
+ return {key: "days", count: ((difference+800)/(60*60*24)).to_i.to_s}
30
+ end
31
+ end
32
+ end
33
+
34
+ end
35
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: creative_rails_utilities
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Creative
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-11-20 00:00:00.000000000 Z
11
+ date: 2015-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -160,8 +160,10 @@ files:
160
160
  - lib/creative_rails_utilities/error_helper.rb
161
161
  - lib/creative_rails_utilities/hash.rb
162
162
  - lib/creative_rails_utilities/numeric.rb
163
+ - lib/creative_rails_utilities/railtie.rb
163
164
  - lib/creative_rails_utilities/string.rb
164
165
  - lib/creative_rails_utilities/version.rb
166
+ - lib/creative_rails_utilities/view_helpers.rb
165
167
  homepage: https://github.com/CreativePublisher/rails_utilities
166
168
  licenses:
167
169
  - MIT