Skip to content

Commit

Permalink
adding episode 243
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanb committed Dec 6, 2010
1 parent 7451c6a commit 1ce8665
Show file tree
Hide file tree
Showing 60 changed files with 10,128 additions and 0 deletions.
10 changes: 10 additions & 0 deletions episode-243/README
@@ -0,0 +1,10 @@
Railscasts Episode #243: Beanstalkd and Stalker

http://railscasts.com/episodes/243

Commands

beanstalkd -d
killall beanstalkd
beanstalkd -d -b ...
stalk ./config/jobs.rb
4 changes: 4 additions & 0 deletions episode-243/anycity/.gitignore
@@ -0,0 +1,4 @@
.bundle
db/*.sqlite3
log/*.log
tmp/**/*
6 changes: 6 additions & 0 deletions episode-243/anycity/Gemfile
@@ -0,0 +1,6 @@
source 'http://rubygems.org'

gem 'rails', '3.0.3'
gem 'sqlite3-ruby', '1.2.5', :require => 'sqlite3'
gem 'nifty-generators'
gem 'stalker'
81 changes: 81 additions & 0 deletions episode-243/anycity/Gemfile.lock
@@ -0,0 +1,81 @@
GEM
remote: http://rubygems.org/
specs:
abstract (1.0.0)
actionmailer (3.0.3)
actionpack (= 3.0.3)
mail (~> 2.2.9)
actionpack (3.0.3)
activemodel (= 3.0.3)
activesupport (= 3.0.3)
builder (~> 2.1.2)
erubis (~> 2.6.6)
i18n (~> 0.4)
rack (~> 1.2.1)
rack-mount (~> 0.6.13)
rack-test (~> 0.5.6)
tzinfo (~> 0.3.23)
activemodel (3.0.3)
activesupport (= 3.0.3)
builder (~> 2.1.2)
i18n (~> 0.4)
activerecord (3.0.3)
activemodel (= 3.0.3)
activesupport (= 3.0.3)
arel (~> 2.0.2)
tzinfo (~> 0.3.23)
activeresource (3.0.3)
activemodel (= 3.0.3)
activesupport (= 3.0.3)
activesupport (3.0.3)
arel (2.0.6)
beanstalk-client (1.1.0)
builder (2.1.2)
erubis (2.6.6)
abstract (>= 1.0.0)
i18n (0.5.0)
json_pure (1.4.6)
mail (2.2.12)
activesupport (>= 2.3.6)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.16)
nifty-generators (0.4.2)
polyglot (0.3.1)
rack (1.2.1)
rack-mount (0.6.13)
rack (>= 1.0.0)
rack-test (0.5.6)
rack (>= 1.0)
rails (3.0.3)
actionmailer (= 3.0.3)
actionpack (= 3.0.3)
activerecord (= 3.0.3)
activeresource (= 3.0.3)
activesupport (= 3.0.3)
bundler (~> 1.0)
railties (= 3.0.3)
railties (3.0.3)
actionpack (= 3.0.3)
activesupport (= 3.0.3)
rake (>= 0.8.7)
thor (~> 0.14.4)
rake (0.8.7)
sqlite3-ruby (1.2.5)
stalker (0.5.0)
beanstalk-client
json_pure
thor (0.14.6)
treetop (1.4.9)
polyglot (>= 0.3.1)
tzinfo (0.3.23)

PLATFORMS
ruby

DEPENDENCIES
nifty-generators
rails (= 3.0.3)
sqlite3-ruby (= 1.2.5)
stalker
7 changes: 7 additions & 0 deletions episode-243/anycity/Rakefile
@@ -0,0 +1,7 @@
# 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.expand_path('../config/application', __FILE__)
require 'rake'

Anycity::Application.load_tasks
3 changes: 3 additions & 0 deletions episode-243/anycity/app/controllers/application_controller.rb
@@ -0,0 +1,3 @@
class ApplicationController < ActionController::Base
protect_from_forgery
end
45 changes: 45 additions & 0 deletions episode-243/anycity/app/controllers/cities_controller.rb
@@ -0,0 +1,45 @@
class CitiesController < ApplicationController
def index
@cities = City.all
end

def show
@city = City.find(params[:id])
end

def new
@city = City.new
end

def create
@city = City.new(params[:city])
if @city.save
Stalker.enqueue("city.fetch_name", :id => @city.id)
flash[:notice] = "Successfully created city."
redirect_to @city
else
render :action => 'new'
end
end

def edit
@city = City.find(params[:id])
end

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

def destroy
@city = City.find(params[:id])
@city.destroy
flash[:notice] = "Successfully destroyed city."
redirect_to cities_url
end
end
2 changes: 2 additions & 0 deletions episode-243/anycity/app/helpers/application_helper.rb
@@ -0,0 +1,2 @@
module ApplicationHelper
end
2 changes: 2 additions & 0 deletions episode-243/anycity/app/helpers/cities_helper.rb
@@ -0,0 +1,2 @@
module CitiesHelper
end
23 changes: 23 additions & 0 deletions episode-243/anycity/app/helpers/error_messages_helper.rb
@@ -0,0 +1,23 @@
module ErrorMessagesHelper
# Render error messages for the given objects. The :message and :header_message options are allowed.
def error_messages_for(*objects)
options = objects.extract_options!
options[:header_message] ||= "Invalid Fields"
options[:message] ||= "Correct the following errors and try again."
messages = objects.compact.map { |o| o.errors.full_messages }.flatten
unless messages.empty?
content_tag(:div, :class => "error_messages") do
list_items = messages.map { |msg| content_tag(:li, msg) }
content_tag(:h2, options[:header_message]) + content_tag(:p, options[:message]) + content_tag(:ul, list_items.join.html_safe)
end
end
end

module FormBuilderAdditions
def error_messages(options = {})
@template.error_messages_for(@object, options)
end
end
end

ActionView::Helpers::FormBuilder.send(:include, ErrorMessagesHelper::FormBuilderAdditions)
22 changes: 22 additions & 0 deletions episode-243/anycity/app/helpers/layout_helper.rb
@@ -0,0 +1,22 @@
# 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) { h(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) }
end

def javascript(*args)
content_for(:head) { javascript_include_tag(*args) }
end
end
9 changes: 9 additions & 0 deletions episode-243/anycity/app/models/city.rb
@@ -0,0 +1,9 @@
class City < ActiveRecord::Base
attr_accessible :zip_code, :name

def fetch_name
url = "http://ws.geonames.org/postalCodeLookupJSON?postalcode=#{zip_code}&country=US"
json = Net::HTTP.get_response(URI.parse(url)).body
update_attribute(:name, JSON.parse(json)["postalcodes"].first["placeName"])
end
end
8 changes: 8 additions & 0 deletions episode-243/anycity/app/views/cities/_form.html.erb
@@ -0,0 +1,8 @@
<%= form_for @city do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :zip_code %><br />
<%= f.text_field :zip_code %>
</p>
<p><%= f.submit %></p>
<% end %>
8 changes: 8 additions & 0 deletions episode-243/anycity/app/views/cities/edit.html.erb
@@ -0,0 +1,8 @@
<% title "Edit City" %>
<%= render 'form' %>

<p>
<%= link_to "Show", @city %> |
<%= link_to "View All", cities_path %>
</p>
19 changes: 19 additions & 0 deletions episode-243/anycity/app/views/cities/index.html.erb
@@ -0,0 +1,19 @@
<% title "Cities" %>

<table>
<tr>
<th>Zip Code</th>
<th>Name</th>
</tr>
<% for city in @cities %>
<tr>
<td><%= city.zip_code %></td>
<td><%= city.name %></td>
<td><%= link_to "Show", city %></td>
<td><%= link_to "Edit", edit_city_path(city) %></td>
<td><%= link_to "Destroy", city, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>

<p><%= link_to "New City", new_city_path %></p>
5 changes: 5 additions & 0 deletions episode-243/anycity/app/views/cities/new.html.erb
@@ -0,0 +1,5 @@
<% title "New City" %>
<%= render 'form' %>

<p><%= link_to "Back to List", cities_path %></p>
16 changes: 16 additions & 0 deletions episode-243/anycity/app/views/cities/show.html.erb
@@ -0,0 +1,16 @@
<% title "City" %>

<p>
<strong>Zip Code:</strong>
<%= @city.zip_code %>
</p>
<p>
<strong>Name:</strong>
<%= @city.name %>
</p>

<p>
<%= link_to "Edit", edit_city_path(@city) %> |
<%= link_to "Destroy", @city, :confirm => 'Are you sure?', :method => :delete %> |
<%= link_to "View All", cities_path %>
</p>
19 changes: 19 additions & 0 deletions episode-243/anycity/app/views/layouts/application.html.erb
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<title><%= content_for?(:title) ? yield(:title) : "Untitled" %></title>
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
<%= yield(:head) %>
</head>
<body>
<div id="container">
<% flash.each do |name, msg| %>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
<% end %>
<%= content_tag :h1, yield(:title) if show_title? %>
<%= yield %>
</div>
</body>
</html>
4 changes: 4 additions & 0 deletions episode-243/anycity/config.ru
@@ -0,0 +1,4 @@
# This file is used by Rack-based servers to start the application.

require ::File.expand_path('../config/environment', __FILE__)
run Anycity::Application
42 changes: 42 additions & 0 deletions episode-243/anycity/config/application.rb
@@ -0,0 +1,42 @@
require File.expand_path('../boot', __FILE__)

require 'rails/all'

# If you have a Gemfile, require the gems listed there, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env) if defined?(Bundler)

module Anycity
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.

# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)

# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named.
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]

# Activate observers that should always be running.
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer

# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'

# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de

# JavaScript files you want as :defaults (application.js is always included).
# config.action_view.javascript_expansions[:defaults] = %w(jquery rails)

# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"

# Configure sensitive parameters which will be filtered from the log file.
config.filter_parameters += [:password]
end
end
13 changes: 13 additions & 0 deletions episode-243/anycity/config/boot.rb
@@ -0,0 +1,13 @@
require 'rubygems'

# Set up gems listed in the Gemfile.
gemfile = File.expand_path('../../Gemfile', __FILE__)
begin
ENV['BUNDLE_GEMFILE'] = gemfile
require 'bundler'
Bundler.setup
rescue Bundler::GemNotFound => e
STDERR.puts e.message
STDERR.puts "Try running `bundle install`."
exit!
end if File.exist?(gemfile)
22 changes: 22 additions & 0 deletions episode-243/anycity/config/database.yml
@@ -0,0 +1,22 @@
# SQLite version 3.x
# gem install sqlite3-ruby (not necessary on OS X Leopard)
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
pool: 5
timeout: 5000
5 changes: 5 additions & 0 deletions episode-243/anycity/config/environment.rb
@@ -0,0 +1,5 @@
# Load the rails application
require File.expand_path('../application', __FILE__)

# Initialize the rails application
Anycity::Application.initialize!

0 comments on commit 1ce8665

Please sign in to comment.