2006-04-07 [長年日記]

_ [javascript] A (Re)-Introduction to JavaScript

だいぶ前に capsctrldays で知って、 リンクも張ったような気がしてたんだけど、気がしてただけだったようなので今ごろ。

単に p.54 に出てくる for 文の書き方が思い出せなかっただけですが。 で、

js> var a = [0, 1, 2]
js> for(var i=0,j;j=a[i];i++){print(j)}
js>

なんでー?

js> a.shift()
0
js> for(var i=0,j;j=a[i];i++){print(j)}
1
2
js> 0 ? true : false
false

0 が false なのにはまるんです。

_ [ruby][rails] error_messages_for

さらに capsctrldays から。

>> conf.echo = false
>> class Author < ActiveRecord::Base
>>   validates_length_of :name, :in => 2..32
>>   validates_presence_of :name, :age
>>   validates_numericality_of :age, :only_integer => true
>> end
>> class Book < ActiveRecord::Base
>>   validates_presence_of :title, :price
>>   validates_numericality_of :price
>> end
>> author = Author.new
>> p author.save
false
>> book = Book.new
>> p book.save
false
>> helper.instance_variable_set(:@author, author)
>> helper.instance_variable_set(:@book, book)
>> html = helper.error_messages_for("author", "book")
>> puts html
<div class="errorExplanation" id="errorExplanation"><h2>7 errors prohibited this author from being saved</h2><p>There were problems with the following fields:</p><ul><li>Name can't be blank</li><li>Name is too short (minimum is 2 characters)</li><li>Age is not a number</li><li>Age can't be blank</li><li>Title can't be blank</li><li>Price is not a number</li><li>Price can't be blank</li></ul></div>
>> IO.popen("env LANG=C w3m -dump -T text/html", "w"){|w3m| w3m.write(html)}
7 errors prohibited this author from being saved

There were problems with the following fields:

  * Name can't be blank
  * Name is too short (minimum is 2 characters)
  * Age is not a number
  * Age can't be blank
  * Title can't be blank
  * Price is not a number
  * Price can't be blank

>>

おお、いい感じ。author の Price ってのもよくわからんけど :)。どうせなら、これぐらい。

--- init.rb~
+++ init.rb
@@ -4,16 +4,24 @@

   def error_messages_for(*params)
     options = params.last.is_a?(Hash) ? params.pop.symbolize_keys : {}
-    objects = params.collect {|name| instance_variable_get("@#{name}")}
-    count   = objects.inject(0) {|sum, obj| sum + obj.errors.count}
+    objects = params.collect {|name| [name, instance_variable_get("@#{name}")]}
+    invalid_objects = []
+    count   = objects.inject(0) {|sum, (name, obj)|
+      c = obj.errors.count
+      invalid_objects << [name, obj]
+      sum + c
+    }
     unless count == 0
       content_tag("div",
         content_tag(
           options[:header_tag] || "h2",
-          "#{pluralize(count, "error")} prohibited this #{(options[:default] || params[0]).to_s.gsub("_", " ")} from being saved"
+          "#{pluralize(count, "error")} prohibited this #{invalid_objects.map{|name, obj| name}.join("/").gsub("_", " ")} from being saved"
           ) +
         content_tag("p", "There were problems with the following fields:") +
-        content_tag("ul", objects.collect{|object| object.errors.full_messages.collect { |msg| content_tag("li", msg) } }.flatten),
+        content_tag("ul", invalid_objects.map{ |name, obj|
+            content_tag("li", name.camelize) +
+            content_tag("ul", obj.errors.full_messages.map{|msg|
+                  content_tag("li", msg)}.join("\n"))}.join("\n")),
         "id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation"
         )
     end

仕上りは、つ

7 errors prohibited this author/book from being saved

There were problems with the following fields:

  * Author
      + Name can't be blank
      + Name is too short (minimum is 2 characters)
      + Age is not a number
      + Age can't be blank
  * Book
      + Title can't be blank
      + Price is not a number
      + Price can't be blank

なんだかなー。

[]

«前の日記(2006-03-31) 最新 次の日記(2006-05-01)»