diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 11c81f8..8c848ae 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -2,6 +2,10 @@ class PostsController < ApplicationController before_filter :set_post, except: [:index, :new, :create] before_filter :set_subcreddit + def index + @posts = Post.all + end + def show @comments = @post.comments.arrange(order: :created_at) end diff --git a/app/controllers/subcreddits_controller.rb b/app/controllers/subcreddits_controller.rb index 7174794..0f73a87 100644 --- a/app/controllers/subcreddits_controller.rb +++ b/app/controllers/subcreddits_controller.rb @@ -6,6 +6,7 @@ class SubcredditsController < ApplicationController end def show + @posts = @subcreddit.posts end def new diff --git a/app/helpers/subcreddits_helper.rb b/app/helpers/subcreddits_helper.rb new file mode 100644 index 0000000..566bf86 --- /dev/null +++ b/app/helpers/subcreddits_helper.rb @@ -0,0 +1,9 @@ +module SubcredditsHelper + def subcreddit_name(subcreddit) + if subcreddit + content_for(:title) { link_to subcreddit.name, subcreddit } + else + content_for(:title) { link_to 'frontpage', root_path } + end + end +end diff --git a/app/views/posts/_post_meta.html.slim b/app/views/posts/_post_meta.html.slim new file mode 100644 index 0000000..6d14246 --- /dev/null +++ b/app/views/posts/_post_meta.html.slim @@ -0,0 +1,15 @@ +.posts.contents + ul + - @posts.order('created_at DESC').each_with_index do |post, rank| + li + .post + p.title= link_to post.title, subcreddit_post_path(post.subcreddit, post) + p.details + = "submitted #{distance_of_time_in_words post.created_at, Time.now} ago by " + == link_to post.user_username, post.user + - if @subcreddit == nil + = " to " + == link_to "/c/#{post.subcreddit.slug}", post.subcreddit + ul.links.list-inline + li= link_to "#{post.comments_count} comments", subcreddit_post_path(post.subcreddit, post) + li= link_to 'share', '' diff --git a/app/views/posts/index.html.slim b/app/views/posts/index.html.slim new file mode 100644 index 0000000..fd21849 --- /dev/null +++ b/app/views/posts/index.html.slim @@ -0,0 +1 @@ +== render 'post_meta' diff --git a/app/views/subcreddits/show.html.slim b/app/views/subcreddits/show.html.slim index 6901c4d..11f9236 100644 --- a/app/views/subcreddits/show.html.slim +++ b/app/views/subcreddits/show.html.slim @@ -1,16 +1,5 @@ -- content_for(:title) { link_to @subcreddit.name, @subcreddit } -- if @subcreddit.closed? +- subcreddit_name(@subcreddit) +- if @subcreddit && @subcreddit.closed? = "Board has been closed" - else - .posts.contents - ul - - @subcreddit.posts.order('created_at DESC').each_with_index do |post, rank| - li - .post - p.title= link_to post.title, subcreddit_post_path(@subcreddit, post) - p.details - = "submitted #{distance_of_time_in_words post.created_at, Time.now} ago by " - == link_to post.user_username, post.user - ul.links.list-inline - li= link_to "#{post.comments_count} comments", subcreddit_post_path(@subcreddit, post) - li= link_to 'share', '' + == render partial: 'posts/post_meta' diff --git a/config/routes.rb b/config/routes.rb index f036f0a..868c114 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,6 +3,8 @@ Rails.application.routes.draw do get 'signin', to: 'user_sessions#new', as: :signin get 'signout', to: 'user_sessions#destroy', as: :signout + resources :posts, only: [:index] + resources :subcreddits, path: 'c', except: [:destroy] do resources :posts, path: '', constraints: { id: /\d+\-.+/ }, except: [:index] do resources :comments, path: '', constraints: { id: /\d+/ }, except: [:index] @@ -12,5 +14,5 @@ Rails.application.routes.draw do resources :user_sessions, only: [:new, :create, :destroy] resources :users, path: 'u', only: [:show, :new, :create] - root to: 'subcreddits#index' + root to: 'posts#index' end diff --git a/spec/controllers/posts_controller_spec.rb b/spec/controllers/posts_controller_spec.rb index 807857a..7f746fa 100644 --- a/spec/controllers/posts_controller_spec.rb +++ b/spec/controllers/posts_controller_spec.rb @@ -14,6 +14,21 @@ describe PostsController, type: :controller do .to receive(:current_user).and_return(user) end + describe '#index' do + let(:posts) { 10.times.collect { create(:post) } } + before(:each) { get :index } + + it 'should render :index' do + expect(response).to render_template(:index) + end + + it 'should assign all Posts to @post' do + posts.each do |post| + expect(assigns(:posts)).to include(post) + end + end + end + describe '#show' do let(:post) { create(:post) } before(:each) { get :show, subcreddit_id: post.subcreddit, id: post } diff --git a/spec/controllers/subcreddits_controller_spec.rb b/spec/controllers/subcreddits_controller_spec.rb index 28daceb..c6163d8 100644 --- a/spec/controllers/subcreddits_controller_spec.rb +++ b/spec/controllers/subcreddits_controller_spec.rb @@ -29,7 +29,8 @@ describe SubcredditsController, type: :controller do end describe '#show' do - let(:subcreddit) { create(:subcreddit) } + let!(:subcreddit) { create(:subcreddit) } + let(:posts) { 5.times.collect { create(:post, subcreddit: subcreddit) } } before(:each) { get :show, id: subcreddit } it 'should render :show' do @@ -39,6 +40,10 @@ describe SubcredditsController, type: :controller do it 'should assign the Subcreddit to @subcreddit' do expect(assigns(:subcreddit)).to eq(subcreddit) end + + it 'should assign the post to show to @post' do + expect(assigns(:posts)).to eq(posts) + end end describe '#new' do diff --git a/spec/helpers/subcreddits_helper_spec.rb b/spec/helpers/subcreddits_helper_spec.rb new file mode 100644 index 0000000..40982c7 --- /dev/null +++ b/spec/helpers/subcreddits_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +describe SubcredditsHelper do + describe '#subcreddit_name' do + context 'when valid subcreddit provided' do + let(:subcreddit) { create(:subcreddit) } + + it 'should set the title to a link to the subcreddit' + end + + context 'when an invalid subcreddit is provided' do + it 'should set the title to a link to the frontpage' + end + end +end