diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 32e77eb..2c6d2b6 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,3 +1,4 @@ +# controllers/application_controller.rb class ApplicationController < ActionController::Base protect_from_forgery with: :exception diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 62acebf..0b3dcb6 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -1,3 +1,4 @@ +# controllers/comments_controller.rb class CommentsController < ApplicationController before_filter :set_comment, only: [:show, :edit, :update, :destroy] before_filter :set_post diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index beae91e..6d95440 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -1,3 +1,4 @@ +# controllers/posts_controller.rb class PostsController < ApplicationController before_filter :set_post, except: [:index, :new, :create] before_filter :set_subcreddit diff --git a/app/controllers/subcreddits_controller.rb b/app/controllers/subcreddits_controller.rb index 0f73a87..3ed4101 100644 --- a/app/controllers/subcreddits_controller.rb +++ b/app/controllers/subcreddits_controller.rb @@ -1,3 +1,4 @@ +# controllers/subcreddits_controller.rb class SubcredditsController < ApplicationController before_filter :set_subcreddit, only: [:show, :edit, :update] diff --git a/app/controllers/user_sessions_controller.rb b/app/controllers/user_sessions_controller.rb index d3d7220..5780755 100644 --- a/app/controllers/user_sessions_controller.rb +++ b/app/controllers/user_sessions_controller.rb @@ -1,3 +1,4 @@ +# controllers/user_sessions_controller.rb class UserSessionsController < ApplicationController def new @user_session = UserSession.new diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 283c337..2c57b82 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,3 +1,4 @@ +# controllers/users_controller.rb class UsersController < ApplicationController def show @user = User.friendly.find(params[:id]) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 1fae468..5f47dd5 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,3 +1,4 @@ +# helpers/application_helper.rb module ApplicationHelper def bootstrap_class_for(flash_type) bootstrap_classes[flash_type] || flash_type.to_s diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb index 714dd93..4fcf8af 100644 --- a/app/helpers/comments_helper.rb +++ b/app/helpers/comments_helper.rb @@ -1,7 +1,8 @@ +# helpers/comments_helper.rb module CommentsHelper def nested_comments(comments) comments.map do |comment, sub_comments| - render(comment, post: comment.post, subcreddit: comment.post.subcreddit) + + render(comment, post: comment.post, subcreddit: comment.post_subcreddit) + content_tag(:div, nested_comments(sub_comments), class: 'nested_comments') diff --git a/app/helpers/subcreddits_helper.rb b/app/helpers/subcreddits_helper.rb index 566bf86..46c1a03 100644 --- a/app/helpers/subcreddits_helper.rb +++ b/app/helpers/subcreddits_helper.rb @@ -1,9 +1,12 @@ +# helpers/subcreddits__helper.rb module SubcredditsHelper - def subcreddit_name(subcreddit) - if subcreddit - content_for(:title) { link_to subcreddit.name, subcreddit } + def subcreddit_name(subcreddit = '') + if subcreddit.blank? + link = link_to 'frontpage', root_path else - content_for(:title) { link_to 'frontpage', root_path } + link = link_to subcreddit.name, subcreddit end + + content_for(:title) { link } end end diff --git a/app/models/comment.rb b/app/models/comment.rb index 011745d..cc68073 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,3 +1,4 @@ +# models/comment.rb class Comment < ActiveRecord::Base has_ancestry @@ -5,6 +6,7 @@ class Comment < ActiveRecord::Base belongs_to :post, counter_cache: true delegate :username, to: :user, prefix: true + delegate :subcreddit, to: :post, prefix: true validates :content, presence: true diff --git a/app/models/post.rb b/app/models/post.rb index ed7ed7f..4b579ab 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1,3 +1,4 @@ +# models/post.rb class Post < ActiveRecord::Base belongs_to :user belongs_to :subcreddit @@ -5,6 +6,7 @@ class Post < ActiveRecord::Base has_many :comments delegate :username, to: :user, prefix: true + delegate :slug, to: :subcreddit, prefix: true validates :title, presence: true, diff --git a/app/models/subcreddit.rb b/app/models/subcreddit.rb index ea37b6a..13432bb 100644 --- a/app/models/subcreddit.rb +++ b/app/models/subcreddit.rb @@ -1,3 +1,4 @@ +# models/subcreddit.rb class Subcreddit < ActiveRecord::Base extend FriendlyId diff --git a/app/models/user.rb b/app/models/user.rb index ead07a5..0ee835e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,3 +1,4 @@ +# models/user.rb class User < ActiveRecord::Base extend FriendlyId diff --git a/app/models/user_session.rb b/app/models/user_session.rb index a8ab134..331da1b 100644 --- a/app/models/user_session.rb +++ b/app/models/user_session.rb @@ -1,3 +1,4 @@ +# models/user_session.rb class UserSession < ActiveRecord::Base belongs_to :user diff --git a/app/validators/sluguuidless_validator.rb b/app/validators/sluguuidless_validator.rb index 7e1d65b..5b6e6d5 100644 --- a/app/validators/sluguuidless_validator.rb +++ b/app/validators/sluguuidless_validator.rb @@ -1,7 +1,8 @@ +# validators/sluguuidless_validator.rb 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' + record.errors[attribute] << "#{value} must be unique" end end end diff --git a/app/views/posts/_post_meta.html.slim b/app/views/posts/_post_meta.html.slim index 6d14246..4efa375 100644 --- a/app/views/posts/_post_meta.html.slim +++ b/app/views/posts/_post_meta.html.slim @@ -1,15 +1,15 @@ .posts.contents ul - - @posts.order('created_at DESC').each_with_index do |post, rank| + - 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 + - if subcreddit == nil = " to " - == link_to "/c/#{post.subcreddit.slug}", post.subcreddit + == 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 index fd21849..1be6903 100644 --- a/app/views/posts/index.html.slim +++ b/app/views/posts/index.html.slim @@ -1 +1,2 @@ -== render 'post_meta' +- subcreddit_name +== render 'post_meta', posts: @posts, subcreddit: @subcreddit diff --git a/app/views/subcreddits/show.html.slim b/app/views/subcreddits/show.html.slim index 11f9236..22eee07 100644 --- a/app/views/subcreddits/show.html.slim +++ b/app/views/subcreddits/show.html.slim @@ -2,4 +2,4 @@ - if @subcreddit && @subcreddit.closed? = "Board has been closed" - else - == render partial: 'posts/post_meta' + == render 'posts/post_meta', posts: @posts, subcreddit: @subcreddit diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index 2108f89..9bb3041 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -7,6 +7,7 @@ describe Comment, type: :model do it { should belong_to(:post).counter_cache(true) } it { should delegate_method(:username).to(:user).with_prefix } + it { should delegate_method(:subcreddit).to(:post).with_prefix } context 'with valid data' do it 'should be valid' do diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 4c26448..4a40b32 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -8,6 +8,7 @@ describe Post, type: :model do it { should have_many(:comments) } it { should delegate_method(:username).to(:user).with_prefix } + it { should delegate_method(:slug).to(:subcreddit).with_prefix } context 'when adding a comment' do let(:post) { create(:post) }