diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index 9be4ec7..3bf4dd7 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -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 diff --git a/CHANGELOG.md b/CHANGELOG.md index 008810c..6da08e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). diff --git a/Gemfile b/Gemfile index d65e2a6..f7200f1 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + source 'http://rubygems.org' gemspec diff --git a/MIT-LICENSE b/MIT-LICENSE index 42e0df4..0eb048b 100644 --- a/MIT-LICENSE +++ b/MIT-LICENSE @@ -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 diff --git a/README.markdown b/README.markdown index e183299..3d31eb2 100644 --- a/README.markdown +++ b/README.markdown @@ -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) @@ -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 @@ -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: @@ -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': @@ -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': @@ -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 @@ -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 diff --git a/Rakefile b/Rakefile index 9bbf755..652dee7 100644 --- a/Rakefile +++ b/Rakefile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'rubygems' require 'rake' require 'bundler' diff --git a/dotiw.gemspec b/dotiw.gemspec index 75361a5..33a6cf9 100644 --- a/dotiw.gemspec +++ b/dotiw.gemspec @@ -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| @@ -16,8 +18,8 @@ Gem::Specification.new do |s| s.email = ['radarlistener@gmail.com', 'github@lauranjansen.com'] 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' diff --git a/lib/dotiw.rb b/lib/dotiw.rb index fb58a75..cc1bf50 100755 --- a/lib/dotiw.rb +++ b/lib/dotiw.rb @@ -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 @@ -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 diff --git a/lib/dotiw/action_view/helpers/date_helper.rb b/lib/dotiw/action_view/helpers/date_helper.rb new file mode 100644 index 0000000..b704317 --- /dev/null +++ b/lib/dotiw/action_view/helpers/date_helper.rb @@ -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 diff --git a/lib/dotiw/action_view_ext/helpers/date_helper.rb b/lib/dotiw/action_view_ext/helpers/date_helper.rb deleted file mode 100644 index ce5a932..0000000 --- a/lib/dotiw/action_view_ext/helpers/date_helper.rb +++ /dev/null @@ -1,102 +0,0 @@ -module ActionView - module Helpers - module DateHelper - alias_method :old_distance_of_time_in_words, :distance_of_time_in_words - - def distance_of_time_in_words_hash(from_time, to_time, options = {}) - from_time = from_time.to_time if !from_time.is_a?(Time) && from_time.respond_to?(:to_time) - to_time = to_time.to_time if !to_time.is_a?(Time) && to_time.respond_to?(:to_time) - - DOTIW::TimeHash.new(nil, from_time, to_time, options).to_hash - end - - def distance_of_time(seconds, options = {}) - options[:include_seconds] ||= true - display_time_in_words DOTIW::TimeHash.new(seconds, nil, nil, options).to_hash, options - end - - def distance_of_time_in_words(from_time, to_time = 0, include_seconds_or_options = {}, options = {}) - if include_seconds_or_options.is_a?(Hash) - options = include_seconds_or_options - else - options[:include_seconds] ||= !!include_seconds_or_options - end - return distance_of_time(from_time, options) if to_time == 0 - return old_distance_of_time_in_words(from_time, to_time, options) if options.delete(:vague) - hash = distance_of_time_in_words_hash(from_time, to_time, options) - display_time_in_words(hash, options) - 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 - - alias_method :old_time_ago_in_words, :time_ago_in_words - - def time_ago_in_words(from_time, include_seconds_or_options = {}) - distance_of_time_in_words(from_time, Time.current, include_seconds_or_options) - end - - private - - def display_time_in_words(hash, options = {}) - options.reverse_merge!( - include_seconds: false - ).symbolize_keys! - - include_seconds = options.delete(:include_seconds) - hash.delete(:seconds) if !include_seconds && hash[:minutes] - - options[:except] = Array.wrap(options[:except]).map!(&:to_sym) if options[:except] - options[:only] = Array.wrap(options[:only]).map!(&:to_sym) if options[:only] - - # Remove all the values that are nil or excluded. Keep the required ones. - hash.delete_if do |key, value| - value.nil? || value.zero? || - (options[:except] && options[:except].include?(key)) || - (options[:only] && !options[:only].include?(key)) - end - - i18n_scope = options.delete(:scope) || DOTIW::DEFAULT_I18N_SCOPE - if hash.empty? - fractions = DOTIW::TimeHash::TIME_FRACTIONS - fractions &= options[:only] if options[:only] - fractions -= options[:except] if options[:except] - - I18n.with_options locale: options[:locale], scope: i18n_scope do |locale| - # e.g. try to format 'less than 1 days', fallback to '0 days' - return locale.translate :less_than_x, - distance: locale.translate(fractions.first, count: 1), - default: locale.translate(fractions.first, count: 0) - end - end - - output = [] - I18n.with_options locale: options[:locale], scope: i18n_scope do |locale| - output = hash.map { |key, value| locale.t(key, count: value) } - end - - options.delete(:except) - options.delete(:only) - highest_measures = options.delete(:highest_measures) - highest_measures = 1 if options.delete(:highest_measure_only) - output = output[0...highest_measures] if highest_measures - - options[:words_connector] ||= I18n.translate :'datetime.dotiw.words_connector', - default: :'support.array.words_connector', - locale: options[:locale] - options[:two_words_connector] ||= I18n.translate :'datetime.dotiw.two_words_connector', - default: :'support.array.two_words_connector', - locale: options[:locale] - options[:last_word_connector] ||= I18n.translate :'datetime.dotiw.last_word_connector', - default: :'support.array.last_word_connector', - locale: options[:locale] - - output.to_sentence(options) - end - end # DateHelper - end # Helpers -end # ActionView diff --git a/lib/dotiw/methods.rb b/lib/dotiw/methods.rb new file mode 100644 index 0000000..8c782ad --- /dev/null +++ b/lib/dotiw/methods.rb @@ -0,0 +1,91 @@ +# frozen_string_literal: true + +module DOTIW + module Methods + def distance_of_time_in_words_hash(from_time, to_time, options = {}) + from_time = from_time.to_time if !from_time.is_a?(Time) && from_time.respond_to?(:to_time) + to_time = to_time.to_time if !to_time.is_a?(Time) && to_time.respond_to?(:to_time) + + DOTIW::TimeHash.new(nil, from_time, to_time, options).to_hash + end + + def distance_of_time(seconds, options = {}) + options[:include_seconds] ||= true + _display_time_in_words DOTIW::TimeHash.new(seconds, nil, nil, options).to_hash, options + end + + def distance_of_time_in_words(from_time, to_time = 0, include_seconds_or_options = {}, options = {}) + if include_seconds_or_options.is_a?(Hash) + options = include_seconds_or_options + else + options[:include_seconds] ||= !!include_seconds_or_options + end + return distance_of_time(from_time, options) if to_time == 0 + + hash = distance_of_time_in_words_hash(from_time, to_time, options) + _display_time_in_words(hash, options) + end + + def time_ago_in_words(from_time, include_seconds_or_options = {}) + distance_of_time_in_words(from_time, Time.current, include_seconds_or_options) + end + + private + + def _display_time_in_words(hash, options = {}) + options.reverse_merge!( + include_seconds: false + ).symbolize_keys! + + include_seconds = options.delete(:include_seconds) + hash.delete(:seconds) if !include_seconds && hash[:minutes] + + options[:except] = Array.wrap(options[:except]).map!(&:to_sym) if options[:except] + options[:only] = Array.wrap(options[:only]).map!(&:to_sym) if options[:only] + + # Remove all the values that are nil or excluded. Keep the required ones. + hash.delete_if do |key, value| + value.nil? || value.zero? || + options[:except]&.include?(key) || + (options[:only] && !options[:only].include?(key)) + end + + i18n_scope = options.delete(:scope) || DOTIW::DEFAULT_I18N_SCOPE + if hash.empty? + fractions = DOTIW::TimeHash::TIME_FRACTIONS + fractions &= options[:only] if options[:only] + fractions -= options[:except] if options[:except] + + I18n.with_options locale: options[:locale], scope: i18n_scope do |locale| + # e.g. try to format 'less than 1 days', fallback to '0 days' + return locale.translate :less_than_x, + distance: locale.translate(fractions.first, count: 1), + default: locale.translate(fractions.first, count: 0) + end + end + + output = [] + I18n.with_options locale: options[:locale], scope: i18n_scope do |locale| + output = hash.map { |key, value| locale.t(key, count: value) } + end + + options.delete(:except) + options.delete(:only) + highest_measures = options.delete(:highest_measures) + highest_measures = 1 if options.delete(:highest_measure_only) + output = output[0...highest_measures] if highest_measures + + options[:words_connector] ||= I18n.translate :'datetime.dotiw.words_connector', + default: :'support.array.words_connector', + locale: options[:locale] + options[:two_words_connector] ||= I18n.translate :'datetime.dotiw.two_words_connector', + default: :'support.array.two_words_connector', + locale: options[:locale] + options[:last_word_connector] ||= I18n.translate :'datetime.dotiw.last_word_connector', + default: :'support.array.last_word_connector', + locale: options[:locale] + + output.to_sentence(options) + end + end +end # DOTIW diff --git a/lib/dotiw/time_hash.rb b/lib/dotiw/time_hash.rb index 939d540..eca46f6 100644 --- a/lib/dotiw/time_hash.rb +++ b/lib/dotiw/time_hash.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module DOTIW class TimeHash TIME_FRACTIONS = %i[seconds minutes hours days weeks months years].freeze @@ -5,7 +7,7 @@ class TimeHash attr_reader :distance, :smallest, :largest, :from_time, :to_time def initialize(distance, from_time, to_time = nil, options = {}) - @output = ActiveSupport::OrderedHash.new + @output = {} @options = options @distance = distance @from_time = from_time || Time.current @@ -36,6 +38,7 @@ def build_time_hash if accumulate_on = options.delete(:accumulate_on) accumulate_on = accumulate_on.to_sym return build_time_hash if accumulate_on == :years + TIME_FRACTIONS.index(accumulate_on).downto(0) { |i| send("build_#{TIME_FRACTIONS[i]}") } else while distance > 0 diff --git a/lib/dotiw/version.rb b/lib/dotiw/version.rb index 4942596..3d1e20a 100644 --- a/lib/dotiw/version.rb +++ b/lib/dotiw/version.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module DOTIW - VERSION = '4.0.1'.freeze + VERSION = '5.0.0' end diff --git a/spec/lib/dotiw_spec.rb b/spec/lib/dotiw_spec.rb index c96af77..9713c97 100755 --- a/spec/lib/dotiw_spec.rb +++ b/spec/lib/dotiw_spec.rb @@ -1,9 +1,15 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'A better distance_of_time_in_words' do - include ActionView::Helpers::DateHelper - include ActionView::Helpers::TextHelper - include ActionView::Helpers::NumberHelper + if defined?(ActionView) + include ActionView::Helpers::DateHelper + include ActionView::Helpers::TextHelper + include ActionView::Helpers::NumberHelper + else + include DOTIW::Methods + end START_TIME = '01-08-2009'.to_time @@ -17,13 +23,13 @@ fragments = [ [0.5.minutes, '30 seconds'], [4.5.minutes, '4 minutes and 30 seconds'], - [5.minutes.to_i, '5 minutes'], - [10.minutes.to_i, '10 minutes'], - [1.hour.to_i, '1 hour'], + [5.minutes, '5 minutes'], + [10.minutes, '10 minutes'], + [1.hour, '1 hour'], [1.hour + 30.seconds, '1 hour and 30 seconds'], - [4.weeks.to_i, '4 weeks'], + [4.weeks, '4 weeks'], [4.weeks + 2.days, '4 weeks and 2 days'], - [24.weeks.to_i, '5 months, 2 weeks, and 1 day'] + [24.weeks, '5 months, 2 weeks, and 1 day'] ] fragments.each do |number, result| it "#{number} == #{result}" do @@ -60,8 +66,9 @@ end it 'should be happy with lots of measurements' do - hash = distance_of_time_in_words_hash(START_TIME, - START_TIME + 1.year + 2.months + 3.weeks + 4.days + 5.hours + 6.minutes + 7.seconds) + hash = distance_of_time_in_words_hash( + START_TIME, + START_TIME + 1.year + 2.months + 3.weeks + 4.days + 5.hours + 6.minutes + 7.seconds) expect(hash[:years]).to eq(1) expect(hash[:months]).to eq(2) expect(hash[:weeks]).to eq(3) @@ -192,22 +199,6 @@ START_TIME + 1.hour + 1.day + 1.minute, { only: %w[minutes hours] }, '1 hour and 1 minute'], - [START_TIME, - START_TIME + 1.year + 2.months + 3.weeks + 4.days + 5.hours + 6.minutes + 7.seconds, - { vague: true }, - 'about 1 year'], - [START_TIME, - START_TIME + 1.year + 2.months + 3.weeks + 4.days + 5.hours + 6.minutes + 7.seconds, - { vague: 'Yes please' }, - 'about 1 year'], - [START_TIME, - START_TIME + 1.year + 2.months + 3.weeks + 4.days + 5.hours + 6.minutes + 7.seconds, - { vague: false }, - '1 year, 2 months, 3 weeks, 4 days, 5 hours, 6 minutes, and 7 seconds'], - [START_TIME, - START_TIME + 1.year + 2.months + 3.weeks + 4.days + 5.hours + 6.minutes + 7.seconds, - { vague: nil }, - '1 year, 2 months, 3 weeks, 4 days, 5 hours, 6 minutes, and 7 seconds'], [START_TIME, START_TIME + 1.year + 2.months + 3.weeks + 4.days + 5.hours + 6.minutes + 7.seconds, { except: 'minutes' }, @@ -249,6 +240,28 @@ { highest_measures: 1, only: %i[years months] }, 'less than 1 month'] ] + + if defined?(ActionView) + fragments += [ + [START_TIME, + START_TIME + 1.year + 2.months + 3.weeks + 4.days + 5.hours + 6.minutes + 7.seconds, + { vague: true }, + 'about 1 year'], + [START_TIME, + START_TIME + 1.year + 2.months + 3.weeks + 4.days + 5.hours + 6.minutes + 7.seconds, + { vague: 'Yes please' }, + 'about 1 year'], + [START_TIME, + START_TIME + 1.year + 2.months + 3.weeks + 4.days + 5.hours + 6.minutes + 7.seconds, + { vague: false }, + '1 year, 2 months, 3 weeks, 4 days, 5 hours, 6 minutes, and 7 seconds'], + [START_TIME, + START_TIME + 1.year + 2.months + 3.weeks + 4.days + 5.hours + 6.minutes + 7.seconds, + { vague: nil }, + '1 year, 2 months, 3 weeks, 4 days, 5 hours, 6 minutes, and 7 seconds'] + ] + end + fragments.each do |start, finish, options, output| it "should be #{output}" do expect(distance_of_time_in_words(start, finish, true, options)).to eq(output) @@ -268,17 +281,19 @@ end # include_seconds end - describe 'percentage of time' do - def time_in_percent(options = {}) - distance_of_time_in_percent('04-12-2009'.to_time, '29-01-2010'.to_time, '04-12-2010'.to_time, options) - end + if defined?(ActionView) + describe 'percentage of time' do + def time_in_percent(options = {}) + distance_of_time_in_percent('04-12-2009'.to_time, '29-01-2010'.to_time, '04-12-2010'.to_time, options) + end - it 'calculates 15%' do - expect(time_in_percent).to eq('15%') - end + it 'calculates 15%' do + expect(time_in_percent).to eq('15%') + end - it 'calculates 15.3%' do - expect(time_in_percent(precision: 1)).to eq('15.3%') + it 'calculates 15.3%' do + expect(time_in_percent(precision: 1)).to eq('15.3%') + end end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index aff30e3..29db5c6 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,12 +1,8 @@ +# frozen_string_literal: true + ROOT_PATH = File.join(File.dirname(__FILE__), '..') $LOAD_PATH.unshift ROOT_PATH unless $LOAD_PATH.include? ROOT_PATH -# Files that are usually required by Rails, but in a testing context will not be. -require 'erb' - -require 'active_support/all' -require 'action_view' - require 'dotiw' Time.zone = 'UTC'