Skip to content

Instantly share code, notes, and snippets.

@woodie
Created November 12, 2009 09:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save woodie/232754 to your computer and use it in GitHub Desktop.
Save woodie/232754 to your computer and use it in GitHub Desktop.
Rails 3.0.pre GAE Guide

Rails 3.0.pre on App Engine

This will get Rails up and running on App Engine

Create a New Rails App

Use the Gemfile below to get started, and use the latest appengine-tools.

sudo gem install google-appengine
mkdir rails_app
cd rails_app
touch config.ru                    
vim Gemfile                   # scroll down and copy it
appcfg.rb bundle --update .
appcfg.rb run -S bin/rails .  # don't replace Gemfile

Configure for App Engine

Create or modify these files with content from below.

vim app/controllers/rails/info_controller.rb
vim config/application.rb
vim config/boot.rb
vim config/initializers/notifications.rb

Run Local Server and Console

These commands provide the environment with the APIs

dev_appserver.rb .
appcfg.rb run -S script/console

Push the app to production

Make sure your application-id in config.ru is correct

appcfg.rb update .

Generate a Model

We need to pass the location of the local gem cache [FIXME] Note: These models need to be converted to datamapper models

appcfg.rb run -r '.gems/bundler_gems/environment' \
   -S script/generate scaffold person name:string
# add this to your main class (whatever is may be called)
class RailsApp < Rails::Application
...
# Set DataMapper to use dm-appengine adapter
require 'dm-core'
require 'dm-timestamps'
require 'dm-validations'
DataMapper.setup(:default, "appengine://auto")
# Set Logger from appengine-apis, all environments
require 'appengine-apis/logger'
config.logger = AppEngine::Logger.new
# Skip frameworks you're not going to use.
config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
...
end
# replace this file with the following
require 'fileutils'
FileUtils = FileUtils::NoWrite if ENV['RAILS_ENV'].eql? 'production'
$LOAD_PATH << 'lib'
require 'rails'
require 'appengine-rack'
AppEngine::Rack.configure_app(
:application => 'application-id',
:precompilation_enabled => true,
:version => 1)
AppEngine::Rack.app.resource_files.exclude :rails_excludes
ENV['RAILS_ENV'] = AppEngine::Rack.environment
# Require your environment file to bootstrap Rails
require ::File.expand_path('../config/environment', __FILE__)
# Dispatch the request
run RailsApp
# Critical default settings:
disable_system_gems
disable_rubygems
bundle_path ".gems/bundler_gems"
# List gems to bundle here:
gem "rails", "3.0.pre", :git => "git://github.com/rails/rails.git"
gem "arel", :git => "git://github.com/rails/arel.git"
gem "i18n"
gem "dm-appengine"
gem 'dm-timestamps'
gem 'dm-validations'
class Rails::InfoController < ActionController::Base
def properties
info = [['Ruby version', "#{RUBY_VERSION} (#{RUBY_PLATFORM})"]]
if defined? Gem::RubyGemsVersion
info << ['RubyGems version', Gem::RubyGemsVersion]
else
info << ['RubyGems','disabled']
end
info << ['Rack version', Rack.release]
# get versions from rails frameworks
info << ['Rails version', Rails::VERSION::STRING]
frameworks = %w{action_pack active_model active_support}
frameworks.unshift('active_record') if defined? ActiveRecord
frameworks.push('active_resource') if defined? ActiveResource
frameworks.push('action_mailer') if defined? ActionMailer
frameworks.each do |f|
require "#{f}/version"
info << [ "#{f.titlecase} version",
"#{f.classify}::VERSION::STRING".constantize]
end
info << ['DataMapper version', DataMapper::VERSION] if defined? DataMapper
info << ['Environment', RAILS_ENV]
# get versions from jruby environment
if defined?(JRuby::Rack::VERSION)
info << ['JRuby version', JRUBY_VERSION]
info << ['JRuby-Rack version', JRuby::Rack::VERSION]
end
# get information from app engine
if defined?(AppEngine::ApiProxy)
require 'appengine-apis' # for VERSION
env = AppEngine::ApiProxy.current_environment
ver = env.getVersionId[0,env.getVersionId.rindex(".")]
info << ['AppEngine APIs version', AppEngine::VERSION]
info << ['Auth domain', env.getAuthDomain]
info << ['Application id:version', env.getAppId + ":#{ver}"]
end
# render as an HTML table
html = "<table><tbody>"
info.each { |k,v| html += "<tr><td>#{k}</td><td>#{v}</td></tr>" }
html += "</tbody></table>"
render :text => html
end
end
# Set our own default Notifier
module ActiveSupport::Notifications
self.notifier = Notifier.new(Fanout.new(true))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment