This article was migrated from http://rails.office.drecom.jp/takiuchi/archive/101

Ruby on Railsでは、コントローラの挙動をテストするためにFunctionalテストという仕組みがあります。 実際にページを取得し、レンダリング結果のタグを解析したり、リダイレクト先が正しいかどうか、指定したIDのタグが出力されているかどうか、などなど、様々なテストを行う事ができます。

そこで、ログインした状態でないとアクセスできないようなページのテストを記述するために、Cookieやセッションを使ってテストを記述する必要が出てきます。しかし、こちら(Rails - Functional Test with Cookie)で報告されているように、FunctionalテストでCookieを使用する方法には若干癖があり、ドキュメントも少ないようなので、使い方をまとめておきます。

Cookieを設定する場合は、@requestを通して以下のように行います。

@request.cookies['name'] = CGI::Cookie.new('name', value)
ポイントは、キー(name)としてシンボルを使わない事です。コントローラ内で参照する側では、シンボルを使って参照しても問題ない(to_sされている)のですが、Cookieを設定するテストコード側では、キーを文字列で指定する必要があります。 'name'を二度指定しなければならず、若干かっこ悪い感じですね。

ちなみに、コントローラ内から参照されるcookiesは、ActionController::CookieJarクラスのインスタンス変数です。 ところが、@request.cookiesはただのHashなので、その辺のミスマッチを埋めるためにこのような指定を行う必要が出てきます。

This article was migrated from http://rails.office.drecom.jp/takiuchi/archive/101

posted by Png genki on Mon 18 Sep 2006 at 05:17

This article was migrated from http://rails.office.drecom.jp/hibi/archive/30

http://www.ruby-lang.org/ja/man/?cmd=view;name=String#intern

p "foo".intern
=> :foo

備忘録に。
へーーー
便利だなぁ、rubyは。 http://rails.office.drecom.jp/hibi/archive/30

This article was migrated from http://rails.office.drecom.jp/hibi/archive/30

posted by Png satoko on Fri 15 Sep 2006 at 03:54

This article was migrated from http://rails.office.drecom.jp/hibi/archive/29

備忘録。これは便利!
Rubyレシピブック 268の技のレシピNo.111です。

array = [1, 2, 3]
str = array.join(",")
p str        #"1, 2, 3"


http://rails.office.drecom.jp/hibi/archive/29

This article was migrated from http://rails.office.drecom.jp/hibi/archive/29

posted by Png satoko on Thu 14 Sep 2006 at 14:51

This article was migrated from http://rails.office.drecom.jp/hibi/archive/28

ActiveRecord::Baseにfind_or_create_by_カラム名というのがあるらしい。

探して、なかったら作るという便利なもの。
AWDRにも載ってないみたいだし。

ノグはすごいねー http://rails.office.drecom.jp/hibi/archive/28

This article was migrated from http://rails.office.drecom.jp/hibi/archive/28

posted by Png satoko on Thu 14 Sep 2006 at 12:46

This article was migrated from http://rails.office.drecom.jp/hibi/archive/27

家のネットワークでは、DHCPを利用していたのですが、
Windows->DebianにアクセスするのにIPアドレスが毎回変わるため、
windowsのhostsを変更したりととても面倒。
瀧内さんに相談したところ、固定できますよという話!


DebianはNATを使用。

下記のファイルを編集。
#/etc/network/interfaces
--------------------------------------------------
# The primary network interface
auto eth0
#iface eth0 inet dhcp
iface eth0 inet static
address 192.168.1.20
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255 #ここは255を使うのが定石
gateway 192.168.1.1


#/etc/init.d/networking restart
で設定更新!


いや〜楽チンです。
これで一つバッドノウハウが減った。
瀧内さん、感謝です♪ http://rails.office.drecom.jp/hibi/archive/27

This article was migrated from http://rails.office.drecom.jp/hibi/archive/27

posted by Png satoko on Thu 14 Sep 2006 at 11:26

This article was migrated from http://rails.office.drecom.jp/takiuchi/archive/100

IE7でJuggernautを使ったときにエラーが出るようなので、原因を調べてみました。

その結果、IE7では、script、embed、objectなどの特定のDOMエレメントに対して、innerHTMLに値を設定しようとするとエラーになるようです。

   1  document.createElement('script').innerHTML = 'alert(1)'; // エラー!

いまのところ、以下のように別なDOMエレメントのinnerHTMLにHTMLとして 埋め込む分には大丈夫なようです。

   1  var span = document.createElement('span');
   2  span.innerHTML = '<script type="text/javascript">alert(1)</script>';
   3  document.body.appendChild(span);
どんな意図があっての仕様変更なんだろう?

  <a id="BlogEntryExtend"></a>
  <span id="extended-text" /><p>

ともあれ、IE7に対応するようにJuggernautのJavaScriptを書き換えてみました。

   1  ---- code of public/javascripts/juggernaut.js ----
   2   
   3    _appendIECommandHandler: function() {
   4      var scriptbox = document.createElement('div');
   5      scriptbox.style.display = 'none';
   6      scriptbox.innerHTML = [
   7        '&lt;script language="VBScript"&gt;',
   8        '  on error resume next',
   9        '  Sub juggernaut_flash_FSCommand(ByVal command, ByVal args)',
  10        '   call myFlash_DoFSCommand(command, args)',
  11        '  end sub',
  12        '&lt;/script&gt;'].join("\n");
  13      document.body.appendChild(scriptbox);
  14    },
  15   
  16    _appendFlashObject: function() {
  17      var host = Juggernaut.host;
  18      var port = Juggernaut.port;
  19      var data = Juggernaut.data;
  20      var flashbox = document.createElement('div');
  21      flashbox.style.display = 'none';
  22      flashbox.innerHTML = [
  23        '&lt;object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ',
  24          'codebase="http://download.macromedia.com/pub/shockwave/cabs/',
  25          'flash/swflash.cab#version=5,0,0,0" id="juggernaut_flash"&gt;',
  26        '&lt;param name="movie" value="/juggernaut.swf?host=', host,
  27          '&port=', port, '&juggernaut_data=', data, '"&gt;',
  28        '&lt;param name="quality" value="high"&gt;',
  29        '&lt;embed src="/juggernaut.swf?host=', host, '&port=', port,
  30          '&juggernaut_data=', data + '" quality="high" ',
  31          'name="juggernaut_flash" swLiveConnect="true" ',
  32          'type="application/x-shockwave-flash" ',
  33          'pluginspage="http://www.macromedia.com/go/getflashplayer"&gt;',
  34        '&lt;/embed&gt;'].join('');
  35      document.body.appendChild(flashbox);
  36     },

作者のAlex氏にはパッチを送っておきました。

This article was migrated from http://rails.office.drecom.jp/takiuchi/archive/100

posted by Png genki on Wed 13 Sep 2006 at 20:26

This article was migrated from http://rails.office.drecom.jp/hibi/archive/26

あふれたバッファを見るには下記。
瀧内さんに教えてもらった。
おーーすごい便利。ヤッター!
いやー小さい工夫の積み重ねって大事だね。
(Ctrl-Tは人によって違うかも。Ctrl-Zとかなのかな。。。)


Ctrl-t Ctrl-b:1画面?スクロール上へ
Ctrl-f :1画面?スクロール下へ


あとscreenでUTF8なファイルを見るには、-Uオプションで立ち上げるのは有名な話らしい。
そりゃそうだ、UTF8オプションだもの!


screen -U


がんばってバージョンアップしなくちゃだなぁ。 http://rails.office.drecom.jp/hibi/archive/26

This article was migrated from http://rails.office.drecom.jp/hibi/archive/26

posted by Png satoko on Wed 13 Sep 2006 at 14:35

This article was migrated from http://rails.office.drecom.jp/hibi/archive/25

# apt-get install imagemagick librmagick-ruby1.8

すんなりOK。
次、RMagic

# gem install RMagick
Bulk updating Gem source index for: http://gems.rubyforge.org
Building native extensions. This could take a while...
configure: error: Can't install RMagick. Can't find Magick-config or GraphicsMagick-config program.

すんなりNG
先にインストールしたやつをremove
apt-get remove imagemagick
apt-get remove librmagick-ruby1.8

というわけでぐぐる。
http://pub.cozmixng.org/~the-rwiki/rw-cgi.rb?cmd=view;name=RMagick

ImageMagick
#wget http://jaist.dl.sourceforge.net/sourceforge/imagemagick/ImageMagick-6.2.9-4.tar.gz
#tar xvfz ImageMagick-6.2.9-4.tar.gz
#cd ImageMagick-6.2.9
#./configure --enable-shared
#make
#make install

RMagick
ちょっと持ち越し。。。
wget http://rubyforge.org/frs/download.php/11314/rmagick-1.13.0.gem
gem install rmagick-1.13.0.gem


#wget http://rubyforge.org/frs/download.php/11316/RMagick-1.13.0.tar.gz
#./configure --enable-shared
http://rails.office.drecom.jp/hibi/archive/25

This article was migrated from http://rails.office.drecom.jp/hibi/archive/25

posted by Png satoko on Wed 13 Sep 2006 at 03:28

This article was migrated from http://rails.office.drecom.jp/hibi/archive/24

http://d.hatena.ne.jp/naoya/20051118/1132279240

ランダムにレコードを取り出したいときに使うといいらしい。


select id from entry order by rand() limit1;


のですが、データ量がたくさんあるテーブルだと遅くなる罠があるので要注意。
ということで、今回使えるかと思ったのだけど無理っぽそうです。
flickrとかどうやってるのかなぁ。 http://rails.office.drecom.jp/hibi/archive/24

This article was migrated from http://rails.office.drecom.jp/hibi/archive/24

posted by Png satoko on Wed 13 Sep 2006 at 00:43

This article was migrated from http://rails.office.drecom.jp/takiuchi/archive/99

yuguiさんのhas_one :throughはABD的にRailsを使うのには欠かせない存在です。 今回は、オリジナルのhas_one :throughを拡張して、Unsaved objectも扱えるようにしてみました。

class ActiveRecord::Associations::HasOneThroughAssociation <
  ActiveRecord::Associations::HasOneAssociation
private
  〜 略 〜
  def load_target
    if !loaded? && @owner.new_record?
      table_name = @reflection.through_reflection.table_name
      @target = @owner.send(table_name.singularize).send(@reflection.name)
    end
    super
  end
end
上記のコードをオリジナルのコードに追加すると、Unsaved objectに対しても、アソシエーションを参照することができるようになります。
member = Member.new
member.build_membership(:group => Group.new)
member.group
というわけで、yuguiさんお願いしますー。

This article was migrated from http://rails.office.drecom.jp/takiuchi/archive/99

posted by Png genki on Mon 11 Sep 2006 at 23:31
Contents
FunctionalテストでCookieを使う方法
シンボル(:user)の動的生成 
配列を「,」で連結し文字列に 
User.find_or_create_by_id 
IPを固定にする 
IE7のinnerHTMLに関する仕様変更
screenでバッファ履歴を見る:Ctrl-t [ 
RMagick 
MySQL の order by rand() 
has_one :throughでUnsaved Objectを扱う
Comments
瀧内元気: MacOS版は以下にあります * [genki/ViMouse](https://githu... '23-1
KingofSmack: Here also good reads for this mobile applicatio... '14-5
Spencer: You don't have to re-compile it, this version w... '14-4
staiano: Any chance we can get a recompile for 10.9? '14-1
dsjf: https://gist.github.com/6bf1bf2c3cbb5eb6e7a7 これ... '13-1
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ