-
Notifications
You must be signed in to change notification settings - Fork 0
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 #267 from pulibrary/i265_banner_flipper
I265 banner flipper
- Loading branch information
Showing
17 changed files
with
467 additions
and
102 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
class BannerController < ApplicationController | ||
def show | ||
banner_json = Banner.first.as_json(except: [:id, :created_at, :updated_at]) | ||
|
||
render json: banner_json | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
# This class is responsible for coordinating displaying an informational banner | ||
class Banner < ApplicationRecord | ||
enum alert_status: { | ||
info: 1, | ||
success: 2, | ||
warning: 3, | ||
error: 4 | ||
} | ||
|
||
def display_banner | ||
@display_banner = Flipper.enabled?(:banner) | ||
end | ||
|
||
# args is an instance of Rake::TaskArguments | ||
def rake_update(args) | ||
update(args.to_h) | ||
save! | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class CreateFlipperTables < ActiveRecord::Migration[7.1] | ||
def up | ||
create_table :flipper_features do |t| | ||
t.string :key, null: false | ||
t.timestamps null: false | ||
end | ||
add_index :flipper_features, :key, unique: true | ||
|
||
create_table :flipper_gates do |t| | ||
t.string :feature_key, null: false | ||
t.string :key, null: false | ||
t.text :value | ||
t.timestamps null: false | ||
end | ||
add_index :flipper_gates, [:feature_key, :key, :value], unique: true, length: { value: 255 } | ||
end | ||
|
||
def down | ||
drop_table :flipper_gates | ||
drop_table :flipper_features | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class CreateBanners < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :banners do |t| | ||
t.text :text, default: '' | ||
t.boolean :display_banner, default: false | ||
t.integer :alert_status, default: 1 | ||
t.boolean :dismissible, default: true | ||
t.boolean :autoclear, default: false | ||
|
||
t.timestamps | ||
end | ||
|
||
def create_banner_if_none_exists | ||
return Rails.logger.info('Already have a banner object, update that one') if Banner.count >= 1 | ||
|
||
Banner.create! | ||
end | ||
|
||
create_banner_if_none_exists | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
def create_banner_if_none_exists | ||
return Rails.logger.info('Already have a banner object, update that one') if Banner.count >= 1 | ||
|
||
Banner.create! | ||
end | ||
|
||
create_banner_if_none_exists |
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 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'flipper' | ||
|
||
namespace :banner do | ||
desc 'Enable banner' | ||
task :enable do | ||
on roles(:db) do | ||
within '/opt/allsearch_api/current' do | ||
execute :bundle, :exec, :flipper, :enable, :banner | ||
end | ||
end | ||
run_locally { puts('The banner is now enabled and should display.') } | ||
end | ||
|
||
desc 'Disable banner' | ||
task :disable do | ||
on roles(:db) do | ||
within '/opt/allsearch_api/current' do | ||
execute :bundle, :exec, :flipper, :disable, :banner | ||
end | ||
end | ||
run_locally { puts('The banner is now disabled and should not display.') } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
namespace :banner do | ||
desc 'Update banner [text,alert_status[info|success|warning|error],dismissible,autoclear]' | ||
task :update, [:text, :alert_status, :dismissible, :autoclear] => :environment do |_task, args| | ||
banner = if Banner.count.zero? | ||
Banner.new | ||
else | ||
Banner.first | ||
end | ||
banner.rake_update(args) | ||
Rake::Task['banner:show'].invoke | ||
end | ||
|
||
desc 'Show current banner configuration' | ||
task show: :environment do | ||
banner = Banner.first | ||
puts "The currently set banner text is: #{banner.text}\n" | ||
puts "The currently set banner alert_status is: #{banner.alert_status}\n" | ||
puts "The currently set banner is dismissible: #{banner.dismissible}\n" | ||
puts "The currently set banner is autoclear: #{banner.autoclear}" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
require 'rake' | ||
|
||
RSpec.describe Banner do | ||
let(:banner) { described_class.first } | ||
|
||
it 'can be instantiated' do | ||
expect(banner).to be_an_instance_of(described_class) | ||
end | ||
|
||
it 'has the expected defaults' do | ||
expect(banner.text).to eq('') | ||
expect(banner.display_banner).to be(false) | ||
expect(banner.alert_status).to eq('info') | ||
expect(banner.dismissible).to be(true) | ||
expect(banner.autoclear).to be(false) | ||
end | ||
|
||
it 'has an as_json method' do | ||
expect(banner.as_json).to be_an_instance_of(Hash) | ||
expect(banner.as_json(except: [:id, :created_at, :updated_at]).keys) | ||
.to contain_exactly('text', 'display_banner', 'alert_status', | ||
'dismissible', 'autoclear') | ||
end | ||
|
||
it 'can be updated' do | ||
banner.text = 'some other text' | ||
banner.save! | ||
expect(banner.text).to eq('some other text') | ||
end | ||
|
||
it 'only allows valid alert statuses' do | ||
expect do | ||
banner.alert_status = 'warning' | ||
banner.save! | ||
end.to change(banner, :alert_status) | ||
|
||
expect do | ||
banner.alert_status = 'potato' | ||
banner.save! | ||
end.to raise_error(ArgumentError, /not a valid alert_status/) | ||
end | ||
|
||
it 'can be updated to include html' do | ||
banner.text = '<h2>This is a big heading about an important thing</h2><p>It includes a <a href="https://www.example.com">link</a></p>' | ||
banner.save! | ||
expect(banner.text).to eq('<h2>This is a big heading about an important thing</h2><p>It includes a <a href="https://www.example.com">link</a></p>') | ||
end | ||
|
||
context 'when updated from the rake task' do | ||
context 'with four arguments' do | ||
let(:args) { Rake::TaskArguments.new(%w[text alert_status dismissible autoclear], ['<a href="https://www.example.com">a is a sentence</a>', 'warning', 'false', 'true']) } | ||
|
||
it 'can be updated using a rake task' do | ||
banner.rake_update(args) | ||
expect(banner.reload.text).to eq('<a href="https://www.example.com">a is a sentence</a>') | ||
expect(banner.alert_status).to eq('warning') | ||
expect(banner.dismissible).to be false | ||
expect(banner.autoclear).to be true | ||
end | ||
end | ||
|
||
context 'with two arguments' do | ||
let(:args) { Rake::TaskArguments.new(%w[text alert_status], ['<a href="https://www.example.com">a is a sentence</a>', 'warning']) } | ||
|
||
it 'can be updated using a rake task' do | ||
banner.rake_update(args) | ||
expect(banner.reload.text).to eq('<a href="https://www.example.com">a is a sentence</a>') | ||
expect(banner.alert_status).to eq('warning') | ||
end | ||
end | ||
|
||
context 'with one argument' do | ||
let(:args) { Rake::TaskArguments.new(%w[text], ['different text argument']) } | ||
|
||
it 'can be updated using a rake task' do | ||
banner.rake_update(args) | ||
expect(banner.reload.text).to eq('different text argument') | ||
expect(banner.alert_status).to eq('info') | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'swagger_helper' | ||
|
||
RSpec.describe 'banner' do | ||
path '/banner' do | ||
get('show banner') do | ||
tags 'Banner' | ||
operationId 'displayBanner' | ||
produces 'application/json' | ||
description 'Displays the current settings for the banner' | ||
response(200, 'successful') do | ||
after do |example| | ||
example.metadata[:response][:content] = { | ||
'application/json' => { | ||
example: JSON.parse(response.body, symbolize_names: true) | ||
} | ||
} | ||
end | ||
|
||
run_test! | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.