Skip to content

Commit

Permalink
Working example! Yay!
Browse files Browse the repository at this point in the history
'
  • Loading branch information
thiagopradi committed Jun 23, 2010
1 parent 907eac1 commit 638866f
Show file tree
Hide file tree
Showing 21 changed files with 394 additions and 18 deletions.
5 changes: 2 additions & 3 deletions Gemfile
@@ -1,12 +1,11 @@
source 'http://rubygems.org'

gem 'rails', '3.0.0.beta4'
gem 'mysql'
gem 'ar-octopus', '0.0.4'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'sqlite3-ruby', :require => 'sqlite3'

# Use unicorn as the web server
# gem 'unicorn'

Expand Down
83 changes: 83 additions & 0 deletions app/controllers/users_controller.rb
@@ -0,0 +1,83 @@
class UsersController < ApplicationController
# GET /users
# GET /users.xml
def index
@users = User.all

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @users }
end
end

# GET /users/1
# GET /users/1.xml
def show
@user = User.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @user }
end
end

# GET /users/new
# GET /users/new.xml
def new
@user = User.new

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @user }
end
end

# GET /users/1/edit
def edit
@user = User.find(params[:id])
end

# POST /users
# POST /users.xml
def create
@user = User.new(params[:user])

respond_to do |format|
if @user.save
format.html { redirect_to(@user, :notice => 'User was successfully created.') }
format.xml { render :xml => @user, :status => :created, :location => @user }
else
format.html { render :action => "new" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end

# PUT /users/1
# PUT /users/1.xml
def update
@user = User.find(params[:id])

respond_to do |format|
if @user.update_attributes(params[:user])
format.html { redirect_to(@user, :notice => 'User was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end

# DELETE /users/1
# DELETE /users/1.xml
def destroy
@user = User.find(params[:id])
@user.destroy

respond_to do |format|
format.html { redirect_to(users_url) }
format.xml { head :ok }
end
end
end
2 changes: 2 additions & 0 deletions app/helpers/users_helper.rb
@@ -0,0 +1,2 @@
module UsersHelper
end
3 changes: 3 additions & 0 deletions app/models/user.rb
@@ -0,0 +1,3 @@
class User < ActiveRecord::Base
replicated_model()
end
25 changes: 25 additions & 0 deletions app/views/users/_form.html.erb
@@ -0,0 +1,25 @@
<%= form_for(@user) do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>

<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :birthday %><br />
<%= f.date_select :birthday %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/users/edit.html.erb
@@ -0,0 +1,6 @@
<h1>Editing user</h1>

<%= render 'form' %>
<%= link_to 'Show', @user %> |
<%= link_to 'Back', users_path %>
25 changes: 25 additions & 0 deletions app/views/users/index.html.erb
@@ -0,0 +1,25 @@
<h1>Listing users</h1>

<table>
<tr>
<th>Name</th>
<th>Birthday</th>
<th></th>
<th></th>
<th></th>
</tr>

<% @users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= user.birthday %></td>
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'New User', new_user_path %>
5 changes: 5 additions & 0 deletions app/views/users/new.html.erb
@@ -0,0 +1,5 @@
<h1>New user</h1>

<%= render 'form' %>
<%= link_to 'Back', users_path %>
15 changes: 15 additions & 0 deletions app/views/users/show.html.erb
@@ -0,0 +1,15 @@
<p id="notice"><%= notice %></p>

<p>
<b>Name:</b>
<%= @user.name %>
</p>

<p>
<b>Birthday:</b>
<%= @user.birthday %>
</p>


<%= link_to 'Edit', edit_user_path(@user) %> |
<%= link_to 'Back', users_path %>
22 changes: 7 additions & 15 deletions config/database.yml
@@ -1,22 +1,14 @@
# SQLite version 3.x
# gem install sqlite3-ruby (not necessary on OS X Leopard)
#This is the master database
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
adapter: mysql
host: localhost
database: octopus_replication_1

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
adapter: mysql
host: localhost
database: octopus_replication_1

production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
1 change: 1 addition & 0 deletions config/initializers/octopus.rb
@@ -0,0 +1 @@
require "octopus"
2 changes: 2 additions & 0 deletions config/routes.rb
@@ -1,4 +1,6 @@
OctopusReplicationExample::Application.routes.draw do |map|
resources :users

# The priority is based upon order of creation:
# first created -> highest priority.

Expand Down
19 changes: 19 additions & 0 deletions config/shards.yml
@@ -0,0 +1,19 @@
development:
replicated: true
shards:
slave1:
adapter: mysql
host: localhost
database: octopus_replication_2
slave2:
adapter: mysql
host: localhost
database: octopus_replication_3
slave3:
adapter: mysql
host: localhost
database: octopus_replication_4
slave4:
adapter: mysql
host: localhost
database: octopus_replication_5
16 changes: 16 additions & 0 deletions db/migrate/20100622180217_create_users.rb
@@ -0,0 +1,16 @@
class CreateUsers < ActiveRecord::Migration
using(:master, :slave1, :slave2, :slave3, :slave4)

def self.up
create_table :users do |t|
t.string :name
t.date :birthday

t.timestamps
end
end

def self.down
drop_table :users
end
end
22 changes: 22 additions & 0 deletions db/schema.rb
@@ -0,0 +1,22 @@
# This file is auto-generated from the current state of the database. Instead of editing this file,
# please use the migrations feature of Active Record to incrementally modify your database, and
# then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your database schema. If you need
# to create the application database on another system, you should be using db:schema:load, not running
# all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20100622180217) do

create_table "users", :id => false, :force => true do |t|
t.integer "id", :null => false
t.string "name"
t.date "birthday"
t.datetime "created_at"
t.datetime "updated_at"
end

end
35 changes: 35 additions & 0 deletions lib/tasks/setup.rake
@@ -0,0 +1,35 @@
require "octopus"
namespace :db do
desc 'Build the databases for tests'
task :build_databases do
mysql_user = ENV['MYSQL_USER'] || "root"

(1..4).each do |idx|
%x( echo "create DATABASE octopus_replication_#{idx} DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci " | mysql --user=#{mysql_user})
end
end

desc 'Drop the tests databases'
task :drop_databases do
mysql_user = ENV['MYSQL_USER'] || "root"

(1..4).each do |idx|
%x( mysqladmin --user=#{mysql_user} -f drop octopus_replication_#{idx} )
end
end

desc 'Create tables on tests databases'
task :create_tables do
[:slave1, :slave2, :slave3, :slave4].each do |shard_symbol|
ActiveRecord::Base.using(shard_symbol).connection.create_table(:schema_migrations) do |u|
u.string :version, :unique => true, :null => false
end
end
end

desc 'Prepare the test databases'
task :prepare => [:drop_databases, :build_databases, :create_tables]
end



56 changes: 56 additions & 0 deletions public/stylesheets/scaffold.css
@@ -0,0 +1,56 @@
body { background-color: #fff; color: #333; }

body, p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}

pre {
background-color: #eee;
padding: 10px;
font-size: 11px;
}

a { color: #000; }
a:visited { color: #666; }
a:hover { color: #fff; background-color:#000; }

div.field, div.actions {
margin-bottom: 10px;
}

#notice {
color: green;
}

.field_with_errors {
padding: 2px;
background-color: red;
display: table;
}

#error_explanation {
width: 450px;
border: 2px solid red;
padding: 7px;
padding-bottom: 0;
margin-bottom: 20px;
background-color: #f0f0f0;
}

#error_explanation h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px;
margin-bottom: 0px;
background-color: #c00;
color: #fff;
}

#error_explanation ul li {
font-size: 12px;
list-style: square;
}
9 changes: 9 additions & 0 deletions test/fixtures/users.yml
@@ -0,0 +1,9 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

one:
name: MyString
birthday: 2010-06-22

two:
name: MyString
birthday: 2010-06-22

0 comments on commit 638866f

Please sign in to comment.