跳至内容 跳至搜索

Railties – 将 Engine 粘合到 Rails

Railties 负责将所有框架粘合在一起。 总的来说,它

  • 处理 Rails 应用程序的引导过程;

  • 管理 rails 命令行界面;

  • 并提供 Rails 生成器核心。

下载

可以使用 RubyGems 安装最新版本的 Railties

  • gem install railties

源代码可以作为 GitHub 上 Rails 项目的一部分下载

许可证

Railties 在 MIT 许可下发布

支持

API 文档位于

可以在此处为 Ruby on Rails 项目提交错误报告

功能请求应在以下位置的 rails-core 邮件列表中进行讨论

命名空间
方法
A
B
C
E
G
P
R
V

属性

[RW] app_class
[W] application
[RW] cache
[RW] logger

类公共方法

application()

# File railties/lib/rails.rb, line 45
def application
  @application ||= (app_class.instance if app_class)
end

autoloaders()

# File railties/lib/rails.rb, line 126
def autoloaders
  application.autoloaders
end

backtrace_cleaner()

# File railties/lib/rails.rb, line 56
def backtrace_cleaner
  @backtrace_cleaner ||= Rails::BacktraceCleaner.new
end

configuration()

用于配置 Rails 环境的 Configuration 实例

# File railties/lib/rails.rb, line 52
def configuration
  application.config
end

env()

返回当前 Rails 环境。

Rails.env # => "development"
Rails.env.development? # => true
Rails.env.production? # => false
Rails.env.local? # => true              true for "development" and "test", false for anything else
# File railties/lib/rails.rb, line 75
def env
  @_env ||= ActiveSupport::EnvironmentInquirer.new(ENV["RAILS_ENV"].presence || ENV["RACK_ENV"].presence || "development")
end

env=(environment)

设置 Rails 环境。

Rails.env = "staging" # => "staging"
# File railties/lib/rails.rb, line 82
def env=(environment)
  @_env = ActiveSupport::EnvironmentInquirer.new(environment)
end

error()

返回当前 Rails 项目的 ActiveSupport::ErrorReporter,否则如果不存在项目,则返回 nil

Rails.error.handle(IOError) do
  # ...
end
Rails.error.report(error)
# File railties/lib/rails.rb, line 93
def error
  ActiveSupport.error_reporter
end

gem_version()

返回当前加载的 Rails 版本作为 Gem::Version

# File railties/lib/rails/gem_version.rb, line 5
def self.gem_version
  Gem::Version.new VERSION::STRING
end

groups(*groups)

返回所有基于以下内容的加载 Rails 组

  • Rails 环境;

  • 环境变量 RAILS_GROUPS;

  • 作为参数给出的可选环境以及包含组依赖项的哈希;

Rails.groups assets: [:development, :test]
# => [:default, "development", :assets] for Rails.env == "development"
# => [:default, "production"]           for Rails.env == "production"
# File railties/lib/rails.rb, line 106
def groups(*groups)
  hash = groups.extract_options!
  env = Rails.env
  groups.unshift(:default, env)
  groups.concat ENV["RAILS_GROUPS"].to_s.split(",")
  groups.concat hash.map { |k, v| k if v.map(&:to_s).include?(env) }
  groups.compact!
  groups.uniq!
  groups
end

public_path()

返回当前 Rails 项目的公共文件夹的 Pathname 对象,否则如果不存在项目,则返回 nil

Rails.public_path
  # => #<Pathname:/Users/someuser/some/path/project/public>
# File railties/lib/rails.rb, line 122
def public_path
  application && Pathname.new(application.paths["public"].first)
end

root()

返回当前 Rails 项目的 Pathname 对象,否则如果不存在项目,则返回 nil

Rails.root
  # => #<Pathname:/Users/someuser/some/path/project>
# File railties/lib/rails.rb, line 65
def root
  application && application.config.root
end

version()

返回当前加载的 Rails 版本的字符串。

# File railties/lib/rails/version.rb, line 7
def self.version
  VERSION::STRING
end