Creaet simple user profile

This commit is contained in:
Andrew Tomaka 2015-08-06 15:10:31 -04:00
parent 4636bc837d
commit c880ba31b8
16 changed files with 104 additions and 16 deletions

View file

@ -1,4 +1,9 @@
class UsersController < ApplicationController class UsersController < ApplicationController
def show
@user = User.friendly.find(params[:id])
@comments = @user.comments
end
def new def new
@user = User.new @user = User.new
end end

View file

@ -16,13 +16,12 @@ class Subcreddit < ActiveRecord::Base
presence: true, presence: true,
format: /\A(?! )[a-z0-9 ]*(?<! )\z/i, format: /\A(?! )[a-z0-9 ]*(?<! )\z/i,
uniqueness: true, #{ case_sensitive: false }, uniqueness: true, #{ case_sensitive: false },
length: { minimum: 3, maximum: 21 } length: { minimum: 3, maximum: 21 },
sluguuidless: true
validates :closed, validates :closed,
format: /\A[01]?\z/ format: /\A[01]?\z/
validate :slug_does_not_have_uuid
def closed? def closed?
self.closed_at != nil self.closed_at != nil
end end
@ -36,10 +35,4 @@ class Subcreddit < ActiveRecord::Base
self.closed_at = nil self.closed_at = nil
end end
end end
def slug_does_not_have_uuid
if self.slug.match /([a-z0-9]+\-){4}[a-z0-9]+\z/
errors.add(:name, 'must be unique')
end
end
end end

View file

@ -1,10 +1,17 @@
class User < ActiveRecord::Base class User < ActiveRecord::Base
extend FriendlyId
has_secure_password has_secure_password
has_many :comments
has_many :posts
friendly_id :username, use: :slugged
before_save :downcase_email before_save :downcase_email
validates :email, presence: true, uniqueness: true validates :email, presence: true, uniqueness: true
validates :username, presence: true, uniqueness: true validates :username, presence: true, uniqueness: true, sluguuidless: true
validates :password, length: { minimum: 8 } validates :password, length: { minimum: 8 }
private private

View file

@ -0,0 +1,7 @@
class SluguuidlessValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if record.slug.match /([a-z0-9]+\-){4}[a-z0-9]+\z/
record.errors[attribute] << 'must be unique'
end
end
end

View file

@ -18,4 +18,4 @@
.title Moderators .title Moderators
.box .box
ul ul
li= @subcreddit.owner_username li= link_to @subcreddit.owner_username, @subcreddit.owner

View file

@ -1,5 +1,7 @@
.comment .comment
p.details= "#{comment.user_username} X points #{distance_of_time_in_words comment.created_at, Time.now} ago" p.details
== link_to comment.user_username, comment.user
= " X points #{distance_of_time_in_words comment.created_at, Time.now} ago"
p.content= comment.content p.content= comment.content
ul.links.list-inline ul.links.list-inline
li= link_to 'permalink', subcreddit_post_comment_path(subcreddit, post, comment) li= link_to 'permalink', subcreddit_post_comment_path(subcreddit, post, comment)

View file

@ -1,7 +1,9 @@
.posts.contents .posts.contents
.post.show .post.show
p.title= link_to post.title, [post.subcreddit, post] p.title= link_to post.title, [post.subcreddit, post]
p.details= "submitted #{distance_of_time_in_words post.created_at, Time.now} ago by #{post.user_username}" p.details
= "submitted #{distance_of_time_in_words post.created_at, Time.now} ago by "
== link_to post.user_username, post.user
p.content= post.content p.content= post.content
ul.links.list-inline ul.links.list-inline
li= link_to "#{post.comments_count} comments", subcreddit_post_path(post.subcreddit, post) li= link_to "#{post.comments_count} comments", subcreddit_post_path(post.subcreddit, post)

View file

@ -7,7 +7,9 @@
li li
.post .post
p.title= link_to post.title, subcreddit_post_path(@subcreddit, 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 #{post.user_username}" 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 ul.links.list-inline
li= link_to "#{post.comments_count} comments", subcreddit_post_path(@subcreddit, post) li= link_to "#{post.comments_count} comments", subcreddit_post_path(@subcreddit, post)
li= link_to 'share', '' li= link_to 'share', ''

View file

@ -0,0 +1,5 @@
h1 #{@user.username}
.comments.contents
- @comments.each do |comment|
== render 'comments/comment', subcreddit: comment.post.subcreddit, post: comment.post, comment: comment

View file

@ -22,5 +22,8 @@ module Creddit
# Do not swallow errors in after_commit/after_rollback callbacks. # Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true config.active_record.raise_in_transactional_callbacks = true
# So our console continues to work
config.autoload_paths += %W["#{config.root}/app/validators/"]
end end
end end

View file

@ -10,7 +10,7 @@ Rails.application.routes.draw do
end end
resources :user_sessions, only: [:new, :create, :destroy] resources :user_sessions, only: [:new, :create, :destroy]
resources :users, only: [:new, :create] resources :users, path: 'u', only: [:show, :new, :create]
root to: 'subcreddits#index' root to: 'subcreddits#index'
end end

View file

@ -0,0 +1,6 @@
class AddFriendlyIdToUser < ActiveRecord::Migration
def change
add_column :users, :slug, :string
add_index :users, :slug, unique: true
end
end

View file

@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20150804145405) do ActiveRecord::Schema.define(version: 20150806191452) do
create_table "comments", force: :cascade do |t| create_table "comments", force: :cascade do |t|
t.integer "user_id" t.integer "user_id"
@ -84,6 +84,9 @@ ActiveRecord::Schema.define(version: 20150804145405) do
t.string "password_digest" t.string "password_digest"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.string "slug"
end end
add_index "users", ["slug"], name: "index_users_on_slug", unique: true
end end

View file

@ -1,6 +1,25 @@
require 'rails_helper' require 'rails_helper'
describe UsersController, type: :controller do describe UsersController, type: :controller do
describe '#show' do
let(:user) { create(:user) }
before(:each) { get :show, id: user }
it 'should render :show' do
expect(response).to render_template(:show)
end
it 'should assign user comments to @comments' do
comments = 5.times.collect { create(:comment, user: user) }
expect(assigns(:comments)).to eq(comments)
end
it 'should assign requested User to @user' do
expect(assigns(:user)).to eq(user)
end
end
describe '#new' do describe '#new' do
it 'should render :new' do it 'should render :new' do
get :new get :new

View file

@ -0,0 +1,23 @@
require 'rails_helper'
describe 'Profile', type: :feature do
let!(:user) { create(:user) }
it 'should display a user profile' do
visit user_path(user)
expect(page).to have_content(user.username)
end
context 'when the user has commented' do
let!(:comments) { 5.times.collect { create(:comment, user: user) } }
it 'should display user comments' do
visit user_path(user)
comments.each do |comment|
expect(page).to have_content(comment.content)
end
end
end
end

View file

@ -1,10 +1,15 @@
require 'rails_helper' require 'rails_helper'
require 'securerandom'
describe User, type: :model do describe User, type: :model do
let(:user) { build(:user) } let(:user) { build(:user) }
it { should have_secure_password } it { should have_secure_password }
it { should have_many :comments }
it { should have_many :posts }
context 'with valid data' do context 'with valid data' do
it 'should be valid' do it 'should be valid' do
expect(user).to be_valid expect(user).to be_valid
@ -44,5 +49,11 @@ describe User, type: :model do
user.password = 'a' * 4 user.password = 'a' * 4
expect(user).to be_invalid expect(user).to be_invalid
end end
it 'should not allow a slug with a UUID' do
user.slug = "test-#{SecureRandom.uuid}"
expect(user).to be_invalid
end
end end
end end