Skip to content

Commit

Permalink
Show tenejo version info on production environments (#81)
Browse files Browse the repository at this point in the history
When the application is running in a production environment only the
current release version or branch is displayed.

In non-production environments, more detailed git commit information
and delpoyment dates are displayed.

For lack of a better method to detect produciton environments, the
code assumes that hostnames without hyphens are production:
* tenejo = production (only display version)
* tenejo-dev = non-production (display detailed deploy info)
* localhost = non-production (display detailed commit info)
  • Loading branch information
mark-dce authored Jan 11, 2022
1 parent 529452f commit cf34283
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
10 changes: 10 additions & 0 deletions app/helpers/footer_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module FooterHelper
# returns true if running in a full production environment
def production_host?
return false if request.host == 'localhost' # dev and test environments
return false if request.host.include?('-') # hostnames like qa-etd, tenejo-dev
true # hostnames without a dash
end
end
8 changes: 7 additions & 1 deletion app/views/shared/_footer.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
<div class="container-fluid">
<div class="navbar-text text-left">
<span>Powered by Tenejo</span>
<% if Rails.env.development? %>
<% if production_host? %>
<div class="version">
<%= BRANCH %>
</div>
<% end %>
<% unless production_host? %>
<%# Display detailed git information in dev, test, and non-production environments %>
<div class="version">
ENV: <%= Rails.env %>
<br>SHA: <%= GIT_SHA %>
Expand Down
4 changes: 2 additions & 2 deletions config/initializers/git_sha.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
BRANCH =
if CAPISTRANO_RELEASE
CAPISTRANO_RELEASE[1]
elsif Rails.env.development? || Rails.env.test?
elsif Rails.env.development?
`git rev-parse --abbrev-ref HEAD`.chomp
else
else # Rails.env.test?
"Unknown branch"
end

Expand Down
19 changes: 19 additions & 0 deletions spec/helpers/footer_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true
require "rails_helper"

describe FooterHelper do
describe '#production_host?' do
it 'returns true for hosts without a hyphen in their name' do
controller.request.host = 'production.example.com'
expect(helper.production_host?).to eq true
end
it 'returns false for localhost' do
controller.request.host = 'localhost'
expect(helper.production_host?).to eq false
end
it 'returns false for hosts with a hyphen in their name' do
controller.request.host = 'prod-like.example.com'
expect(helper.production_host?).to eq false
end
end
end
32 changes: 32 additions & 0 deletions spec/views/shared/_footer.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true
require 'rails_helper'

RSpec.describe "shared/_footer", type: :view do
context "on prod-like servers" do
# Assumes that prod-like servers have an environment name separated by a dash in the hostname\
# See FooterHelper for details
it 'displays detailed git information' do
controller.request.host = 'prod-like.example.com'
render
expect(rendered).to have_selector('.version', text: "SHA")
end
end

context "on production servers" do
# Assumes that production servers do not have dashed in hostnames
it 'displays only version info' do
controller.request.host = 'production.example.com'
render
expect(rendered).to have_selector('.version', text: "Unknown branch")
expect(rendered).to have_no_selector('.version', text: "SHA")
end
end

context "on local development and test environments" do
it 'displays detailed git information' do
controller.request.host = 'localhost'
render
expect(rendered).to have_selector('.version', text: "SHA")
end
end
end

0 comments on commit cf34283

Please sign in to comment.