论坛首页 编程语言技术论坛

Rails 2.0 Preview Release

浏览 6073 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-10-04  
原文: http://weblog.rubyonrails.org/2007/9/30/rails-2-0-0-preview-release

Action Pack: Resources
1,RESTful风格改进:
/people/1;edit将变成/people/1/edit

2,添加routing名字空间
map.namespace(:admin) do |admin|
  admin.resources :projects,
  :collection => { :inventory => :get },
  :member => { :duplicate => :post },
  :has_many => { :tags, :images, :variants }
end

这将生成类似inventory_admin_projects_url和admin_products_tags_url的命名routes

3,添加"rake routes"任务,将列出通过routes.rb生成的所有命名routes

4,一个新的convention:所有基于resource的controller都默认为复数形式,这样对不同context下的map都会对应到同一controller:
# /avatars/45 => AvatarsController#show
map.resources :avatars

# /people/5/avatar => AvatarsController#show
map.resources :people, :has_one => :avatar


Action Pack: Multiview
#respond_to得到进一步深入,对multiview使用形如action.format.renderer的模板名,如:
show.erb: 对所有formats使用同一模板
show.html.erb: html格式所使用的模板
index.atom.builder: 使用Builder渲染atom格式
edit.iphone.haml: 使用自定义HAML模板引擎对Mime::IPHONE格式渲染edit action

我们可以声明伪类型来为内部routing使用:
# should go in config/initializers/mime_types.rb
Mime.register_alias "text/html", :iphone

class ApplicationController < ActionController::Base
  before_filter :adjust_format_for_iphone

  private
    def adjust_format_for_iphone
      if request.env["HTTP_USR_AGENT"] && request.env["HTTP_USER_AGENT"][(iPhone|iPod)/]
        request.format = :iphone
    end
end

class PostsController < ApplicationController
  def index
    respond_to do |format|
      format.html # renders index.html.erb
      format.iphone # renders index.iphone.erb
    end
  end
end

我们可以在config/initializers/mime_types.rb文件里声明mime-type

Action Pack: Record identification
资源routes的使用简化
# person is a Person object, which by convention will
# be mapped to person_url for lookup
redirect_to(person)
link_to(person.name, person)
form_for(person)


Action Pack: HTTP Loving
1,HTTP Basic Authentication的简化使用:
class PostsController < ApplicationController
  USER_NAME, PASSWORD = "dhh", "secret"

  before_filter :authenticate, :except => [ :index ]

  def index
    render :text => "Everyone can see me!"
  end

  def edit
    render :text => "I'm only accessible if you know the password"
  end

  private
    def authenticate
      authenticate_or_request_with_http_basic do |user_name, password|
        user_name == USER_NAME && password == PASSWORD
      end
    end
end


2,JavaScript&stylesheet文件缓存
production模式下javascript_include_tag(:all, :cache => true)将把public/javascripts/*.js弄到public/javascripts/all.js里

3,设置ActionController::Base.asset_hot = "assets%d.example.com",则image_tag等asset calls会被自动分发到asset1~asset4

Action Pack: Security
1,预防CRSF攻击:
ActionController::Base.protect_from_forgery

2,预防XSS攻击:
TextHelper#sanitize

3,HTTP only cookies支持

Action Pack: Exception handling
1,rescue_action_in_public
class ApplicationController < ActionController::Base
  def rescue_action_in_public(exception)
    logger.error("rescue_action_in_public executed")
    case exception
    when ActiveRecord::RecordNotFound
      logger.error("404 displayed")
      render(:file => "#{RAILS_ROOT}/public/404.html", :status => "404 Not Found")
    # ...
  end
end


2,rescue_from
class PostsController < ApplicationController
  rescue_from User::NotAuthorized, :with => :deny_access

  protected
    def deny_access
      # ...
    end
end


Action Pack: Miscellaneous
1,AtomFeedHelper
# index.atom.builder:
atom_feed do |feed|
  feed.title("My great blog!")
  feed.updated(@posts.first.created_at)

  for post in @posts
    feed.entry(post) do |entry|
      entry.title(post.title)
      entry.content(post.body, :type => 'html')

      entry.author do |author|
        author.name("DHH")
      end
    end
  end
end


2,asset tag调用的性能提升和简单命名routes的缓存

3,将in_place_editor和autocomplete_for变成插件

Active Record: Performance
Query Cache,N+1查询的性能提升

Active Record: Sexy migrations
# old
create_table :people do |t|
  t.column,   "account_id",   :integer
  t.column,   "first_name",   :string,   :null => false
  t.column,   "last_name",    :string,   :null => false
  t.column,   "description",  :text
  t.column,   "created_at",   :datetime
  t.column,   "updated_at",   :datetime
end

# new
create_table :people do |t|
  t.integer    :account_id
  t.string     :first_name, :last_name, :null => false
  t.text       :description
  t.timestamps
end


Active Record: XML in JSON out
Person.new.from_xml("David")
person.to_json

Active Record: Shedding some weight
1,将acts_as_XYZ移到plugins

2,所有商业数据库adapters移到各自的gems里,Rails仅仅自带MySQL,SQLite和PostgreSQL的adapters
商业数据库adapters的gems命名规范为activerecord-XYZ-adapter,所以可以使用gem install activerecord-oracle-adapter来安装

Active Record: with_scope with a dash of syntactic vinegar
ActiveRecord::Base.with_scope成为protected以防止在controller里误用,因为它是设计来在Model里使用的

Action WebService out, ActiveResource in
在SOAP vs REST的战争里,Rails选择了REST,所以Action WebService被移出为一个gem,而引入的是著名的ActiveResource

ActiveSupport
添加Array#rand方法来从Array里随机得到一个元素
添加Hash#except方法来过滤不想要的keys
Date的一些扩展

Acion Mailer
一些bug fixes以及添加assert_emails测试方法

Rails: The debugger is back
gem install ruby-debug,然后在程序里某处使用"debugger",使用--debugger或-u来启动server即可

Rails: Clean up your environment
以前各种程序的配置细节都扔在config/environment.rb里,现在我们可以在config/initializers里建立不同的文件来配置不同的选项

Rails: Easier plugin order
以前plugins有依赖顺序时我们需要在config.plugins里列出来所有的plugins,现在可以这样config.plugins=[:acts_as_list, :all]

And hundreds uupon hundreds of other improvements
hundreds of bug fixes

So how do I upgrade?
首先升级到Rails 1.2.3,如果没有deprecation warnings,则可以升级到Rails 2.0
即将发布的Rails 1.2.4还会添加一些deprecation warnings

Thanks to everyone who’ve been involved with the development of Rails 2.0. We’ve been working on this for more than
six months and it’s great finally to be able to share it with a larger audience. Enjoy!
   发表时间:2007-10-04  
谢谢,一些地方的简化是一直期待的。
还有route.rb 有了rake应该更容易看懂了- -
0 请登录后投票
   发表时间:2007-10-05  
?这篇文章怎么发了两遍
0 请登录后投票
   发表时间:2007-12-10  
blackanger 写道
?这篇文章怎么发了两遍
应该是激动的
恩 还是看中文的舒服
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics