如果您一直在寻求由 environment.rb 读取前对 Rails 程序进行设置的方式,今天有 答案 了。
如果一个 config/preinitializer.rb 的文件存在,Rails 将在读取 environment.rb 文件之前先加载这个文件。通过这个方法使您在加载插件之前可以使程序读取 Rails 的类代码。
Controller 里刚刚 被更新 的 filters 功能 也许会为您现存的代码带来一点变动。 现在,false 输出将不再停止当前运行的进程,真正停止进程的主角换为了 rendering 或 redirecting。
如果您深入的去理解,通过在 filter 里的 render/redirect 来停止一个进程更加符合逻辑,因为它们在每个进程里只能被使用一次,这样更加能够指明这个进程将不再需要调用其他资源。
...
现在有众多 的 fixture插件 尝试着使您的 test fixtures 变得更加简单。而大多数插件都通过不同的方式达到了目的。
它们之间最好最特别的 fixture 插件 Rathole “整合进了 rails 之中”http://dev.rubyonrails.org/changeset/8036 。让我们来总结一下它的功能,以便您来使用:
让我们为示例建立下面的 model:
1 2 |
class Company < ActiveRecord::Base; has_many :employees; end class Employee < ActiveRecord::Base; belongs_to :company end |
这是一个很小的更新,但很值得一提。种类繁多的 activerecord validation method 现在接受 :allow_blank 选项。通过 :allow_blank 当值为 nil 或空白的 string 时,validation 也会通过。
1 2 3 4 5 6 |
class Post < ActiveRecord::Base validates_length_of :meta, :maximum => 3, :allow_blank => true end p = Post.new(:meta => "") p.valid? #=> true |
不是很重要想法,不过我更加倾向于 :allow_nil。
无论在任何时候,您都可以准确设置启动 rails 程序所需要的插件,而不是通过存在于 environment.rb 里的这段代码来痛苦的加载 vender/plugins 目录中的所有插件:
1 2 3 |
# Only load the plugins named here, by default all plugins in vendor/plugins are loaded # 只加载这里列出的插件,默认时加载 vendor/plugins 中的所有插件。(默认时下面代码被设定为 comment) config.plugins = %W( exception_notification ssl_requirement ) |