Created
February 28, 2012 20:07
-
-
Save skalnik/1934785 to your computer and use it in GitHub Desktop.
Rails Workshop 2
This file contains hidden or 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
require 'tempfile' | |
class Demo | |
def initialize(&block) | |
instance_eval &block | |
end | |
def wait_for_enter | |
puts "Press enter to continue" | |
gets | |
end | |
def keynote | |
puts "Keynote!" | |
wait_for_enter | |
end | |
def run(cmd) | |
puts "$ #{cmd}" | |
puts `#{cmd}` | |
wait_for_enter | |
end | |
def echo_run(to_print, to_run) | |
puts "$ #{to_print}" | |
puts `#{to_run}` | |
wait_for_enter | |
end | |
def open(file) | |
puts "Check out #{file}" | |
`open #{file}` | |
wait_for_enter | |
end | |
def update(file, args) | |
if content = get_contents(args) | |
puts "updating #{file} with:" | |
puts content | |
File.delete(file) | |
File.open(file, 'w') do |file| | |
file.write content | |
end | |
end | |
wait_for_enter | |
end | |
def write(file, args) | |
if content = get_contents(args) | |
puts "writing #{file} with:" | |
puts content | |
File.open(file, 'w') do |file| | |
file.write content | |
end | |
end | |
wait_for_enter | |
end | |
def patch(file, args) | |
if content = get_contents(args) | |
puts "Applying:" | |
puts content | |
Tempfile.open('patch') do |patch_file| | |
patch_file.write content | |
patch_file.flush | |
puts `patch -p0 #{file} < #{patch_file.path}` | |
end | |
end | |
wait_for_enter | |
end | |
private | |
def get_contents(args) | |
if content = args[:with] | |
content | |
else | |
raise ArguementError, 'Expecting with argument' | |
end | |
end | |
end |
This file contains hidden or 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
#!/usr/bin/env ruby | |
require_relative 'demo' | |
$dir = "blog" | |
Demo.new do | |
puts "Get code from workshop 1" | |
puts "I've put our code up in a repository on Github" | |
run "git clone git://github.com/skalnik/workshop_blog.git #{$dir}" | |
puts "Gives us a simple blog with posts" | |
Dir.chdir $dir | |
run "bundle install" | |
run "rake db:create" | |
run "rake db:schema:load" | |
puts "Lets add comments" | |
puts "We'll need assocations for this" | |
keynote | |
run "rails generate model Comment author:string email:string body:text post:references" | |
puts "And run migrations" | |
run "rake db:migrate" | |
post_model = <<-CODE | |
class Post < ActiveRecord::Base | |
has_many :comments | |
end | |
CODE | |
update 'app/models/post.rb', with: post_model | |
run "rails generate controller Comments create destroy" | |
comments_controller = <<-CODE | |
class CommentsController < ApplicationController | |
def create | |
@post = Post.find(params[:post_id]) | |
@comment = @post.comments.create(params[:comment]) | |
redirect_to post_url(@post) | |
end | |
def destroy | |
@post = Post.find(params[:post_id]) | |
@comment = @post.comments.find(params[:id]) | |
@comment.destroy | |
redirect_to post_url(@post) | |
end | |
end | |
CODE | |
update 'app/controllers/comments_controller.rb', with: comments_controller | |
routes = <<-CODE | |
WorkshopBlog::Application.routes.draw do | |
resources :posts do | |
resources :comments | |
end | |
root to: 'posts#index' | |
end | |
CODE | |
update 'config/routes.rb', with: routes | |
puts "We won't use all the actions on comments, but lets just keep it simple" | |
post_show = <<-CODE | |
<%= render 'post', :post => @post %> | |
<%= form_for [post, comment] do |f| %> | |
<p> | |
<%= f.label :author, 'Name' %><br /> | |
<%= f.text_field :author %> | |
</p> | |
<p> | |
<%= f.label :email %><br /> | |
<%= f.text_field :email %> | |
</p> | |
<p> | |
<%= f.label :body %><br /> | |
<%= f.text_area :body%> | |
</p> | |
<% end %> | |
<%= link_to 'Home', posts_path %> | |
CODE | |
update 'app/views/posts/show.html.erb', with: post_show | |
puts "Lets get some validations on those comments" | |
keynote | |
comment_validations = <<-DIFF | |
--- app/models/comment.rb | |
+++ app/models/comment.rb | |
@@ -1,3 +1,7 @@ | |
class Comment < ActiveRecord::Base | |
belongs_to :post | |
+ | |
+ validates_presence_of :body, :author | |
+ validates :email, :presence => true, | |
+ :format => { :with => /^[^@]+@[^@]+$/ } | |
end | |
DIFF | |
patch 'app/models/comment.rb', with: comment_validations | |
post_validations = <<-DIFF | |
--- app/models/post.rb | |
+++ app/models/post.rb | |
@@ -1,2 +1,3 @@ | |
class Post < ActiveRecord::Base | |
+ validates_presence_of :title, :author, :body | |
end | |
DIFF | |
patch 'app/models/post.rb', with: post_validations | |
puts "Lets add some basic authentication" | |
posts_controller_authentication = <<-DIFF | |
--- app/controllers/posts_controller.rb | |
+++ app/controllers/posts_controller.rb | |
@@ -1,6 +1,9 @@ | |
class PostsController < ApplicationController | |
before_filter :find_post, :only => [:show, :edit, :update, :destroy] | |
+ http_basic_authenticate_with :name => 'skalnik', :password => 'sekret', | |
+ :except => [:index, :show] | |
+ | |
def index | |
@posts = Post.all | |
end | |
DIFF | |
patch 'app/controllers/posts_controller.rb', with: posts_controller_authentication | |
comments_controller_authentication = <<-DIFF | |
--- app/controllers/comments_controller.rb | |
+++ app/controllers/comments_controller.rb | |
@@ -1,4 +1,5 @@ | |
class CommentsController < ApplicationController | |
+ http_basic_authenticate_with :name => 'skalnik', :password => 'sekret' | |
def create | |
@post = Post.find(params[:post_id]) | |
@comment = @post.comments.create(params[:comment]) | |
DIFF | |
patch 'app/controllers/comments_controller.rb', with: comments_controller_authentication | |
end | |
def finish_up | |
puts "All done!" | |
clean_up | |
end | |
def clean_up | |
puts "Cleaning up!" | |
puts `rm -rf #{$dir}` | |
end | |
at_exit { Dir.chdir '..'; finish_up } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment