From c880ba31b81e88face2b7013a4b4185176fcb72a Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Thu, 6 Aug 2015 15:10:31 -0400 Subject: [PATCH 1/2] Creaet simple user profile --- app/controllers/users_controller.rb | 5 ++++ app/models/subcreddit.rb | 11 ++------- app/models/user.rb | 9 +++++++- app/validators/sluguuidless_validator.rb | 7 ++++++ app/views/application/_sidebar.html.slim | 2 +- app/views/comments/_comment.html.slim | 4 +++- app/views/posts/_post.html.slim | 4 +++- app/views/subcreddits/show.html.slim | 4 +++- app/views/users/show.html.slim | 5 ++++ config/application.rb | 3 +++ config/routes.rb | 2 +- .../20150806191452_add_friendly_id_to_user.rb | 6 +++++ db/schema.rb | 5 +++- spec/controllers/users_controller_spec.rb | 19 +++++++++++++++ spec/features/users/profile_spec.rb | 23 +++++++++++++++++++ spec/models/user_spec.rb | 11 +++++++++ 16 files changed, 104 insertions(+), 16 deletions(-) create mode 100644 app/validators/sluguuidless_validator.rb create mode 100644 app/views/users/show.html.slim create mode 100644 db/migrate/20150806191452_add_friendly_id_to_user.rb create mode 100644 spec/features/users/profile_spec.rb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index b1e6401..c8cd616 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,4 +1,9 @@ class UsersController < ApplicationController + def show + @user = User.friendly.find(params[:id]) + @comments = @user.comments + end + def new @user = User.new end diff --git a/app/models/subcreddit.rb b/app/models/subcreddit.rb index f1aa3d1..ea37b6a 100644 --- a/app/models/subcreddit.rb +++ b/app/models/subcreddit.rb @@ -16,13 +16,12 @@ class Subcreddit < ActiveRecord::Base presence: true, format: /\A(?! )[a-z0-9 ]*(? Date: Thu, 6 Aug 2015 16:31:08 -0400 Subject: [PATCH 2/2] Refactor page header code --- app/helpers/application_helper.rb | 4 ++++ app/views/application/_navbar.html.slim | 3 +-- app/views/comments/show.html.slim | 1 + app/views/posts/show.html.slim | 1 + app/views/subcreddits/show.html.slim | 1 + app/views/users/show.html.slim | 2 +- spec/helpers/application_helper_spec.rb | 16 ++++++++++++++++ 7 files changed, 25 insertions(+), 3 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 4b0e707..1fae468 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -3,6 +3,10 @@ module ApplicationHelper bootstrap_classes[flash_type] || flash_type.to_s end + def page_header + content_for(:title) || '' + end + private def bootstrap_classes diff --git a/app/views/application/_navbar.html.slim b/app/views/application/_navbar.html.slim index 754f55f..437764b 100644 --- a/app/views/application/_navbar.html.slim +++ b/app/views/application/_navbar.html.slim @@ -10,8 +10,7 @@ = link_to 'Creddit', root_path, class: 'navbar-brand' .collapse.navbar-collapse ul.nav.navbar-nav - - if @subcreddit && @subcreddit.name - li= link_to @subcreddit.name, @subcreddit + li= page_header ul.nav.navbar-nav.navbar-right - if logged_in? li= link_to 'Sign Out', signout_path diff --git a/app/views/comments/show.html.slim b/app/views/comments/show.html.slim index b55d49f..420f1ba 100644 --- a/app/views/comments/show.html.slim +++ b/app/views/comments/show.html.slim @@ -1,3 +1,4 @@ +- content_for(:title, link_to(@subcreddit.name, @subcreddit)) == render 'posts/post', post: @post .alert.alert-info.in-page p you are viewing a single comment's thread. diff --git a/app/views/posts/show.html.slim b/app/views/posts/show.html.slim index 2a6c2e7..5a86e55 100644 --- a/app/views/posts/show.html.slim +++ b/app/views/posts/show.html.slim @@ -1,3 +1,4 @@ +- content_for(:title, link_to(@subcreddit.name, @subcreddit)) == render 'post', post: @post - if @post.comments? .title= "all #{@post.comments_count} comments" diff --git a/app/views/subcreddits/show.html.slim b/app/views/subcreddits/show.html.slim index cb109f2..6901c4d 100644 --- a/app/views/subcreddits/show.html.slim +++ b/app/views/subcreddits/show.html.slim @@ -1,3 +1,4 @@ +- content_for(:title) { link_to @subcreddit.name, @subcreddit } - if @subcreddit.closed? = "Board has been closed" - else diff --git a/app/views/users/show.html.slim b/app/views/users/show.html.slim index edd0b6a..bef5346 100644 --- a/app/views/users/show.html.slim +++ b/app/views/users/show.html.slim @@ -1,4 +1,4 @@ -h1 #{@user.username} +- content_for(:title, link_to(@user.username, @user)) .comments.contents - @comments.each do |comment| diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 59b4ce6..44dae05 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -26,4 +26,20 @@ describe ApplicationHelper do end end end + + describe '#page_header' do + context 'when title is provided' do + it 'should return the set title' do + allow(helper).to receive(:content_for).with(:title).and_return('Test') + + expect(helper.page_header).to eq 'Test' + end + end + + context 'when title is not provided' do + it 'should return blank' do + expect(helper.page_header).to be_blank + end + end + end end