Use Date.current to find published milestones

Using `Date.today` caused some milestones to be published before/after
the date defined by `Rails.application.config.time_zone`.

See also commit AyuntamientoMadird/consul@088c76d for a more detailed
explanation.
This commit is contained in:
Javi Martín
2018-09-03 01:29:37 +02:00
parent 126941a335
commit ab870c756a
3 changed files with 24 additions and 1 deletions

View File

@@ -18,7 +18,7 @@ class Budget
validate :description_or_status_present?
scope :order_by_publication_date, -> { order(publication_date: :asc) }
scope :published, -> { where("publication_date <= ?", Date.today) }
scope :published, -> { where("publication_date <= ?", Date.current) }
scope :with_status, -> { where("status_id IS NOT NULL") }
def self.title_max_length

View File

@@ -69,4 +69,16 @@ describe Budget::Investment::Milestone do
end
end
describe ".published" do
it "uses the application's time zone date", :with_different_time_zone do
published_in_local_time_zone = create(:budget_investment_milestone,
publication_date: Date.today)
published_in_application_time_zone = create(:budget_investment_milestone,
publication_date: Date.current)
expect(Budget::Investment::Milestone.published).to include(published_in_application_time_zone)
expect(Budget::Investment::Milestone.published).not_to include(published_in_local_time_zone)
end
end
end

View File

@@ -90,6 +90,17 @@ RSpec.configure do |config|
travel_back
end
config.before(:each, :with_different_time_zone) do
system_zone = ActiveSupport::TimeZone.new("UTC")
local_zone = ActiveSupport::TimeZone.new("Madrid")
# Make sure the date defined by `config.time_zone` and
# the local date are different.
allow(Time).to receive(:zone).and_return(system_zone)
allow(Time).to receive(:now).and_return(Date.current.at_end_of_day.in_time_zone(local_zone))
allow(Date).to receive(:today).and_return(Time.now.to_date)
end
# Allows RSpec to persist some state between runs in order to support
# the `--only-failures` and `--next-failure` CLI options.
config.example_status_persistence_file_path = "spec/examples.txt"