くそっ!くそう・・・・。
おれは、、、おれはだだ
このリンクをブログに張ってエスパー伊東ステッカーが欲しいだけなのに・・・
もう、MTがむずすぎる・・・ウィジェットとかウィジェットセットとか・・・よくわかんない・・・。
簡単テストライブラリShouldaを使うと、RailsのModelのテストが楽だ。
class RegionTest < ActiveSupport::TestCase
should_require_attributes :name
should_require_unique_attributes :roman
end
豊富なマクロのおかげでこんな感じに宣言的にテストが書ける。
でも単体でテストを実行したとき(ruby test/units/region_test.rb)はOKなのにrakeで実行すると(rake test:units)エラーになる。
33) Failure:
test: Region should require name to be set. (RegionTest)
(略)
/blank/ not found in ["名前を入力してください。"] when set to nil.
<nil> is not true.
34) Failure:
test: Region should require unique value for roman. (RegionTest)
(略)
/taken/ not found in ["ローマ字はすでに存在します。"] when set to "hokkaidou-touhoku".
<nil> is not true.
うーん・・・。
def should_require_attributes(*attributes)
message = get_options!(attributes, :message)
message ||= <strong>/blank/</strong>
(略)
end
end
ソースを見てみると、should_require_attributesメソッドはエラーメッセージを/blank/という正規表現で決め打ち比較してるみたい。(validates_presence_ofのデフォルトが”“can‘t be blank”“だから)
そういういい加減なの、嫌いじゃない。
environments読み込むとgettextがエラーメッセージを日本語化するのでエラーになっちゃうのか。
でも、
class RegionTest < ActiveSupport::TestCase
should_require_attributes :name,
:message => "名前を入力してください。"
should_require_unique_attributes :roman,
:message => "ローマ字はすでに存在します。"
end
いちいちこんな風にテスト書かなきゃいけないんでは面倒過ぎてアメリカに亡命したくなる。
プラグインより後に何かを読み込むにはどうすりゃいいんだろう?
% tree vendor/plugins/shoulda_ja
vendor/plugins/shoulda_ja
|-- init.rb
`-- lib
`-- shoulda
`-- active_record_helpers.rb
良い方法とは思えないけど、プラグインはアルファベット順に読まれるはずなのでshoulda_jaというプラグインを置いて上書きしてみた。
init.rb:
require 'shoulda'
require File.dirname(__FILE__) + '/lib/shoulda/active_record_helpers'
lib/shoulda/active_record_helpers.rb:
module ThoughtBot
module Shoulda
module ActiveRecord
def should_require_attributes(*attributes)
message = get_options!(attributes, :message)
message ||= <strong>/入力してください/</strong>
(略)
end
def should_require_unique_attributes(*attributes)
message, scope = get_options!(attributes, :message, :scoped_to)
scope = [*scope].compact
message ||= <strong>/すでに存在します/</strong>
(略)
end
end
end
end
うほ、動いた。
・・・・・・・・・アドホック!!!
WebでFIGletなんて使うところないよなあ、と思ってたら。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!--
88 88
,d 88 88
88 88 88
MM88MMM 88 88 88,dPYba,,adPYba, 88,dPPYba, 88 8b,dPPYba,
88 88 88 88P' "88" "8a 88P' "8a 88 88P' "Y8
88 88 88 88 88 88 88 d8 88 88
88, "8a, ,a88 88 88 88 88b, ,a8" 88 88
"Y888 `"YbbdP'Y8 88 88 88 8Y"Ybbd8"' 88 88
-->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
TumblrのソースにはFIGletが!
Googleマップのリファレンスによれば、GoogleマップはまだSSLに対応してないらしい。(SSLでのGoogleマップ)
うぜぇっ。
Proxy作るもの面倒だしなあ・・・。
・・・・・・・・・っっっ!?
<script type="text/javascript">var _alert = alert; <strong>alert = function(){}</strong></script>
<%= javascript_include_tag "http://maps.google.com/maps?file=api&v="+GOOGLE_MAPS_API_VERSION+"&key="+GOOGLE_MAPS_API_KEY %>
<script type="text/javascript">alert = _alert</script>
これでよしと・・・。
・・・最低だ。
大体の画像系と同じようにnilとかを入れるだけでOK。
% ./script/console
Loading development environment (Rails 2.1.0)
>> grave = Grave.first
=> (略)
>> grave.overview.url
=> "/system/graves/overviews/1/normal/test_picture_large.gif"
>> grave.overview = nil
=> nil
>> grave.save
=> true
>> grave.overview.url
=> "/overviews/normal/missing.png"
しかも、削除した場合のURLが自動的にmissing.pngに変わってる。
この勝手な挙動が嫌いな人もいるかもしれないけど面倒臭がりの俺としては嬉しい。
画像があるかどうかは、grave.overview.exists? で分かる。
thoughtbot: Paperclip File AttachmentsRequirements
Only ImageMagick, which you can install quite easily install via apt, yum, or port, or the package manager of your choice.
あとやっぱりRMagick無くても動く!そういやImageMagick自体が問題なんじゃなくて、殆どの場合、パッケージのImageMagickが言語バインディングと合わないことが問題だったんだよね。PureRubyで再実装しかないと思ってた。頭良いな作者。
Helperはrails2.xからApplicationControllerにhelper :allとか書いてあって、こちらに書かれてるように優先順位があってハマった。(名前がかぶってたので)
パフォーマンスが厳しいアプリはhelper :allを外して、俺の様にそういうアプリがめったに無い場合はどうせ全部読まれるんだったらApplicationHelperに全部書いた方が見通しがいいと思った(実際個別のHelperに書いたのを何度も見失った)。そうなると、コントローラー名Helperがなければいけないという制約がうざい。
最近 config/initializers/rails_ext.rb に追加したもの。
module ApplicationHelper
def web_root
request.protocol+request.host_with_port
end
def free_dial?(str)
/^0120/ =~ str ? true : false
end
def notice
content_tag(:div, image_tag("notice.gif", :class => "icon")+flash[:notice], :id => "notice") if flash[:notice]
end
def warn
content_tag(:div, image_tag("warn.gif", :class => "icon")+flash[:warn], :id => "warn") if flash[:warn]
end
def focus(element)
content_tag(:script, "$('#{element}').focus()", :type => "text/javascript")
end
def options_by_prefectures
# Prefecture.all.collect {|p| [p.name, p.id] }.unshift ["-- 選択してください --", nil]
end
end
~/.rails/default/config/initializers/rails_ext.rb とかに置いといて、railsプロジェクト作成後にdefaultディレクトリ毎上書きする。
説明しようもないもんばっかり。free_dial? がお気に入りです。何に使うかというとおなじみのフリーダイアルアイコンを出すかどうか?とかだけです・・・。
options_by_prefecturesは都道府県プルダウン。どうせ毎回使うんだけどさすがに無いとエラーなので一応コメントアウトを。
何か、こういうライブラリとかにするまでもない小粒なコード辺が好きだ。ライフハックとか生活の裏技のプログラマー版みたいなもんか。
コード辺は実際に動いてくれる。それでいて主張は小さく、押しは弱い。常に「何とか志向」や「何とかドリブン開発」のような次々出てくる新しい思考法やメソッドに自分を合わせていかねばならない我々を一瞬ほっこりさせてくれるある種の清涼剤です。
参照:
RailsでGrave(お墓)というクラスを作った。
そしたらところどころで”“grafe”“が無いとか言われる。
おれの得意技typoだと思ってgrepしまくるも見つからず。
の格言に従って調べると・・・
% irb -r activesupport
>> "grave".pluralize
=> "graves"
>> "grave".pluralize.singularize
=> "grafe"
!?
graveの複数形はgravesだけどgravesの単数形はgrafe!?
あー!
knife => knivesのルールが適応されてるのか。
config/initializers/inflections.rbに例外を書いて治た。
Inflector.inflections do |inflect|
inflect.irregular 'grave', 'graves'
end
leaf => leaves や wolf => wolves のルールより優先されてる?
もう英語力がね・・・アレなんですわ。