From a0955d2bfc46eb50235ec89da230d5b6b6b147d7 Mon Sep 17 00:00:00 2001 From: Skud Date: Wed, 13 Feb 2013 16:28:23 +1100 Subject: [PATCH] post in (or not in) a forum --- app/controllers/posts_controller.rb | 1 + app/models/post.rb | 2 +- app/views/posts/_form.html.haml | 26 ++++++++++++--------- app/views/posts/edit.html.haml | 7 ++---- app/views/posts/new.html.haml | 11 +++------ spec/controllers/posts_controller_spec.rb | 11 +++++++++ spec/views/forums/show.html.haml_spec.rb | 4 ++++ spec/views/posts/edit.html.haml_spec.rb | 28 +++++++++++++++++++++++ spec/views/posts/new.html.haml_spec.rb | 28 ++++++++++++++++++++++- 9 files changed, 92 insertions(+), 26 deletions(-) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index ba2ee6e04..f78a476c6 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -29,6 +29,7 @@ class PostsController < ApplicationController # GET /posts/new.json def new @post = Post.new + @forum = Forum.find_by_id(params[:forum_id]) || Forum.new respond_to do |format| format.html # new.html.haml diff --git a/app/models/post.rb b/app/models/post.rb index 050f13c26..a4b03ebfc 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1,7 +1,7 @@ class Post < ActiveRecord::Base extend FriendlyId friendly_id :author_date_subject, use: :slugged - attr_accessible :body, :subject, :author_id + attr_accessible :body, :subject, :author_id, :forum_id belongs_to :author, :class_name => 'Member' belongs_to :forum has_many :comments, :dependent => :destroy diff --git a/app/views/posts/_form.html.haml b/app/views/posts/_form.html.haml index 429ce48e0..d4fe1704a 100644 --- a/app/views/posts/_form.html.haml +++ b/app/views/posts/_form.html.haml @@ -6,14 +6,18 @@ - @post.errors.full_messages.each do |msg| %li= msg - .field - = f.label :author_id - = f.number_field :author_id - .field - = f.label :subject - = f.text_field :subject - .field - = f.label :body - = f.text_area :body - .actions - = f.submit 'Save' + + = label_tag :post, "Subject" + = f.text_field :subject, :class => 'input-block-level' + = label_tag :body, "What's going on in your food garden?" + = f.text_area :body, :rows => 12, :class => 'input-block-level' + + - if @post.forum || @forum + - forum = @post.forum || @forum + %p + This post will be posted in the forum + =link_to forum.name, forum + .field + = f.hidden_field :forum_id, :value => forum.id + + = f.submit "Post", :class => 'btn' diff --git a/app/views/posts/edit.html.haml b/app/views/posts/edit.html.haml index 502a49faa..fcf66a4cb 100644 --- a/app/views/posts/edit.html.haml +++ b/app/views/posts/edit.html.haml @@ -1,7 +1,4 @@ = content_for :title, "Edit post" -= form_for @post do |f| - = label_tag :subject, "Subject" - = f.text_field :subject, :class => 'input-block-level' - = f.text_area :body, :rows => 12, :class => 'input-block-level' - = f.submit "Post" +=render 'form' + diff --git a/app/views/posts/new.html.haml b/app/views/posts/new.html.haml index 8a5ac9b46..e1014c41f 100644 --- a/app/views/posts/new.html.haml +++ b/app/views/posts/new.html.haml @@ -1,9 +1,4 @@ -= content_for :title, "Post a post" += content_for :title, @forum.name ? "Post in #{@forum.name}" : "Post something" + +=render 'form' -= form_for @post, :url => { :action => "create" } do |f| - %fieldset - = label_tag :post, "Subject" - = f.text_field :subject, :class => 'input-block-level' - = label_tag :body, "What's going on in your food garden?" - = f.text_area :body, :rows => 12, :class => 'input-block-level' - = f.submit "Post", :class => 'btn' diff --git a/spec/controllers/posts_controller_spec.rb b/spec/controllers/posts_controller_spec.rb index c8ad395c6..4eb13d9f6 100644 --- a/spec/controllers/posts_controller_spec.rb +++ b/spec/controllers/posts_controller_spec.rb @@ -39,6 +39,17 @@ describe PostsController do get :new, {} assigns(:post).should be_a_new(Post) end + + it "picks up forum from params" do + forum = FactoryGirl.create(:forum) + get :new, {:forum_id => forum.id} + assigns(:forum).should eq(forum) + end + + it "doesn't die if no forum specified" do + get :new, {} + assigns(:forum).should be_a_new(Forum) + end end describe "GET edit" do diff --git a/spec/views/forums/show.html.haml_spec.rb b/spec/views/forums/show.html.haml_spec.rb index e41265a67..c5b62f69b 100644 --- a/spec/views/forums/show.html.haml_spec.rb +++ b/spec/views/forums/show.html.haml_spec.rb @@ -10,4 +10,8 @@ describe "forums/show" do rendered.should contain @forum.description rendered.should contain @forum.owner.to_s end + + it 'links to new post with the forum id' do + assert_select "a[href=#{new_post_path(:forum_id => @forum.id)}]" + end end diff --git a/spec/views/posts/edit.html.haml_spec.rb b/spec/views/posts/edit.html.haml_spec.rb index bb67e9dfa..91e2d9b4f 100644 --- a/spec/views/posts/edit.html.haml_spec.rb +++ b/spec/views/posts/edit.html.haml_spec.rb @@ -19,5 +19,33 @@ describe "posts/edit" do assert_select "textarea#post_body", :name => "post[body]" end end + + it 'no hidden forum field' do + assert_select "input#post_forum_id[type=hidden]", false + end + + it 'no forum mentioned' do + rendered.should_not contain "This post will be posted in the forum" + end + + context "forum specified" do + before(:each) do + @forum = assign(:forum, FactoryGirl.create(:forum)) + assign(:post, FactoryGirl.create( :post, + :forum => @forum, + :author => @author + )) + render + end + + it 'creates a hidden field' do + assert_select "input#post_forum_id[type=hidden][value=#{@forum.id}]" + end + + it 'tells the user what forum it will be posted in' do + rendered.should contain "This post will be posted in the forum #{@forum.name}" + end + end + end end diff --git a/spec/views/posts/new.html.haml_spec.rb b/spec/views/posts/new.html.haml_spec.rb index 99e644ed4..aa7da6fba 100644 --- a/spec/views/posts/new.html.haml_spec.rb +++ b/spec/views/posts/new.html.haml_spec.rb @@ -4,15 +4,41 @@ describe "posts/new" do before(:each) do @author = FactoryGirl.create(:member) assign(:post, FactoryGirl.create(:post, :author => @author)) + assign(:forum, Forum.new) sign_in @author controller.stub(:current_user) { @author } - render end it "renders new post form" do + render assert_select "form", :action => posts_path, :method => "post" do assert_select "input#post_subject", :name => "post[subject]" assert_select "textarea#post_body", :name => "post[body]" end end + + it 'no hidden forum field' do + assert_select "input#post_forum_id[type=hidden]", false + end + + it 'no forum mentioned' do + rendered.should_not contain "This post will be posted in the forum" + end + + context "forum specified" do + before(:each) do + @forum = assign(:forum, FactoryGirl.create(:forum)) + assign(:post, FactoryGirl.create(:post, :forum => @forum)) + render + end + + it 'creates a hidden field' do + assert_select "input#post_forum_id[type=hidden][value=#{@forum.id}]" + end + + it 'tells the user what forum it will be posted in' do + rendered.should contain "This post will be posted in the forum #{@forum.name}" + end + end + end