From 86e1cd891cc1e6e8e57fa2478fc276c1303ae591 Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Mon, 20 Jul 2015 15:12:26 -0400 Subject: [PATCH] Fix some law of demeter violations --- app/models/post.rb | 2 ++ app/models/subcreddit.rb | 2 ++ app/views/application/_sidebar.html.slim | 2 +- app/views/posts/show.html.slim | 2 +- app/views/subcreddits/show.html.slim | 2 +- spec/models/post_spec.rb | 2 ++ spec/models/subcreddit_spec.rb | 2 ++ 7 files changed, 11 insertions(+), 3 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 1cde9dc..ad4bb41 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -2,6 +2,8 @@ class Post < ActiveRecord::Base belongs_to :user belongs_to :subcreddit + delegate :username, to: :user, prefix: true + validates :title, presence: true, length: { maximum: 300 } diff --git a/app/models/subcreddit.rb b/app/models/subcreddit.rb index f057639..f1aa3d1 100644 --- a/app/models/subcreddit.rb +++ b/app/models/subcreddit.rb @@ -8,6 +8,8 @@ class Subcreddit < ActiveRecord::Base attr_accessor :closed + delegate :username, to: :owner, prefix: true + before_save :set_closed_at validates :name, diff --git a/app/views/application/_sidebar.html.slim b/app/views/application/_sidebar.html.slim index 9226d89..66663e0 100644 --- a/app/views/application/_sidebar.html.slim +++ b/app/views/application/_sidebar.html.slim @@ -18,4 +18,4 @@ .title Moderators .box ul - li= @subcreddit.owner.username + li= @subcreddit.owner_username diff --git a/app/views/posts/show.html.slim b/app/views/posts/show.html.slim index 5eba19a..0addc8b 100644 --- a/app/views/posts/show.html.slim +++ b/app/views/posts/show.html.slim @@ -1,7 +1,7 @@ .posts .post.show p.title= link_to @post.title, [@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 #{@post.user_username}" ul.links.list-inline li= link_to 'XXX comments', '' li= link_to 'source', '' diff --git a/app/views/subcreddits/show.html.slim b/app/views/subcreddits/show.html.slim index dfe22a8..7400463 100644 --- a/app/views/subcreddits/show.html.slim +++ b/app/views/subcreddits/show.html.slim @@ -7,7 +7,7 @@ li .post p.title= link_to post.title, [@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 #{post.user_username}" ul.links.list-inline li= link_to 'XXX comments', '' li= link_to 'share', '' diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index f4d88bd..43264cd 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -6,6 +6,8 @@ describe Post, type: :model do it { should belong_to(:user) } it { should belong_to(:subcreddit) } + it { should delegate_method(:username).to(:user).with_prefix } + context 'with valid data' do it 'should be valid' do expect(post).to be_valid diff --git a/spec/models/subcreddit_spec.rb b/spec/models/subcreddit_spec.rb index 064d221..6547ada 100644 --- a/spec/models/subcreddit_spec.rb +++ b/spec/models/subcreddit_spec.rb @@ -7,6 +7,8 @@ describe Subcreddit, type: :model do it { should belong_to(:owner).class_name('User') } + it { should delegate_method(:username).to(:owner).with_prefix } + context 'with valid data' do it 'should be valid' do expect(subcreddit).to be_valid