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

后端 未结 8 927
礼貌的吻别
礼貌的吻别 2020-12-23 13:28

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

相关标签:
8条回答
  • 2020-12-23 13:46

    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).

    0 讨论(0)
  • 2020-12-23 13:49

    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.

    0 讨论(0)
  • 2020-12-23 13:50

    Rails.application returns your MyApp::Application class.

    0 讨论(0)
  • 2020-12-23 13:57

    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
    
    0 讨论(0)
  • 2020-12-23 14:00

    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.

    0 讨论(0)
  • 2020-12-23 14:08

    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

    0 讨论(0)
提交回复
热议问题