forked from ryanb/railscasts-episodes
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
60 changed files
with
10,128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.bundle | ||
db/*.sqlite3 | ||
log/*.log | ||
tmp/**/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
3
episode-243/anycity/app/controllers/application_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class ApplicationController < ActionController::Base | ||
protect_from_forgery | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module ApplicationHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module CitiesHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<% title "Edit City" %> | ||
<%= render 'form' %> | ||
|
||
<p> | ||
<%= link_to "Show", @city %> | | ||
<%= link_to "View All", cities_path %> | ||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<% title "New City" %> | ||
<%= render 'form' %> | ||
|
||
<p><%= link_to "Back to List", cities_path %></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
19
episode-243/anycity/app/views/layouts/application.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Load the rails application | ||
require File.expand_path('../application', __FILE__) | ||
|
||
# Initialize the rails application | ||
Anycity::Application.initialize! |
Oops, something went wrong.