Adding consistency in banner scopes
Since the :post_started_at and :post_ended_at fields are of type Date, we check with Date.current and not with Time.current. This change has been caused because some test suites were failing (https://github.com/consul/consul/runs/2170798218?check_suite_focus=true). The code we had was causing the banners to be available a few hours earlier or later than they should be depending on the time zone of the application.
This commit is contained in:
@@ -19,9 +19,7 @@ class Banner < ApplicationRecord
|
|||||||
has_many :sections
|
has_many :sections
|
||||||
has_many :web_sections, through: :sections
|
has_many :web_sections, through: :sections
|
||||||
|
|
||||||
scope :with_active, -> { where("post_started_at <= ?", Time.current).where("post_ended_at >= ?", Time.current) }
|
scope :with_active, -> { where("post_started_at <= :date and post_ended_at >= :date", date: Date.current) }
|
||||||
|
scope :with_inactive, -> { where.not(id: with_active) }
|
||||||
scope :with_inactive, -> { where("post_started_at > ? or post_ended_at < ?", Time.current, Time.current) }
|
|
||||||
|
|
||||||
scope :in_section, ->(section_name) { joins(:web_sections, :sections).where("web_sections.name ilike ?", section_name) }
|
scope :in_section, ->(section_name) { joins(:web_sections, :sections).where("web_sections.name ilike ?", section_name) }
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -18,4 +18,50 @@ describe Banner do
|
|||||||
expect(banner.background_color).to be_present
|
expect(banner.background_color).to be_present
|
||||||
expect(banner.font_color).to be_present
|
expect(banner.font_color).to be_present
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "scope" do
|
||||||
|
describe ".with_active" do
|
||||||
|
it "works when UTC date is different", :with_non_utc_time_zone do
|
||||||
|
banner = create(:banner, post_started_at: Date.current, post_ended_at: Date.current)
|
||||||
|
|
||||||
|
travel_to((Date.current - 1.day).end_of_day) do
|
||||||
|
expect(Banner.with_active).to be_empty
|
||||||
|
end
|
||||||
|
|
||||||
|
travel_to(Date.current.beginning_of_day) do
|
||||||
|
expect(Banner.with_active).to eq [banner]
|
||||||
|
end
|
||||||
|
|
||||||
|
travel_to(Date.current.end_of_day) do
|
||||||
|
expect(Banner.with_active).to eq [banner]
|
||||||
|
end
|
||||||
|
|
||||||
|
travel_to((Date.current + 1.day).beginning_of_day) do
|
||||||
|
expect(Banner.with_active).to be_empty
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe ".with_inactive" do
|
||||||
|
it "works when UTC date is different", :with_non_utc_time_zone do
|
||||||
|
banner = create(:banner, post_started_at: Date.current, post_ended_at: Date.current)
|
||||||
|
|
||||||
|
travel_to((Date.current - 1.day).end_of_day) do
|
||||||
|
expect(Banner.with_inactive).to eq [banner]
|
||||||
|
end
|
||||||
|
|
||||||
|
travel_to(Date.current.beginning_of_day) do
|
||||||
|
expect(Banner.with_inactive).to be_empty
|
||||||
|
end
|
||||||
|
|
||||||
|
travel_to(Date.current.end_of_day) do
|
||||||
|
expect(Banner.with_inactive).to be_empty
|
||||||
|
end
|
||||||
|
|
||||||
|
travel_to((Date.current + 1.day).beginning_of_day) do
|
||||||
|
expect(Banner.with_inactive).to eq [banner]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user