• 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

git でファイルの復活

  • コミット後に勢いよくリファクタリングするも途中で挫折
  • p なデバッグコードを埋め込んでいくも修正箇所を忘れた

なんてとき、指定した一部のファイルだけをコミット後の綺麗な状態に戻したいときがある。

svn の場合

svn は楽だった。消して update するだけでよかったから。 具体的には、モデルを全部戻したくなった場合は

% rm -rf app/models
% svn update

で楽に復旧できる。

git の場合

でも、git では fetch しても削除ファイルは華麗にスルーされて困っていた。 git は超便利だが、ここの一点で svn の方が使いやすいと思っていた。 が、checkout が所望のコマンドだと知った。 (Rails勉強会++)

% rm -rf app/models
% ls app/models
ls: cannot access app/models: No such file or directory
% git checkout app/models
% ls app/models
user.rb

あぁ、もうgitでいいです。 クゥ~ン♪

posted by Png maiha on Thu 28 Aug 2008 at 05:47 with 2 comments

   1  class Parts < ActiveRecord::Base # STI
   2  end
   3  
   4  class Parts::Image < Parts
   5  end
   6  
   7  part = Parts::Image.create!
   8  p part[:type]

で何が返るのかの話。

before 2.1

   1  part[:type]  # => "Image"

after 2.1

   1  part[:type]  # => "Parts::Image"

互換性

2.1 では AR::Base.sti_name メソッドが返す値を利用しており、 AR::Base.store_full_sti_class で制御できる。

   1  Parts::Image.store_full_sti_class  # => true
   2  Parts::Image.sti_name              # => "Parts::Image"
   3  Parts::Image.create[:type]         # => "Parts::Image"
   4  
   5  Parts::Image.store_full_sti_class = false
   6  Parts::Image.sti_name              # => "Image"
   7  Parts::Image.create[:type]         # => "Image"

応用

2.1 のSTIを利用するとナベアツな世界を簡単に作成できる。

   1  class Person < ActiveRecord::Base; end
   2  class Nabe < Person; end
   3  class Aho < Person; end
   4  def Person.sti_name
   5    Time.now.day % 3 == 0 ? "Aho" : "Nabe"
   6  end
   7  
   8  (1..3).map{ Nabe.create[:type] }
   9  => ["Nabe", "Nabe", "Nabe"]
  10  
  11  # Stub
  12  class Time
  13    def day
  14      3
  15    end
  16  end
  17  
  18  (1..3).map{ Nabe.create[:type] }
  19  => ["Aho", "Aho", "Aho"]

posted by Png maiha on Fri 8 Aug 2008 at 13:35

あるフィルタが定義されているかどうかを知りたいとき、 Controller.find_filter は非常に便利で 1.x 時代から一部で重宝されていたが、 このたび Rails2.1 ではなくなってしまった。 これは obsoleted というより内部のクラス構成が変わってしまったことに起因する。

before 2.1

   1  >> ApplicationController.filter_chain.class
   2  => Array

after 2.1

   1  >> ApplicationController.filter_chain.class
   2  => ActionController::Filters::FilterChain

Controller.filter_chain は定義されたフィルタが格納されたオブジェクトであるが、これが 2.1 からは専用のFilterChainクラスに進化した。 これにより、以下のような数々のフィルタ操作メソッド

  • find_filter
  • append_filter_to_chain
  • prepend_filter_to_chain
  • ...

を直接コントローラに定義する(2.1以前)のではなく、 同クラスに持たせることができるようになった。 これは、名前空間的にも責務の明瞭化としても進化と言える。

2.1 でのフィルタの検索

従って、今後は Controller クラスに直接問い合わせるのでなく、 フィルタを管理する FilterChain インスタンスに尋ねることになる。

   1  >> ApplicationController.filter_chain.find(:login_required)
   2  => #<ActionController::Filters::BeforeFilter:0x2621914 ...

互換性

しかしながら、未だに1.2ユーザも多いプラグインなどでは後方互換性が必要である。 色んな対応策が考えられるが、 ここでは、2.1 側に涙を飲んでもらって、 せっかく奇麗にした所を悪いが find_filter を追加させてもらう。

   1  ActionController::Base.class_eval do
   2    unless respond_to?(:find_filter)
   3      def self.find_filter(name)
   4        filter_chain.find(name)
   5      end
   6    end

(vendor/plugins/xxx/init.rb)

posted by Png maiha on Thu 7 Aug 2008 at 10:49
Contents
優しいgitの育て方 : svn update
Rails2.1 の STI
Rails2.1 で find_filter
Comments
maiha: 追記)対応は約1営業日でした。チームの忙しさに応じて変動するとは思いますが、大体の目安情報とい... '10-4
maiha: なるほど。rails のログから実行したクエリを抜き出したいと思うことがよくありまして。 あ... '10-3
winebarrel: レポートありがとうございます。 削除できない件は修正しました。 SQLのparserはTC/... '10-2
maiha: なるほど。ありがとうございます! '10-2
ursm: Haml 2.2 以降はどこでも式展開が使えるようになったので、== は意味がなくなりました。... '10-2
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ