Commit 72a942dd

mo k <mo@mokhan.ca>
2012-09-06 13:37:50
add tutorial scaffold.
1 parent 4582b91
app/assets/javascripts/tutorials.js.coffee
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
app/controllers/tutorials_controller.rb
@@ -0,0 +1,2 @@
+class TutorialsController < InheritedResources::Base
+end
app/helpers/tutorials_helper.rb
@@ -0,0 +1,2 @@
+module TutorialsHelper
+end
app/models/tutorial.rb
@@ -0,0 +1,3 @@
+class Tutorial < ActiveRecord::Base
+  attr_accessible :description, :heading, :url
+end
app/views/tutorials/_form.html.erb
@@ -0,0 +1,29 @@
+<%= form_for(@tutorial) do |f| %>
+  <% if @tutorial.errors.any? %>
+    <div id="error_explanation">
+      <h2><%= pluralize(@tutorial.errors.count, "error") %> prohibited this tutorial from being saved:</h2>
+
+      <ul>
+      <% @tutorial.errors.full_messages.each do |msg| %>
+        <li><%= msg %></li>
+      <% end %>
+      </ul>
+    </div>
+  <% end %>
+
+  <div class="field">
+    <%= f.label :heading %><br />
+    <%= f.text_field :heading %>
+  </div>
+  <div class="field">
+    <%= f.label :description %><br />
+    <%= f.text_area :description %>
+  </div>
+  <div class="field">
+    <%= f.label :url %><br />
+    <%= f.text_field :url %>
+  </div>
+  <div class="actions">
+    <%= f.submit %>
+  </div>
+<% end %>
app/views/tutorials/edit.html.erb
@@ -0,0 +1,6 @@
+<h1>Editing tutorial</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @tutorial %> |
+<%= link_to 'Back', tutorials_path %>
app/views/tutorials/index.html.erb
@@ -0,0 +1,32 @@
+<% provide(:description, "The latest tutorials") -%>
+
+<div class="row">
+  <div class="span12">
+    <h1>Latest Tutorials</h1>
+
+    <table>
+      <tr>
+        <th>Heading</th>
+        <th>Description</th>
+        <th>Url</th>
+        <th></th>
+        <th></th>
+        <th></th>
+      </tr>
+
+      <% @tutorials.each do |tutorial| %>
+        <tr>
+          <td><%= tutorial.heading %></td>
+          <td><%= tutorial.description %></td>
+          <td><a href="<%= tutorial.url %>">read</a></td>
+          <td><%= link_to 'Show', tutorial %></td>
+          <td><%= link_to 'Edit', edit_tutorial_path(tutorial) %></td>
+          <td><%= link_to 'Destroy', tutorial, method: :delete, data: { confirm: 'Are you sure?' } %></td>
+        </tr>
+      <% end %>
+    </table>
+
+
+    <%= link_to 'New Tutorial', new_tutorial_path %>
+  </div>
+</div>
app/views/tutorials/new.html.erb
@@ -0,0 +1,5 @@
+<h1>New tutorial</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', tutorials_path %>
app/views/tutorials/show.html.erb
@@ -0,0 +1,20 @@
+<p id="notice"><%= notice %></p>
+
+<p>
+  <b>Heading:</b>
+  <%= @tutorial.heading %>
+</p>
+
+<p>
+  <b>Description:</b>
+  <%= @tutorial.description %>
+</p>
+
+<p>
+  <b>Url:</b>
+  <%= @tutorial.url %>
+</p>
+
+
+<%= link_to 'Edit', edit_tutorial_path(@tutorial) %> |
+<%= link_to 'Back', tutorials_path %>
config/routes.rb
@@ -1,4 +1,6 @@
 Cake::Application.routes.draw do
+  resources :tutorials
+
   ActiveAdmin.routes(self)
 
   devise_for :admin_users, ActiveAdmin::Devise.config
db/migrate/20120906132947_create_tutorials.rb
@@ -0,0 +1,12 @@
+class CreateTutorials < ActiveRecord::Migration
+  def change
+    create_table :tutorials do |t|
+      t.string :heading
+      t.text :description
+      t.string :url
+      t.integer :user_id
+
+      t.timestamps
+    end
+  end
+end
db/schema.rb
@@ -11,7 +11,7 @@
 #
 # It's strongly recommended to check this file into your version control system.
 
-ActiveRecord::Schema.define(:version => 20120825152140) do
+ActiveRecord::Schema.define(:version => 20120906132947) do
 
   create_table "active_admin_comments", :force => true do |t|
     t.integer  "resource_id",   :null => false
@@ -29,18 +29,18 @@ ActiveRecord::Schema.define(:version => 20120825152140) do
   add_index "active_admin_comments", ["resource_type", "resource_id"], :name => "index_admin_notes_on_resource_type_and_resource_id"
 
   create_table "admin_users", :force => true do |t|
-    t.string   "email",                                 :default => "", :null => false
-    t.string   "encrypted_password",     :limit => 128, :default => "", :null => false
+    t.string   "email",                  :default => "", :null => false
+    t.string   "encrypted_password",     :default => "", :null => false
     t.string   "reset_password_token"
     t.datetime "reset_password_sent_at"
     t.datetime "remember_created_at"
-    t.integer  "sign_in_count",                         :default => 0
+    t.integer  "sign_in_count",          :default => 0
     t.datetime "current_sign_in_at"
     t.datetime "last_sign_in_at"
     t.string   "current_sign_in_ip"
     t.string   "last_sign_in_ip"
-    t.datetime "created_at",                                            :null => false
-    t.datetime "updated_at",                                            :null => false
+    t.datetime "created_at",                             :null => false
+    t.datetime "updated_at",                             :null => false
   end
 
   add_index "admin_users", ["email"], :name => "index_admin_users_on_email", :unique => true
@@ -113,19 +113,28 @@ ActiveRecord::Schema.define(:version => 20120825152140) do
     t.datetime "updated_at",  :null => false
   end
 
+  create_table "tutorials", :force => true do |t|
+    t.string   "heading"
+    t.text     "description"
+    t.string   "url"
+    t.integer  "user_id"
+    t.datetime "created_at",  :null => false
+    t.datetime "updated_at",  :null => false
+  end
+
   create_table "users", :force => true do |t|
-    t.string   "email",                                 :default => "", :null => false
-    t.string   "encrypted_password",     :limit => 128, :default => "", :null => false
+    t.string   "email",                  :default => "", :null => false
+    t.string   "encrypted_password",     :default => "", :null => false
     t.string   "reset_password_token"
     t.datetime "reset_password_sent_at"
     t.datetime "remember_created_at"
-    t.integer  "sign_in_count",                         :default => 0
+    t.integer  "sign_in_count",          :default => 0
     t.datetime "current_sign_in_at"
     t.datetime "last_sign_in_at"
     t.string   "current_sign_in_ip"
     t.string   "last_sign_in_ip"
-    t.datetime "created_at",                                            :null => false
-    t.datetime "updated_at",                                            :null => false
+    t.datetime "created_at",                             :null => false
+    t.datetime "updated_at",                             :null => false
     t.string   "name"
     t.string   "website"
     t.string   "twitter"
spec/controllers/tutorials_controller_spec.rb
@@ -0,0 +1,164 @@
+require 'spec_helper'
+
+# This spec was generated by rspec-rails when you ran the scaffold generator.
+# It demonstrates how one might use RSpec to specify the controller code that
+# was generated by Rails when you ran the scaffold generator.
+#
+# It assumes that the implementation code is generated by the rails scaffold
+# generator.  If you are using any extension libraries to generate different
+# controller code, this generated spec may or may not pass.
+#
+# It only uses APIs available in rails and/or rspec-rails.  There are a number
+# of tools you can use to make these specs even more expressive, but we're
+# sticking to rails and rspec-rails APIs to keep things simple and stable.
+#
+# Compared to earlier versions of this generator, there is very limited use of
+# stubs and message expectations in this spec.  Stubs are only used when there
+# is no simpler way to get a handle on the object needed for the example.
+# Message expectations are only used when there is no simpler way to specify
+# that an instance is receiving a specific message.
+
+describe TutorialsController do
+
+  # This should return the minimal set of attributes required to create a valid
+  # Tutorial. As you add validations to Tutorial, be sure to
+  # update the return value of this method accordingly.
+  def valid_attributes
+    {}
+  end
+
+  # This should return the minimal set of values that should be in the session
+  # in order to pass any filters (e.g. authentication) defined in
+  # TutorialsController. Be sure to keep this updated too.
+  def valid_session
+    {}
+  end
+
+  describe "GET index" do
+    it "assigns all tutorials as @tutorials" do
+      tutorial = Tutorial.create! valid_attributes
+      get :index, {}, valid_session
+      assigns(:tutorials).should eq([tutorial])
+    end
+  end
+
+  describe "GET show" do
+    it "assigns the requested tutorial as @tutorial" do
+      tutorial = Tutorial.create! valid_attributes
+      get :show, {:id => tutorial.to_param}, valid_session
+      assigns(:tutorial).should eq(tutorial)
+    end
+  end
+
+  describe "GET new" do
+    it "assigns a new tutorial as @tutorial" do
+      get :new, {}, valid_session
+      assigns(:tutorial).should be_a_new(Tutorial)
+    end
+  end
+
+  describe "GET edit" do
+    it "assigns the requested tutorial as @tutorial" do
+      tutorial = Tutorial.create! valid_attributes
+      get :edit, {:id => tutorial.to_param}, valid_session
+      assigns(:tutorial).should eq(tutorial)
+    end
+  end
+
+  describe "POST create" do
+    describe "with valid params" do
+      it "creates a new Tutorial" do
+        expect {
+          post :create, {:tutorial => valid_attributes}, valid_session
+        }.to change(Tutorial, :count).by(1)
+      end
+
+      it "assigns a newly created tutorial as @tutorial" do
+        post :create, {:tutorial => valid_attributes}, valid_session
+        assigns(:tutorial).should be_a(Tutorial)
+        assigns(:tutorial).should be_persisted
+      end
+
+      it "redirects to the created tutorial" do
+        post :create, {:tutorial => valid_attributes}, valid_session
+        response.should redirect_to(Tutorial.last)
+      end
+    end
+
+    describe "with invalid params" do
+      it "assigns a newly created but unsaved tutorial as @tutorial" do
+        # Trigger the behavior that occurs when invalid params are submitted
+        Tutorial.any_instance.stub(:save).and_return(false)
+        post :create, {:tutorial => {}}, valid_session
+        assigns(:tutorial).should be_a_new(Tutorial)
+      end
+
+      it "re-renders the 'new' template" do
+        # Trigger the behavior that occurs when invalid params are submitted
+        Tutorial.any_instance.stub(:save).and_return(false)
+        post :create, {:tutorial => {}}, valid_session
+        response.should render_template("new")
+      end
+    end
+  end
+
+  describe "PUT update" do
+    describe "with valid params" do
+      it "updates the requested tutorial" do
+        tutorial = Tutorial.create! valid_attributes
+        # Assuming there are no other tutorials in the database, this
+        # specifies that the Tutorial created on the previous line
+        # receives the :update_attributes message with whatever params are
+        # submitted in the request.
+        Tutorial.any_instance.should_receive(:update_attributes).with({'these' => 'params'})
+        put :update, {:id => tutorial.to_param, :tutorial => {'these' => 'params'}}, valid_session
+      end
+
+      it "assigns the requested tutorial as @tutorial" do
+        tutorial = Tutorial.create! valid_attributes
+        put :update, {:id => tutorial.to_param, :tutorial => valid_attributes}, valid_session
+        assigns(:tutorial).should eq(tutorial)
+      end
+
+      it "redirects to the tutorial" do
+        tutorial = Tutorial.create! valid_attributes
+        put :update, {:id => tutorial.to_param, :tutorial => valid_attributes}, valid_session
+        response.should redirect_to(tutorial)
+      end
+    end
+
+    describe "with invalid params" do
+      it "assigns the tutorial as @tutorial" do
+        tutorial = Tutorial.create! valid_attributes
+        # Trigger the behavior that occurs when invalid params are submitted
+        Tutorial.any_instance.stub(:save).and_return(false)
+        put :update, {:id => tutorial.to_param, :tutorial => {}}, valid_session
+        assigns(:tutorial).should eq(tutorial)
+      end
+
+      it "re-renders the 'edit' template" do
+        tutorial = Tutorial.create! valid_attributes
+        # Trigger the behavior that occurs when invalid params are submitted
+        Tutorial.any_instance.stub(:save).and_return(false)
+        put :update, {:id => tutorial.to_param, :tutorial => {}}, valid_session
+        response.should render_template("edit")
+      end
+    end
+  end
+
+  describe "DELETE destroy" do
+    it "destroys the requested tutorial" do
+      tutorial = Tutorial.create! valid_attributes
+      expect {
+        delete :destroy, {:id => tutorial.to_param}, valid_session
+      }.to change(Tutorial, :count).by(-1)
+    end
+
+    it "redirects to the tutorials list" do
+      tutorial = Tutorial.create! valid_attributes
+      delete :destroy, {:id => tutorial.to_param}, valid_session
+      response.should redirect_to(tutorials_url)
+    end
+  end
+
+end
spec/helpers/tutorials_helper_spec.rb
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+# Specs in this file have access to a helper object that includes
+# the TutorialsHelper. For example:
+#
+# describe TutorialsHelper do
+#   describe "string concat" do
+#     it "concats two strings with spaces" do
+#       helper.concat_strings("this","that").should == "this that"
+#     end
+#   end
+# end
+describe TutorialsHelper do
+  pending "add some examples to (or delete) #{__FILE__}"
+end
spec/models/tutorial_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe Tutorial do
+  pending "add some examples to (or delete) #{__FILE__}"
+end
spec/requests/tutorials_spec.rb
@@ -0,0 +1,11 @@
+require 'spec_helper'
+
+describe "Tutorials" do
+  describe "GET /tutorials" do
+    it "works! (now write some real specs)" do
+      # Run the generator again with the --webrat flag if you want to use webrat methods/matchers
+      get tutorials_path
+      response.status.should be(200)
+    end
+  end
+end
spec/routing/tutorials_routing_spec.rb
@@ -0,0 +1,35 @@
+require "spec_helper"
+
+describe TutorialsController do
+  describe "routing" do
+
+    it "routes to #index" do
+      get("/tutorials").should route_to("tutorials#index")
+    end
+
+    it "routes to #new" do
+      get("/tutorials/new").should route_to("tutorials#new")
+    end
+
+    it "routes to #show" do
+      get("/tutorials/1").should route_to("tutorials#show", :id => "1")
+    end
+
+    it "routes to #edit" do
+      get("/tutorials/1/edit").should route_to("tutorials#edit", :id => "1")
+    end
+
+    it "routes to #create" do
+      post("/tutorials").should route_to("tutorials#create")
+    end
+
+    it "routes to #update" do
+      put("/tutorials/1").should route_to("tutorials#update", :id => "1")
+    end
+
+    it "routes to #destroy" do
+      delete("/tutorials/1").should route_to("tutorials#destroy", :id => "1")
+    end
+
+  end
+end
spec/views/tutorials/edit.html.erb_spec.rb
@@ -0,0 +1,22 @@
+require 'spec_helper'
+
+describe "tutorials/edit" do
+  before(:each) do
+    @tutorial = assign(:tutorial, stub_model(Tutorial,
+      :heading => "MyString",
+      :description => "MyText",
+      :url => "MyString"
+    ))
+  end
+
+  it "renders the edit tutorial form" do
+    render
+
+    # Run the generator again with the --webrat flag if you want to use webrat matchers
+    assert_select "form", :action => tutorials_path(@tutorial), :method => "post" do
+      assert_select "input#tutorial_heading", :name => "tutorial[heading]"
+      assert_select "textarea#tutorial_description", :name => "tutorial[description]"
+      assert_select "input#tutorial_url", :name => "tutorial[url]"
+    end
+  end
+end
spec/views/tutorials/index.html.erb_spec.rb
@@ -0,0 +1,26 @@
+require 'spec_helper'
+
+describe "tutorials/index" do
+  before(:each) do
+    assign(:tutorials, [
+      stub_model(Tutorial,
+        :heading => "Heading",
+        :description => "MyText",
+        :url => "Url"
+      ),
+      stub_model(Tutorial,
+        :heading => "Heading",
+        :description => "MyText",
+        :url => "Url"
+      )
+    ])
+  end
+
+  it "renders a list of tutorials" do
+    render
+    # Run the generator again with the --webrat flag if you want to use webrat matchers
+    assert_select "tr>td", :text => "Heading".to_s, :count => 2
+    assert_select "tr>td", :text => "MyText".to_s, :count => 2
+    assert_select "tr>td", :text => "Url".to_s, :count => 2
+  end
+end
spec/views/tutorials/new.html.erb_spec.rb
@@ -0,0 +1,22 @@
+require 'spec_helper'
+
+describe "tutorials/new" do
+  before(:each) do
+    assign(:tutorial, stub_model(Tutorial,
+      :heading => "MyString",
+      :description => "MyText",
+      :url => "MyString"
+    ).as_new_record)
+  end
+
+  it "renders new tutorial form" do
+    render
+
+    # Run the generator again with the --webrat flag if you want to use webrat matchers
+    assert_select "form", :action => tutorials_path, :method => "post" do
+      assert_select "input#tutorial_heading", :name => "tutorial[heading]"
+      assert_select "textarea#tutorial_description", :name => "tutorial[description]"
+      assert_select "input#tutorial_url", :name => "tutorial[url]"
+    end
+  end
+end
spec/views/tutorials/show.html.erb_spec.rb
@@ -0,0 +1,19 @@
+require 'spec_helper'
+
+describe "tutorials/show" do
+  before(:each) do
+    @tutorial = assign(:tutorial, stub_model(Tutorial,
+      :heading => "Heading",
+      :description => "MyText",
+      :url => "Url"
+    ))
+  end
+
+  it "renders attributes in <p>" do
+    render
+    # Run the generator again with the --webrat flag if you want to use webrat matchers
+    rendered.should match(/Heading/)
+    rendered.should match(/MyText/)
+    rendered.should match(/Url/)
+  end
+end