Stop Smoking 6

Posted by yudi
on Tuesday, January 22

Years ago, when he’s young, He got infected with a virus, which unfortunately reached his heart, and spread to his joints. Over years he’s been suffered and suffered. Couple of years ago an unexpected heart attack almost took his life when he’s on his way delivering tons of industrial materials. Last year, he finally scheduled his heart surgery, and lost his job. He hoped to be healthy than anyone else in the world…

......

Nothing has to change except for two things:
  • I became healthier.
  • The dog has less chance to hang out and pee. (And I’m not sorry for that)

Edge Rails 的更新:为 environment.rb 前部加载“挂钩” 0

Posted by yudi
on Monday, January 07

如果您一直在寻求由 environment.rb 读取前对 Rails 程序进行设置的方式,今天有 答案 了。

如果一个 config/preinitializer.rb 的文件存在,Rails 将在读取 environment.rb 文件之前先加载这个文件。通过这个方法使您在加载插件之前可以使程序读取 Rails 的类代码。

原文作者是 Ryan Daigle, 请访问他的博客
本片译文的原文地址:Track

Edge Rails 的更新:filters 的更新 1

Posted by yudi
on Monday, January 07

Controller 里刚刚 被更新filters 功能 也许会为您现存的代码带来一点变动。 现在,false 输出将不再停止当前运行的进程,真正停止进程的主角换为了 rendering 或 redirecting。

如果您深入的去理解,通过在 filter 里的 render/redirect 来停止一个进程更加符合逻辑,因为它们在每个进程里只能被使用一次,这样更加能够指明这个进程将不再需要调用其他资源。

...

Edge Rails 的更新:Rails Validation 现在 允许_为空

Posted by yudi
on Saturday, January 05

这是一个很小的更新,但很值得一提。种类繁多的 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。

原文作者是 Ryan Daigle, 请访问他的博客
本片译文的原文地址:Track

Edge Rails 的更新:设置插件加载顺序

Posted by yudi
on Saturday, January 05

无论在任何时候,您都可以准确设置启动 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 ) 
...

Edge Rails 的更新:Exception Handling 更新

Posted by yudi
on Saturday, January 05

根据不同的 application exception 提供不同的解决方式(比如 redirect user 或 render 特定的页面 )是很常见的设计模式。在这个 更新系列 之前我们经常需要在 controller 里超量加载 rescue_action_in_public method:

1
2
3
4
5
6
7
8
9
class PostsController < ApplicationController
  def rescue_action_in_public(exception)
    case(exception)
      when ActiveRecord::RecordNotFound then render :file => '/bad_record'
      when NoMethodError then render :file => '/no_method'
      else render :file => '/error'
    end
  end
end

很容易发现这段代码立即被扩展成了多组乱了套的 if/else 声明 ...

Ruby off Rails

Posted by yudi
on Saturday, January 05

Right, you love Ruby, you love the syntax and rake.

But for some reason you hate hate being forced to MVC structure and want to apply different patterns or no pattern at all..

Here is a list of off-Rails framework that you can try out: (I read it from the rails-mailing-list)

It’s not that they are better than Rails, rather they are more or less connected to Rails and have various personalities. Camping is 4k and has a metaphor of a little white blood cell in the vein of Rails , while merb declares ORM-agnostic, JavaScript library agnostic, and template language agnostic .

If falling in love with Ruby was like seeing some girl from China, then Camping is a girl from BeiJing, and merb is from Shanghai. ( I love them all.. don’t you? )

Anyway, I keep this reference as a list of framworks to myself.

A sip of better doc

Posted by yudi
on Friday, January 04

I still remember the sweet time that we had open book finals for a couple of basic computing classes (100 level). As the professor said, as a programmer we all keep a handy book while writing code in order to look up.

For ruby, we got ri, but there is a lot faster and much more intelligent tool called fast-ri. I happen to read it in the textmate changlog today. After a little digging around, here is what I find:

Start by install it with the command:


sudo gem install fastri

Then build index with:


sudo fastri-server -b

...

Edge Rails 的更新:网站签名防护的改进

Posted by yudi
on Monday, December 31

今日讨论 Rails 安全性的话题越来越热,因此 Rails 将 CsrfKiller 插件加入了核心部分。

CsrfKiller 插件为所有的非GET1请求 中的form 添加了一个独特的session token 一栏。这一栏内容确保了接收到的请求必须是来自于 授权用户的 session2

现在轮到默认功能了。如果您只想应用默认的行为则不必设置任何东西。然而您仍然可以通过以下示例针对自己的设定来进行调整:

1
2
3
4
5
class PostsController < ApplicationController

  protect_from_forgery :secret => '2kdjnaLI8', :only => [:update, :delete, :create]
  ...
end

...

Edge Rails 的更新:日志记录变利索点儿了

Posted by yudi
on Thursday, December 27

Edge Rails 刚刚发布了一个使用缓存的日志记录器。看起来,它可以把 Rails 的日志整理的更加利索…

Edge Rails 的更新: 新的 Rails 数据库接口

Posted by yudi
on Thursday, December 27

如果您的 Edge Rails 程序不支持 MySQL, PostgreSQL 或 SQLite 以外的任何数据库,那么您将需要确保在本机上安装了正确的gem。正如现在,Rails 核心开发人员在近期制作了一系列新的 Rails 数据库接口,并以gem发布。这一系列中的任何一个都应当解决您一直以来的数据库接口问题…

Edge Rails 的更新: Partial 拥有了 Layout

Posted by yudi
on Thursday, December 27

如果您时常将不同 Rails views 中的重复代码放到 partials里,也许您也会察觉到我们还可以针对那些partials继续 DRY[1]。于是现在我们又有了一个新的工具来帮助 partials 成为 view-世界里的第一等公民: partial layout 。这个工具的使用正如同我们为 render call 中添加一个 :layout 选项般简单…

Edge Rails 的更新: 使用 rake 查看路由列表

Posted by yudi
on Thursday, December 27

在需要的时候,您可以亲自查看程序中的路由是如何被定义的。现在,rake task扩展了新的功能,使路由的显示变得更加直观和方便。

只要运行:


rake routes

瞧,所有被定义的路由都会被整齐的摆进下面顺序编排的列表:

路由名称, HTTP method,路由路径,路由要求...

Edge Rails 的更新: Fixtures 堆积

Posted by yudi
on Wednesday, December 26

如果您已经厌倦了建立 Fixtures 堆积时不断的引用 fixture method 的话,今天是您的幸运日!

1
2
3
fixtures :states
..
assert_equal states(:nc, :va), State.find(:all)

Edge Rails 的更新: 不再使用 (传统的) 分页

Posted by yudi
on Wednesday, December 26

如果您一直以来一直因为无法掌握分页功能而苦恼,这对您来说也许是个好消息 :-) 在未来的版本里,分页功能将从Rails核心被分离出去。相对的,我们将需要使用 经典的分页插件 。那么,现在哪个插件最适合您的需求呢?当然是 will_paginate

原文作者是 Ryan Daigle, 请访问他的博客
本片译文的原文地址:http://ryandaigle.com/articles/2007/6/11/what-s-new-in-edge-rails-no-more-conventional-pagination