-
-
Save vajapravin/bc839d49c092c5fa44893876ab25d2f9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ApplicationRecord | |
has_many :posts | |
has_many :comments | |
# id :integer not null, primary key | |
# name :string(50) default("") | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Post < ApplicationRecord | |
belongs_to :user | |
has_many :comments | |
# id :integer not null, primary key | |
# user_id :integer | |
# content :text default("") | |
# created_at :datetime | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Comment < ApplicationRecord | |
belongs_to :user | |
belongs_to :post | |
# id :integer not null, primary key | |
# user_id :integer | |
# post_id :integer | |
# content :text default("") | |
# created_at :datetime | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NewsfeedController < ApplicationController | |
# JSON endpoint that returns an array of Post objects in order of | |
# newest first, to oldest last. Each Post contains a User object | |
# (the author of the Post), and an array of Comments. Each Comment | |
# will also include the User object of the Comment's author. | |
# TODO: Newsfeed endpoint here | |
include ApplicationHelper | |
def index | |
render json: fetch_newsfeeds(params[:page] || 1, 10) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"type": "Post", | |
"content": "First post", | |
"user": { | |
"type": "User", | |
"name": "Luke" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "Leia" | |
}, | |
"content": "First comment" | |
}, | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "Han" | |
}, | |
"content": "Second comment" | |
}, | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "Second post", | |
"user": { | |
"type": "User", | |
"name": "Darth Vader" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "Boba Fett" | |
}, | |
"content": "Third comment" | |
}, | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "Jabba" | |
}, | |
"content": "Fourth comment" | |
}, | |
] | |
} | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /views/newsfeed/_comment.json.jbuilder | |
json.type comment.class.name | |
json.user format_user(comment.user) | |
json.content comment.content |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /views/newsfeed/_post.json.jbuilder | |
json.type post.class.name | |
json.content post.content | |
json.user format_user(post.user) | |
json.comments post.comments, partial: 'comment', as: :comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ApplicationHelper | |
def format_user user | |
{type: user.class.name, name: user.name} | |
end | |
def fetch_newsfeeds(page, limit) | |
@posts = Post.order(:created_at).paginate(page: page, per_page: limit) | |
render_to_string(template: "/newsfeed/feed.json.jbuilder", locals: { posts: @posts}, format: :json) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /views/newsfeed/feed.json.jbuilder | |
json.array! @posts, partial: 'post', as: :post |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment