Monday, August 25, 2008

rubyjs_on_rails hackfest at erubycon

I had a really fun time hacking on rubyjs at eRubycon. It's amazing how much more fun and productive it is to hack with someone else than by yourself. Thanks to Corey Haines for staying up late at the hotel being my hacking buddy. As a result, we now have what will be the beginning of an ActiveResource client for rubyjs_on_rails. I generated a simple scaffold resource example called Customer with a few fields. I then created a Customer class in the view and started getting it to speak restfully with rails. Here's the result:


require 'dom_element'
require 'json'
require 'rwt/HTTPRequest'

class Customer


attr_accessor :attributes

def initialize(attrs)
@attributes = attrs
end

def method_missing(method, *args)
if method =~ /(.*)=$/
attributes[$1] = args[0]
elsif attributes[method]
attributes[method]
else
super
end
end

def self.main
@name = DOMElement.find("name")
@address = DOMElement.find("address")
find_button = DOMElement.find("choose_customer_button")
save_button = DOMElement.find("save_button")
customer_id_text = DOMElement.find("customer_id")

find_button.observe("click") do |event|
Customer.find(customer_id_text["value"]) do |customer|
@name["value"] = customer.name
@address["value"] = customer.address
@customer = customer
end
end

save_button.observe("click") do |event|
@customer.name = @name["value"]
@customer.address = @address["value"]
@customer.save
end

rescue StandardError => ex
puts ex
end

def self.find(id)
HTTPRequest.asyncGet "/customers/#{id}.json" do |json|
hash = JSON.load(json)
yield Customer.new(hash["customer"])
end
end

def save
request_json = {:customer => attributes}.to_json
HTTPRequest.asyncImpl "/customers/#{id}.json", "PUT", request_json, "application/json" do |json|
self.attributes = JSON.load(json)
end
end

def to_json
attributes.to_json
end
end



The first part I did was find. I've had that done for a couple weeks. This was super easy because rubyjs and rails support json so easily. The only odd bit is that it takes a block instead of returning a result. This is because the find call does an ajax request, and the a in ajax stands for.. you guessed it, asynchronous. Passing a block in makes it nice and simple to handle the result easily though.

The part I got working at rubycon was the save. This proved to be much easier than I thought it would be as well due to rails now supporting json updates natively. The controller code doesn't even need to be aware that the parameters are coming in as json, you can access param[:customer] the same as you would if it was coming from a normal form submission. I had to be sure and specify it was a PUT request when I sent it in, which I didn't realize was as straightforward to do as it is. I monkeyed around with a _method param and all sorts of stuff before realizing I could just tell the HTTPRequest object what kind of request I wanted to make. Duh.

Though this is a specific example right now, I think a large portion of this code should be easy to extract out into an ActiveResouce client base class. I haven't had any more time to hack on this since erubycon, sadly, but this is what I'll be focusing on when next I do. Also, I just now put up a new repo on github to hold this and any other rubyjs_on_rails example code: git://github.com/superchris/rubyjs_rails_example.git. Enjoy!

3 comments:

Anonymous said...

池袋 風俗
渋谷 風俗
新宿 風俗
性病検査 女性
アダルトDVD
av 写真
大人のおもちゃ
おとなのおもちゃ
アダルトDVD
アンドロペニス
オナホール
コンドーム
ローション
SM 通販
セクシー下着
男性下着
メンズTバック
大規模修繕
決済代行
SEO
SEO
Hicht hetta hoop
カード決済
ブライダルエステ
FX 比較ランキング
クレジットカード 比較
仔犬
子ウサギ
仔ウサギ
アダルトショップ
アダルトグッツ
ゴールドカード ランキング
調査料金 探偵
行動調査 探偵

Anonymous said...

夫の浮気調査 探偵
身元調査 探偵
所在調査 探偵
浮気調査
ストーカー調査
素行調査
盗聴調査
身元調査
不倫調査
不倫調査
尾行調査
企業調査
身元調査
身辺調査
夫の浮気 相談
浮気 相談
身上調査 相談
所在調査 相談
不倫調査 相談
盗聴調査 相談
結婚詐欺 相談
オナホール
ビジネス英語 メール
toeic対策

1234 said...

最後の最後で美女が登場しましたので、今回もお構いなしにチェンジをします。しかし今回は、何度チェンジしても美女は現れません。普通の風俗、置屋なら問題無のルックスなのですが、台湾風俗 ここの相場(持ち帰り料9000ドル程・日本円で3万程)を考えると・・・やはり妥協はできません。呆れたチーママが、女の子が待機する部屋に案内してくれて、好きな子を選んで良いよ、との事。 今回は、良い子が居らず、飲むだけにしました。