Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add timed update helper
Browse files Browse the repository at this point in the history
- was part of the original component in static, but lack of it only caused an error once trying to render the component guide page
- modified a bit from the original, had to require active_support, rewrote minitest to rspec, added timecop, set timezone in tests, configure as app helper
andysellick committed Jan 10, 2025
1 parent b37b709 commit 526b29c
Showing 6 changed files with 41 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ PATH
rouge
sprockets (>= 3)
sprockets-rails
timecop

GEM
remote: https://rubygems.org/
@@ -595,6 +596,7 @@ GEM
terser (1.2.4)
execjs (>= 0.3.0, < 3)
thor (1.3.2)
timecop (0.9.10)
timeout (0.4.2)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
@@ -634,6 +636,7 @@ DEPENDENCIES
rspec-rails
rubocop-govuk
terser
timecop (~> 0.9.10)
webmock
yard

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%
if before_update_time?(year: 2024, month: 7, day: 4, hour: 22, minute: 0)
if GovukPublishingComponents::AppHelpers::TimedUpdateHelper.before_update_time?(year: 2024, month: 7, day: 4, hour: 22, minute: 0)
show_global_bar ||= true # Toggles the appearance of the global bar
title = "Bring photo ID to vote"
title_href = "/how-to-vote/photo-id-youll-need"
1 change: 1 addition & 0 deletions govuk_publishing_components.gemspec
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
s.add_dependency "rouge"
s.add_dependency "sprockets", ">= 3"
s.add_dependency "sprockets-rails"
s.add_dependency "timecop"

s.add_development_dependency "capybara"
s.add_development_dependency "climate_control"
1 change: 1 addition & 0 deletions lib/govuk_publishing_components.rb
Original file line number Diff line number Diff line change
@@ -45,6 +45,7 @@
require "govuk_publishing_components/app_helpers/brand_helper"
require "govuk_publishing_components/app_helpers/environment"
require "govuk_publishing_components/app_helpers/asset_helper"
require "govuk_publishing_components/app_helpers/timed_update_helper"

# Add i18n paths and views for usage outside of a Rails app
I18n.load_path.unshift(
11 changes: 11 additions & 0 deletions lib/govuk_publishing_components/app_helpers/timed_update_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "active_support/all"

module GovukPublishingComponents
module AppHelpers
module TimedUpdateHelper
def self.before_update_time?(year:, month:, day:, hour:, minute:)
Time.zone.now.before? Time.zone.local(year, month, day, hour, minute)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require "spec_helper"
require "timecop"

RSpec.describe GovukPublishingComponents::AppHelpers::TimedUpdateHelper do
describe "Timed Update helper" do
it "#before_update_time? returns true if we haven't reached the requested time yet" do
Time.zone = "London"
Timecop.freeze(2024, 6, 17, 23, 59)
expect(GovukPublishingComponents::AppHelpers::TimedUpdateHelper.before_update_time?(year: 2024, month: 6, day: 18, hour: 0, minute: 0)).to be true
end

it "#before_update_time? returns false if we've reached the requested time" do
Time.zone = "London"
Timecop.freeze(2024, 6, 18, 0, 0)
expect(GovukPublishingComponents::AppHelpers::TimedUpdateHelper.before_update_time?(year: 2024, month: 6, day: 18, hour: 0, minute: 0)).to be false
end

it "#before_update_time? returns false if we've passed the requested time" do
Time.zone = "London"
Timecop.freeze(2024, 6, 20, 10, 10)
expect(GovukPublishingComponents::AppHelpers::TimedUpdateHelper.before_update_time?(year: 2024, month: 6, day: 18, hour: 0, minute: 0)).to be false
end
end
end

0 comments on commit 526b29c

Please sign in to comment.