20

Is there any convention about how/where to specify application's version number?

For example, for the ruby gems lib/mygem/version.rb is the file generally used for that purpose.

My guess would be creating config/version.rb file like that:

module MySite
  VERSION = "0.0.4"

  # or in MySite::Application class
  # 
  # class Application
  #   VERSION = "0.0.4"
  # end
end
Oguz Bilgic
  • 3,392
  • 5
  • 36
  • 59

2 Answers2

29

I will reply my own question, I was not able to find a better answer.

Since Rails application is basically MySite::Application I thought app's version should be accessed by MySite::Application::VERSION so create :

config/initializers/version.rb

module MySite
  class Application
    VERSION = "0.0.4"
  end
end

or config/version.rb and require this file from config/application.rb

Oguz Bilgic
  • 3,392
  • 5
  • 36
  • 59
  • + To go the config/version.rb route, rembmer to put `require File.expand_path('../version', __FILE__)` at the end of config/application.rb – Ninjaxor Jun 16 '15 at 23:04
  • 1
    This is a great idea, but I'm setting it in the `config/application.rb` file, as this file already declares the `MySite::Application` class. In the case you need to rename your app, you actually change only one place. Finally, setting the version from an initializer sounds weird to me. – ZedTuX Jul 15 '21 at 20:07
  • Probably defining `MySite::VERSION`, directly on the module, would be more aligned to what we already do for gems. – collimarco Oct 31 '21 at 10:59
8

I add my own version to the Configuration class with an initializer:

app_version.rb

class Configuration
  class << self
    attr_accessor :app_version
  end
  @app_version = 0.72
end

Within the app, I can pull the version:

@app_version = Configuration.app_version

Not sure why you want to use the version, but I often use versioning so I can see if a particularly version of code is actually running. In that case, I need every code revision to be reflected as a new version, so I use the Git version on my code and often just display the first few characters of it since that is likely unique enough to identify it.

@git_version = `git show --pretty=%H`[0..39]
Carson Cole
  • 4,183
  • 6
  • 25
  • 35