Commit edf0bcca

morilla <mo@mokhan.ca>
2011-05-03 03:41:53
add creation scaffold.
1 parent 97806e5
app/controllers/creations_controller.rb
@@ -0,0 +1,83 @@
+class CreationsController < ApplicationController
+  # GET /creations
+  # GET /creations.xml
+  def index
+    @creations = Creation.all
+
+    respond_to do |format|
+      format.html # index.html.erb
+      format.xml  { render :xml => @creations }
+    end
+  end
+
+  # GET /creations/1
+  # GET /creations/1.xml
+  def show
+    @creation = Creation.find(params[:id])
+
+    respond_to do |format|
+      format.html # show.html.erb
+      format.xml  { render :xml => @creation }
+    end
+  end
+
+  # GET /creations/new
+  # GET /creations/new.xml
+  def new
+    @creation = Creation.new
+
+    respond_to do |format|
+      format.html # new.html.erb
+      format.xml  { render :xml => @creation }
+    end
+  end
+
+  # GET /creations/1/edit
+  def edit
+    @creation = Creation.find(params[:id])
+  end
+
+  # POST /creations
+  # POST /creations.xml
+  def create
+    @creation = Creation.new(params[:creation])
+
+    respond_to do |format|
+      if @creation.save
+        format.html { redirect_to(@creation, :notice => 'Creation was successfully created.') }
+        format.xml  { render :xml => @creation, :status => :created, :location => @creation }
+      else
+        format.html { render :action => "new" }
+        format.xml  { render :xml => @creation.errors, :status => :unprocessable_entity }
+      end
+    end
+  end
+
+  # PUT /creations/1
+  # PUT /creations/1.xml
+  def update
+    @creation = Creation.find(params[:id])
+
+    respond_to do |format|
+      if @creation.update_attributes(params[:creation])
+        format.html { redirect_to(@creation, :notice => 'Creation was successfully updated.') }
+        format.xml  { head :ok }
+      else
+        format.html { render :action => "edit" }
+        format.xml  { render :xml => @creation.errors, :status => :unprocessable_entity }
+      end
+    end
+  end
+
+  # DELETE /creations/1
+  # DELETE /creations/1.xml
+  def destroy
+    @creation = Creation.find(params[:id])
+    @creation.destroy
+
+    respond_to do |format|
+      format.html { redirect_to(creations_url) }
+      format.xml  { head :ok }
+    end
+  end
+end
app/helpers/creations_helper.rb
@@ -0,0 +1,2 @@
+module CreationsHelper
+end
app/models/creation.rb
@@ -0,0 +1,2 @@
+class Creation < ActiveRecord::Base
+end
app/views/creations/_form.html.erb
@@ -0,0 +1,25 @@
+<%= form_for(@creation) do |f| %>
+  <% if @creation.errors.any? %>
+    <div id="error_explanation">
+      <h2><%= pluralize(@creation.errors.count, "error") %> prohibited this creation from being saved:</h2>
+
+      <ul>
+      <% @creation.errors.full_messages.each do |msg| %>
+        <li><%= msg %></li>
+      <% end %>
+      </ul>
+    </div>
+  <% end %>
+
+  <div class="field">
+    <%= f.label :name %><br />
+    <%= f.text_field :name %>
+  </div>
+  <div class="field">
+    <%= f.label :story %><br />
+    <%= f.text_area :story %>
+  </div>
+  <div class="actions">
+    <%= f.submit %>
+  </div>
+<% end %>
app/views/creations/edit.html.erb
@@ -0,0 +1,6 @@
+<h1>Editing creation</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @creation %> |
+<%= link_to 'Back', creations_path %>
app/views/creations/index.html.erb
@@ -0,0 +1,31 @@
+<div class="heading">The most recent creations</div>
+
+<% @creations.reverse.each_with_index do |creation, index| %>
+  <% if index % 3 == 0 %>
+<div class="organic">
+  <% end %>
+    <div class="organicHolder">
+      <div class="organicContent">
+        <a href="#"><img src="/images/img02.jpg" alt="" /></a>
+        <h2><%= creation.name %></h2>
+        <p><%= creation.story %></p>
+        <p>
+        <%= link_to 'more...', creation %>
+        <%= link_to 'edit', edit_creation_path(creation) %> |
+        <%= link_to 'destroy', creation, :confirm => 'Are you sure?', :method => :delete %>
+        </p>
+      </div>
+    </div>
+  <% if index % 3 == 0 %>
+</div>
+  <% end %>
+
+<% end %>
+
+
+<div class="space"></div>
+
+<div class="clear"></div>
+
+<div class="hr"></div>
+<div class="space"></div>
app/views/creations/new.html.erb
@@ -0,0 +1,5 @@
+<h1>New creation</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', creations_path %>
app/views/creations/show.html.erb
@@ -0,0 +1,15 @@
+<p id="notice"><%= notice %></p>
+
+<p>
+  <b>Name:</b>
+  <%= @creation.name %>
+</p>
+
+<p>
+  <b>Story:</b>
+  <%= @creation.story %>
+</p>
+
+
+<%= link_to 'Edit', edit_creation_path(@creation) %> |
+<%= link_to 'Back', creations_path %>
app/views/layouts/application.html.erb
@@ -38,7 +38,7 @@
       <div class="menu">
         <ul id="menu">
           <li class="home"><%= link_to "home", home_index_path %></li>
-          <li class="commit"><a href="#">creations</a></li>
+          <li class="commit"><%= link_to "creations", creations_path %></li>
           <li class="contact"><a href="#">contact us</a></li>
         </ul>
       </div>
config/routes.rb
@@ -1,4 +1,6 @@
 Cake::Application.routes.draw do
+  resources :creations
+
   get "dashboard/index"
 
   get "home/index"
db/migrate/20110503032142_create_creations.rb
@@ -0,0 +1,14 @@
+class CreateCreations < ActiveRecord::Migration
+  def self.up
+    create_table :creations do |t|
+      t.string :name
+      t.text :story
+
+      t.timestamps
+    end
+  end
+
+  def self.down
+    drop_table :creations
+  end
+end
db/schema.rb
@@ -10,7 +10,14 @@
 #
 # It's strongly recommended to check this file into your version control system.
 
-ActiveRecord::Schema.define(:version => 20110417070236) do
+ActiveRecord::Schema.define(:version => 20110503032142) do
+
+  create_table "creations", :force => true do |t|
+    t.string   "name"
+    t.text     "story"
+    t.datetime "created_at"
+    t.datetime "updated_at"
+  end
 
   create_table "users", :force => true do |t|
     t.string   "email",                                 :default => "", :null => false
spec/controllers/creations_controller_spec.rb
@@ -0,0 +1,125 @@
+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 the Rails when you ran the scaffold generator.
+
+describe CreationsController do
+
+  def mock_creation(stubs={})
+    @mock_creation ||= mock_model(Creation, stubs).as_null_object
+  end
+
+  describe "GET index" do
+    it "assigns all creations as @creations" do
+      Creation.stub(:all) { [mock_creation] }
+      get :index
+      assigns(:creations).should eq([mock_creation])
+    end
+  end
+
+  describe "GET show" do
+    it "assigns the requested creation as @creation" do
+      Creation.stub(:find).with("37") { mock_creation }
+      get :show, :id => "37"
+      assigns(:creation).should be(mock_creation)
+    end
+  end
+
+  describe "GET new" do
+    it "assigns a new creation as @creation" do
+      Creation.stub(:new) { mock_creation }
+      get :new
+      assigns(:creation).should be(mock_creation)
+    end
+  end
+
+  describe "GET edit" do
+    it "assigns the requested creation as @creation" do
+      Creation.stub(:find).with("37") { mock_creation }
+      get :edit, :id => "37"
+      assigns(:creation).should be(mock_creation)
+    end
+  end
+
+  describe "POST create" do
+    describe "with valid params" do
+      it "assigns a newly created creation as @creation" do
+        Creation.stub(:new).with({'these' => 'params'}) { mock_creation(:save => true) }
+        post :create, :creation => {'these' => 'params'}
+        assigns(:creation).should be(mock_creation)
+      end
+
+      it "redirects to the created creation" do
+        Creation.stub(:new) { mock_creation(:save => true) }
+        post :create, :creation => {}
+        response.should redirect_to(creation_url(mock_creation))
+      end
+    end
+
+    describe "with invalid params" do
+      it "assigns a newly created but unsaved creation as @creation" do
+        Creation.stub(:new).with({'these' => 'params'}) { mock_creation(:save => false) }
+        post :create, :creation => {'these' => 'params'}
+        assigns(:creation).should be(mock_creation)
+      end
+
+      it "re-renders the 'new' template" do
+        Creation.stub(:new) { mock_creation(:save => false) }
+        post :create, :creation => {}
+        response.should render_template("new")
+      end
+    end
+  end
+
+  describe "PUT update" do
+    describe "with valid params" do
+      it "updates the requested creation" do
+        Creation.stub(:find).with("37") { mock_creation }
+        mock_creation.should_receive(:update_attributes).with({'these' => 'params'})
+        put :update, :id => "37", :creation => {'these' => 'params'}
+      end
+
+      it "assigns the requested creation as @creation" do
+        Creation.stub(:find) { mock_creation(:update_attributes => true) }
+        put :update, :id => "1"
+        assigns(:creation).should be(mock_creation)
+      end
+
+      it "redirects to the creation" do
+        Creation.stub(:find) { mock_creation(:update_attributes => true) }
+        put :update, :id => "1"
+        response.should redirect_to(creation_url(mock_creation))
+      end
+    end
+
+    describe "with invalid params" do
+      it "assigns the creation as @creation" do
+        Creation.stub(:find) { mock_creation(:update_attributes => false) }
+        put :update, :id => "1"
+        assigns(:creation).should be(mock_creation)
+      end
+
+      it "re-renders the 'edit' template" do
+        Creation.stub(:find) { mock_creation(:update_attributes => false) }
+        put :update, :id => "1"
+        response.should render_template("edit")
+      end
+    end
+  end
+
+  describe "DELETE destroy" do
+    it "destroys the requested creation" do
+      Creation.stub(:find).with("37") { mock_creation }
+      mock_creation.should_receive(:destroy)
+      delete :destroy, :id => "37"
+    end
+
+    it "redirects to the creations list" do
+      Creation.stub(:find) { mock_creation }
+      delete :destroy, :id => "1"
+      response.should redirect_to(creations_url)
+    end
+  end
+
+end
spec/helpers/creations_helper_spec.rb
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+# Specs in this file have access to a helper object that includes
+# the CreationsHelper. For example:
+#
+# describe CreationsHelper do
+#   describe "string concat" do
+#     it "concats two strings with spaces" do
+#       helper.concat_strings("this","that").should == "this that"
+#     end
+#   end
+# end
+describe CreationsHelper do
+  pending "add some examples to (or delete) #{__FILE__}"
+end
spec/models/creation_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe Creation do
+  pending "add some examples to (or delete) #{__FILE__}"
+end
spec/requests/creations_spec.rb
@@ -0,0 +1,11 @@
+require 'spec_helper'
+
+describe "Creations" do
+  describe "GET /creations" 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 creations_path
+      response.status.should be(200)
+    end
+  end
+end
spec/routing/creations_routing_spec.rb
@@ -0,0 +1,35 @@
+require "spec_helper"
+
+describe CreationsController do
+  describe "routing" do
+
+    it "recognizes and generates #index" do
+      { :get => "/creations" }.should route_to(:controller => "creations", :action => "index")
+    end
+
+    it "recognizes and generates #new" do
+      { :get => "/creations/new" }.should route_to(:controller => "creations", :action => "new")
+    end
+
+    it "recognizes and generates #show" do
+      { :get => "/creations/1" }.should route_to(:controller => "creations", :action => "show", :id => "1")
+    end
+
+    it "recognizes and generates #edit" do
+      { :get => "/creations/1/edit" }.should route_to(:controller => "creations", :action => "edit", :id => "1")
+    end
+
+    it "recognizes and generates #create" do
+      { :post => "/creations" }.should route_to(:controller => "creations", :action => "create")
+    end
+
+    it "recognizes and generates #update" do
+      { :put => "/creations/1" }.should route_to(:controller => "creations", :action => "update", :id => "1")
+    end
+
+    it "recognizes and generates #destroy" do
+      { :delete => "/creations/1" }.should route_to(:controller => "creations", :action => "destroy", :id => "1")
+    end
+
+  end
+end
spec/views/creations/edit.html.erb_spec.rb
@@ -0,0 +1,20 @@
+require 'spec_helper'
+
+describe "creations/edit.html.erb" do
+  before(:each) do
+    @creation = assign(:creation, stub_model(Creation,
+      :name => "MyString",
+      :story => "MyText"
+    ))
+  end
+
+  it "renders the edit creation form" do
+    render
+
+    # Run the generator again with the --webrat flag if you want to use webrat matchers
+    assert_select "form", :action => creations_path(@creation), :method => "post" do
+      assert_select "input#creation_name", :name => "creation[name]"
+      assert_select "textarea#creation_story", :name => "creation[story]"
+    end
+  end
+end
spec/views/creations/index.html.erb_spec.rb
@@ -0,0 +1,24 @@
+require 'spec_helper'
+
+describe "creations/index.html.erb" do
+  before(:each) do
+    assign(:creations, [
+      stub_model(Creation,
+        :name => "Name",
+        :story => "MyText"
+      ),
+      stub_model(Creation,
+        :name => "Name",
+        :story => "MyText"
+      )
+    ])
+  end
+
+  it "renders a list of creations" do
+    render
+    # Run the generator again with the --webrat flag if you want to use webrat matchers
+    assert_select "tr>td", :text => "Name".to_s, :count => 2
+    # Run the generator again with the --webrat flag if you want to use webrat matchers
+    assert_select "tr>td", :text => "MyText".to_s, :count => 2
+  end
+end
spec/views/creations/new.html.erb_spec.rb
@@ -0,0 +1,20 @@
+require 'spec_helper'
+
+describe "creations/new.html.erb" do
+  before(:each) do
+    assign(:creation, stub_model(Creation,
+      :name => "MyString",
+      :story => "MyText"
+    ).as_new_record)
+  end
+
+  it "renders new creation form" do
+    render
+
+    # Run the generator again with the --webrat flag if you want to use webrat matchers
+    assert_select "form", :action => creations_path, :method => "post" do
+      assert_select "input#creation_name", :name => "creation[name]"
+      assert_select "textarea#creation_story", :name => "creation[story]"
+    end
+  end
+end
spec/views/creations/show.html.erb_spec.rb
@@ -0,0 +1,18 @@
+require 'spec_helper'
+
+describe "creations/show.html.erb" do
+  before(:each) do
+    @creation = assign(:creation, stub_model(Creation,
+      :name => "Name",
+      :story => "MyText"
+    ))
+  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(/Name/)
+    # Run the generator again with the --webrat flag if you want to use webrat matchers
+    rendered.should match(/MyText/)
+  end
+end