diff --git a/app/models/budget/investment/milestone.rb b/app/models/budget/investment/milestone.rb index 2421974da..562be54b8 100644 --- a/app/models/budget/investment/milestone.rb +++ b/app/models/budget/investment/milestone.rb @@ -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 diff --git a/spec/models/budget/investment/milestone_spec.rb b/spec/models/budget/investment/milestone_spec.rb index 59cbe1a68..45d4e7c0b 100644 --- a/spec/models/budget/investment/milestone_spec.rb +++ b/spec/models/budget/investment/milestone_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f14ee4041..d845190ed 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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"