Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
sample_app
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tô Ngọc Ánh
sample_app
Commits
b11e961f
Commit
b11e961f
authored
Jul 08, 2020
by
Tô Ngọc Ánh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add user microposts
parent
f7e94cee
Pipeline
#678
failed with stages
in 0 seconds
Changes
9
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
72 additions
and
1 deletions
+72
-1
app/controllers/microposts_controller.rb
+10
-0
app/controllers/static_pages_controller.rb
+4
-1
app/models/user.rb
+4
-0
app/views/microposts/_micropost.html.erb
+6
-0
app/views/shared/_feed.html.erb
+7
-0
app/views/shared/_feed_item.html.erb
+14
-0
app/views/static_pages/home.html.erb
+4
-0
spec/models/user_spec.rb
+11
-0
spec/requests/micropost_pages_spec.rb
+12
-0
No files found.
app/controllers/microposts_controller.rb
View file @
b11e961f
class
MicropostsController
<
ApplicationController
class
MicropostsController
<
ApplicationController
before_action
:signed_in_user
,
only:
[
:create
,
:destroy
]
before_action
:signed_in_user
,
only:
[
:create
,
:destroy
]
before_action
:correct_user
,
only: :destroy
def
index
def
index
end
end
...
@@ -10,11 +11,14 @@ class MicropostsController < ApplicationController
...
@@ -10,11 +11,14 @@ class MicropostsController < ApplicationController
flash
[
:success
]
=
"Micropost created!"
flash
[
:success
]
=
"Micropost created!"
redirect_to
root_url
redirect_to
root_url
else
else
@feed_items
=
[]
render
'static_pages/home'
render
'static_pages/home'
end
end
end
end
def
destroy
def
destroy
@micropost
.
destroy
redirect_to
root_url
end
end
private
private
...
@@ -22,4 +26,9 @@ class MicropostsController < ApplicationController
...
@@ -22,4 +26,9 @@ class MicropostsController < ApplicationController
def
micropost_params
def
micropost_params
params
.
require
(
:micropost
).
permit
(
:content
)
params
.
require
(
:micropost
).
permit
(
:content
)
end
end
def
correct_user
@micropost
=
current_user
.
microposts
.
find_by
(
id:
params
[
:id
])
redirect_to
root_url
if
@micropost
.
nil?
end
end
end
\ No newline at end of file
app/controllers/static_pages_controller.rb
View file @
b11e961f
class
StaticPagesController
<
ApplicationController
class
StaticPagesController
<
ApplicationController
def
home
def
home
@micropost
=
current_user
.
microposts
.
build
if
signed_in?
if
signed_in?
@micropost
=
current_user
.
microposts
.
build
@feed_items
=
current_user
.
feed
.
paginate
(
page:
params
[
:page
])
end
end
end
def
help
def
help
...
...
app/models/user.rb
View file @
b11e961f
...
@@ -18,6 +18,10 @@ class User < ActiveRecord::Base
...
@@ -18,6 +18,10 @@ class User < ActiveRecord::Base
Digest
::
SHA1
.
hexdigest
(
token
.
to_s
)
Digest
::
SHA1
.
hexdigest
(
token
.
to_s
)
end
end
def
feed
Micropost
.
where
(
"user_id = ?"
,
id
)
end
private
private
def
create_remember_token
def
create_remember_token
...
...
app/views/microposts/_micropost.html.erb
View file @
b11e961f
...
@@ -3,4 +3,9 @@
...
@@ -3,4 +3,9 @@
<span
class=
"timestamp"
>
<span
class=
"timestamp"
>
Posted
<%=
time_ago_in_words
(
micropost
.
created_at
)
%>
ago.
Posted
<%=
time_ago_in_words
(
micropost
.
created_at
)
%>
ago.
</span>
</span>
<%
if
current_user?
(
micropost
.
user
)
%>
<%=
link_to
"delete"
,
micropost
,
method: :delete
,
data:
{
confirm:
"You sure?"
},
title:
micropost
.
content
%>
<%
end
%>
</li>
</li>
\ No newline at end of file
app/views/shared/_feed.html.erb
0 → 100644
View file @
b11e961f
<%
if
@feed_items
.
any?
%>
<ol
class=
"microposts"
>
<%=
render
partial:
'shared/feed_item'
,
collection:
@feed_items
%>
</ol>
<%=
will_paginate
@feed_items
%>
<%
end
%>
\ No newline at end of file
app/views/shared/_feed_item.html.erb
0 → 100644
View file @
b11e961f
<li
id=
"
<%=
feed_item
.
id
%>
"
>
<%=
link_to
gravatar_for
(
feed_item
.
user
),
feed_item
.
user
%>
<span
class=
"user"
>
<%=
link_to
feed_item
.
user
.
name
,
feed_item
.
user
%>
</span>
<span
class=
"content"
>
<%=
feed_item
.
content
%>
</span>
<span
class=
"timestamp"
>
Posted
<%=
time_ago_in_words
(
feed_item
.
created_at
)
%>
ago.
</span>
<%
if
current_user?
(
feed_item
.
user
)
%>
<%=
link_to
"delete"
,
feed_item
,
method: :delete
,
data:
{
confirm:
"You sure?"
},
title:
feed_item
.
content
%>
<%
end
%>
</li>
\ No newline at end of file
app/views/static_pages/home.html.erb
View file @
b11e961f
...
@@ -8,6 +8,10 @@
...
@@ -8,6 +8,10 @@
<%=
render
'shared/micropost_form'
%>
<%=
render
'shared/micropost_form'
%>
</section>
</section>
</aside>
</aside>
<div
class=
"span8"
>
<h3>
Micropost Feed
</h3>
<%=
render
'shared/feed'
%>
</div>
</div>
</div>
<%
else
%>
<%
else
%>
<div
class=
"center hero-unit"
>
<div
class=
"center hero-unit"
>
...
...
spec/models/user_spec.rb
View file @
b11e961f
...
@@ -15,6 +15,7 @@ describe User do
...
@@ -15,6 +15,7 @@ describe User do
it
{
should
respond_to
(
:admin
)
}
it
{
should
respond_to
(
:admin
)
}
it
{
should
respond_to
(
:microposts
)
}
it
{
should
respond_to
(
:microposts
)
}
it
{
should
respond_to
(
:feed
)
}
it
{
should
be_valid
}
it
{
should
be_valid
}
...
@@ -140,5 +141,15 @@ describe User do
...
@@ -140,5 +141,15 @@ describe User do
expect
(
Micropost
.
where
(
id:
micropost
.
id
)).
to
be_empty
expect
(
Micropost
.
where
(
id:
micropost
.
id
)).
to
be_empty
end
end
end
end
describe
"status"
do
let
(
:unfollowed_post
)
do
FactoryGirl
.
create
(
:micropost
,
user:
FactoryGirl
.
create
(
:user
))
end
its
(
:feed
)
{
should
include
(
newer_micropost
)
}
its
(
:feed
)
{
should
include
(
older_micropost
)
}
its
(
:feed
)
{
should_not
include
(
unfollowed_post
)
}
end
end
end
end
end
spec/requests/micropost_pages_spec.rb
View file @
b11e961f
...
@@ -29,4 +29,16 @@ describe "MicropostPages" do
...
@@ -29,4 +29,16 @@ describe "MicropostPages" do
end
end
end
end
end
end
describe
"micropost destruction"
do
before
{
FactoryGirl
.
create
(
:micropost
,
user:
user
)
}
describe
"as correct user"
do
before
{
visit
root_path
}
it
"should delete a micropost"
do
expect
{
click_link
"delete"
}.
to
change
(
Micropost
,
:count
).
by
(
-
1
)
end
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment