-
-
Notifications
You must be signed in to change notification settings - Fork 487
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5800 from rubyforgood/5604-banner-expiration
Add expiration to banners
- Loading branch information
Showing
14 changed files
with
221 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Calculate values when using banner parameters | ||
class BannerParameters < SimpleDelegator | ||
def initialize(params, user, timezone) | ||
new_params = params.require(:banner).permit(:active, :content, :name, :expires_at).merge(user: user) | ||
|
||
if params.dig(:banner, :expires_at) | ||
new_params[:expires_at] = convert_expires_at_with_user_time_zone(params, timezone) | ||
end | ||
|
||
super(new_params) | ||
end | ||
|
||
private | ||
|
||
# `expires_at` comes from the frontend without any timezone information, so we use `in_time_zone` to attach | ||
# timezone information to it before saving to the database. If we don't do this, the time will be stored at UTC | ||
# by default. | ||
def convert_expires_at_with_user_time_zone(params, timezone) | ||
params[:banner][:expires_at].in_time_zone(timezone) | ||
end | ||
|
||
def params | ||
__getobj__ | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddExpiresAtToBanner < ActiveRecord::Migration[7.1] | ||
def change | ||
add_column :banners, :expires_at, :datetime, null: true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe BannerParameters do | ||
subject { described_class.new(params, user, timezone) } | ||
|
||
let(:params) { | ||
ActionController::Parameters.new( | ||
banner: ActionController::Parameters.new( | ||
active: "1", | ||
content: "content", | ||
name: "name" | ||
) | ||
) | ||
} | ||
|
||
let(:user) { create(:user) } | ||
|
||
let(:timezone) { nil } | ||
|
||
it "returns data" do | ||
expect(subject["active"]).to eq("1") | ||
expect(subject["content"]).to eq("content") | ||
expect(subject["name"]).to eq("name") | ||
expect(subject["expires_at"]).to be_blank | ||
expect(subject["user"]).to eq(user) | ||
end | ||
|
||
context "when expires_at is set" do | ||
let(:params) { | ||
ActionController::Parameters.new( | ||
banner: ActionController::Parameters.new( | ||
expires_at: "2024-06-10T12:12" | ||
) | ||
) | ||
} | ||
|
||
let(:timezone) { "America/Los_Angeles" } | ||
|
||
it "attaches timezone information to expires_at" do | ||
expect(subject["expires_at"]).to eq("2024-06-10 12:12:00 -0700") | ||
end | ||
end | ||
end |