2010年7月27日 #rails

一つのHelperメソッドを作って、Modelに必須チェックが入ってるプロパティに対して必須マークの"# "を出力します。

まずapplicaton_helperにmark_requiredのメソッドを作ります。第一引数にはオブジェクト、第二引数はそのクラスのプロパティを渡します。

# application_helper.rb
def mark_required(object, attribute)
  "# " if object.class.validators_on(attribute).map(&:class).include? ActiveModel::Validations::PresenceValidator
end

viewのerbには下記のように@userインスタンス変数と:nameを渡します。もしUserモデルに:nameに対して必須バリデーションが存在すれば必須マークが出力されます。

  <div class="field">
    <%= f.label :name %><%=mark_required(@user, :name) %><br />
    <%= f.text_field :name %>
  </div>

参考リンク:http://railscasts.com/episodes/211-validations-in-rails-3

20110704更新

もしhtmlタグも一緒に出力したいときは、タグがエスケープされてしまいます。


module ApplicationHelper
  def mark_required(object, attribute)
    mark = "<span class='require_mark'># </span>"
    mark if object.class.validators_on(attribute).map(&:class).include? ActiveModel::Validations::PresenceValidator
  end
end

  <div class="field">
    <%= f.label :name %><%=raw mark_required(@user, :name) %><br />
    <%= f.text_field :name %>
  </div>

この場合はrawを書けばエスケープされずにちゃんとHTMLタグが出力します。

中文

我们将创建一个Helper方法来输出"# "表示必须项目。
(原谅我这蹩脚的汉语,实在是不知道这些术语用中文该怎么叫)

首先在application_helper里创建一个叫mark_required的方法。他的第一个参数为对象,第二个参数接收的是对象的属性。

# application_helper.rb
def mark_required(object, attribute)
  "# " if object.class.validators_on(attribute).map(&:class).include? ActiveModel::Validations::PresenceValidator
end

然后在View里我们只要把@user和:name传过去即可。如果你的User Model对:name进行了必须验证(presence validator),"# "就会显示出来。

  <div class="field">
    <%= f.label :name %><%= mark_required(@user, :name) %><br />
    <%= f.text_field :name %>
  </div>

链接:http://railscasts.com/episodes/211-validations-in-rails-3

English

Let's make a helper method that shows a required mark "# " when the attribute of the object has a presence validator.

First, create a mark_required method in the application_helper, pass the object as 1st parameter, the attribute as the second one.

# application_helper.rb
def mark_required(object, attribute)
  "# " if object.class.validators_on(attribute).map(&:class).include? ActiveModel::Validations::PresenceValidator
end

Then, just call the custom method in the erb view file. In this sample we pass the @user instance variable and the :name attribute symbol. So if your User model has a presence validator on the :name attribute, the required-mark "# " will show up.

  <div class="field">
    <%= f.label :name %><%= mark_required(@user, :name) %><br />
    <%= f.text_field :name %>
  </div>

I learned from this site:http://railscasts.com/episodes/211-validations-in-rails-3

2010年7月21日 #wordpress

http://b.hatena.ne.jp/guide/blogpartsにて自分のサイトの人気エントリーをブログパーツとして設置できます。
手順にしたがってブログのURLを入力して簡単の設定を行ったあとは貼り付けるコードが生成されるため、それをブログにコピペすればOKです。

できたらこんな感じです。なぜか画像が暗くなっちゃいました。

hatena-bookmark-entry

2010年7月 3日 #iphone

iPhoneでアプリケーションを購入して普通はそのままiTunesに同期できるはずですが、時々iTunesがやってくれないです。
あるいはiPhoneで一杯アプリを購入したり削除したりして、パソコンのiTunesとの差分が紛らわしくなったこともあります。その場合はiTunesのアプリを一旦全部削除して下記の方法で一気に同期すればOKです。

アプリケーションをiPhoneからiTunesに同期する

iTunesからiPhoneのデバイスを右クリックして、メニューから「Transfer Purchase」を選択

2010年6月24日 #iphone

iOS4をインストールできたら、メールアカウントの設定画面をチェックしてみてください。
「メモ(Notes)」という項目が追加されているはずです。
この項目を「オン」にすればOK。
メモは、Gmail上で「Notes」というラベルがつけられています。
ちなみにカレンダーもGoogleに同期できます。

iphone notes

iphone notes

iphone-notes-4

iphone-notes-3

http://www.lifehacker.jp/2010/06/100623iphonenotes_sync.html

English

Apple’s iOS 4 (iPhone OS 4) added an important missing feature: a way to save your notes online. If you add an IMAP mail account like Gmail or Yahoo Mail and enable the notes feature, you can create notes that are saved to your email account.

2010年6月21日 #wordpress

qTranslateを試しました。
ブログを多言語に対応する上でとても便利なプラグインです。
これからは日本語、英語、中国語で記事を書いていきたいと思います。
まだカスタマイズができてないので、
整理できたら投入メモとか書きます。

中文

试了试qTranslate
好久不用中文写东西了有点生疏。。
今后博客将用中日英三语,
英语的不分应该会不熟练吧,当是锻炼了。
等把qTranslate使用明白以后再写一篇笔记。

English

I triedqTranslate, a multilingual wordpress plugin.
I'm going to write blogs in English, Chinese and Japanese.
I will post a memo about qTranslate after I master it.

2010年6月 1日 #facebook

Googleのhttp://www.google.com/adplanner/static/top1000/によりますと、
Top1のFacebookのPageviewはなんと第二位のYahooの8倍だそうです。
日本と中国ではまだFacebookが主流のSNSサイトにはなっていないようですが、
そのうちどうなりますかね。。

Graph APIとかまだ全然わかりませんが、
とりあえずLikeボタンをブログに設置しました。
このページでURLとwidthを入力するだけで簡単に作れます。
生成されたコードは二種類ありまして一つはiframeのやつと
もう一つはFacebookのカスタムタグのやつで
これはJavascript SDKが必要だそうです。
もちろん後者のほうがもっとパワフルな感じです。



ソーシャルアプリとかゲームの開発はちょっとやりたいので
時間があればFacebookの各種APIも勉強するつもりです。



ひとまずサイドバーに設置して自分が'Like'を押しました。。

2010年5月17日 #jquery

elem指定とID指定のjQueryセレクタパフォーマンス実験を行いました。
"先にJavascriptでelementをとってそれをjQueryの$セレクタに囲むやつ"
vs
"直接jQueryのidセレクタでとるやつ"。


コードで言うと
$(document.getElementById("foo"))
vs
$("#foo")



普通前者が勝だろうと思うだろう、
どれくらいの差かを確認したくて。

結果

一万回ループでとった結果です。

  • IE8: 187 vs 344
  • IE6: 203 vs 453
  • Firefox×(3.6.3): 21 vs 63
  • Chrome(4.1.xx): 9 vs 39

サンプルコード

デモページへ

var t0 = new Date();

for (var i=0; i<10000; i++){
    $(document.getElementById("foo"));
}

var t1 = new Date();

for (var i=0; i<10000; i++){
    $("#foo");
}

var t2 = new Date();

alert("直接elementを渡す時間は:" + (t1-t0) + " , ID指定でとる時間は:"+ (t2-t1));

2010年5月 9日 #flex

カスタムイベントのパッケージ名を"event"にしました。

結果、CTRL+SPACEでの自動補完機能がうまく行かないです。
例えば自動でイベントハンドラを生成してくれる場合のパラメータはeventになりますが、
この"event"続いてドット(.)を叩いて出たのは各eventパッケージしたのクラスになります。

flex_autocomplete_confused

イベントハンドラの変数を"event"以外にすれば回避できますが、
ハンドラの自動生成は結構便利なものなので
やはりパッケージ名を"events"などに変えたほうがいいと思います。

これで自動補完はちゃんと出ます。

flex_autocomplete_correct