Merge branch 'atomaka/refactor/metric-clean-up' into 'master'

Clean up metric_fu violations

Clean up many of our violations. Most of these are meaningless fixes that do not resolve the actual issue.  For example, we use delegate to remove the law of demeter violations, but that doesn't really resolve the issue.  We pass some more stuff though and that's good enough for now.

See merge request !23
This commit is contained in:
Andrew Tomaka 2015-12-11 21:08:28 +00:00
commit 51f4d71dfd
20 changed files with 33 additions and 11 deletions

View file

@ -1,3 +1,4 @@
# controllers/application_controller.rb
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
protect_from_forgery with: :exception protect_from_forgery with: :exception

View file

@ -1,3 +1,4 @@
# controllers/comments_controller.rb
class CommentsController < ApplicationController class CommentsController < ApplicationController
before_filter :set_comment, only: [:show, :edit, :update, :destroy] before_filter :set_comment, only: [:show, :edit, :update, :destroy]
before_filter :set_post before_filter :set_post

View file

@ -1,3 +1,4 @@
# controllers/posts_controller.rb
class PostsController < ApplicationController class PostsController < ApplicationController
before_filter :set_post, except: [:index, :new, :create] before_filter :set_post, except: [:index, :new, :create]
before_filter :set_subcreddit before_filter :set_subcreddit

View file

@ -1,3 +1,4 @@
# controllers/subcreddits_controller.rb
class SubcredditsController < ApplicationController class SubcredditsController < ApplicationController
before_filter :set_subcreddit, only: [:show, :edit, :update] before_filter :set_subcreddit, only: [:show, :edit, :update]

View file

@ -1,3 +1,4 @@
# controllers/user_sessions_controller.rb
class UserSessionsController < ApplicationController class UserSessionsController < ApplicationController
def new def new
@user_session = UserSession.new @user_session = UserSession.new

View file

@ -1,3 +1,4 @@
# controllers/users_controller.rb
class UsersController < ApplicationController class UsersController < ApplicationController
def show def show
@user = User.friendly.find(params[:id]) @user = User.friendly.find(params[:id])

View file

@ -1,3 +1,4 @@
# helpers/application_helper.rb
module ApplicationHelper module ApplicationHelper
def bootstrap_class_for(flash_type) def bootstrap_class_for(flash_type)
bootstrap_classes[flash_type] || flash_type.to_s bootstrap_classes[flash_type] || flash_type.to_s

View file

@ -1,7 +1,8 @@
# helpers/comments_helper.rb
module CommentsHelper module CommentsHelper
def nested_comments(comments) def nested_comments(comments)
comments.map do |comment, sub_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, content_tag(:div,
nested_comments(sub_comments), nested_comments(sub_comments),
class: 'nested_comments') class: 'nested_comments')

View file

@ -1,9 +1,12 @@
# helpers/subcreddits__helper.rb
module SubcredditsHelper module SubcredditsHelper
def subcreddit_name(subcreddit) def subcreddit_name(subcreddit = '')
if subcreddit if subcreddit.blank?
content_for(:title) { link_to subcreddit.name, subcreddit } link = link_to 'frontpage', root_path
else else
content_for(:title) { link_to 'frontpage', root_path } link = link_to subcreddit.name, subcreddit
end end
content_for(:title) { link }
end end
end end

View file

@ -1,3 +1,4 @@
# models/comment.rb
class Comment < ActiveRecord::Base class Comment < ActiveRecord::Base
has_ancestry has_ancestry
@ -5,6 +6,7 @@ class Comment < ActiveRecord::Base
belongs_to :post, counter_cache: true belongs_to :post, counter_cache: true
delegate :username, to: :user, prefix: true delegate :username, to: :user, prefix: true
delegate :subcreddit, to: :post, prefix: true
validates :content, presence: true validates :content, presence: true

View file

@ -1,3 +1,4 @@
# models/post.rb
class Post < ActiveRecord::Base class Post < ActiveRecord::Base
belongs_to :user belongs_to :user
belongs_to :subcreddit belongs_to :subcreddit
@ -5,6 +6,7 @@ class Post < ActiveRecord::Base
has_many :comments has_many :comments
delegate :username, to: :user, prefix: true delegate :username, to: :user, prefix: true
delegate :slug, to: :subcreddit, prefix: true
validates :title, validates :title,
presence: true, presence: true,

View file

@ -1,3 +1,4 @@
# models/subcreddit.rb
class Subcreddit < ActiveRecord::Base class Subcreddit < ActiveRecord::Base
extend FriendlyId extend FriendlyId

View file

@ -1,3 +1,4 @@
# models/user.rb
class User < ActiveRecord::Base class User < ActiveRecord::Base
extend FriendlyId extend FriendlyId

View file

@ -1,3 +1,4 @@
# models/user_session.rb
class UserSession < ActiveRecord::Base class UserSession < ActiveRecord::Base
belongs_to :user belongs_to :user

View file

@ -1,7 +1,8 @@
# validators/sluguuidless_validator.rb
class SluguuidlessValidator < ActiveModel::EachValidator class SluguuidlessValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value) def validate_each(record, attribute, value)
if record.slug.match /([a-z0-9]+\-){4}[a-z0-9]+\z/ 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 end
end end

View file

@ -1,15 +1,15 @@
.posts.contents .posts.contents
ul ul
- @posts.order('created_at DESC').each_with_index do |post, rank| - posts.order('created_at DESC').each_with_index do |post, rank|
li li
.post .post
p.title= link_to post.title, subcreddit_post_path(post.subcreddit, post) p.title= link_to post.title, subcreddit_post_path(post.subcreddit, post)
p.details p.details
= "submitted #{distance_of_time_in_words post.created_at, Time.now} ago by " = "submitted #{distance_of_time_in_words post.created_at, Time.now} ago by "
== link_to post.user_username, post.user == link_to post.user_username, post.user
- if @subcreddit == nil - if subcreddit == nil
= " to " = " to "
== link_to "/c/#{post.subcreddit.slug}", post.subcreddit == link_to "/c/#{post.subcreddit_slug}", post.subcreddit
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)
li= link_to 'share', '' li= link_to 'share', ''

View file

@ -1 +1,2 @@
== render 'post_meta' - subcreddit_name
== render 'post_meta', posts: @posts, subcreddit: @subcreddit

View file

@ -2,4 +2,4 @@
- if @subcreddit && @subcreddit.closed? - if @subcreddit && @subcreddit.closed?
= "Board has been closed" = "Board has been closed"
- else - else
== render partial: 'posts/post_meta' == render 'posts/post_meta', posts: @posts, subcreddit: @subcreddit

View file

@ -7,6 +7,7 @@ describe Comment, type: :model do
it { should belong_to(:post).counter_cache(true) } it { should belong_to(:post).counter_cache(true) }
it { should delegate_method(:username).to(:user).with_prefix } it { should delegate_method(:username).to(:user).with_prefix }
it { should delegate_method(:subcreddit).to(:post).with_prefix }
context 'with valid data' do context 'with valid data' do
it 'should be valid' do it 'should be valid' do

View file

@ -8,6 +8,7 @@ describe Post, type: :model do
it { should have_many(:comments) } it { should have_many(:comments) }
it { should delegate_method(:username).to(:user).with_prefix } it { should delegate_method(:username).to(:user).with_prefix }
it { should delegate_method(:slug).to(:subcreddit).with_prefix }
context 'when adding a comment' do context 'when adding a comment' do
let(:post) { create(:post) } let(:post) { create(:post) }