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 15 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
3 changes: 2 additions & 1 deletion app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def new

def create
@article = Article.new(article_params)
@article.user = current_user
if @article.save
flash[:success] = "Article was successfully created."
redirect_to @article
Expand Down Expand Up @@ -48,6 +49,6 @@ def destroy
private

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

belongs_to :user, optional: true
has_many :comments, dependent: :destroy
end
1 change: 1 addition & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Comment < ApplicationRecord
validates_presence_of :content, :email

belongs_to :article
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

has_many :articles
end
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 Author:
[email protected]
%p
%strong Text:
= @article.content
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20180319093819_add_user_ref_to_articles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddUserRefToArticles < ActiveRecord::Migration[5.2]
def change
add_reference :articles, :user, foreign_key: true
end
end
36 changes: 35 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,49 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2018_03_18_155404) do
ActiveRecord::Schema.define(version: 2018_03_21_145002) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "active_admin_comments", force: :cascade do |t|
t.string "namespace"
t.text "body"
t.string "resource_type"
t.bigint "resource_id"
t.string "author_type"
t.bigint "author_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["author_type", "author_id"], name: "index_active_admin_comments_on_author_type_and_author_id"
t.index ["namespace"], name: "index_active_admin_comments_on_namespace"
t.index ["resource_type", "resource_id"], name: "index_active_admin_comments_on_resource_type_and_resource_id"
end

create_table "admin_users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_admin_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_admin_users_on_reset_password_token", unique: true
end

create_table "articles", force: :cascade do |t|
t.string "title"
t.text "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "user_id"
t.index ["user_id"], name: "index_articles_on_user_id"
end

create_table "comments", force: :cascade do |t|
Expand Down Expand Up @@ -49,5 +82,6 @@
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end

add_foreign_key "articles", "users"
add_foreign_key "comments", "articles"
end
19 changes: 19 additions & 0 deletions features/article_author.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Feature:
As a user
In order to know who's the author of an article
I should see the author's name in the article

Background:
Given I visit the Homepage
Given the following user exists
| email | password | password_confirmation |
| [email protected] | OsloOslo123 | OsloOslo123 |
And I am logged in as "[email protected]"

Scenario: Successfully create an article
When I click "New Article" link
Then I fill in "Title" with "A Whole New World"
And I fill in "Content" with "A new fantastic point of view"
And I click "Create Article" button
Then I should be on "A Whole New World" page
And I should see "[email protected]"
4 changes: 4 additions & 0 deletions features/create_articles.feature
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ Feature: Create articles

Background:
Given I visit the Homepage
Given the following user exists
| email | password | password_confirmation |
| [email protected] | OsloOslo123 | OsloOslo123 |
And I am logged in as "[email protected]"

Scenario: Successfully create an article
When I click "New Article" link
Expand Down
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! }
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(: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.

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 { is_expected.to belong_to :user }
end
end
4 changes: 4 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@
expect(create(:user)).to be_valid
end
end

describe 'Associations' do
it { is_expected.to have_many :articles }
end
end