The Rails Way 读书笔记 | Chapter 1
推荐 原创
©著作权归作者所有:来自51CTO博客作者blackanger的原创作品,请联系作者获取转载授权,否则将追究法律责任
曾经有人说,the rails way这本书,只是个字典, 字典有必要看吗? 昨天晚上看了看, 发现不仅仅是字典,于是就做了点笔记。 不喜欢看字典的人就绕道吧。
因为Rails Way这本书出版的时候,Rails2.0刚出来,书里的内容有点跟不上Rails发展,所以我也加了一些Rails2.2.2的变更内容在我的笔记里。
Rails Environments and
Configuration
Rails applications are preconfigured with three standard modes of operation: development,
test, and production.
The current environment is always specified in the environment variable
RAILS_ENV.
Start UP 当你启动一个进程去处理Rails请求的时候,会发生什么?(比如Webrick server)
Rails启动的第一件就是:加载config/environment.rb,
给个实例,看看public/dispatch.rb:最顶上那行:
require File.dirname(__FILE__) + “/../config/environment”
我们来看看config/environment.rb文件里面你看到的默认设置:
第一, Mode Override
# Uncomment below to force Rails into production mode when
# you don't control web/app server and can't set it the proper way
# ENV['RAILS_ENV'] ||= 'production'
如果你这里把注释去掉, 那么rails启动以后做的一切都是在production模式下。举个实例,你可以看看test_helper.rb里最顶上的那行:
ENV["RAILS_ENV"] = "test"
是让代码运行在测试环境下。
第二,Rails Gem Version
# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '2.2.2' unless defined? RAILS_GEM_VERSION
事实上这个时候Rails还没有被加载,这句配置就是告诉script加载的rails版本。
当你使用rails edge的时候, 这个设置就没有意义了。 你可以运行rake命令:
rake rails:freeze:edge
这个命令可以让rails eage 复制到你的vendor/rails目录里。
第三, Bootstrapping
environment.rb里的下一行是加载config/boot.rb文件的。
# Bootstrap the Rails environment, frameworks, and default configuration
require File.join(File.dirname(__FILE__), 'boot')
虽然这个boot脚本是Rails生成的,但并不意味着你可以编辑它。 它有几方面作用:
首先, 它确保RAILS_ROOT环境变量是被设置好的:它持有当前Rails应用的根路径。
RAILS_ROOT是rails的代码基础, 用以找到那些文件,例如view templates等。
在非windows平台下,有用的Ruby标准库文件的pathname是被用来清除路径字符串里双斜线和无用的点的
Boot脚本第一件事就会检查你vendor下是否有rails
接下来, boot脚本会读取environment.rb文件。
Initializer
boot脚本需要Rails脚本initializer.rb,
Rails, Moudles, 自动加载code
在ruby里,如果你需要包含另一个文件里的代码,你就必须要一句require声明。然而Rails通过一些简单的规则可以让你自动的加载这些code,而不需要写require声明。
那么是如何实现的了?
是这样工作的:如果Rails在代码里碰到一个类或者是module是没有被定义的,那么Rails就会用下面规则去猜它应该是require哪个文件来加载类或module;
1.如果class和module不是嵌套的插入一个下划线在这常量名字之间,就是required的文件名字。例如:
EstimationCalculator 需要require ‘estimation_calculator’
KittTurboBosst 可能需要 ‘kitt_turbo_bosst’
如果class和module是嵌套关系, 例如:
MacGyver::SwissArmyKnife becomes require ‘mac_gyver/swiss_army_knife’
3. Some::ReallyRatherDeeply::NestedClass需要require ‘some/
really_rather_deeply/nested_class’。 如果没有加载,Rails就会乱找符合nested_class.rb的文件。
Configuration
回到environment脚本关于开发者定义的部分。
Rails::Initializer.run do |config|
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
。。。。
end
这里第一步设置是:
Skipping Frameworks
# Skip frameworks you're not going to use. To use Rails without a database
# you must remove the Active Record framework.
# config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
这里可以设置你不需要加载的Rails模块。
Additional Load Paths
# Add additional load paths for your own custom dirs
# config.load_paths += %W( #{RAILS_ROOT}/extras )
你可能需要加载你自定义的目录。
Log-Level Override
# Force all environments to use the same logger level
# (by default production uses :info, the others :debug)
# config.log_level = :debug
定义log级别。
ActiveRecord Session Store
# Use the database for sessions instead of the cookie-based default,
# which shouldn't be used to store highly confidential information
# (create the session table with "rake db:sessions:create")
# config.action_controller.session_store = :active_record_store
Schema Dumper
# Use SQL instead of Active Record's schema dumper when creating the test database.
# This is necessary if your schema can't be completely dumped by the schema dumper,
# like if you have constraints or database-specific column types
# config.active_record.schema_format = :sql
每次你运行测试的时候, rails都会根据schema.rb脚本copy你development环境下的数据库到test数据库里。看上去就像ActiveRecord migration脚本一样,实际上,他们用的是一个API.
Observers
# Activate observers that should always be running
# Please note that observers generated using script/generate observer need to have an _observer suffix
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
Time Zones
# Make Time.zone default to the specified zone, and make Active Record store time values
# in the database in UTC, and return them converted to the specified local zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Comment line to use default local time.
config.time_zone = 'UTC'
internationalization
# The internationalization framework can be changed to have another default locale (standard is :en) or more load paths.
# All files from config/locales/*.rb,yml are added automatically.
# config.i18n.load_path << Dir[File.join(RAILS_ROOT, 'my', 'locales', '*.{rb,yml}')]
# config.i18n.default_locale = :de
Specify gems
# Specify gems that this application depends on.
# They can then be installed with "rake gems:install" on new installations.
# You have to specify the :lib option for libraries, where the Gem name (sqlite3-ruby) differs from the file itself (sqlite3)
# config.gem "bj"
# config.gem "hpricot", :version => '0.6', :source => "[url]http://code.whytheluckystiff.net[/url]"
# config.gem "sqlite3-ruby", :lib => "sqlite3"
# config.gem "aws-s3", :lib => "aws/s3"
Specify plugins
# Only load the plugins named here, in the order given. By default, all plugins
# in vendor/plugins are loaded in alphabetical order.
# :all can be used as a placeholder for all plugins not explicitly named
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
config/environments/development.rb 里,如果config.cache_classes = false, Rails会用load方法来加载文件, 而设置为true的时候,会使用require来加载文件。当你require一个文件的时候,解释器执行并缓存了它。如果这文件再次被require,那么解释器就会忽略这次声明。当你load一个ruby文件的时候,不管你load多少遍,解释器都会重新load它。
rails log文件
rake log:clear # Truncates all *.log files in log/ to zero bytes
命令行里看日志:
$ tail -f log/development.log
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章