Merge branch 'atomaka/refactor/demeter-fixes' into 'master'

Fix some law of demeter violations

```
@post.user.username
```

is not good

```
delegate :username, to: :user, prefix: true
@post.user_username
```

is apparently good? Although I've never really seen the advantage since I feel like we're just masking an underlying problem.  But at least it looks a little nicer.

See merge request !14
This commit is contained in:
Andrew Tomaka 2015-07-20 15:18:56 -04:00
commit 7f540a73e1
7 changed files with 11 additions and 3 deletions

View file

@ -2,6 +2,8 @@ class Post < ActiveRecord::Base
belongs_to :user belongs_to :user
belongs_to :subcreddit belongs_to :subcreddit
delegate :username, to: :user, prefix: true
validates :title, validates :title,
presence: true, presence: true,
length: { maximum: 300 } length: { maximum: 300 }

View file

@ -8,6 +8,8 @@ class Subcreddit < ActiveRecord::Base
attr_accessor :closed attr_accessor :closed
delegate :username, to: :owner, prefix: true
before_save :set_closed_at before_save :set_closed_at
validates :name, validates :name,

View file

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

View file

@ -1,7 +1,7 @@
.posts .posts
.post.show .post.show
p.title= link_to @post.title, [@subcreddit, @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 ul.links.list-inline
li= link_to 'XXX comments', '' li= link_to 'XXX comments', ''
li= link_to 'source', '' li= link_to 'source', ''

View file

@ -7,7 +7,7 @@
li li
.post .post
p.title= link_to post.title, [@subcreddit, 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 ul.links.list-inline
li= link_to 'XXX comments', '' li= link_to 'XXX comments', ''
li= link_to 'share', '' li= link_to 'share', ''

View file

@ -6,6 +6,8 @@ describe Post, type: :model do
it { should belong_to(:user) } it { should belong_to(:user) }
it { should belong_to(:subcreddit) } it { should belong_to(:subcreddit) }
it { should delegate_method(:username).to(:user).with_prefix }
context 'with valid data' do context 'with valid data' do
it 'should be valid' do it 'should be valid' do
expect(post).to be_valid expect(post).to be_valid

View file

@ -7,6 +7,8 @@ describe Subcreddit, type: :model do
it { should belong_to(:owner).class_name('User') } it { should belong_to(:owner).class_name('User') }
it { should delegate_method(:username).to(:owner).with_prefix }
context 'with valid data' do context 'with valid data' do
it 'should be valid' do it 'should be valid' do
expect(subcreddit).to be_valid expect(subcreddit).to be_valid