Rails 2.0 Preview Release ! 日本Rubyの会 藤岡岳之@Rais東北 (xibbar) 発表 諸橋恭介@Rails東京(moro)フォロー
Sep 11, 2014
Rails 2.0Preview Release !
日本Rubyの会藤岡岳之@Rais東北 (xibbar) 発表諸橋恭介@Rails東京(moro)フォロー
アジェンダ
• 9月30日にRailsのメジャーバージョンアップである2.0のPreview Releaseが行われました
• そのあおり(?)をうけて、昨日1.2.4までリリースされてしまいました
• 2.0リリース文を読んで変更点を説明します。• 注意:私はちょっとしか試していません
概要1
• 2.0がほぼ出来上がった• 正式リリースまではお試し期間が必要• その第一弾が今回のリリース• 試してちょうだい by DHH
Behold, behold, Rails 2.0 is almost here. But before we can slap on the final stamp, we’re going to pass through a couple of trial release phases. The first is this preview release, which allows you to sample the goodies in their almost finished state.
概要2
• おそらく大幅な修正ではなく、少しの修正やコードの追加で2.0は体感できるだろう
• お試し期間の後に、リリース直前版かプレビュー2を出して、その後に最終的にリリースする
• (もちろん問題があったらプレビュー3とかも出す)
We might change a few things or add something else, but by and large, this is how Rails 2.0 is going to look and feel. After this release have had a chance to be tried out, we’re going to move to a release candidate or two (or three, depending on how many we need). Then, the final release.
概要3
• 2.0のリリース前に1.2.4を出す• と思っていたら、昨日リリースされた
• いろんなバグ潰しを含む• 既存アプリを2.0にアップグレードしやすくするためのdeprecation warningが入っているとか
Before the release of 2.0, we’re also going to be putting out 1.2.4, which will include a variety of bug fixes and the last deprecation warnings to get you ready for upgrading an existing application to 2.0 standards.
概要4
• Rails 2.0の何が新しいかをこれから説明
Enough about process. Let me tell you a little bit about what’s new in Rails 2.0:
Action Pack: Resources
• 大幅な機能強化(大分変態的に)• namespaceの導入• rake routesで、動的に生成されるroutesの確認など
• なんだかすごいことになっているぞ
Action Pack: Resources(続) map.resources( :users, :has_many => :friends )
とconfig/routesに書くと
users GET /users {:controller=>"users", :action=>"index"} POST /users {:controller=>"users", :action=>"create"} new_user GET /users/new {:controller=>"users", :action=>"new"} edit_user GET /users/:id/edit {:controller=>"users", :action=>"edit"} user GET /users/:id {:controller=>"users", :action=>"show"} PUT /users/:id {:controller=>"users", :action=>"update"} DELETE /users/:id {:controller=>"users", :action=>"destroy"} user_friends GET /users/:user_id/friends {:controller=>"friends", :action=>"index"} POST /users/:user_id/friends {:controller=>"friends", :action=>"create"} new_user_friend GET /users/:user_id/friends/new {:controller=>"friends", :action=>"new"} edit_user_friend GET /users/:user_id/friends/:id/edit {:controller=>"friends", :action=>"edit"} user_friend GET /users/:user_id/friends/:id {:controller=>"friends", :action=>"show"} PUT /users/:user_id/friends/:id {:controller=>"friends", :action=>"update"} DELETE /users/:user_id/friends/:id {:controller=>"friends", :action=>"destroy"} /:controller/:action/:id ̃
Action Pack: Multiview
• respond_toを拡張• 今まで使っていたshow.rhtmlはshow.html.erbが推奨に
• 3.0でshow.rhtmlがdeprecated• show.csv.erbはtext/csvでCSVを出力
• リクエストを見て、ビュ−を切り替える例
# 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_USER_AGENT"] && request.env["HTTP_USER_AGENT"][/(iPhone|iPod)/] request.format = :iphone end 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
Action Pack: Record identification
• さらに簡素化をすすめて、• URLを扱いやすくするために、コントローラとビューを単純に
• URLにモデルを書けるように規約を追加• 今までは:controller=>:user,:action=>:index• form_to(person)でpersonが既存レコードなら更新(PUT)、新規レコードなら追加(POST)用のform actionを生成
# 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
• Basic認証をサポート(右の例)• javascript_include_tag
(:all,:cache=>true)とするとpublic/javascripts/all.jsに展開
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
Action Pack: Security
• 全てのformとAjaxリクエストにトークンを入れて、CSRF攻撃に対応(これがデフォルト)
• XSSに対応したコードを埋め込みやすくした(詳しくはTextHelper#sanitizeを見てだそうな)
• HTTP only cookiesに対応。(ただし、すべてのブラウザで使えるわけではない。)
Action Pack: Exception handling
• rescue_fromを使ってアクションごとに例外処理を書けるように
class PostsController < ApplicationController rescue_from User::NotAuthorized, :with => :deny_access
protected def deny_access ... end end
Action Pack: Miscellaneous
• atom形式でRSSを生成するのがより簡単になった。(例)
• パフォーマンス面でもたくさんの改良• assetタグの呼び出し• ルーティングのキャッシュ
• in_place_editorとauto_complete_forをRailsの公式リポジトリのプラグインに入れた
# 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
Active Record: Performance
• Queryのキャッシュ• fixtureが50-100%早くなった
Active Record: Sexy migrationscreate_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", :datetimeend
以下のように書けるようになったcreate_table :people do |t| t.integer :account_id t.string :first_name, :last_name, :null => false t.text :description t.timestampsend
Active Record: XML in, JSON out
• XMLやJSON形式でシリアライズとでシリアライズに対応
Person.new.from_xml(“David“) person.to_json
Active Record: Shedding some weight
• ActiveRecordをスリムでわかりやすくするために、acts_as_XYZを将来的に削除して、プラグインに移動(これはRailsのリポジトリとは独立)
./script/plugin install acts_as_list
acts_as_XYZを削除
Active Record: Shedding some weight
• 商用DBのアダプタをgemにして追加• 既に入っているものと名前がバッティングする場合はactiverecord-XYZ-adapterになる
商用DBのアダプタを追加
Active Record: with_scope with a dash of syntactic vinegar
• .with_scopeがpublicではなくprotectedに• モデルの中では使える• コントローラでは使えない• .send(:with_scope)を使ってモデルの外側でも
with_scopeを使うことができる
ActionWebService out, ActiveResource in
• SOAP vs REST• SOAPで統合するのはやりたくない• ActionWebServiceはgemで入れてくれ• ActiveResourceを入れた• ActiveResourceはActiveRecordに似ている
ActiveSupport
• 新機能の全てじゃないけど、一部を説明すると、• Array#rand の追加• Hash#except• Dateクラスにたくさんの拡張• assert_defference
• かっこいい修正、拡張がたくさんある
Action Mailer
• そんなに変更はない• テンプレートエンジンを選べる機能• assert_emailの追加
Rails: The debugger is back
• ruby-debugを入れるとデバッガが使える• サーバを--debuggerまたは-u付きで立ち上げる• そうすると、./script/breakpointerなどを使わなくてもデバッグできる
Rails: Clean up your environment
• inflections.rb (独自の複数形のルールのため)• mime_types.rb (独自のmime typeのため)以上の2つをenvironment.rbに書かずにconfigディレクトリの中の独自ファイルに書ける
Rails: Easier plugin order
• acts_as_listなどがプラグインになっている• 優先的に読み込みたい場合は、environment.rbにconfig.plugins = [ :acts_as_list, :all ]とかく
And hundreds upon hundreds of other improvements
• まだまだ変更点がたくさんあるので、くわしくはCHANGELOGを見てください
おしまい
• 勘弁してくれ!という変更はなさそうだ• basic認証標準搭載はうれしい• ハマるとしたらwith_scopeか• プラグインに行ってしまったものがそこそこある