Created
February 21, 2012 14:35
-
-
Save skalnik/1876817 to your computer and use it in GitHub Desktop.
Rails Workshop
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
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 = args[:with] | |
puts "updating #{file} with:" | |
puts content | |
File.delete(file) | |
File.open(file, 'w') do |file| | |
file.write content | |
end | |
else | |
raise ArguementError, 'Expecting with argument' | |
end | |
wait_for_enter | |
end | |
def write(file, args) | |
if content = args[:with] | |
puts "writing #{file} with:" | |
puts content | |
File.open(file, 'w') do |file| | |
file.write content | |
end | |
else | |
raise ArguementError, 'Expecting with argument' | |
end | |
wait_for_enter | |
end | |
def self.finish_up | |
puts "All done!" | |
wait_for_ener | |
clean_up | |
end | |
def self.clean_up | |
puts "Cleaning up!" | |
`rm -rf blog` | |
end | |
at_exit { `cd ..`; finish_up } | |
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' | |
Demo.new do | |
puts "Install Rails" | |
puts "Latest version is 3.2.1" | |
run "gem install rails" | |
puts "Generate app named blog" | |
run "rails new blog" | |
puts "That made bunch of files" | |
puts "The Rails skeleton" | |
puts "It also installed dependencies" | |
Dir.chdir "blog" | |
puts "We actually have a running app" | |
echo_run('rails server', 'rails server -d') | |
open 'http://0.0.0.0:3000' | |
puts "Lets generate a model named post" | |
puts "Has a string as title and text as body" | |
run "rails generate model Post title:string body:text" | |
open 'app/models/post.rb' | |
puts "It's empty!? Dynamic from table!" | |
open 'db/migrate/*create_posts.rb' | |
puts "Title, check" | |
puts "Body, check" | |
puts "Timestamps? Automatic!" | |
puts "So what's a model do for us?" | |
keynote | |
puts "Lets setup our database and run migrations" | |
run "rake db:migrate" | |
puts "rails console" | |
puts <<-CONSOLE | |
>> # Persistence: | |
>> first_post = Post.new(:title => 'First post!', :body => \"Welcome to my new blog!\nI hope you enjoy your stay!\") | |
>> first_post.new_record? | |
>> first_post.save | |
>> Post.count | |
>> first_post.new_record? | |
>> first_post.body = 'New post body!' | |
>> first_post.save | |
>> # Database Access: | |
>> Post.find(1) | |
>> Post.first | |
>> Post.all | |
>> # Behavior: | |
>> class Post | |
>> def to_newspaper_format | |
>> \"\#{title}\n\#{created_at}\n\n\#{body}\" | |
>> end | |
>> end | |
>> puts first_post.to_newspaper_format | |
CONSOLE | |
wait_for_enter | |
# Generate controller with all our actions | |
run "rails generate controller Posts index show edit update new create destroy" | |
puts "What's a controller do for us?" | |
keynote | |
posts_controller = <<-CODE | |
class PostsController < ApplicationController | |
def index | |
@posts = Post.all | |
end | |
def new | |
@post = Post.new | |
end | |
def create | |
@post = Post.new(params[:post]) | |
if @post.save | |
redirect_to @post | |
else | |
render :action => 'new' | |
end | |
end | |
def show | |
@post = Post.find(params[:id]) | |
end | |
def edit | |
@post = Post.find(params[:id]) | |
end | |
def update | |
@post = Post.find(params[:id]) | |
if @post.update_attributes(params[:post]) | |
redirect_to @post | |
else | |
render :action => 'edit' | |
end | |
end | |
def destroy | |
@post = Post.find(params[:id]) | |
@post.destroy | |
redirect_to posts_url | |
end | |
end | |
CODE | |
update 'app/controllers/posts_controller.rb', with: posts_controller | |
puts "What about the views?" | |
keynote | |
posts_index = <<-CODE | |
<h1>Posts</h1> | |
<p> | |
<%= link_to 'New Post', new_post_path %> | |
</p> | |
<% @posts.each do |post| %> | |
<h3><%= post.title %></h3> | |
<p class="date"><%= post.created_at %></p> | |
<p><%= post.body %></p> | |
<p><%= link_to 'Edit', edit_post_path(post) %></p> | |
<% end %> | |
CODE | |
update 'app/views/posts/index.html.erb', with: posts_index | |
puts "We want RESTful routes" | |
routes = <<-CODE | |
Blog::Application.routes.draw do | |
resources :posts | |
root :to => "posts#index" | |
end | |
CODE | |
update 'config/routes.rb', with: routes | |
run "rm public/index.html" | |
posts_new = <<-CODE | |
<h1>Create Post</h1> | |
<%= form_for @post do |f| %> | |
<p> | |
<%= f.label :title %> <br/> | |
<%= f.text_field :title %> | |
</p> | |
<p> | |
<%= f.label :body %><br/> | |
<%= f.text_area :body %> | |
</p> | |
<%= f.submit %> | |
<% end %> | |
CODE | |
update 'app/views/posts/new.html.erb', with: posts_new | |
posts_edit = <<-CODE | |
<h1>Editing Post</h1> | |
<%= render 'form', { post: @post } %> | |
CODE | |
update 'app/views/posts/edit.html.erb', with: posts_edit | |
posts_form_partial = <<-CODE | |
<%= form_for post do |f| %> | |
<p> | |
<%= f.label :title %> <br/> | |
<%= f.text_field :title %> | |
</p> | |
<p> | |
<%= f.label :body %><br/> | |
<%= f.text_area :body %> | |
</p> | |
<%= f.submit %> | |
<% end %> | |
CODE | |
write 'app/views/posts/_form.html.erb', with: posts_form_partial | |
posts_new = <<-CODE | |
<h1>Create Post</h1> | |
<%= render 'form', { post: @post } %> | |
CODE | |
update 'app/views/posts/new.html.erb', with: posts_new | |
puts "Cool! We have some posts now. Lets add comments" | |
run "rails generate model Comment name:string body:text post:references" | |
run "rails generate controller Comments create destroy" | |
open 'app/models/comment.rb' | |
open 'db/migrate/*create_comments.rb' | |
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 | |
routes = <<-CODE | |
Blog::Application.routes.draw do | |
resources :posts do | |
resources :comments | |
end | |
end | |
CODE | |
update 'config/routes.rb', with: routes | |
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 | |
posts_show = <<-CODE | |
<h1><%= @post.title %></h1> | |
<p><%= @post.created_at %></p> | |
<p><%= @post.body %></p> | |
<br/> | |
<% @post.comments.each do |comment| %> | |
<p> | |
Commenter: <%= comment.name %> | |
</p> | |
<p> | |
<%= comment.body %> | |
</p> | |
<p> | |
<%= link_to 'Delete', [comment.post, comment], :confirm => 'Are you sure?', | |
:method => :delete %> | |
</p> | |
<% end %> | |
<br/> | |
<%= form_for [@post, @post.comments.build] do |f| %> | |
<p> | |
<%= f.label :name %> <br/> | |
<%= f.text_field :name %> | |
</p> | |
<p> | |
<%= f.label :comment %><br/> | |
<%= f.text_area :comment %> | |
</p> | |
<%= f.submit %> | |
<% end %> | |
<p> | |
<%= link_to 'Posts', posts_path %>, | |
<%= link_to 'New Post', new_post_path %> | |
</p> | |
CODE | |
update 'app/views/posts/show.html.erb', with: posts_show | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment