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

After upgrade from 4.0.1 to 5.3.1 option[:vague] raises error #126

Merged
merged 11 commits into from
Aug 18, 2021
7 changes: 6 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ The third argument for this method is whether or not to include seconds. By defa
=> "1 year, and 1 second"
```

Yes this could just be merged into the options hash but I'm leaving it here to ensure "backwards-compatibility", because that's just an insanely radical thing to do. \m/
Yes this could just be merged into the options hash but I'm leaving it here to ensure "backwards-compatibility", because that's just an insanely radical thing to do.
Copy link
Collaborator

@dblock dblock Aug 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should put back the original author's \m/ here, keep the spirit of the project alive.

Alternatively this can be included in the options hash as `include_seconds: true` removing this argument altogether.

The last argument is an optional options hash that can be used to manipulate behavior and (which uses `to_sentence`).

Expand Down Expand Up @@ -99,6 +100,10 @@ This will also be passed to `to_sentence`.

Specify this if you want it to use the old `distance_of_time_in_words`. The value can be anything except `nil` or `false`.

#### :include_seconds

As described above this option is the equivalent to the third argument whether to include seconds.

#### :accumulate_on

Specifies the maximum output unit which will accumulate all the surplus. Say you set it to seconds and your time difference is of 2 minutes then the output would be 120 seconds.
Expand Down
18 changes: 14 additions & 4 deletions lib/dotiw/action_view/helpers/date_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,25 @@ module DateHelper
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[:vague]
if include_seconds_or_options.is_a?(Hash)
options = include_seconds_or_options
else
options[:include_seconds] ||= !!include_seconds_or_options
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This modifies the incoming hash, which is bad (TM). Do options = options.merge(...) here.

end
return _distance_of_time_in_words(from_time, to_time, options.except(:vague)) if options[:vague]

DOTIW::Methods.distance_of_time_in_words(from_time, to_time, include_seconds_or_options, options.except(:vague))
DOTIW::Methods.distance_of_time_in_words(from_time, to_time, options.except(:vague))
end

def distance_of_time_in_words_to_now(to_time = 0, include_seconds_or_options = {}, options = {})
return _distance_of_time_in_words(Time.now, to_time, options) if options[:vague]
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_in_words(Time.now, to_time, options.except(:vague)) if options[:vague]

DOTIW::Methods.distance_of_time_in_words(Time.now, to_time, include_seconds_or_options, options.except(:vague))
DOTIW::Methods.distance_of_time_in_words(Time.now, to_time, options.except(:vague))
end

def distance_of_time_in_percent(from_time, current_time, to_time, options = {})
Expand Down
104 changes: 96 additions & 8 deletions spec/lib/dotiw_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,34 @@
end

if defined?(ActionView)
describe 'ActionView' do
describe 'ActionView without include seconds argument' do
[
[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, and 6 minutes'],
[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, and 6 minutes']
].each do |start, finish, options, output|
it "should be #{output}" do
expect(distance_of_time_in_words(start, finish, options)).to eq(output)
end
end
end
end

if defined?(ActionView)
describe 'ActionView with include seconds argument' do
[
[START_TIME,
START_TIME + 1.year + 2.months + 3.weeks + 4.days + 5.hours + 6.minutes + 7.seconds,
Expand All @@ -343,7 +370,7 @@
expect(distance_of_time_in_words(start, finish, true, options)).to eq(output)
end
end

context 'via ActionController::Base.helpers' do
it '#distance_of_time_in_words' do
end_time = START_TIME + 1.year + 2.months + 3.weeks + 4.days + 5.hours + 6.minutes + 7.seconds
Expand All @@ -369,32 +396,93 @@
end

context 'without options' do
it 'shows detailed duration' do
it 'shows detailed duration without seconds' do
expected = '3 days and 14 minutes'
actual = ActionController::Base.helpers.distance_of_time_in_words_to_now(Time.now - 3.days - 14.minutes)
expect(actual).to eq(expected)
end
end

context 'with vague option true' do
context 'with seconds false and vague option true' do
it 'shows vague duration' do
expected = '3 days'
actual = ActionController::Base.helpers.distance_of_time_in_words_to_now(
Time.now - 3.days - 14.minutes, false, vague: true
Time.now - 3.days - 14.minutes - 20.seconds, false, vague: true
)
expect(actual).to eq(expected)
end
end

context 'with seconds true and vague option true' do
it 'shows vague duration' do
expected = '3 days'
actual = ActionController::Base.helpers.distance_of_time_in_words_to_now(
Time.now - 3.days - 14.minutes - 20.seconds, true, vague: true
)
expect(actual).to eq(expected)
end
end

context 'with seconds false and vague option false' do
it 'shows detailed duration without seconds' do
expected = '3 days and 14 minutes'
actual = ActionController::Base.helpers.distance_of_time_in_words_to_now(
Time.now - 3.days - 14.minutes - 20.seconds, false, vague: false
)
expect(actual).to eq(expected)
end
end

context 'with vague option false' do
it 'shows detailed duration' do
context 'with seconds true and vague option false' do
it 'shows detailed duration with seconds' do
expected = '3 days, 14 minutes, and 20 seconds'
actual = ActionController::Base.helpers.distance_of_time_in_words_to_now(
Time.now - 3.days - 14.minutes - 20.seconds, true, vague: false
)
expect(actual).to eq(expected)
end
end

context 'without seconds and vague option false' do
it 'shows detailed duration without seconds' do
expected = '3 days and 14 minutes'
actual = ActionController::Base.helpers.distance_of_time_in_words_to_now(
Time.now - 3.days - 14.minutes, false, vague: false
Time.now - 3.days - 14.minutes - 20.seconds, vague: false
)
expect(actual).to eq(expected)
end
end

context 'without seconds and vague option true' do
it 'shows vague duration' do
expected = '3 days'
actual = ActionController::Base.helpers.distance_of_time_in_words_to_now(
Time.now - 3.days - 14.minutes - 20.seconds, vague: true
)
expect(actual).to eq(expected)
end
end

context 'with options include_seconds true and vague option false' do
it 'shows detailed duration with seconds' do
expected = '3 days, 14 minutes, and 20 seconds'
actual = ActionController::Base.helpers.distance_of_time_in_words_to_now(
Time.now - 3.days - 14.minutes - 20.seconds, {include_seconds: true, vague: false}
)
expect(actual).to eq(expected)
end
end

context 'with options include_seconds false and vague option true' do
it 'shows vague duration' do
expected = '3 days'
actual = ActionController::Base.helpers.distance_of_time_in_words_to_now(
Time.now - 3.days - 14.minutes - 20.seconds, {include_seconds: false, vague: true}
)
expect(actual).to eq(expected)
end
end

end
end
end
Expand Down