Commits

Allan Davis  committed a719dd7

created micropost

  • Participants
  • Parent commits 6c0a1ff

Comments (0)

Files changed (15)

File app/controllers/microposts_controller.rb

+class MicropostsController < ApplicationController
+  # GET /microposts
+  # GET /microposts.xml
+  def index
+    @microposts = Micropost.all
+
+    respond_to do |format|
+      format.html # index.html.erb
+      format.xml  { render :xml => @microposts }
+    end
+  end
+
+  # GET /microposts/1
+  # GET /microposts/1.xml
+  def show
+    @micropost = Micropost.find(params[:id])
+
+    respond_to do |format|
+      format.html # show.html.erb
+      format.xml  { render :xml => @micropost }
+    end
+  end
+
+  # GET /microposts/new
+  # GET /microposts/new.xml
+  def new
+    @micropost = Micropost.new
+
+    respond_to do |format|
+      format.html # new.html.erb
+      format.xml  { render :xml => @micropost }
+    end
+  end
+
+  # GET /microposts/1/edit
+  def edit
+    @micropost = Micropost.find(params[:id])
+  end
+
+  # POST /microposts
+  # POST /microposts.xml
+  def create
+    @micropost = Micropost.new(params[:micropost])
+
+    respond_to do |format|
+      if @micropost.save
+        format.html { redirect_to(@micropost, :notice => 'Micropost was successfully created.') }
+        format.xml  { render :xml => @micropost, :status => :created, :location => @micropost }
+      else
+        format.html { render :action => "new" }
+        format.xml  { render :xml => @micropost.errors, :status => :unprocessable_entity }
+      end
+    end
+  end
+
+  # PUT /microposts/1
+  # PUT /microposts/1.xml
+  def update
+    @micropost = Micropost.find(params[:id])
+
+    respond_to do |format|
+      if @micropost.update_attributes(params[:micropost])
+        format.html { redirect_to(@micropost, :notice => 'Micropost was successfully updated.') }
+        format.xml  { head :ok }
+      else
+        format.html { render :action => "edit" }
+        format.xml  { render :xml => @micropost.errors, :status => :unprocessable_entity }
+      end
+    end
+  end
+
+  # DELETE /microposts/1
+  # DELETE /microposts/1.xml
+  def destroy
+    @micropost = Micropost.find(params[:id])
+    @micropost.destroy
+
+    respond_to do |format|
+      format.html { redirect_to(microposts_url) }
+      format.xml  { head :ok }
+    end
+  end
+end

File app/helpers/microposts_helper.rb

+module MicropostsHelper
+end

File app/models/micropost.rb

+class Micropost < ActiveRecord::Base
+  validates :content, :length =>{:maximum => 140}
+end

File app/views/microposts/_form.html.erb

+<%= form_for(@micropost) do |f| %>
+  <% if @micropost.errors.any? %>
+    <div id="error_explanation">
+      <h2><%= pluralize(@micropost.errors.count, "error") %> prohibited this micropost from being saved:</h2>
+
+      <ul>
+      <% @micropost.errors.full_messages.each do |msg| %>
+        <li><%= msg %></li>
+      <% end %>
+      </ul>
+    </div>
+  <% end %>
+
+  <div class="field">
+    <%= f.label :content %><br />
+    <%= f.text_field :content %>
+  </div>
+  <div class="field">
+    <%= f.label :user_id %><br />
+    <%= f.text_field :user_id %>
+  </div>
+  <div class="actions">
+    <%= f.submit %>
+  </div>
+<% end %>

File app/views/microposts/edit.html.erb

+<h1>Editing micropost</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @micropost %> |
+<%= link_to 'Back', microposts_path %>

File app/views/microposts/index.html.erb

+<h1>Listing microposts</h1>
+
+<table>
+  <tr>
+    <th>Content</th>
+    <th>User</th>
+    <th></th>
+    <th></th>
+    <th></th>
+  </tr>
+
+<% @microposts.each do |micropost| %>
+  <tr>
+    <td><%= micropost.content %></td>
+    <td><%= micropost.user_id %></td>
+    <td><%= link_to 'Show', micropost %></td>
+    <td><%= link_to 'Edit', edit_micropost_path(micropost) %></td>
+    <td><%= link_to 'Destroy', micropost, :confirm => 'Are you sure?', :method => :delete %></td>
+  </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New Micropost', new_micropost_path %>

File app/views/microposts/new.html.erb

+<h1>New micropost</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', microposts_path %>

File app/views/microposts/show.html.erb

+<p id="notice"><%= notice %></p>
+
+<p>
+  <b>Content:</b>
+  <%= @micropost.content %>
+</p>
+
+<p>
+  <b>User:</b>
+  <%= @micropost.user_id %>
+</p>
+
+
+<%= link_to 'Edit', edit_micropost_path(@micropost) %> |
+<%= link_to 'Back', microposts_path %>

File config/routes.rb

 DemoApp::Application.routes.draw do
+  resources :microposts
+
   resources :users
 
   # The priority is based upon order of creation:

File db/migrate/20111003131413_create_microposts.rb

+class CreateMicroposts < ActiveRecord::Migration
+  def self.up
+    create_table :microposts do |t|
+      t.string :content
+      t.integer :user_id
+
+      t.timestamps
+    end
+  end
+
+  def self.down
+    drop_table :microposts
+  end
+end

File db/schema.rb

 #
 # It's strongly recommended to check this file into your version control system.
 
-ActiveRecord::Schema.define(:version => 20111003130612) do
+ActiveRecord::Schema.define(:version => 20111003131413) do
+
+  create_table "microposts", :force => true do |t|
+    t.string   "content"
+    t.integer  "user_id"
+    t.datetime "created_at"
+    t.datetime "updated_at"
+  end
 
   create_table "users", :force => true do |t|
     t.string   "name"

File test/fixtures/microposts.yml

+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+one:
+  content: MyString
+  user_id: 1
+
+two:
+  content: MyString
+  user_id: 1

File test/functional/microposts_controller_test.rb

+require 'test_helper'
+
+class MicropostsControllerTest < ActionController::TestCase
+  setup do
+    @micropost = microposts(:one)
+  end
+
+  test "should get index" do
+    get :index
+    assert_response :success
+    assert_not_nil assigns(:microposts)
+  end
+
+  test "should get new" do
+    get :new
+    assert_response :success
+  end
+
+  test "should create micropost" do
+    assert_difference('Micropost.count') do
+      post :create, :micropost => @micropost.attributes
+    end
+
+    assert_redirected_to micropost_path(assigns(:micropost))
+  end
+
+  test "should show micropost" do
+    get :show, :id => @micropost.to_param
+    assert_response :success
+  end
+
+  test "should get edit" do
+    get :edit, :id => @micropost.to_param
+    assert_response :success
+  end
+
+  test "should update micropost" do
+    put :update, :id => @micropost.to_param, :micropost => @micropost.attributes
+    assert_redirected_to micropost_path(assigns(:micropost))
+  end
+
+  test "should destroy micropost" do
+    assert_difference('Micropost.count', -1) do
+      delete :destroy, :id => @micropost.to_param
+    end
+
+    assert_redirected_to microposts_path
+  end
+end

File test/unit/helpers/microposts_helper_test.rb

+require 'test_helper'
+
+class MicropostsHelperTest < ActionView::TestCase
+end

File test/unit/micropost_test.rb

+require 'test_helper'
+
+class MicropostTest < ActiveSupport::TestCase
+  # Replace this with your real tests.
+  test "the truth" do
+    assert true
+  end
+end