-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance Question Resource - issue 18
Why? To return a question's user object, votes_count, answers_count, views_count and time_updated
- Loading branch information
Showing
8 changed files
with
67 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,4 @@ | |
|
||
codesnip | ||
/vendor/* | ||
/.idea/ |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ def index | |
end | ||
|
||
def show | ||
@question.increment_views | ||
render json: @question, status: 200 | ||
end | ||
|
||
|
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,28 @@ | ||
class QuestionSerializer < ActiveModel::Serializer | ||
attributes :id, :user_id, :title, :content, :votes, :comments, :tags, :answers, | ||
:created_at, :updated_at | ||
:created_at, :updated_at, :votes_count, :answers_count, :views_count, | ||
:time_updated, :user | ||
|
||
has_many :comments, as: :comment_on | ||
has_many :tags, as: :subscriber | ||
has_many :answers | ||
has_many :comments, as: :comment_on | ||
has_many :tags, as: :subscriber | ||
has_many :answers | ||
|
||
def votes_count | ||
object.votes.count | ||
end | ||
|
||
def answers_count | ||
object.answers.count | ||
end | ||
|
||
def views_count | ||
object.views | ||
end | ||
|
||
def user | ||
{ | ||
name: object.user.name, | ||
image: object.user.social_providers.first.profile_picture | ||
} | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddViewsToQuestions < ActiveRecord::Migration | ||
def change | ||
add_column :questions, :views, :integer, default: 0 | ||
end | ||
end |
This file contains 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