blog.sowatchasayin

Rails etc.

Rails 2.x.xでException Notifierが動かない時

Rails2.3.5アプリにException Notifierを入れました。

$ script/plugin install http://github.com/rails/exception_notification

諸々設定して起動したのですがこんなエラーが。
/var/lib/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:105:in `const_missing': uninitialized constant ApplicationController::ExceptionNotifiable (NameError)



調べてみると、Exception NotifierがRails 3に移行している影響みたいです。
Missing ExceptionNotifiable module with exception_notification plugin

なので、3系対応開始前のコミットを使えとのこと。特定のコミットを使うにはscript/pluginじゃないでgit submoduleを使います。
Git clone vs Git submodule
これで一つのリポジトリの中に他のリポジトリが持てます(言い方合ってますかね)。

素直にやってみます。

$ git submodule add git://github.com/rails/exception_notification.git vendor/plugins/exception_notification
$ cd vendor/plugins/exception_notification
$ git checkout -b rails2 e8b603e523c14f145da7b3a1729f5cc06eba2dd1
$ script/server -e production
=> Booting WEBrick
=> Rails 2.3.5 application starting on http://0.0.0.0:3000
** Erubis 2.6.5
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2010-01-29 17:56:33] INFO WEBrick 1.3.1
[2010-01-29 17:56:33] INFO ruby 1.8.7 (2009-06-12) [i486-linux]

動いた♪

Firefoxでいろいろ隠したい

まずはメニュー
Hide Menubar
そしてタブ
Hide Tabber
最後に全部
Hide GUI bars

Hide GUI barsが非常によい。

Fakerとfactory_girlで偽物大量生産

適当なデータを作るときのお供に。
factory_girlと組み合わせて便利でした。

これが

Factory.define :message do |f|
f.title Faker::Lorem.sentence
f.body Faker::Lorem.paragraph
end


こうなります!

>> y Factory.build :message
--- !ruby/object:Message
attributes:
created_at:
body: Quasi ab fuga reprehenderit quia. Nostrum laud...(以下略)
title: Alias dolor velit ut et vitae magnam aut non.
updated_at:
recipient_id:
sender_id:

適当なデータが入ってくれますねー
他にもメールアドレス、名前などいろいろFakeしてくれます。

実際はこんな風に使いました。

@a_to_b = Factory.create(:message, :sender_id => @Aさん.id, :recipient_id => @Bさん.id)
@c_to_d = Factory.create(:message, :sender_id => @Cさん.id, :recipient_id => @Dさん.id)


悪くないす。
http://rubyforge.org/projects/faker
Faker: Quick “Fake Data” Generation in Ruby

shoudaのマクロにブロックを渡すときの罠

shouldaのマクロを追加してActiveRecord::RecordNotFoundが起こるかチェックしてみたらどうだろう、と思っていろいろやってみて、こうなりました。

こう、呼んであげればと…。

should_raise_record_not_found {get :edit, :id => @なんか.id}


block.bind(self).callで悩んだ。
shouldメソッドに渡すブロックには違うbindingが与えられるので、わざわざselfのを渡しなおしてあげないといけないってことのようでした(たぶん)。
ちょっとしたブロックつきのマクロをたくさん作れるのは便利なんですが、このあたりがちょっと面倒ですね…。

zsh導入

以下を参考に入れました。
最初使ったときに対話式でいろいろ設定を促してくれるんで、助かりました。
特に変態的な使い方をしなくとも、もろもろbashより気が利いてると思います。viモードもbashより調子いい。
漢のzsh
zsh: Prompt Expansion
zshの基本的な使い方 1/3 「導入~基本設定」編
ANSI color codes

.zshrcです。

# Lines configured by zsh-newuser-install
HISTFILE=~/.histfile
HISTSIZE=100000
SAVEHIST=100000
setopt appendhistory autocd extendedglob notify auto_pushd correct
unsetopt beep nomatch
bindkey -v
# End of lines configured by zsh-newuser-install
# The following lines were added by compinstall
zstyle :compinstall filename '/home/fu/.zshrc'

autoload -Uz compinit
compinit
# End of lines added by compinstall

autoload colors
colors
SPROMPT="%B%{${bg[green]}%}%r ? %{${reset_color}%}%b n,y,a,e :"
PROMPT="%B%{${fg[green]}%}%n@%m %~% %(!.#.$) %{${reset_color}%}%b"
RPROMPT="%{${fg[magenta]}%}%*%{${reset_color}%}%"


autoload history-search-end
zle -N history-beginning-search-backward-end history-search-end
zle -N history-beginning-search-forward-end history-search-end
bindkey "^r" history-incremental-search-backward
alias lv='lv -c'
alias ls='ls --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
export PATH=/var/lib/gems/1.8/bin:$PATH


右のプロンプトに時計が出るのはいいですよ。ふと見るとハマってたのに気がつくという。

CUIのユーザビリティって、ありますよね。

shouldaでlogin_requiedのテスト

shouldaです。
ログイン用のマクロを作った。

こういう書き方ができるのはいいすね。これはauthlogicですが、authlogic以外でも勿論できる。

context "ArticlesController" do
should_require_login :get, :new
should_require_login :post, :create
should_require_login :get, :edit
should_require_login :put, :update
should_require_login :delete, :destroy
end


でも、リダイレクト先でテストは適当すぎる…


余談ですが、ついにauthlogicがrestful_authenticationを追い越した。

redirect_to :back のテスト!!!

でっきたー
@request.envに適当なリファラを入れてあげればOK。
Ruby Nuggets: Testing redirect_to :back shortcut
適当すぎる

shouldaの結果を読みやすくするgemを作りました

shouldaでautotestしてると".FE"しか出ないのが嫌でした!なので、gemを作りました。

http://github.com/fujimura/speak_your_mind

実行結果が全部テストメソッド名で出るようにしました。例えば

context "Test" do
should 'success' do
assert true
end

should 'fail' do
assert false
end

should 'error' do
raise
end
end

を走らせると

E Test should error.
F Test should fail.
. Test should success.

と出てくれるようにしてあげました。
私はspecが全部字で出てくれないと心配になってしまうので、これで安心です。
元々自分用だったんですが、gemにしました。gemにすればどこでも使えるから。

中身はtest/unitの出力部分のラッパーです。コンソール用出力の部分でmediatorとか使われてて、コード追うのが大変でした。

Author

Fujimura Daisuke
http://fujimuradaisuke.com

Labels