From 7de522a82f8c7bd2ca447e0a4b5707f232f79621 Mon Sep 17 00:00:00 2001 From: nilsding Date: Sun, 7 Dec 2014 20:51:44 +0100 Subject: [PATCH] added question#show thing and more... --- Rakefile | 12 +++++++ app/controllers/question_controller.rb | 6 ++++ app/models/inbox.rb | 6 ++-- app/views/inbox/show.html.haml | 5 +++ app/views/question/show.html.haml | 18 ++++++++++ app/views/shared/_answerbox.html.haml | 33 +++++++++++-------- config/routes.rb | 2 ++ ...207194424_add_answer_count_to_questions.rb | 5 +++ db/schema.rb | 3 +- 9 files changed, 74 insertions(+), 16 deletions(-) create mode 100644 app/controllers/question_controller.rb create mode 100644 app/views/question/show.html.haml create mode 100644 db/migrate/20141207194424_add_answer_count_to_questions.rb diff --git a/Rakefile b/Rakefile index 6cd625f1..83db07a0 100644 --- a/Rakefile +++ b/Rakefile @@ -27,6 +27,18 @@ namespace :justask do end progress.increment end + + total = Question.count + progress = ProgressBar.create title: 'Processing questions', format: format, starting_at: 0, total: total + Question.all.each do |question| + begin + answers = Answer.where(question: question).count + question.answer_count = answers + question.save! + end + progress.increment + end + total = Answer.count progress = ProgressBar.create title: 'Processing answers', format: format, starting_at: 0, total: total Answer.all.each do |answer| diff --git a/app/controllers/question_controller.rb b/app/controllers/question_controller.rb new file mode 100644 index 00000000..4789a8d2 --- /dev/null +++ b/app/controllers/question_controller.rb @@ -0,0 +1,6 @@ +class QuestionController < ApplicationController + def show + @question = Question.find(params[:id]) + @answers = @question.answers.reverse_order + end +end diff --git a/app/models/inbox.rb b/app/models/inbox.rb index 46f995a5..335d3413 100644 --- a/app/models/inbox.rb +++ b/app/models/inbox.rb @@ -7,15 +7,17 @@ class Inbox < ActiveRecord::Base user: user, question: self.question) user.increment! :answered_count + self.question.increment! :answer_count self.destroy end def remove + self.question.decrement! :answer_count unless self.question.user.nil? - self.question.user.decrement! :asked_count if self.question.answers.count == 1 + self.question.user.decrement! :asked_count if self.question.answer_count == 1 end - self.question.destroy if self.question.answers.count == 1 + self.question.destroy if self.question.answer_count == 1 self.destroy end end diff --git a/app/views/inbox/show.html.haml b/app/views/inbox/show.html.haml index edee2efe..acb5c0dd 100644 --- a/app/views/inbox/show.html.haml +++ b/app/views/inbox/show.html.haml @@ -13,6 +13,11 @@ asked = time_ago_in_words(i.question.created_at) ago + - unless a.question.author_is_anonymous + - if a.question.answer_count > 0 + · + %a{href: show_user_question_path(i.question.user.screen_name, i.question.id)} + #{a.question.answer_count} response(s) %p.answerbox-question-text= i.question.content .panel-body %textarea.form-control{name: 'ib-answer', 'data-id' => i.id} diff --git a/app/views/question/show.html.haml b/app/views/question/show.html.haml new file mode 100644 index 00000000..a85e7780 --- /dev/null +++ b/app/views/question/show.html.haml @@ -0,0 +1,18 @@ +.container.j2-page + / TODO: make this pretty (it's currently C-c'd straight from shared/_answerbox) + .panel-heading + .media + - unless @question.author_is_anonymous + %a.pull-left{href: show_user_profile_path(@question.user.screen_name)} + %img.img-rounded.img-answerbox{src: gravatar_url(@question.user)} + .media-body + %h6.text-muted.media-heading.answerbox-question-user + = user_screen_name @question.user, @question.author_is_anonymous + asked + = time_ago_in_words(@question.created_at) + ago + %p.answerbox-question-text= @question.content + + - @answers.each do |a| + = render 'shared/answerbox', a: a, show_question: false + diff --git a/app/views/shared/_answerbox.html.haml b/app/views/shared/_answerbox.html.haml index 5df7a005..8350dfdb 100644 --- a/app/views/shared/_answerbox.html.haml +++ b/app/views/shared/_answerbox.html.haml @@ -1,17 +1,24 @@ .panel.panel-default.answer-box{'data-id' => a.id} - .panel-heading - .media - - unless a.question.author_is_anonymous - %a.pull-left{href: show_user_profile_path(a.question.user.screen_name)} - %img.img-rounded.img-answerbox{src: gravatar_url(a.question.user)} - .media-body - %h6.text-muted.media-heading.answerbox-question-user - = user_screen_name a.question.user, a.question.author_is_anonymous - asked - %a{href: show_user_answer_path(a.user.screen_name, a.id)} - = time_ago_in_words(a.question.created_at) - ago - %p.answerbox-question-text= a.question.content + - if @question.nil? + .panel-heading + .media + - unless a.question.author_is_anonymous + %a.pull-left{href: show_user_profile_path(a.question.user.screen_name)} + %img.img-rounded.img-answerbox{src: gravatar_url(a.question.user)} + .media-body + %h6.text-muted.media-heading.answerbox-question-user + = user_screen_name a.question.user, a.question.author_is_anonymous + asked + %a{href: show_user_answer_path(a.user.screen_name, a.id)} + = time_ago_in_words(a.question.created_at) + ago + - unless a.question.author_is_anonymous + - if a.question.answer_count > 1 + · + %a{href: show_user_question_path(a.question.user.screen_name, a.question.id)} + #{a.question.answer_count} answers + %p.answerbox-question-text + = a.question.content .panel-body %p= a.content - if @user.nil? diff --git a/config/routes.rb b/config/routes.rb index 374a663d..58976d6c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -41,6 +41,8 @@ Rails.application.routes.draw do match '/user/:username(/p/:page)', to: 'user#show', via: 'get', defaults: {page: 1} match '/@:username(/p/:page)', to: 'user#show', via: 'get', as: :show_user_profile, defaults: {page: 1} match '/@:username/a/:id', to: 'answer#show', via: 'get', as: :show_user_answer + match '/@:username/q/:id', to: 'question#show', via: 'get', as: :show_user_question match '/:username(/p/:page)', to: 'user#show', via: 'get', as: :show_user_profile_alt, defaults: {page: 1} match '/:username/a/:id', to: 'answer#show', via: 'get', as: :show_user_answer_alt + match '/:username/q/:id', to: 'question#show', via: 'get', as: :show_user_question_alt end diff --git a/db/migrate/20141207194424_add_answer_count_to_questions.rb b/db/migrate/20141207194424_add_answer_count_to_questions.rb new file mode 100644 index 00000000..76f139ac --- /dev/null +++ b/db/migrate/20141207194424_add_answer_count_to_questions.rb @@ -0,0 +1,5 @@ +class AddAnswerCountToQuestions < ActiveRecord::Migration + def change + add_column :questions, :answer_count, :integer, default: 0, null: false + end +end diff --git a/db/schema.rb b/db/schema.rb index f151bbfb..15b65050 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: 20141201191324) do +ActiveRecord::Schema.define(version: 20141207194424) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -54,6 +54,7 @@ ActiveRecord::Schema.define(version: 20141201191324) do t.integer "user_id" t.datetime "created_at" t.datetime "updated_at" + t.integer "answer_count", default: 0, null: false end add_index "questions", ["user_id", "created_at"], name: "index_questions_on_user_id_and_created_at", using: :btree