Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for using dotw with Ruby w/o Rails #105

Merged
merged 1 commit into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ jobs:
fail-fast: false
matrix:
include:
- ruby-version: 2.4.x
- ruby-version: 2.5.x
- ruby-version: 2.6.x
- ruby-version: 2.4.x
Expand Down
25 changes: 19 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
## 5.0.0 (Next)

* [#105](https://github.com/radar/distance_of_time_in_words/pull/105): Support for Ruby w/o Rails - [@dblock](https://github.com/dblock).

## 4.0.1 (2018/06/01)

## 3.1.1 (2016/03/08)
* Add CHANGELOG.md

## 3.1.0 (2016/03/07)

* [#68](https://github.com/radar/dotiw/pull/68): Add support for weeks - [@lauranjansen](https://github.com/lauranjansen)
* [#69](https://github.com/radar/dotiw/pull/69): Add Indonesian language support - [@avidmaulanas](https://github.com/avidmaulanas)
* [#70](https://github.com/radar/dotiw/pull/70): Add French language support - [@geo1004](https://github.com/geo1004)
* [#72](https://github.com/radar/dotiw/pull/72): Add Danish language support - [@kaspernj](https://github.com/kaspernj)
* [#71](https://github.com/radar/dotiw/pull/71): Update bundler and ruby versions for Travis CI - [@lauranjansen](https://github.com/lauranjansen)
* [#68](https://github.com/radar/dotiw/pull/68): Add support for weeks - [@lauranjansen](https://github.com/lauranjansen).
* [#69](https://github.com/radar/dotiw/pull/69): Add Indonesian language support - [@avidmaulanas](https://github.com/avidmaulanas).
* [#70](https://github.com/radar/dotiw/pull/70): Add French language support - [@geo1004](https://github.com/geo1004).
* [#72](https://github.com/radar/dotiw/pull/72): Add Danish language support - [@kaspernj](https://github.com/kaspernj).
* [#71](https://github.com/radar/dotiw/pull/71): Update bundler and ruby versions for Travis CI - [@lauranjansen](https://github.com/lauranjansen).

## 3.0.1 (2015/04/09)

## 3.0 (2015/04/09)

## 0.2.4 (2009/10/18)

* Initial public release - [@radar](https://github.com/radar).
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'http://rubygems.org'

gemspec
2 changes: 1 addition & 1 deletion MIT-LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2009 Ryan Bigg
Copyright (c) 2009-2020 Ryan Bigg

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
63 changes: 46 additions & 17 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -1,6 +1,40 @@
# dotiw ![Ruby](https://github.com/radar/distance_of_time_in_words/workflows/Ruby/badge.svg)

dotiw is a gem for Rails that overrides the default `distance_of_time_in_words` and provides a more accurate output. Do you crave accuracy down to the second? So do I. That's why I made this gem. Take this for a totally kick-ass example:
The `dotiw` library that adds `distance_of_time_in_words` to any Ruby project, or overrides the default implementation in Rails with more accurate output.

Do you crave accuracy down to the second? So do I. That's why I made this gem.

## Install

Add to your `Gemfile`.

```ruby
gem 'dotiw'
```

Run `bundle install`.

### Pure Ruby

```ruby
require 'dotiw'

include DOTIW::Methods
```

### Rails

```ruby
require 'dotiw'

include ActionView::Helpers::DateHelper
include ActionView::Helpers::TextHelper
include ActionView::Helpers::NumberHelper
```

## distance\_of\_time\_in\_words

Take this for a totally kick-ass example:

```ruby
>> distance_of_time_in_words(Time.now, Time.now + 1.year + 2.months + 3.weeks + 4.days + 5.hours + 6.minutes + 7.seconds, true)
Expand Down Expand Up @@ -45,16 +79,6 @@ world*:

Oh, and did I mention it supports I18n? Oh yeah. Rock on!

## Install

Add to your `Gemfile`.

```ruby
gem 'dotiw'
```

Run `bundle install`.

### Options

#### :locale
Expand Down Expand Up @@ -142,7 +166,7 @@ When you want variable precision from `DOTIW`:

#### :words_connector

**This is an option for `to_sentence`, defaults to ', '**
This is an option for `to_sentence`, defaults to ', '.

Using something other than a comma:

Expand All @@ -153,7 +177,7 @@ Using something other than a comma:

#### :two\_words\_connector

**This is an option for `to_sentence`, defaults to ' and '**
This is an option for `to_sentence`, defaults to ' and '.

Using something other than 'and':

Expand All @@ -164,7 +188,7 @@ Using something other than 'and':

#### :last\_word\_connector

**This is an option for `to_sentence`, defaults to ', and '**
This is an option for `to_sentence`, defaults to ', and '.

Using something other than ', and':

Expand Down Expand Up @@ -195,16 +219,20 @@ Indifferent means that you can access all keys by their `String` or `Symbol` ver

## distance\_of\_time\_in\_percent

If you want to calculate a distance of time in percent, use `distance_of_time_in_percent`. The first argument is the beginning time, the second argument the "current" time and the third argument is the end time. This method takes the same options as [`number_with_precision`](http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#method-i-number_with_precision).
This method is only available with Rails ActionView.

If you want to calculate a distance of time in percent, use `distance_of_time_in_percent`. The first argument is the beginning time, the second argument the "current" time and the third argument is the end time.

```ruby
>> distance_of_time_in_percent("04-12-2009".to_time, "29-01-2010".to_time, "04-12-2010".to_time)
"15%"
=> '15%'
```

This method takes the same options as [`number_with_precision`](http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#method-i-number_with_precision).

```ruby
>> distance_of_time_in_percent("04-12-2009".to_time, "29-01-2010".to_time, "04-12-2010".to_time, precision: 1)
=> "15.3%"
=> '15.3%'
```

## Contributors
Expand All @@ -213,3 +241,4 @@ If you want to calculate a distance of time in percent, use `distance_of_time_in
* [Derander](http://github.com/derander) - correct Spanish translations
* [DBA](http://github.com/dba) - commits leading up to the 0.7 release
* [Sija](http://github.com/Sija) - rails 4 support, v2.0 release
* [dblock](http://github.com/dblock) - Ruby w/o Rails support
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'rubygems'
require 'rake'
require 'bundler'
Expand Down
6 changes: 4 additions & 2 deletions dotiw.gemspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
$LOAD_PATH.push File.expand_path('../lib', __FILE__)
# frozen_string_literal: true

$LOAD_PATH.push File.expand_path('lib', __dir__)
require 'dotiw/version'

Gem::Specification.new do |s|
Expand All @@ -16,8 +18,8 @@ Gem::Specification.new do |s|
s.email = ['[email protected]', '[email protected]']
s.homepage = 'https://github.com/radar/distance_of_time_in_words'

s.add_dependency 'actionpack', '>= 4'
s.add_dependency 'i18n'
s.add_dependency 'activesupport'

s.add_development_dependency 'appraisal'
s.add_development_dependency 'bundler'
Expand Down
30 changes: 21 additions & 9 deletions lib/dotiw.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
# frozen_string_literal: true

require 'i18n'

# Rails hacks
if defined?(ActionView::Helpers)
require 'dotiw/action_view_ext/helpers/date_helper'
end
require 'active_support'
require 'active_support/core_ext'

module DOTIW
extend self
extend ActiveSupport::Autoload

eager_autoload do
autoload :VERSION, 'dotiw/version'
autoload :TimeHash, 'dotiw/time_hash'
autoload :Methods, 'dotiw/methods'
end

autoload :VERSION, 'dotiw/version'
autoload :TimeHash, 'dotiw/time_hash'
extend self

DEFAULT_I18N_SCOPE = :'datetime.dotiw'

def init_i18n
def init_i18n!
I18n.load_path.unshift(*locale_files)
I18n.reload!
end
Expand All @@ -26,4 +31,11 @@ def locale_files
end
end # DOTIW

DOTIW.init_i18n
DOTIW.init_i18n!

begin
require 'action_view'
require_relative 'dotiw/action_view/helpers/date_helper'
rescue LoadError
# TODO: don't rely on exception
end
24 changes: 24 additions & 0 deletions lib/dotiw/action_view/helpers/date_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module ActionView
module Helpers
module DateHelper
alias_method :_distance_of_time_in_words, :distance_of_time_in_words
alias_method :_time_ago_in_words, :time_ago_in_words

include DOTIW::Methods

def distance_of_time_in_words(from_time, to_time = 0, include_seconds_or_options = {}, options = {})
return _distance_of_time_in_words(from_time, to_time, options) if options.delete(:vague)
super
end

def distance_of_time_in_percent(from_time, current_time, to_time, options = {})
options[:precision] ||= 0
distance = to_time - from_time
result = ((current_time - from_time) / distance) * 100
number_with_precision(result, options).to_s + '%'
end
end # DateHelper
end # Helpers
end # ActionView
102 changes: 0 additions & 102 deletions lib/dotiw/action_view_ext/helpers/date_helper.rb

This file was deleted.

Loading