そろそろRails4もリリースされるんで
Santiago Pastorino先輩(@spastorino)のRubyConfArgentinaでのスライドを読みました。
Rails 4 in 30'
Rails3系と4系のソースが載せてあって分かりやすいです。
いろいろ良くはなっているようですが、とんでもなく大きな変化ではないように見えます。
個人的には、streamingは実験的で面白い印象です。
主な機能。
STRONG PARAMETERS
MASS ASIGNMENT対策。
# rails 3.2 raises ActiveModel::MassAssignmentSecurity::Error # rails 4.0 railses ActiveMode::ForbiddenAttributesError
# rails 3.2 attr_accessibleで定義 attr_accessible :username, :password # rails 4.0 Usesr.create! params.require(:user). permit(:username, :password) or User.create! user_params private def user_params params.require(:user).permit(:username, :password) end
DECLARATIVE ETAGS
# rails 3.2 fresh_when etag: @todolist # rails 4.0 #fresh_whenに加えて controllerに下記のように書くことができるそうです。 etag { current_user.try :admin } etag { @project.try :cache_key }
fresh_when って知らなかったわ。
CACHE DIGESTS
フラグメントキャッシュをする箇所で似たようなものがたくさんある場合
冗長的だったのですが、簡略化できるようになっているようです。
# rails 3.2 # projects/show.html.erb <% cache [ "v5", project ] do %> <p>All my todo lists:</p> <%= render project.todolists %> <% end %> # todolists/_todolist.html.erb <% cache [ "v5", todolist ] do %> <p><%= todolist.name %>:</p> <%= render todolist.todos %> <% end %> # rails 4.0 # projects/show.html.erb <% cache project do %> <p>All my todo lists:</p> <%= render project.todolists %> <% end %> # todolists/_todolist.html.erb <% cache todolist do %> <p><%= todolist.name %>:</p> <%= render todolist.todos %> <% end %>
ACTIVESUPPORT::QUEUE
Rails.queue.push Job.new job = Rails.queue.pop job.run #config/application.rbで定義 config.queue = ActiveSupport:SyncronousQueue.new config.queue = ActiveSupport:Queue.new
ASYNC MAILERS
書き方は一緒。
ROUTING CONCERNS
# rails 3.2 # config/routes.rb resources :messages do resources :comments end resources :forwards do resources :comments end resources :uploads do resources :comments end # rails 4.0 # config/routes.rb concern :commentable do resources :comments end resources :messages, concerns: :commentable resources :forwards, concerns: :commentable resources :uploads, concerns: :commentable
concernを使うことでより読みやすくなりました。
ACTIONCONTROLLER::LIVE
stream機能が実装されたようです。
Is it live? | Tenderlovemaking
tenderloveさんのこの記事の動画がすごいのですが
サーバのファイルを更新するたびに、streamデータとして送信することが
できるようです。
記事を読むと、Unicornだと30秒でconnectionが切れるらしいです。
Webサーバとしては、Rainbows!, Puma, Thinがいいらしいです。
class MyControlle < ActionController::Base include ActionController::Live def index 100.times { response.stream.write "hi\n" } response.stream.close end end
Security
X-Frame-Options: SAMEORIGIN
などなど
他
memcachedへのアクセスがDalli経由になったりとかいろいろ。