简体   繁体   English

如何访问 Rails 3 应用程序对象的名称?

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

我需要从 Rails 3 引擎中知道程序员应用程序的名称。

Rails.application.class.parent_name

The original poster asked for the name of the rails app , not for the class name.原始海报要求提供rails app 的名称,而不是类名。 Those are two different things.这是两个不同的东西。 eg the spelling of the rails app can be different from what Rails expects, eg 'test-app' instead of 'test_app'.例如,rails 应用程序的拼写可能与 Rails 期望的不同,例如“test-app”而不是“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.很难找到Rails 应用程序名称,因为它的拼写方式是签入的,因为该名称不会被保留。 Only the options for the session_store seem to contain the original string slightly modified.只有 session_store 的选项似乎包含稍微修改过的原始字符串。

The best way to get to the name of the Rails application is:获得 Rails 应用程序名称的最佳方法是:

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... eg with '-' characters instead of '_';因为应用程序的作者可能拼写它的名称与 Rails 预期的不同......例如使用“-”字符而不是“_”; eg "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 :或者只是将其添加到您的./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 (eg during deployment to '"20121001_110512" or "latest"... then the following would break:这几乎是正确的,但如果应用程序目录被重命名,它仍然会失败(例如,在部署到“20121001_110512”或“最新”期间……那么以下内容会中断:

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 '-'.但请注意,这并不是万无一失的,因为有人可以将应用命名为“test-app”,您将看到上面的结果,而不是带有“-”的正确名称。

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.这是班级名称,但您无法确定如何按照作者拼写的方式获得名称。

In Rails 3, the application that is generated is given a module namespace matching the application name.在 Rails 3 中,生成的应用程序被赋予一个与应用程序名称匹配的模块命名空间。 So if your application was called "Twitter", the class of Rails.application is "Twitter::Application".因此,如果您的应用程序被称为“Twitter”,则 Rails.application 的类是“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 应用程序的类名字符串以获得如下所示的应用程序名称:

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

In our example, the resulting string would be "Twitter".在我们的示例中,结果字符串将是“Twitter”。

Rails.application返回您的MyApp::Application类。

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.对于更“人性化”的版本(基于您可以在此处找到的第一个回复),请考虑以下方法:它将为您的TestApp应用程序返回Test App

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

For Rails 6 and beyond对于 Rails 6 及更高版本

Rails.application.class.module_parent.name

More context:更多背景:

Module#parent has been renamed to module_parent . Module#parent已重命名为module_parent parent is deprecated and will be removed in Rails 6.1. parent已弃用,将在 Rails 6.1 中删除。

Using Scott's answer, I put the following in my app/controllers/application_controller.rb file使用 Scott 的回答,我将以下内容放入我的app/controllers/application_controller.rb文件中

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).然后在我的模板中,我可以在任何需要的地方执行#{app_name} ,如果您需要重命名您的应用程序(我今天早上必须这样做),这会减少很多麻烦。

You can find this in the config.ru file:您可以在 config.ru 文件中找到它:

run Dinosaur::Application

or in the Rakefile:或在 Rakefile 中:

Dinosaur::Application.load_tasks

The name prior to the "::" is the app name “::”之前的名称是应用程序名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM