citizen428.blog()

Try to learn something about everything

Rails — Display Branch in Development Mode

At work we are building a Rails app that’s supposed to run on appliances instead of being hosted by us. To make it easier to deal with bug reports and feature requests, we always display the version number in the footer. A couple of days ago I thought it’d be handy to replace this with the current branch in development mode. Using Grit the solution couldn’t have been any simpler:

app/helpers/application_helper.rb
1
2
3
4
def version_div
  version = Rails.env.development? ? ::Grit::Repo.new(Rails.root).head.name : ::OurApp::Application::Version
  content_tag(:div, content_tag(:span, version), :class => "version")
end

Call version_div in a view and you are good to go. If dragging in another dev dependency for just one call bothers you, you can also shell out to get the current branch: git rev-parse --abbrev-ref HEAD.

Comments