Ruby on Rails 4.1 Release Notes — Ruby on Rails Guides
2章は、機能紹介です。
Rails 4.1 の新機能 - rochefort's blog で書いたときより理解が深まりました。
2 Major Features
2.1 Spring Application Preloader
backgroundでRailsを起動してくれるSpringについてです。
bin/rails、 bin/rake は自動でspringをpreloadするようになります。
利用例:
bin/rake test:models bin/rails console $ bin/spring status Spring is running: 1182 spring server | my_app | started 29 mins ago 3656 spring app | my_app | started 23 secs ago | test mode 3746 spring app | my_app | started 10 secs ago | development mode
・ See Also
spring/README.md at master · rails/spring
2.2 config/secrets.yml
前回 でも触れましたが
config/initializers/secret_token.rb secret_key_base が外出しになりました。
Rails.application.secrets で取得/変更もできます。
2.3 Action Pack Variants
これもRails 4.1 の新機能で紹介していますが、一応おさらいしておきます。
HTML/JSON/XML templatesをtablet、phone、desktop向けなどに用意できます。
before_actionで定義。通常この手のものは、ApplicationController で実装でしょうか。
request.variant = :tablet if request.user_agent =~ /iPad/
respond_to。blockも書けたのか、知らなかった。
respond_to do |format| format.html do |html| html.tablet # renders app/views/projects/show.html+tablet.erb html.phone { extra_setup; render ... } end end
template fileはこんな感じになります。
app/views/projects/show.html.erb app/views/projects/show.html+tablet.erb app/views/projects/show.html+phone.erb
個別にrenderも書けます。
respond_to do |format| format.js { render "trash" } format.html.phone { redirect_to progress_path } format.html.none { render "trash" } end
2.4 Action Mailer Previews
これ、最初よく分かんなかったのですが
開発向けのMailのPreview機能のようです。
下記のように書くとPreviewできるそうです。
test/mailers/previews
class NotifierPreview < ActionMailer::Preview def welcome Notifier.welcome(User.first) end end
http://localhost:3000/rails/mailers でリストアップできて、
http://localhost:3000/rails/mailers/notifier/welcome でPreviewできると。
これは便利そうなので、後で試してみましょう。
※1/15試してみました
Rails4.1の新機能Action Mailer Previewsとは - rochefort's blog
2.5 Active Record enums
これもRails 4.1 の新機能で紹介済みなので、軽く流します。
class Conversation < ActiveRecord::Base enum status: [ :active, :archived ] end conversation.archived! conversation.active? # => false conversation.status # => "archived" Conversation.archived # => Relation for all archived Conversations
Conversation.archived という書き方ができるんですね。
これはキャッチアップできていなかった。
2.6 Message Verifiers
メール認証などで用いられるtoken付きURLからアクセスするような場合の話ですね。
sensitive_data というverifier nameでの例です。
message = Rails.application.message_verifier('sensitive_data').generate('my sensible data') Rails.application.message_verifier('sensitive_data').verify(message)
2.7 Module#concerning
extend ActiveSupport::Concern を使ったmixinの書き方が簡潔に書けるようになったようです。
(この辺り、詳しくないので合ってるか不安)
class Todo < ActiveRecord::Base concerning :EventTracking do included do has_many :events end def latest_event ... end private def some_internal_method ... end end end
2.8 CSRF protection from remote script tags
JavaScriptのGET RequestもCSRFの対象となっています。
xhr でアクセスすればOKです。
See Also
Ruby on Rails 4.1 Release Notes
・#1(Rails4.1へのupgrade方法)
・#3(Railties)
・#4(Action Pack)
・#5(Action Mailer)
・#6(Active Record)
・#7(Active Model)
・#8(Active Support)