-
-
Save ahmadhasankhan/9427f50ba87e2b4835c2ebe848911564 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 | |
DEFAULT_PAGE_LIMIT = 50 | |
# By default fetching records in descending order, however, we can always override the default scope using unscoped method | |
default_scope -> { order(created_at: :desc) } | |
# We can reuse this anywhere and pass the different limit as per the need, By default limit is 50 | |
scope :most_recent, ->(limit = DEFAULT_PAGE_LIMIT) { preload_conditions.limit(limit) } | |
# This will preload related objects when you include it in the query | |
def self.preload_conditions | |
includes(:user, comments: [:user]) | |
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
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 | |
def index | |
@news_feeds = Post.most_recent.paginate(page: params[:page], per_page: 50) | |
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
json.comments comments do |comment| | |
json.type comment.class.name | |
json.partial! 'newsfeed/user', user: comment.user | |
json.content comment.content | |
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
json.type post.class.name | |
json.content post.content | |
json.partial! 'newsfeed/user', user: post.user | |
json.partial! 'newsfeed/comment', comments: post.comments |
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
json.user do | |
json.type user.class.name | |
json.name user.name | |
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
json.array! @news_feeds, partial: 'newsfeed/feed', as: :post |
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": "using loop 50", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 49", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 56" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 48", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 54" | |
}, | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "Hassan" | |
}, | |
"content": "This is comment 55" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 47", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 51" | |
}, | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 52" | |
}, | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 53" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 46", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 45", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 44", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 43", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 41", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 49" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 42", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "Hassan" | |
}, | |
"content": "This is comment 50" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 0", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 8" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 1", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 9" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 2", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 10" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 3", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 11" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 4", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 12" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 5", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 13" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 6", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 14" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 7", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 15" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 8", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 16" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 9", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 17" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 10", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 18" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 11", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 19" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 12", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 20" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 13", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 21" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 14", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 22" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 15", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 23" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 16", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 24" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 17", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 25" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 18", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 26" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 19", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 27" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 20", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 28" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 21", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 29" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 22", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 30" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 23", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 31" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 24", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 32" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 25", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 33" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 26", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 34" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 27", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "Hassan" | |
}, | |
"content": "This is comment 35" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 28", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 36" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 29", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 37" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 30", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "Hassan" | |
}, | |
"content": "This is comment 38" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 31", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 39" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 32", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 40" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 33", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 41" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 34", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 42" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 35", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "Hassan" | |
}, | |
"content": "This is comment 43" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 36", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 44" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 37", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 45" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 38", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "Hassan" | |
}, | |
"content": "This is comment 46" | |
} | |
] | |
}, | |
{ | |
"type": "Post", | |
"content": "using loop 39", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"comments": [ | |
{ | |
"type": "Comment", | |
"user": { | |
"type": "User", | |
"name": "ahmad" | |
}, | |
"content": "This is comment 47" | |
} | |
] | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment