Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Author to Article Show Page #19

Open
wants to merge 20 commits into
base: develop
Choose a base branch
from

Conversation

AKidd95
Copy link
Contributor

@AKidd95 AKidd95 commented Mar 18, 2018

PT Story: https://www.pivotaltracker.com/story/show/155970828

Description

Changes proposed in this pull request:

  • Add author field to new article
  • display author article show page
  • add associations between author and article
  • add database table for authors

What I have learned working on this feature:

  • How to add references to an existing table, and their benefits
  • how to add associations and fix the errors they create

add author column to article db
display author on article show page
validate and sanitze author params
add validation for author
add tests for associations
add author ref to article
fix author tests
add author ref to article
fix author tests
rspec all green
Copy link
Member

@tochman tochman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to rethink the entire approach to this

@@ -8,7 +8,7 @@ def create
@article = Article.new(article_params)
if @article.save
flash[:success] = "Article was successfully created."
redirect_to @article
redirect_to article_path(@article)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to make this change

validates :author, presence: true

has_many :articles
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to create an Author class. User can do

@@ -3,6 +3,10 @@
= form.label :title
%br/
= form.text_field :title, id: :article_title
%p
= form.label :author
%br/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to add this field. Whoever’s logged in should be assigned as the author of the article. You can deal with it in the controller

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i thought that the requirements for the author part of the assignment was to be able to assign multiple authors to a piece. Would using an author form field be a bad way approaching this?

@@ -1,6 +1,9 @@
%p
%strong Title:
= @article.title
%p
%strong Authors:
[email protected]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is authors then use plural form

@@ -0,0 +1,5 @@
class AddAuthorRefToArticle < ActiveRecord::Migration[5.2]
def change
add_reference :articles, :author, foreign_key: true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably want to have a has_many relationship between Article and User

end
end

describe 'Validations' do
it { is_expected.to validate_presence_of :title }
it { is_expected.to validate_presence_of :content }
end

describe 'Associations' do
it { should belong_to :author }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have many

change unnecessary code
make correct assocations between user and articles
add user logged in to create article tests
take out show page from tests
add optional user assocation with article
Copy link

@diraulo diraulo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... Try to be a bit descriptive with your PR titles. When you say Author? what about it? 🤔

end
end

describe 'Validations' do
it { is_expected.to validate_presence_of :title }
it { is_expected.to validate_presence_of :content }
end

describe 'Associations' do
it { should belong_to :user }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer expect syntax over should

@@ -11,4 +11,8 @@
expect(create(:user)).to be_valid
end
end

describe 'Associations' do
it { should have_many :articles }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer expect syntax over should

@AKidd95 AKidd95 changed the title [WIP] Author [WIP] Add Author to Article Show Page Mar 21, 2018
edit rspec syntax to is expected
add author feature test
@@ -8,12 +8,16 @@

describe 'Factory' do
it 'should have valid Factory' do
expect(create(:article)).to be_valid
expect(create(:user)).to be_valid
Copy link
Contributor

@holgertidemand holgertidemand Mar 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be article. You are testing the article factory :)
If you are getting an error maybe you forgot to add user to the factory?

Copy link
Contributor

@holgertidemand holgertidemand Mar 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looked at the article factory and it's missing the user. :) See comment above.

@@ -8,12 +8,16 @@

describe 'Factory' do
it 'should have valid Factory' do
expect(create(:article)).to be_valid
expect(create(:user)).to be_valid
Copy link
Contributor

@holgertidemand holgertidemand Mar 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looked at the article factory and it's missing the user. :) See comment above.

@AKidd95 AKidd95 changed the title [WIP] Add Author to Article Show Page Add Author to Article Show Page Mar 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants