add posts

This commit is contained in:
Jeremy Ellison
2011-01-07 13:02:59 -05:00
parent f6b7122b02
commit c613d0b653
15 changed files with 238 additions and 3 deletions

View File

@@ -0,0 +1,38 @@
class PostsController < ApplicationController
resource_controller
belongs_to :topic
protect_from_forgery :except => [:create, :update, :destroy]
before_filter :require_user, :only => [:create, :update, :destroy]
before_filter :requre_owner, :only => [:update, :destroy]
index.response do |wants|
wants.js { render :json => collection.to_json(:methods => :attachment_path) }
end
create.response do |wants|
wants.js { render :json => object.to_json(:methods => :attachment_path) }
end
update.response do |wants|
wants.js { render :json => object.to_json(:methods => :attachment_path) }
end
destroy.response do |wants|
wants.js { render :json => {} }
end
show.response do |wants|
wants.js { render :json => object.to_json(:methods => :attachment_path) }
end
create.before do
object.user = @user
end
protected
def requre_owner
unless @user == object.user
render :json => {:error => "Unauthorized"}
end
end
end

View File

@@ -16,6 +16,9 @@ class TopicsController < ApplicationController
destroy.response do |wants|
wants.js { render :json => {} }
end
show.response do |wants|
wants.js { render :json => object }
end
create.before do
object.user = @user

View File

@@ -0,0 +1,2 @@
module PostsHelper
end

View File

@@ -0,0 +1,10 @@
class Post < ActiveRecord::Base
has_attached_file :attachment, :default_url => "" # no default attachment.
belongs_to :user
belongs_to :topic
def attachment_path
attachment.url
end
end

View File

@@ -1,3 +1,4 @@
class Topic < ActiveRecord::Base
belongs_to :user
has_many :posts
end

View File

@@ -3,6 +3,8 @@ DiscussionBoardBackend::Application.routes.draw do
match 'login' => 'users#login'
match 'signup' => 'users#signup'
resources :topics
resources :topics do
resources :posts
end
end

View File

@@ -0,0 +1,19 @@
class CreatePosts < ActiveRecord::Migration
def self.up
create_table :posts do |t|
t.text :body
t.integer :user_id
t.integer :topic_id
t.string :attachment_file_name
t.string :attachment_content_type
t.integer :attachment_file_size
t.datetime :attachment_updated_at
t.timestamps
end
end
def self.down
drop_table :posts
end
end

View File

@@ -10,7 +10,19 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20110107165818) do
ActiveRecord::Schema.define(:version => 20110107173512) do
create_table "posts", :force => true do |t|
t.text "body"
t.integer "user_id"
t.integer "topic_id"
t.string "attachment_file_name"
t.string "attachment_content_type"
t.integer "attachment_file_size"
t.datetime "attachment_updated_at"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "topics", :force => true do |t|
t.string "name"

View File

@@ -16,7 +16,19 @@ curl -d "topic[name]=NewTopic" -H "USER_ACCESS_TOKEN: LvQmmzh6je1XzhC9XASg" http
curl -X PUT -d "topic[name]=UpdatedTopic" -H "USER_ACCESS_TOKEN: LvQmmzh6je1XzhC9XASg" http://localhost:3000/topics/5
{"topic":{"name":"UpdatedTopic","created_at":"2011-01-07T17:18:10Z","updated_at":"2011-01-07T17:25:51Z","id":5,"user_id":1}}
curl -X DELETE -d "topic[name]=UpdatedTopic" -H "USER_ACCESS_TOKEN: LvQmmzh6je1XzhC9XASg" http://localhost:3000/topics/5
curl -X DELETE -H "USER_ACCESS_TOKEN: LvQmmzh6je1XzhC9XASg" http://localhost:3000/topics/5
{}
curl http://localhost:3000/topics/1/posts
curl -F "post[attachment]=@/usr/share/httpd/icons/world2.png" -F "post[body]=post_body" -H "USER_ACCESS_TOKEN: LvQmmzh6je1XzhC9XASg" http://localhost:3000/topics/1/posts
{"post":{"attachment_file_name":"world2.png","attachment_content_type":"application/octet-stream","attachment_file_size":363,"created_at":"2011-01-07T17:51:44Z","body":"post_body","updated_at":"2011-01-07T17:51:44Z","id":2,"user_id":1,"topic_id":1,"attachment_updated_at":"2011-01-07T17:51:44Z"}}
curl -X PUT -F "post[attachment]=@/usr/share/httpd/icons/world1.png" -F "post[body]=post_body_update" -H "USER_ACCESS_TOKEN: LvQmmzh6je1XzhC9XASg" http://localhost:3000/topics/1/posts/1
{"post":{"attachment_file_name":"world1.png","attachment_content_type":"application/octet-stream","attachment_file_size":332,"created_at":"2011-01-07T17:46:56Z","body":"post_body_update","updated_at":"2011-01-07T18:01:35Z","attachment_path":"/system/attachments/1/original/world1.png?1294423295","id":1,"user_id":1,"topic_id":1,"attachment_updated_at":"2011-01-07T18:01:35Z"}}
curl -X DELETE -H "USER_ACCESS_TOKEN: LvQmmzh6je1XzhC9XASg" http://localhost:3000/topics/1/posts/1
{}

View File

@@ -0,0 +1,56 @@
body { background-color: #fff; color: #333; }
body, p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}
pre {
background-color: #eee;
padding: 10px;
font-size: 11px;
}
a { color: #000; }
a:visited { color: #666; }
a:hover { color: #fff; background-color:#000; }
div.field, div.actions {
margin-bottom: 10px;
}
#notice {
color: green;
}
.field_with_errors {
padding: 2px;
background-color: red;
display: table;
}
#error_explanation {
width: 450px;
border: 2px solid red;
padding: 7px;
padding-bottom: 0;
margin-bottom: 20px;
background-color: #f0f0f0;
}
#error_explanation h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px;
margin-bottom: 0px;
background-color: #c00;
color: #fff;
}
#error_explanation ul li {
font-size: 12px;
list-style: square;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 363 B

View File

@@ -0,0 +1,19 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
one:
body: MyText
user_id: 1
topic_id: 1
attachment_file_name: MyString
attachment_content_type: MyString
attachment_file_size: 1
attachment_updated_at: 2011-01-07 12:35:12
two:
body: MyText
user_id: 1
topic_id: 1
attachment_file_name: MyString
attachment_content_type: MyString
attachment_file_size: 1
attachment_updated_at: 2011-01-07 12:35:12

View File

@@ -0,0 +1,49 @@
require 'test_helper'
class PostsControllerTest < ActionController::TestCase
setup do
@post = posts(:one)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:posts)
end
test "should get new" do
get :new
assert_response :success
end
test "should create post" do
assert_difference('Post.count') do
post :create, :post => @post.attributes
end
assert_redirected_to post_path(assigns(:post))
end
test "should show post" do
get :show, :id => @post.to_param
assert_response :success
end
test "should get edit" do
get :edit, :id => @post.to_param
assert_response :success
end
test "should update post" do
put :update, :id => @post.to_param, :post => @post.attributes
assert_redirected_to post_path(assigns(:post))
end
test "should destroy post" do
assert_difference('Post.count', -1) do
delete :destroy, :id => @post.to_param
end
assert_redirected_to posts_path
end
end

View File

@@ -0,0 +1,4 @@
require 'test_helper'
class PostsHelperTest < ActionView::TestCase
end

View File

@@ -0,0 +1,8 @@
require 'test_helper'
class PostTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end