techium

このブログは何かに追われないと頑張れない人たちが週一更新をノルマに技術情報を発信するブログです。もし何か調査して欲しい内容がありましたら、@kobashinG or @muchiki0226 までいただけますと気が向いたら調査するかもしれません。

Rails 5.0 RC1が出たと聞いたのでインストールしてみた

こんにちは。RoR永遠の初心者seitです。
Railsに5.0 RC1が出たというのをチラッと目にしたので、インストールして少しさわってみました。

f:id:uentseit:20160617015855p:plain

インストール

まずはインストールします。せっかくなので、前回Chefで作ったVagrant環境にインストールします。
と言っても以下のコマンド一発であっさりインストール完了です。

# gem install rails --version=5.0.0.rc1
# rails -v
Rails 5.0.0.rc1

ただし、Rubyのバージョンが2.2以上でないといけないようなので、注意が必要です。

アクセスしてみると、トップページが変わっていました。 f:id:uentseit:20160617015855p:plain Yay! I'm on Rails!

RakeとRailsコマンドの統合

Rails 5.0 RC1の新機能を調べていて地味に気になったのは、RakeのRailsコマンドへの統合です。
今まで結構めんどくさかったというか間違えて打ち込むことがあったのでうれしいです。
よく使うコマンドroutesを試してみると、

# rake routes
   Prefix Verb   URI Pattern               Controller#Action
    users GET    /users(.:format)          users#index
          POST   /users(.:format)          users#create
 new_user GET    /users/new(.:format)      users#new
edit_user GET    /users/:id/edit(.:format) users#edit
     user GET    /users/:id(.:format)      users#show
          PATCH  /users/:id(.:format)      users#update
          PUT    /users/:id(.:format)      users#update
          DELETE /users/:id(.:format)      users#destroy
     root GET    /                         users#index
# rails routes
   Prefix Verb   URI Pattern               Controller#Action
    users GET    /users(.:format)          users#index
          POST   /users(.:format)          users#create
 new_user GET    /users/new(.:format)      users#new
edit_user GET    /users/:id/edit(.:format) users#edit
     user GET    /users/:id(.:format)      users#show
          PATCH  /users/:id(.:format)      users#update
          PUT    /users/:id(.:format)      users#update
          DELETE /users/:id(.:format)      users#destroy
     root GET    /                         users#index

おぉッ確かに同じ結果が返ります。
Rakeコマンドの一覧が見たければ、これまでrake -Tとしていたところを、rails -Tでも確認ができます。
当然、railsコマンドはこれまでと変わらず利用できています。

ちなみにGithubのソースコードを見てみると、commands_tasks.rbファイルに以下のような記述がありました。

    ・・・略
    COMMAND_WHITELIST = %w(plugin generate destroy console server dbconsole runner new version help test)
    
    ・・・略・・・

    def run_command!(command)
      command = parse_command(command)

      if COMMAND_WHITELIST.include?(command)
        send(command)
      else
        run_rake_task(command)
      end
    end
    ・・・略・・・

rails関連のコマンド以外はrun_rake_taskメソッドを実行するような記述になっています。 4.2では以下のようにエラーメッセージを出すだけでした。

    def run_command!(command)
      command = parse_command(command)
      if COMMAND_WHITELIST.include?(command)
        send(command)
      else
        write_error_message(command)
      end
    end

また、/rails-master/railties/lib/rails/commands/ディレクトリにrake_proxy.rbファイル追加されていて、中にRakeProxyモジュールが定義されています。

require 'rake'
require 'active_support'

module Rails
  module RakeProxy #:nodoc:
    private
      def run_rake_task(command)
        ARGV.unshift(command) # Prepend the command, so Rake knows how to run it.

        Rake.application.standard_exception_handling do
          Rake.application.init('rails')
          Rake.application.load_rakefile
          Rake.application.top_level
        end
      end

   ・・・略・・・
  end
end

ここにrun_rake_taskメソッドが定義されていて、Rakeコマンドが実行されているっぽいです。

[/ruby-2.3.1/gems/rake-10.4.2/lib/rake/application.rb]

module Rake

  CommandLineOptionError = Class.new(StandardError)

  class Application

  ・・・略
    # Run the top level tasks of a Rake application.
    def top_level
      run_with_threads do
        if options.show_tasks
          display_tasks_and_comments
        elsif options.show_prereqs
          display_prerequisites
        else
          top_level_tasks.each { |task_name| invoke_task(task_name) }
        end
      end
    end
  ・・・略・・・
  end

他にもRailsでWeb Socketを利用するためのAction Cableというフレームワークが追加されているようなので、また試してみたいと思います。 久しぶりにRailsと遊んでみよう。