diff --git a/.DS_Store b/.DS_Store index 8ee329e..0bc3d0e 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/.gitignore b/.gitignore index 2979777..4207671 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ codesnip /vendor/* +/.idea/ diff --git a/Gemfile.lock b/Gemfile.lock index add3a6d..46fce61 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -171,7 +171,7 @@ GEM unicorn-rails (2.2.0) rack unicorn - will_paginate (3.0.7) + will_paginate (3.1.0) PLATFORMS ruby diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index e5f1d02..6f68004 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -8,6 +8,7 @@ def index end def show + @question.increment_views render json: @question, status: 200 end diff --git a/app/models/question.rb b/app/models/question.rb index 1eb5eee..cb43700 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -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 diff --git a/app/serializers/question_serializer.rb b/app/serializers/question_serializer.rb index 84f4bff..fe6bc9a 100644 --- a/app/serializers/question_serializer.rb +++ b/app/serializers/question_serializer.rb @@ -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 diff --git a/db/migrate/20160205110448_add_views_to_questions.rb b/db/migrate/20160205110448_add_views_to_questions.rb new file mode 100644 index 0000000..af9eacf --- /dev/null +++ b/db/migrate/20160205110448_add_views_to_questions.rb @@ -0,0 +1,5 @@ +class AddViewsToQuestions < ActiveRecord::Migration + def change + add_column :questions, :views, :integer, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index 022b094..5cfa904 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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| @@ -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"