edge railsでto_dateがprivateになった

edge railsというのは、今マシンにインストールされているrailsのバージョンと異なるバージョンでアプリケーションを開発したい際に使います。
これを使えば、アプリケーション内にrails環境を構築でき、自分のマシンのrailsのバージョンを上げても影響を受けないメリットがあります。
こちら記事等が参考になります。→ Buku nota aku | railsのfreezeとunfreeze

実際にやってみました

rails のバージョンを1.0.0 でかためます。2年前くらいから使っているアプリの開発の引継ぎがあり、環境を併せる必要があったためです。

  • 現在の環境
ruby script/about
About your application's environment
Ruby version                 1.8.6 (i386-mswin32)
RubyGems version             0.9.2
Rails version                1.2.3
Active Record version        1.15.3
Action Pack version          1.13.3
Action Web Service version   1.2.3
Action Mailer version        1.3.3
Active Support version       1.4.2
・・・
  • 引継ぎ元の環境
About your application's environment
Ruby version                 1.8.5 (i686-linux)
RubyGems version             0.9.4
Rails version                1.0.0
Active Record version        1.13.2
Action Pack version          1.11.2
Action Web Service version   1.0.0
Action Mailer version        1.1.5
Active Support version       1.2.5
・・・


引継ぎもとのrailsのバージョンが1.0.0なので、ローカルに落としたアプリケーションを1.0.0で固める。

rake rails:freeze:edge TAG=rel_1-0-0
  • rails 2.x くらいからか、固めるやりかたが変わった(詳しくは、rake -T | grep freeze あたりで)
rake rails:freeze:edge RELEASE=1.0.0

rakeでテストを通したらエラー発生

テスト通してみたら以下のようなエラー発生。

NoMethodError: private method `to_date' called for Wed Apr 30 10:32:08 +0900 2008:Time


おぉ。1.0.0のactive_supportでは、Time#to_dateが実装されていないのかー。一応固めた先のファイルを見てみる。

  • RAILS_ROOT/vender/railsa/ctivesupport/lib/active_support/core_ext/time/conversions.rb
def to_date
  ::Date.new(year, month, day)
end


あるぞ。なんでだ。active_supportにパスが通ってないのか?
script/consoleで確認。

ruby script/console
Loading development environment.
>>$:
・・・・
./script/../config/../config/../vendor/rails/activerecord/lib/../../activesupport/lib


通ってますよ!なんでだヾ(`д´)ノシ
もうしょいいじってみると、同じconversion.rb にあるTime#to_s(format)の拡張は動いている。

ruby script/console
Loading development environment.
>>Time.new.to_s(:db)
=> "2008-05-01 09:29:36"
>>Time.new.to_date
=>private method `to_date' called for Thu May 01 09:30:08 +0900 2008:Time


なんでto_dateだけ動かないの。
そういえば、もともとのTime#to_dateはprivateメソッドだ。

irb(main):001:0> Time.new.to_date
NoMethodError: private method `to_date' called for Thu May 01 09:31:46 +0900 2008:Time


こっちが働いている可能性が高い。

Googleに聞く

あった。


以下を参考に書き換え。

  • time.rb
#-------------------------------
#patch refs http://dev.rubyonrails.org/ticket/7322
require 'date' 
require 'time' 
 
# Ruby 1.8-cvs and 1.9 define private Time#to_date 
class Time 
  %w(to_date to_datetime).each do |method| 
    public method if private_instance_methods.include?(method) 
  end 
end 
#-------------------------------

require File.dirname(__FILE__) + '/time/calculations'
require File.dirname(__FILE__) + '/time/conversions'

class Time#:nodoc:
  include ActiveSupport::CoreExtensions::Time::Calculations
  include ActiveSupport::CoreExtensions::Time::Conversions
end


できたかな。

ruby script/console
Loading development environment.
>> Time.new.to_date
=> #<Date: 4909175/2,0,2299161>


やほほーい丶(´▽`)ノ