Skip to content

Commit

Permalink
Fix core extension load order between ActiveSupport and I18n
Browse files Browse the repository at this point in the history
Avoids warnings for already-defined methods.

DRY locales path definition for loading

I18n should be lazy-loaded in order to require it later in order after
ActiveSupport. It appears that I18n is better at detecting already-defined core
extension methods on ruby classes.
  • Loading branch information
avit committed Sep 16, 2017
1 parent 89ea812 commit c7b305c
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 16 deletions.
4 changes: 1 addition & 3 deletions lib/ice_cube.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
require 'date'
require 'ice_cube/deprecated'
require 'ice_cube/i18n'

IceCube::I18n.detect_backend!

module IceCube

autoload :VERSION, 'ice_cube/version'

autoload :TimeUtil, 'ice_cube/time_util'
autoload :FlexibleHash, 'ice_cube/flexible_hash'
autoload :I18n, 'ice_cube/i18n'

autoload :Rule, 'ice_cube/rule'
autoload :Schedule, 'ice_cube/schedule'
Expand Down
17 changes: 10 additions & 7 deletions lib/ice_cube/i18n.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
require 'ice_cube/null_i18n'

module IceCube
module I18n

LOCALES_PATH = File.expand_path(File.join('..', '..', '..', 'config', 'locales'), __FILE__)

def self.t(*args)
backend.t(*args)
end
Expand All @@ -9,16 +14,14 @@ def self.l(*args)
end

def self.backend
@backend
@backend ||= detect_backend!
end

def self.detect_backend!
require 'i18n'
::I18n.load_path += Dir[File.expand_path('../../../config/locales/*{rb,yml}', __FILE__)]
@backend = ::I18n
rescue LoadError
require 'ice_cube/null_i18n'
@backend = NullI18n
::I18n.load_path += Dir[File.join(LOCALES_PATH, '*.yml')]
::I18n
rescue NameError
NullI18n
end
end
end
2 changes: 1 addition & 1 deletion lib/ice_cube/null_i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def self.l(date_or_time, options = {})
end

def self.config
@config ||= YAML.load(File.read(File.join(File.dirname(__FILE__), '..', '..', 'config', 'locales', 'en.yml')))['en']
@config ||= YAML.load_file(File.join(IceCube::I18n::LOCALES_PATH, 'en.yml'))['en']
end
end
end
1 change: 0 additions & 1 deletion spec/examples/occurrence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
end

it "accepts a format option to comply with ActiveSupport" do
# require 'active_support/core_ext/time'
time_now = Time.current
occurrence = Occurrence.new(time_now)

Expand Down
2 changes: 0 additions & 2 deletions spec/examples/to_s_en_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
require File.dirname(__FILE__) + '/../spec_helper'
require 'i18n'
require 'ice_cube/null_i18n'

describe IceCube::Schedule, 'to_s' do

Expand Down
6 changes: 4 additions & 2 deletions spec/examples/to_yaml_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
require File.dirname(__FILE__) + '/../spec_helper'
require 'active_support/time'

module IceCube
describe Schedule, 'to_yaml' do

before(:all) { Time.zone = 'Eastern Time (US & Canada)' }
before(:all) do
require 'active_support/time'
Time.zone = 'Eastern Time (US & Canada)'
end

[:yearly, :monthly, :weekly, :daily, :hourly, :minutely, :secondly].each do |type|
it "should make a #{type} round trip with to_yaml [#47]" do
Expand Down

0 comments on commit c7b305c

Please sign in to comment.