Skip to content

Commit

Permalink
Enhance Question Resource - issue 18
Browse files Browse the repository at this point in the history
Why?
To return a question's user object, votes_count, answers_count, views_count and time_updated
  • Loading branch information
ChigboIO committed Feb 5, 2016
1 parent 189853f commit 2d52985
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 23 deletions.
Binary file modified .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@

codesnip
/vendor/*
/.idea/
30 changes: 15 additions & 15 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ GEM
erubis (~> 2.7.0)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
active_model_serializers (0.8.3)
activemodel (>= 3.0)
active_model_serializers (0.9.4)
activemodel (>= 3.2)
activejob (4.2.5)
activesupport (= 4.2.5)
globalid (>= 0.3.0)
Expand Down Expand Up @@ -57,7 +57,7 @@ GEM
hashie (3.4.3)
i18n (0.7.0)
json (1.8.3)
jwt (1.5.2)
jwt (1.5.1)
kgio (2.10.0)
loofah (2.0.3)
nokogiri (>= 1.5.9)
Expand All @@ -66,18 +66,18 @@ GEM
method_source (0.8.2)
mime-types (2.99)
mini_portile2 (2.0.0)
minitest (5.8.3)
minitest (5.8.4)
multi_json (1.11.2)
multi_xml (0.5.5)
multipart-post (2.0.0)
nokogiri (1.6.7.1)
nokogiri (1.6.7.2)
mini_portile2 (~> 2.0.0.rc2)
oauth2 (1.0.0)
oauth2 (1.1.0)
faraday (>= 0.8, < 0.10)
jwt (~> 1.0)
jwt (~> 1.0, < 1.5.2)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (~> 1.2)
rack (>= 1.2, < 3)
omniauth (1.3.1)
hashie (>= 1.2, < 4)
rack (>= 1.0, < 3)
Expand Down Expand Up @@ -124,7 +124,7 @@ GEM
activesupport (>= 4.2.0.beta, < 5.0)
nokogiri (~> 1.6.0)
rails-deprecated_sanitizer (>= 1.0.1)
rails-html-sanitizer (1.0.2)
rails-html-sanitizer (1.0.3)
loofah (~> 2.0)
railties (4.2.5)
actionpack (= 4.2.5)
Expand All @@ -133,15 +133,15 @@ GEM
thor (>= 0.18.1, < 2.0)
raindrops (0.15.0)
rake (10.5.0)
rspec-core (3.4.1)
rspec-core (3.4.2)
rspec-support (~> 3.4.0)
rspec-expectations (3.4.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
rspec-mocks (3.4.0)
rspec-mocks (3.4.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
rspec-rails (3.4.0)
rspec-rails (3.4.2)
actionpack (>= 3.0, < 4.3)
activesupport (>= 3.0, < 4.3)
railties (>= 3.0, < 4.3)
Expand All @@ -151,11 +151,11 @@ GEM
rspec-support (~> 3.4.0)
rspec-support (3.4.1)
slop (3.6.0)
spring (1.6.2)
spring (1.6.3)
sprockets (3.5.2)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (3.0.0)
sprockets-rails (3.0.1)
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
Expand All @@ -171,7 +171,7 @@ GEM
unicorn-rails (2.2.0)
rack
unicorn
will_paginate (3.0.7)
will_paginate (3.1.0)

PLATFORMS
ruby
Expand Down
1 change: 1 addition & 0 deletions app/controllers/questions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def index
end

def show
@question.increment_views
render json: @question, status: 200
end

Expand Down
16 changes: 16 additions & 0 deletions app/models/question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ class Question < ActiveRecord::Base
validates :content, presence: true

include Modify
include ActionView::Helpers::DateHelper

def time_updated
created = DateTime.parse(created_at.to_s).in_time_zone
updated = DateTime.parse(updated_at.to_s).in_time_zone

if (updated - created).to_i > 2
return distance_of_time_in_words(updated, Time.zone.now) + " ago"
end

nil
end

def increment_views
update(views: views + 1)
end

class << self
def with_answers
Expand Down
28 changes: 24 additions & 4 deletions app/serializers/question_serializer.rb
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
5 changes: 5 additions & 0 deletions db/migrate/20160205110448_add_views_to_questions.rb
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
9 changes: 5 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160201170223) do
ActiveRecord::Schema.define(version: 20160205110448) do

create_table "answers", force: :cascade do |t|
t.integer "user_id"
Expand All @@ -36,8 +36,9 @@
t.integer "user_id"
t.string "title"
t.string "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "views", default: 0
end

create_table "social_providers", force: :cascade do |t|
Expand All @@ -51,7 +52,7 @@
t.string "token"
t.string "profile_picture"
t.string "profile_url"
t.string "email"
t.string "profile_email"
end

add_index "social_providers", ["user_id"], name: "index_social_providers_on_user_id"
Expand Down

0 comments on commit 2d52985

Please sign in to comment.