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
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

else
flash[:error] = "Field cannot be blank"
render 'new'
Expand All @@ -22,6 +22,6 @@ def show
private

def article_params
params[:article].permit(:title, :content)
params[:article].permit(:title, :content, :author)
end
end
4 changes: 3 additions & 1 deletion app/models/article.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
class Article < ApplicationRecord
validates :title, :content, presence: true
validates :title, :content, :author, presence: true

belongs_to :author
end
5 changes: 5 additions & 0 deletions app/models/author.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Author < ApplicationRecord
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

4 changes: 4 additions & 0 deletions app/views/articles/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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?

= form.text_field :author, id: :article_author
%p
= form.label :content
%br/
Expand Down
3 changes: 3 additions & 0 deletions app/views/articles/show.html.haml
Original file line number Diff line number Diff line change
@@ -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

%p
%strong Text:
= @article.content
7 changes: 7 additions & 0 deletions db/migrate/20180318172631_create_author.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class CreateAuthor < ActiveRecord::Migration[5.2]
def change
create_table :authors do |t|
t.string :author
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20180318173006_add_author_ref_to_article.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 9 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2018_03_17_113155) do
ActiveRecord::Schema.define(version: 2018_03_18_173006) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -20,6 +20,13 @@
t.text "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "author"
t.bigint "author_id"
t.index ["author_id"], name: "index_articles_on_author_id"
end

create_table "authors", force: :cascade do |t|
t.string "author"
end

create_table "users", force: :cascade do |t|
Expand All @@ -40,4 +47,5 @@
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end

add_foreign_key "articles", "authors"
end
2 changes: 1 addition & 1 deletion features/support/warden.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Warden.test_mode!
World Warden::Test::Helpers
After { Warden.test_reset! }
After { Warden.test_reset! }
5 changes: 5 additions & 0 deletions spec/factories/authors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryBot.define do
factory :author do
author 'Man McMaster'
end
end
6 changes: 5 additions & 1 deletion spec/models/article_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@

describe 'Factory' do
it 'should have valid Factory' do
expect(create(:article)).to be_valid
expect(create(:author)).to be_valid
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

end
end
17 changes: 17 additions & 0 deletions spec/models/author_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'rails_helper'

RSpec.describe Author, type: :model do
describe 'Associations' do
it { should have_many :articles}
end

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

describe 'Factory' do
it 'should have valid Factory' do
expect(create(:author)).to be_valid
end
end
end