How to access the object name of a Rails 3 application?

I need to know the name of the Rails 3 programmer application.

+45
ruby-on-rails
Aug 21 '10 at 21:51
source share
7 answers
Rails.application.class.parent_name 
+106
Mar 12 2018-11-12T00:
source share

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.

+19
Oct 18 '12 at 17:53
source share

In Rails 3, the application namespace is set to the module namespace corresponding to the application name. Therefore, if your application was named "Twitter", the Rails.application class will be "Twitter :: Application".

This means that you can 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 will be "Twitter."

+15
Aug 22 '10 at 16:36
source share

Rails.application returns the MyApp::Application class.

+5
Aug 22 '10 at 19:41
source share

For a more β€œhumanized” version (based on the first answer you can find here), consider the following method: it will return the Test App for your TestApp application.

 def app_name Rails.application.class.parent_name .underscore .humanize .split .map(&:capitalize) .join(' ') end 
+3
Jan 19 '16 at 21:35
source share

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 is needed, which makes it much less complicated if you need to rename your application (which I should have done this morning).

+1
Nov 09 '10 at 20:57
source share

You can find this in the config.ru file:

 run Dinosaur::Application 

or in the Rakefile:

 Dinosaur::Application.load_tasks 

The name before "::" is the name of the application

0
May 22 '13 at 6:01
source share



All Articles