Skip to content

Commit 36a65ea

Browse files
committed
Make improvements to GitCommitSha functionality.
- refactor GitCommitSha class according to suggestions by @justin808 - access GitCommitSha directly from pages_helper instead of going through controller - display shorter version of the hash - make the hash link to the corresponding commit on GitHub
1 parent 3257663 commit 36a65ea

File tree

5 files changed

+18
-10
lines changed

5 files changed

+18
-10
lines changed

app/controllers/pages_controller.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
class PagesController < ApplicationController
22
def index
33
@comments = Comment.all
4-
@git_commit_sha = GitCommitSha.current_sha
54

65
# NOTE: The below notes apply if you want to set the value of the props in the controller, as
76
# compared to he view. However, it's more convenient to use Jbuilder from the view. See

app/helpers/pages_helper.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
module PagesHelper
2+
def git_commit_sha
3+
GitCommitSha.current_sha
4+
end
5+
6+
def git_commit_sha_short
7+
full_sha = git_commit_sha
8+
full_sha.slice(full_sha.size - 7, full_sha.size)
9+
end
210
end

app/models/git_commit_sha.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
class GitCommitSha
22
def self.current_sha
3-
return @commit_sha unless @commit_sha.blank?
4-
@commit_sha = (retrieve_sha_from_env_var || retrieve_sha_from_git_folder)
3+
@commit_sha ||= retrieve_sha_from_env_var.presence || retrieve_sha_from_git_folder
54
end
65

76
def self.current_sha=(sha)
87
@commit_sha = sha
98
end
109

1110
def self.reset_current_sha
12-
self.current_sha = ""
11+
self.current_sha = nil
1312
end
1413

1514
def self.retrieve_sha_from_git_folder
@@ -18,6 +17,6 @@ def self.retrieve_sha_from_git_folder
1817

1918
def self.retrieve_sha_from_env_var
2019
env_var = ENV["DEPLOYMENT_SHA"]
21-
env_var.blank? ? false : env_var
20+
env_var.blank? ? nil : env_var
2221
end
2322
end

app/views/pages/index.html.erb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<h2>Using React + Redux + Rails Backend (using the react_on_rails gem)</h2>
2-
<p>current Git commit:
3-
<span id="current-git-commit-sha">
4-
<%= @git_commit_sha %>
5-
</span>
2+
<p>Current Commit:
3+
<%= link_to git_commit_sha_short,
4+
"https://github.com/shakacode/react-webpack-rails-tutorial/commit/#{git_commit_sha}",
5+
id: "git-commit-sha" %>
66
</p>
77
<ul>
88
<li>

spec/features/pages_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@
66
after { ENV["DEPLOYMENT_SHA"] = old_sha }
77
background { visit root_path }
88
it "displays the current git commit" do
9-
expect(page).to have_css("#current-git-commit-sha", text: sha)
9+
expect(page).to have_css("#git-commit-sha", text: expected_text)
1010
end
1111
end
1212

1313
feature "Git Commit SHA" do
1414
context "when env var is not set" do
1515
let(:sha) { "94d92356828a56db25fccff9d50f41c525eead5x" }
16+
let(:expected_text) { "5eead5x" }
1617
before { GitCommitSha.current_sha = sha }
1718
it_behaves_like "Git Commit SHA"
1819
end
1920
context "when env var is set" do
2021
let(:sha) { "94d92356828a56db25fccff9d50f41c525eead5y" }
22+
let(:expected_text) { "5eead5y" }
2123
before do
2224
ENV["DEPLOYMENT_SHA"] = sha
2325
GitCommitSha.reset_current_sha

0 commit comments

Comments
 (0)