Permalink
Browse files

added user model and setup queueing for user after_create

  • Loading branch information...
Ryan R. Smith
Ryan R. Smith committed May 22, 2011
1 parent 84162e1 commit fdef01e0571728544bc80b78d8d4d66fc604709f
View
@@ -5,7 +5,8 @@ gem 'rails', '3.0.7'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
-gem 'sqlite3'
+gem 'pg'
+gem 'queue_classic'
# Use unicorn as the web server
# gem 'unicorn'
View
@@ -0,0 +1,78 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ abstract (1.0.0)
+ actionmailer (3.0.7)
+ actionpack (= 3.0.7)
+ mail (~> 2.2.15)
+ actionpack (3.0.7)
+ activemodel (= 3.0.7)
+ activesupport (= 3.0.7)
+ builder (~> 2.1.2)
+ erubis (~> 2.6.6)
+ i18n (~> 0.5.0)
+ rack (~> 1.2.1)
+ rack-mount (~> 0.6.14)
+ rack-test (~> 0.5.7)
+ tzinfo (~> 0.3.23)
+ activemodel (3.0.7)
+ activesupport (= 3.0.7)
+ builder (~> 2.1.2)
+ i18n (~> 0.5.0)
+ activerecord (3.0.7)
+ activemodel (= 3.0.7)
+ activesupport (= 3.0.7)
+ arel (~> 2.0.2)
+ tzinfo (~> 0.3.23)
+ activeresource (3.0.7)
+ activemodel (= 3.0.7)
+ activesupport (= 3.0.7)
+ activesupport (3.0.7)
+ arel (2.0.10)
+ builder (2.1.2)
+ erubis (2.6.6)
+ abstract (>= 1.0.0)
+ i18n (0.5.0)
+ json (1.5.1)
+ mail (2.2.19)
+ activesupport (>= 2.3.6)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
+ mime-types (1.16)
+ pg (0.11.0)
+ polyglot (0.3.1)
+ queue_classic (0.3.1)
+ json
+ pg (>= 0.10.1)
+ rack (1.2.2)
+ rack-mount (0.6.14)
+ rack (>= 1.0.0)
+ rack-test (0.5.7)
+ rack (>= 1.0)
+ rails (3.0.7)
+ actionmailer (= 3.0.7)
+ actionpack (= 3.0.7)
+ activerecord (= 3.0.7)
+ activeresource (= 3.0.7)
+ activesupport (= 3.0.7)
+ bundler (~> 1.0)
+ railties (= 3.0.7)
+ railties (3.0.7)
+ actionpack (= 3.0.7)
+ activesupport (= 3.0.7)
+ rake (>= 0.8.7)
+ thor (~> 0.14.4)
+ rake (0.8.7)
+ thor (0.14.6)
+ treetop (1.4.9)
+ polyglot (>= 0.3.1)
+ tzinfo (0.3.27)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ pg
+ queue_classic
+ rails (= 3.0.7)
View
@@ -3,5 +3,7 @@
require File.expand_path('../config/application', __FILE__)
require 'rake'
+require 'queue_classic'
+require 'queue_classic/tasks'
QueueClassicExample::Application.load_tasks
@@ -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
@@ -0,0 +1,13 @@
+module UsersHelper
+
+ def self.send_email(user_id)
+ user = User.find(user_id)
+ puts "sending email to #{user.id}"
+ end
+
+ def self.make_avatar(user_id)
+ user = User.find(user_id)
+ puts "making avatar for #{user.id}"
+ end
+
+end
View
@@ -0,0 +1,8 @@
+class User < ActiveRecord::Base
+
+ def after_create
+ $email_jobs.enqueue("UsersHelper.send_email", self.id)
+ $image_jobs.enqueue("UsersHelper.make_avatar", self.id)
+ end
+
+end
@@ -0,0 +1,17 @@
+<%= 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="actions">
+ <%= f.submit %>
+ </div>
+<% end %>
@@ -0,0 +1,6 @@
+<h1>Editing user</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @user %> |
+<%= link_to 'Back', users_path %>
@@ -0,0 +1,21 @@
+<h1>Listing users</h1>
+
+<table>
+ <tr>
+ <th></th>
+ <th></th>
+ <th></th>
+ </tr>
+
+<% @users.each do |user| %>
+ <tr>
+ <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 %>
@@ -0,0 +1,5 @@
+<h1>New user</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', users_path %>
@@ -0,0 +1,5 @@
+<p id="notice"><%= notice %></p>
+
+
+<%= link_to 'Edit', edit_user_path(@user) %> |
+<%= link_to 'Back', users_path %>
View
@@ -1,22 +1,7 @@
# SQLite version 3.x
# gem install sqlite3
development:
- adapter: sqlite3
- database: db/development.sqlite3
- pool: 5
- timeout: 5000
-
-# 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
-
-production:
- adapter: sqlite3
- database: db/production.sqlite3
+ adapter: postgresql
+ database: queue_classic_example
pool: 5
timeout: 5000
@@ -0,0 +1,4 @@
+ENV["DATABASE_URL"] = "postgres://ryandotsmith:@localhost/queue_classic_example"
+
+$email_jobs = QC::Queue.new("email_jobs")
+$image_jobs = QC::Queue.new("image_jobs")
View
@@ -1,4 +1,6 @@
QueueClassicExample::Application.routes.draw do
+ resources :users
+
# The priority is based upon order of creation:
# first created -> highest priority.
@@ -0,0 +1,37 @@
+class AddQueueClassicTables < ActiveRecord::Migration
+
+ def self.up
+ # Create the default queue.
+ # This will allow us to enqueue using QC.enqueue
+ create_table :queue_classic_jobs do |t|
+ t.text :details
+ t.timestamp :locked_at
+ end
+ add_index :queue_classic_jobs, :id
+
+ # We need this queue to process the sending of emails.
+ # Perhaps emails must be sent ASAP. We don't want an email
+ # wiating for an image resize to finish...
+ create_table :email_jobs do |t|
+ t.text :details
+ t.timestamp :locked_at
+ end
+ add_index :email_jobs, :id
+
+ # Working with images is slow. Let's not slow down our system
+ # wiating on these things to finish.
+ create_table :image_jobs do |t|
+ t.text :details
+ t.timestamp :locked_at
+ end
+ add_index :image_jobs, :id
+
+ end
+
+ def self.down
+ drop_table :queue_classic_jobs
+ drop_table :email_jobs
+ drop_table :image_jobs
+ end
+
+end
@@ -0,0 +1,12 @@
+class CreateUsers < ActiveRecord::Migration
+ def self.up
+ create_table :users do |t|
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :users
+ end
+end
@@ -0,0 +1,7 @@
+class AddAdditionQueues < ActiveRecord::Migration
+ def self.up
+ end
+
+ def self.down
+ end
+end
Oops, something went wrong.

0 comments on commit fdef01e

Please sign in to comment.