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

[WIP] Post に更新者の情報を持てるようにする [closes #66] #70

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 5 additions & 3 deletions app/assets/stylesheets/posts.scss
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,18 @@
}
}

.author {
.user-information {
border-top: 1px solid $border-gray;
padding: 1.5rem 0;
text-align: right;
}

.author__nickname {
.author__nickname,
.modifier__nickname {
font-size: .9rem
}

.author__created-at {
.author__created-at,
.modifier__created-at {
font-size: .8rem
}
2 changes: 1 addition & 1 deletion app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def create
end

def update
if @post.update(post_params)
if @post.update(post_params.merge(updated_by_id: current_user.id))
redirect_to @post, notice: 'Post was successfully updated.'
else
render :edit
Expand Down
8 changes: 8 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ class Post < ApplicationRecord

has_paper_trail

before_validation :copy_created_by_id_to_updated_by_id, on: :create

private

def copy_created_by_id_to_updated_by_id
self.updated_by_id = created_by_id
end

concerning :Notifiable do
included do
after_create_commit :notify_to_create if slack_notification_enabled?
Expand Down
27 changes: 19 additions & 8 deletions app/views/posts/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,26 @@
= @post.title
.post__body
== sanitize(auto_link(markdown_to_html(emojify(@post.body))))
.author
.user-information
.row
.col.s3.offset-s9
.author__nickname
| Created by
span.blue-text
| #{@post.created_by.nickname}
.author__created-at.grey-text.text-darken-1
= @post.created_at
.col.s6.offset-s6
.author
.author__nickname
| Created by
span.blue-text
| #{@post.created_by.nickname}
.author__created-at.grey-text.text-darken-1
= @post.created_at
.row
.col.s6.offset-s6
.updater
.modifier__nickname
| Updated by
span.blue-text
/ TODO N+1
| #{User.find(@post.updated_by_id).nickname}
.modifier__created-at.grey-text.text-darken-1
= @post.updated_at
.row
.col.s12
= link_to edit_post_path(@post), class: 'waves-effect waves-light btn' do
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20160828130911_add_updated_by_id_to_posts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddUpdatedByIdToPosts < ActiveRecord::Migration[5.0]
def change
add_reference :posts, :updated_by, references: :users, index: true
add_foreign_key :posts, :users, column: :updated_by_id
end
end
5 changes: 4 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: 20160827120218) do
ActiveRecord::Schema.define(version: 20160828130911) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -21,7 +21,9 @@
t.integer "created_by_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "updated_by_id"
t.index ["created_by_id"], name: "index_posts_on_created_by_id", using: :btree
t.index ["updated_by_id"], name: "index_posts_on_updated_by_id", using: :btree
end

create_table "users", force: :cascade do |t|
Expand All @@ -44,4 +46,5 @@
end

add_foreign_key "posts", "users", column: "created_by_id"
add_foreign_key "posts", "users", column: "updated_by_id"
end
9 changes: 9 additions & 0 deletions test/models/post_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,13 @@ class PostTest < ActiveSupport::TestCase
post.body = 'Alice was beginning to get ...'
refute post.valid?
end

test 'should set updater same as creator before validation on create' do
post = Post.new
post.title = 'Alice In Wonderland'
post.body = 'Alice was beginning to get ...'
post.created_by_id = users(:alice).id
post.valid?
assert_equal users(:alice).id, post.updated_by_id
end
end