Skip to content

Commit

Permalink
Merge pull request #803 from CommitChange/add-use_zone
Browse files Browse the repository at this point in the history
Add Nonprofit#use_zone
  • Loading branch information
wwahammy authored Oct 17, 2023
2 parents 896cfd0 + 09bec76 commit ab456fc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/models/nonprofit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,13 @@ def unpublish!
def zone
(timezone.present? && ActiveSupport::TimeZone[timezone]) || Time.zone
end

# use the Nonprofit's timezone in a block
def use_zone(&block)
Time.use_zone(zone) do
yield block
end
end
end

def has_achievements?
Expand Down
23 changes: 23 additions & 0 deletions spec/models/nonprofit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,29 @@
expect(build(:nonprofit, timezone: 'Central Time (US & Canada)').zone).to eq ActiveSupport::TimeZone['Central Time (US & Canada)']
end
end

describe '#use_zone' do
it 'makes times in UTC if no zone provided' do
np = build(:nonprofit)
beginning_of_year_in_np_zone = nil
np.use_zone do
beginning_of_year_in_np_zone = Time.current.beginning_of_year
end

expect(beginning_of_year_in_np_zone).to eq ActiveSupport::TimeZone['UTC'].now.beginning_of_year
end

it 'makes times in local zones if zone provided' do
np = build(:nonprofit, timezone: 'Central Time (US & Canada)')
beginning_of_year_in_np_zone = nil
np.use_zone do
beginning_of_year_in_np_zone = Time.current.beginning_of_year
end

# do they represent the same time?
expect(beginning_of_year_in_np_zone.to_i).to eq (ActiveSupport::TimeZone['UTC'].now.beginning_of_year + 6.hours).to_i
end
end
end

describe '::Profile' do
Expand Down

0 comments on commit ab456fc

Please sign in to comment.