2008年3月10日月曜日

'GWT on Rails'のサンプル(再び)

ここまでのまとめをスクリプトとして置いておいてみる.この方が私が文書を書くより確実に情報(それが何かはわからないが)が伝わるだろう.
環境: MacOS X 10.5.2 / Ruby 1.8.6(OS付属) / Java 1.5.0(OS付属) / Rails 2.0.2(portsより) / GWT 1.4.61(download)
モデル名: Project
アプリ名: dashboard
#! /bin/sh
#------------------------------------------------------
#
PRJ_NAME=GWT_on_Rails
GWT_HOME=/path/to/gwt-mac-1.4.61

#------------------------------------------------------
#
rm -rf ${PRJ_NAME}

#------------------------------------------------------
#
rails ${PRJ_NAME}

#------------------------------------------------------
#
cd ${PRJ_NAME}

#------------------------------------------------------
#--Rails-----------------------------------------------
#------------------------------------------------------
#
script/plugin install http://labnotes.org/svn/public/ruby/rails_plugins/json_request

#------------------------------------------------------
#
script/generate scaffold Project name:string

#------------------------------------------------------
#
mv app/controllers/projects_controller.rb app/controllers/projects_controller.rb.orig

#------------------------------------------------------
#
sed 's/xml/json/g' app/controllers/projects_controller.rb.orig > app/controllers/projects_controller.rb

#------------------------------------------------------
#
script/generate migration InsertProjects

#------------------------------------------------------
#
patch db/migrate/002_insert_projects.rb << EOF
2a3,4
> Project.create(:name=>"Foo")
> Project.create(:name=>"Bar")
EOF

#------------------------------------------------------
#
rake db:migrate

#------------------------------------------------------
#--GWT-------------------------------------------------
#------------------------------------------------------
#
script/plugin install http://gwt-on-rails.googlecode.com/svn/trunk/gwt_on_rails

#------------------------------------------------------
#
script/generate gwt_resource Project dashboard

#------------------------------------------------------
#
rake db:migrate

#------------------------------------------------------
#
script/generate gwt_client dashboard ${GWT_HOME}

#------------------------------------------------------
#
patch app/gwt/dashboard/src/dashboard/client/Dashboard.java << EOF
3a4,6
> import com.google.gwt.core.client.GWT;
> import com.google.gwt.http.client.Request;
> import com.google.gwt.user.client.Window;
8a12,18
> import com.kaboomerang.gwt.rest.client.handler.ResourceResponseHandler;
> import com.kaboomerang.gwt.rest.client.resource.Resource;
> import com.kaboomerang.gwt.rest.client.resource.ResourceCollection;
> import com.kaboomerang.gwt.rest.client.resource.ResourceFactory;
> import com.kaboomerang.gwt.rest.client.resource.impl.RailsJsonResourceCollection;
>
> import dashboard.client.resource.Project;
23a34,51
> ResourceCollection projects = new RailsJsonResourceCollection(
> "/projects/", new ResourceFactory() {
> public Resource create() {
> return (Resource) GWT
> .create(Project.class);
> }
> });
> projects.find(1,
> new ResourceResponseHandler() {
> public void onSuccess(Resource resource) {
> Window.alert("Project name is " + ((Project) resource).getName());
> }
>
> public void onError(Request request, Throwable exception) {
> Window.alert("Something went wrong.");
> }
> });
>
EOF

#------------------------------------------------------
#
rake dashboard:compile


後はサーバを起動する.
> cd ${PRJ_NAME}
> script/server


と,アクセスするぐらい.
http://localhost:3000/dashboard/

0 件のコメント: