Skip to content

Commit

Permalink
adding episode 171
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanb committed Jul 20, 2009
1 parent f7e5804 commit 9fc2839
Show file tree
Hide file tree
Showing 86 changed files with 9,679 additions and 0 deletions.
3 changes: 3 additions & 0 deletions episode-171/README
@@ -0,0 +1,3 @@
Railscasts Episode #171: Delayed Job

http://railscasts.com/episodes/171
3 changes: 3 additions & 0 deletions episode-171/mailit/.gitignore
@@ -0,0 +1,3 @@
tmp/*
log/*
*.sqlite3
Empty file added episode-171/mailit/README
Empty file.
10 changes: 10 additions & 0 deletions episode-171/mailit/Rakefile
@@ -0,0 +1,10 @@
# 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'
15 changes: 15 additions & 0 deletions episode-171/mailit/app/controllers/application_controller.rb
@@ -0,0 +1,15 @@
# 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 => '3fb7e7755b16e718d0594087efcaddc1'

# 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
end
50 changes: 50 additions & 0 deletions episode-171/mailit/app/controllers/mailings_controller.rb
@@ -0,0 +1,50 @@
class MailingsController < ApplicationController
def deliver
Delayed::Job.enqueue(MailingJob.new(params[:id]))
flash[:notice] = "Mailing currently being delivered."
redirect_to mailings_url
end

def index
@mailings = Mailing.find(:all)
end

def show
@mailing = Mailing.find(params[:id])
end

def new
@mailing = Mailing.new
end

def create
@mailing = Mailing.new(params[:mailing])
if @mailing.save
flash[:notice] = "Successfully created mailing."
redirect_to @mailing
else
render :action => 'new'
end
end

def edit
@mailing = Mailing.find(params[:id])
end

def update
@mailing = Mailing.find(params[:id])
if @mailing.update_attributes(params[:mailing])
flash[:notice] = "Successfully updated mailing."
redirect_to @mailing
else
render :action => 'edit'
end
end

def destroy
@mailing = Mailing.find(params[:id])
@mailing.destroy
flash[:notice] = "Successfully destroyed mailing."
redirect_to mailings_url
end
end
3 changes: 3 additions & 0 deletions episode-171/mailit/app/helpers/application_helper.rb
@@ -0,0 +1,3 @@
# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
end
23 changes: 23 additions & 0 deletions episode-171/mailit/app/helpers/layout_helper.rb
@@ -0,0 +1,23 @@
# These helper methods can be called in your template to set variables to be used in the layout
# This module should be included in all views globally,
# to do so you may need to add this line to your ApplicationController
# helper :layout
module LayoutHelper
def title(page_title, show_title = true)
@content_for_title = page_title.to_s
@show_title = show_title
end

def show_title?
@show_title
end

def stylesheet(*args)
content_for(:head) { stylesheet_link_tag(*args.map(&:to_s)) }
end

def javascript(*args)
args = args.map { |arg| arg == :defaults ? arg : arg.to_s }
content_for(:head) { javascript_include_tag(*args) }
end
end
2 changes: 2 additions & 0 deletions episode-171/mailit/app/helpers/mailings_helper.rb
@@ -0,0 +1,2 @@
module MailingsHelper
end
6 changes: 6 additions & 0 deletions episode-171/mailit/app/models/mailing.rb
@@ -0,0 +1,6 @@
class Mailing < ActiveRecord::Base
def deliver
sleep 10 # placeholder for sending email
update_attribute(:delivered_at, Time.now)
end
end
22 changes: 22 additions & 0 deletions episode-171/mailit/app/views/layouts/application.html.erb
@@ -0,0 +1,22 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title><%= h(yield(:title) || "Untitled") %></title>
<%= stylesheet_link_tag 'application' %>
<%= yield(:head) %>
</head>
<body>
<div id="container">
<%- flash.each do |name, msg| -%>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
<%- end -%>
<%- if show_title? -%>
<h1><%=h yield(:title) %></h1>
<%- end -%>
<%= yield %>
</div>
</body>
</html>
16 changes: 16 additions & 0 deletions episode-171/mailit/app/views/mailings/_form.html.erb
@@ -0,0 +1,16 @@
<%= error_messages_for :mailing %>
<% form_for @mailing do |f| %>
<p>
<%= f.label :subject %><br />
<%= f.text_field :subject %>
</p>
<p>
<%= f.label :content %><br />
<%= f.text_area :content %>
</p>
<p>
<%= f.label :delivered_at %><br />
<%= f.datetime_select :delivered_at %>
</p>
<p><%= f.submit "Submit" %></p>
<% end %>
8 changes: 8 additions & 0 deletions episode-171/mailit/app/views/mailings/edit.html.erb
@@ -0,0 +1,8 @@
<% title "Edit Mailing" %>
<%= render :partial => 'form' %>

<p>
<%= link_to "Show", @mailing %> |
<%= link_to "View All", mailings_path %>
</p>
15 changes: 15 additions & 0 deletions episode-171/mailit/app/views/mailings/index.html.erb
@@ -0,0 +1,15 @@
<% title "Mailings" %>
<% for mailing in @mailings %>
<div class="mailing">
<h2><%= link_to h(mailing.subject), mailing %></h2>
<% unless mailing.delivered_at.nil? %>
Delivered on: <%= mailing.delivered_at.to_s(:long) %><br />
<% end %>
<%= link_to "Edit", edit_mailing_path(mailing) %> |
<%= link_to "Destroy", mailing, :confirm => 'Are you sure?', :method => :delete %> |
<%= link_to "Deliver", deliver_mailing_path(mailing), :confirm => 'Are you sure?', :method => :post %>
</div>
<% end %>

<p><%= link_to "New Mailing", new_mailing_path %></p>
5 changes: 5 additions & 0 deletions episode-171/mailit/app/views/mailings/new.html.erb
@@ -0,0 +1,5 @@
<% title "New Mailing" %>
<%= render :partial => 'form' %>

<p><%= link_to "Back to List", mailings_path %></p>
20 changes: 20 additions & 0 deletions episode-171/mailit/app/views/mailings/show.html.erb
@@ -0,0 +1,20 @@
<% title "Mailing" %>

<p>
<strong>Subject:</strong>
<%=h @mailing.subject %>
</p>
<p>
<strong>Content:</strong>
<%=h @mailing.content %>
</p>
<p>
<strong>Delivered At:</strong>
<%=h @mailing.delivered_at %>
</p>

<p>
<%= link_to "Edit", edit_mailing_path(@mailing) %> |
<%= link_to "Destroy", @mailing, :confirm => 'Are you sure?', :method => :delete %> |
<%= link_to "View All", mailings_path %>
</p>
110 changes: 110 additions & 0 deletions episode-171/mailit/config/boot.rb
@@ -0,0 +1,110 @@
# 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)
Rails::GemDependency.add_frozen_gem_path
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!
19 changes: 19 additions & 0 deletions episode-171/mailit/config/database.yml
@@ -0,0 +1,19 @@
# SQLite version 3.x
# gem install sqlite3-ruby (not necessary on OS X Leopard)
development:
adapter: sqlite3
database: db/development.sqlite3
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
timeout: 5000

production:
adapter: sqlite3
database: db/production.sqlite3
timeout: 5000

0 comments on commit 9fc2839

Please sign in to comment.