Permalink
Browse files

Add example threadsafe Rails 2.2 application

  • Loading branch information...
1 parent 5a5ca9b commit 9364ba9de26339823ef24612793a2952929e88a5 @mperham mperham committed Nov 30, 2008
Showing with 8,580 additions and 0 deletions.
  1. +58 −0 example22/Rakefile
  2. +22 −0 example22/app/controllers/accounts_controller.rb
  3. +39 −0 example22/app/controllers/application.rb
  4. +8 −0 example22/app/controllers/figments_controller.rb
  5. +3 −0 example22/app/helpers/application_helper.rb
  6. +3 −0 example22/app/models/account.rb
  7. +4 −0 example22/app/models/figment.rb
  8. +47 −0 example22/app/views/accounts/index.html.erb
  9. +8 −0 example22/app/views/layouts/application.html.erb
  10. +109 −0 example22/config/boot.rb
  11. +21 −0 example22/config/database.yml
  12. +76 −0 example22/config/environment.rb
  13. +17 −0 example22/config/environments/development.rb
  14. +24 −0 example22/config/environments/production.rb
  15. +22 −0 example22/config/environments/test.rb
  16. +10 −0 example22/config/initializers/inflections.rb
  17. +5 −0 example22/config/initializers/mime_types.rb
  18. +17 −0 example22/config/initializers/new_rails_defaults.rb
  19. +5 −0 example22/config/locales/en.yml
  20. +46 −0 example22/config/routes.rb
  21. +14 −0 example22/db/migrate/20080702154628_create_accounts.rb
  22. +14 −0 example22/db/migrate/20080702154820_create_figments.rb
  23. +30 −0 example22/public/404.html
  24. +30 −0 example22/public/422.html
  25. +33 −0 example22/public/500.html
  26. +10 −0 example22/public/dispatch.cgi
  27. +24 −0 example22/public/dispatch.fcgi
  28. +10 −0 example22/public/dispatch.rb
  29. 0 example22/public/favicon.ico
  30. BIN example22/public/images/rails.png
  31. +274 −0 example22/public/index.html
  32. +2 −0 example22/public/javascripts/application.js
  33. +963 −0 example22/public/javascripts/controls.js
  34. +973 −0 example22/public/javascripts/dragdrop.js
  35. +1,128 −0 example22/public/javascripts/effects.js
  36. +4,320 −0 example22/public/javascripts/prototype.js
  37. +5 −0 example22/public/robots.txt
  38. +4 −0 example22/script/about
  39. +3 −0 example22/script/console
  40. +3 −0 example22/script/dbconsole
  41. +3 −0 example22/script/destroy
  42. +3 −0 example22/script/generate
  43. +3 −0 example22/script/performance/benchmarker
  44. +3 −0 example22/script/performance/profiler
  45. +3 −0 example22/script/performance/request
  46. +3 −0 example22/script/plugin
  47. +3 −0 example22/script/process/inspector
  48. +3 −0 example22/script/process/reaper
  49. +3 −0 example22/script/process/spawner
  50. +3 −0 example22/script/runner
  51. +3 −0 example22/script/server
  52. +7 −0 example22/test/fixtures/accounts.yml
  53. +12 −0 example22/test/functional/accounts_controller_test.rb
  54. +97 −0 example22/test/integration/account_figments_test.rb
  55. +9 −0 example22/test/performance/browsing_test.rb
  56. +38 −0 example22/test/test_helper.rb
View
@@ -0,0 +1,58 @@
+# Add your own tasks in files placed in lib/tasks ending in .rake,
+# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
+
+require(File.join(File.dirname(__FILE__), 'config', 'boot'))
+
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+require 'tasks/rails'
+
+require 'fileutils'
+include FileUtils::Verbose
+
+namespace :db do
+ task :migrate do
+ require 'erb'
+ require 'logger'
+ require 'active_record'
+ reference = YAML::load(ERB.new(IO.read("config/database.yml")).result)
+ env = RAILS_ENV = ENV['RAILS_ENV'] || 'development'
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
+ ActiveRecord::Base.logger.level = Logger::WARN
+ ActiveRecord::Base.configurations = reference.dup
+ old_config = reference[env]
+ reference.each_key do |name|
+ next unless name.include? env
+ next if name.include? 'slave' # Replicated databases should not be touched directly
+
+ puts "Migrating #{name}"
+ ActiveRecord::Base.clear_active_connections!
+ ActiveRecord::Base.configurations[env] = reference[name]
+ ActiveRecord::Base.establish_connection RAILS_ENV
+ ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
+ ActiveRecord::Migrator.migrate("db/migrate/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
+ end
+ end
+end
+
+namespace :app do
+ task :prepare => [:clean, :copy_plugin, :migrate]
+
+ task :copy_plugin do
+ mkdir_p 'vendor/plugins/data_fabric'
+ cp_r '../lib', 'vendor/plugins/data_fabric'
+ cp '../init.rb', 'vendor/plugins/data_fabric'
+ end
+
+ task :clean do
+ rm_rf 'vendor/plugins/data_fabric'
+ rm_f 'db/*.sqlite3'
+ end
+
+ task :migrate do
+ sh "rake db:migrate"
+ sh "rake RAILS_ENV=test db:migrate"
+ end
+end
@@ -0,0 +1,22 @@
+class AccountsController < ApplicationController
+ layout 'application'
+
+ def index
+ @accounts = Account.find(:all)
+ end
+
+ def choose
+ @account = Account.find(params[:id])
+ if @account
+ session[:account_id] = @account.id
+ flash[:notice] = "Selected account: #{@account.name}"
+ end
+ redirect_to '/'
+ end
+
+ def create
+ Account.create!(params[:acct])
+ flash[:notice] = "Account created successfully"
+ redirect_to '/'
+ end
+end
@@ -0,0 +1,39 @@
+# Filters added to this controller apply to all controllers in the application.
+# Likewise, all the methods added will be available for all controllers.
+
+class ApplicationController < ActionController::Base
+ helper :all # include all helpers, all the time
+
+ # See ActionController::RequestForgeryProtection for details
+ # Uncomment the :secret if you're not using the cookie session store
+ protect_from_forgery # :secret => '44f8cf8a0491c23ae99c031a900123cc'
+
+ # See ActionController::Base for details
+ # Uncomment this to filter the contents of submitted sensitive data parameters
+ # from your application log (in this case, all fields with names like "password").
+ # filter_parameter_logging :password
+
+ before_filter :find_account
+ around_filter :select_shard
+
+ private
+ def find_account
+ aid = session[:account_id]
+ if aid && aid.to_i != 0
+ begin
+ @account = Account.find(Integer(aid))
+ rescue ActiveRecord::RecordNotFound => e
+ RAILS_DEFAULT_LOGGER.warn "No such account #{aid}, skipping..."
+ end
+ end
+ session[:account_id] = nil unless @account
+ end
+
+ def select_shard(&block)
+ if @account
+ DataFabric.activate_shard(:shard => @account.shard, &block)
+ else
+ yield
+ end
+ end
+end
@@ -0,0 +1,8 @@
+class FigmentsController < ApplicationController
+
+ def create
+ @account.figments.create!(params[:figment])
+ flash[:notice] = "Figment created"
+ redirect_to '/'
+ end
+end
@@ -0,0 +1,3 @@
+# Methods added to this helper will be available to all templates in the application.
+module ApplicationHelper
+end
@@ -0,0 +1,3 @@
+class Account < ActiveRecord::Base
+ has_many :figments
+end
@@ -0,0 +1,4 @@
+class Figment < ActiveRecord::Base
+ data_fabric :shard_by => 'shard', :replicated => false
+ belongs_to :account
+end
@@ -0,0 +1,47 @@
+<hr/>
+<h2>Accounts</h2>
+<table>
+ <thead>
+ <tr><th>ID</th><th>Name</th><th>Shard</th><th>&nbsp;</th></tr>
+ </thead>
+ <tbody>
+<% @accounts.each do |account| %>
+ <tr>
+ <td><%= account.id %></td>
+ <td><%= account.name %></td>
+ <td><%= account.shard %></td>
+ <td><%= link_to 'Choose', choose_account_path(account) %></td>
+ </tr>
+<% end %>
+ </tbody>
+</table>
+<hr/>
+<h2>New Account</h2>
+<p>
+<% form_for :acct, :url => accounts_path do |f| %>
+ Name : <%= f.text_field :name %><br/>
+ Shard: <%= select 'acct', 'shard', [[0,0],[1,1]] %><br/>
+ <%= submit_tag 'Save' %>
+<% end %>
+</p>
+<hr/>
+<% if @account %>
+<h2>Current Account: <%= @account.name %></h2>
+ <table>
+ <thead>
+ <tr><th>ID</th><th>Account</th><th>Value</th></tr>
+ </thead>
+ <tbody>
+ <% @account.figments.each do |fig| %>
+ <tr>
+ <td><%= fig.id %></td>
+ <td><%= fig.account.name %></td>
+ <td><%= fig.value %></td>
+ </tr>
+ <% end %>
+ </tbody>
+ </table>
+ Add Fig: <% form_for :figment, :url => figments_path do |f| %><%= f.text_field :value %><%= submit_tag 'Save' %><% end %>
+<% else %>
+ <em>No account selected.</em>
+<% end %>
@@ -0,0 +1,8 @@
+<html>
+ <body>
+ <div id='flash' style="background-color: #DDFFDD;">
+ <%= flash[:notice] %>
+ </div>
+ <%= yield %>
+ </body>
+</html>
View
@@ -0,0 +1,109 @@
+# Don't change this file!
+# Configure your app in config/environment.rb and config/environments/*.rb
+
+RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
+
+module Rails
+ class << self
+ def boot!
+ unless booted?
+ preinitialize
+ pick_boot.run
+ end
+ end
+
+ def booted?
+ defined? Rails::Initializer
+ end
+
+ def pick_boot
+ (vendor_rails? ? VendorBoot : GemBoot).new
+ end
+
+ def vendor_rails?
+ File.exist?("#{RAILS_ROOT}/vendor/rails")
+ end
+
+ def preinitialize
+ load(preinitializer_path) if File.exist?(preinitializer_path)
+ end
+
+ def preinitializer_path
+ "#{RAILS_ROOT}/config/preinitializer.rb"
+ end
+ end
+
+ class Boot
+ def run
+ load_initializer
+ Rails::Initializer.run(:set_load_path)
+ end
+ end
+
+ class VendorBoot < Boot
+ def load_initializer
+ require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
+ Rails::Initializer.run(:install_gem_spec_stubs)
+ end
+ end
+
+ class GemBoot < Boot
+ def load_initializer
+ self.class.load_rubygems
+ load_rails_gem
+ require 'initializer'
+ end
+
+ def load_rails_gem
+ if version = self.class.gem_version
+ gem 'rails', version
+ else
+ gem 'rails'
+ end
+ rescue Gem::LoadError => load_error
+ $stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
+ exit 1
+ end
+
+ class << self
+ def rubygems_version
+ Gem::RubyGemsVersion rescue nil
+ end
+
+ def gem_version
+ if defined? RAILS_GEM_VERSION
+ RAILS_GEM_VERSION
+ elsif ENV.include?('RAILS_GEM_VERSION')
+ ENV['RAILS_GEM_VERSION']
+ else
+ parse_gem_version(read_environment_rb)
+ end
+ end
+
+ def load_rubygems
+ require 'rubygems'
+ min_version = '1.3.1'
+ unless rubygems_version >= min_version
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
+ exit 1
+ end
+
+ rescue LoadError
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
+ exit 1
+ end
+
+ def parse_gem_version(text)
+ $1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
+ end
+
+ private
+ def read_environment_rb
+ File.read("#{RAILS_ROOT}/config/environment.rb")
+ end
+ end
+ end
+end
+
+# All that for this:
+Rails.boot!
@@ -0,0 +1,21 @@
+# SQLite version 3.x
+# gem install sqlite3-ruby (not necessary on OS X Leopard)
+
+<% %w(development test production).each do |env| %>
+
+<%= env %>:
+ adapter: sqlite3
+ database: <%= "db/#{env}.sqlite3" %>
+ timeout: 5000
+
+<%= "shard_0_#{env}" %>:
+ adapter: sqlite3
+ database: <%= "db/s0_#{env}.sqlite3" %>
+ timeout: 5000
+
+<%= "shard_1_#{env}" %>:
+ adapter: sqlite3
+ database: <%= "db/s1_#{env}.sqlite3" %>
+ timeout: 5000
+
+<% end %>
Oops, something went wrong.

0 comments on commit 9364ba9

Please sign in to comment.