How do I access the name of the Rails 3 application object?

Ruby on-Rails

Ruby on-Rails Problem Overview


I need to know the name of a programmers application from within a Rails 3 engine.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Rails <= 6.0

Rails.application.class.parent_name

Rails 6.1 and higher

Rails.application.class.module_parent_name

Solution 2 - Ruby on-Rails

The original poster asked for the name of the rails app, not for the class name. Those are two different things. e.g. the spelling of the rails app can be different from what Rails expects, e.g. 'test-app' instead of 'test_app'.

It is difficult to get to the name of the Rails App spelled the way as it was checked-in, because that name is not preserved. Only the options for the session_store seem to contain the original string slightly modified.

The best way to get to the name of the Rails application is:

This will work even if your app directory was renamed, or sym-linked!

Rails.application.config.session_options[:key].sub(/^_/,'').sub(/_session/,'')
=> "test-app"

Why? Because the author of the app could have spelled it's name differently than Rails expects... e.g. with '-' characters instead of '_'; e.g. "test-app". From the class name you can't guarantee to get to the correct spelling.

Using this info, you could 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/,'')

that makes these constants available on the top-level of the app.


Close, but no cigar:

This is almost correct, but it will still fail if the application directory gets renamed (e.g. during deployment to '"20121001_110512" or "latest"... then the following would break:

File.basename(Rails.root.to_s)
=> "test-app"

with the following two approaches you can't get to the correct spelling.. you could only guess a name:

This is sub-optimal, and can give incorrectly spelled results:

You can get to a derivative of the application name like this:

Rails.application.engine_name.gsub(/_application/,'')
=> "test_app"

But please note that this is not fool-proof, because somebody could have named the app "test-app" and you will see the result above, not the correct name with '-'.

Same thing is true if you derive it like this:

Rails.application.class.parent_name
=> "TestApp"

this is the class name, but you can't be sure how to get to the name the way the author spelled it.

Solution 3 - Ruby on-Rails

In Rails 3, the application that is generated is given a module namespace matching the application name. So if your application was called "Twitter", the class of Rails.application is "Twitter::Application".

This means that you could split the string of the class name of your Rails application to get an application name like this:

Rails.application.class.to_s.split("::").first

In our example, the resulting string would be "Twitter".

Solution 4 - Ruby on-Rails

Rails.application returns your MyApp::Application class.

Solution 5 - Ruby on-Rails

For Rails 6 and beyond

Rails.application.class.module_parent.name

More context:

Module#parent has been renamed to module_parent. parent is deprecated and will be removed in Rails 6.1.

Solution 6 - Ruby on-Rails

For a more "humanized" version (based on the first reply you can find here) consider the following method: it will return Test App for your TestApp application.

def app_name
  Rails.application.class.parent_name
    .underscore
    .humanize
    .split
    .map(&:capitalize)
    .join(' ')
end

Solution 7 - Ruby on-Rails

Using Scott's answer, I put the following in my app/controllers/application_controller.rb file

class ApplicationController < ActionController::Base
  protect_from_forgery
  helper_method :app_name
  private
    def app_name
      Rails.application.class.to_s.split("::").first
    end

Then in my templates I can just do #{app_name} wherever it's needed, which makes it much less of a hassle if you need to rename your app (which I had to do this morning).

Solution 8 - Ruby on-Rails

You can find this in the config.ru file:

run Dinosaur::Application

or in the Rakefile:

Dinosaur::Application.load_tasks

The name prior to the "::" is the app name

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSteveView Question on Stackoverflow
Solution 1 - Ruby on-RailsAndrei KulakovView Answer on Stackoverflow
Solution 2 - Ruby on-RailsTiloView Answer on Stackoverflow
Solution 3 - Ruby on-RailsScottView Answer on Stackoverflow
Solution 4 - Ruby on-RailsyfeldblumView Answer on Stackoverflow
Solution 5 - Ruby on-RailsTate ThurstonView Answer on Stackoverflow
Solution 6 - Ruby on-RailsRoxas ShadowView Answer on Stackoverflow
Solution 7 - Ruby on-RailsPeter LyonsView Answer on Stackoverflow
Solution 8 - Ruby on-RailsAndrewView Answer on Stackoverflow