The original poster asked for the application name rails , not the class name. These are two different things. for example, the spelling of a rails application may be different from what Rails expects, for example. 'test-app' instead of 'test_app'.
It is difficult to get to the name of the Rails application, as noted in , because this name is not saved. Only the parameters for session_store seem to contain the original string slightly modified.
The best way to get the name of a Rails application:
This will work even if your application directory has been renamed or associated with a symbol!
Rails.application.config.session_options[:key].sub(/^_/,'').sub(/_session/,'') => "test-app"
Why? Because the author of the application could write the name differently than Rails expects ... for example. with the characters '-' instead of '_'; for example, "Test Application." From the name of the class, you cannot guarantee the correct spelling.
Using this information, you can do this:
class << Rails.application def name Rails.application.config.session_options[:key].sub(/^_/,'').sub(/_session/,'') end end Rails.application.name => 'test-app'
or just add this to your ./config/environment.rb :
APP_VERSION = '1.0.0' APP_NAME = Rails.application.config.session_options[:key].sub(/^_/,'').sub(/_session/,'')
making these constants available at the top level of the application.
Close but no cigar:
This is almost correct, but it will still fail if the application directory is renamed (for example, during deployment to "20121001_110512" or "last" ... then the following broke:
File.basename(Rails.root.to_s) => "test-app"
with the following two approaches you cannot get the correct spelling .. you could only guess the name:
This is not optimal and may produce incorrectly generated results:
You can get the derivative on behalf of the application, for example:
Rails.application.engine_name.gsub(/_application/,'') => "test_app"
But keep in mind that this is not perfect, because someone could call the application "test-app" and you will see the result above, and not the correct name with "-".
The same is true if you print it like this:
Rails.application.class.parent_name => "TestApp"
this is the name of the class, but you cannot be sure how to get to the name as the author wrote it.