From d9805ce52ebf8058976177ce9cb439114cda7216 Mon Sep 17 00:00:00 2001 From: Zach Schatz Date: Fri, 22 Jan 2016 16:22:25 -0500 Subject: [PATCH 001/152] Create db table for answers. Include db validations --- kiwi/db/migrate/20160122211234_create_answers.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 kiwi/db/migrate/20160122211234_create_answers.rb diff --git a/kiwi/db/migrate/20160122211234_create_answers.rb b/kiwi/db/migrate/20160122211234_create_answers.rb new file mode 100644 index 0000000..2f011d3 --- /dev/null +++ b/kiwi/db/migrate/20160122211234_create_answers.rb @@ -0,0 +1,11 @@ +class CreateAnswers < ActiveRecord::Migration + def change + create_table :answers do |t| + t.text :content, presence: true + t.references :user, index: true, foreign_key: true, presence: true + t.references :question, index: true, foreign_key: true, presence: true + + t.timestamps null: false + end + end +end From 6071304f1a7d05abfcd5ae5bc6784ec66616f87c Mon Sep 17 00:00:00 2001 From: Zach Schatz Date: Fri, 22 Jan 2016 16:23:06 -0500 Subject: [PATCH 002/152] Generate Answers model. Validate presence of table fields --- kiwi/app/models/answer.rb | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 kiwi/app/models/answer.rb diff --git a/kiwi/app/models/answer.rb b/kiwi/app/models/answer.rb new file mode 100644 index 0000000..b76b749 --- /dev/null +++ b/kiwi/app/models/answer.rb @@ -0,0 +1,6 @@ +class Answers < ApplicationController + belongs_to :user + belongs_to :question + + validates_presence_of :content, :user, :question +end \ No newline at end of file From ef85c67f1589ff0903fedb7e83826f86c9252541 Mon Sep 17 00:00:00 2001 From: Zach Schatz Date: Fri, 22 Jan 2016 16:33:26 -0500 Subject: [PATCH 003/152] Include association to comments and votes --- kiwi/app/models/answer.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/kiwi/app/models/answer.rb b/kiwi/app/models/answer.rb index b76b749..e11f45c 100644 --- a/kiwi/app/models/answer.rb +++ b/kiwi/app/models/answer.rb @@ -2,5 +2,8 @@ class Answers < ApplicationController belongs_to :user belongs_to :question + has_many :comments, as :commentable + has_many :votes, as :votable + validates_presence_of :content, :user, :question -end \ No newline at end of file +end From af1057fecef144f4655d485f9f9bcd3d41475379 Mon Sep 17 00:00:00 2001 From: Zach Schatz Date: Fri, 22 Jan 2016 16:36:44 -0500 Subject: [PATCH 004/152] Revise as: :commentable --- kiwi/app/models/answer.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kiwi/app/models/answer.rb b/kiwi/app/models/answer.rb index e11f45c..ffea432 100644 --- a/kiwi/app/models/answer.rb +++ b/kiwi/app/models/answer.rb @@ -2,8 +2,8 @@ class Answers < ApplicationController belongs_to :user belongs_to :question - has_many :comments, as :commentable - has_many :votes, as :votable + has_many :comments, as: :commentable + has_many :votes, as: :votable validates_presence_of :content, :user, :question end From 6caba9f96510598ee86d1d2fb9e2695ee07f6d72 Mon Sep 17 00:00:00 2001 From: Nicola Beuscher Date: Fri, 22 Jan 2016 16:17:21 -0500 Subject: [PATCH 005/152] Generate questions migration and model with assocations --- kiwi/app/models/question.rb | 6 ++++++ kiwi/db/migrate/20160122211518_create_questions.rb | 12 ++++++++++++ kiwi/test/fixtures/questions.yml | 13 +++++++++++++ kiwi/test/models/question_test.rb | 7 +++++++ 4 files changed, 38 insertions(+) create mode 100644 kiwi/app/models/question.rb create mode 100644 kiwi/db/migrate/20160122211518_create_questions.rb create mode 100644 kiwi/test/fixtures/questions.yml create mode 100644 kiwi/test/models/question_test.rb diff --git a/kiwi/app/models/question.rb b/kiwi/app/models/question.rb new file mode 100644 index 0000000..753902e --- /dev/null +++ b/kiwi/app/models/question.rb @@ -0,0 +1,6 @@ +class Question < ActiveRecord::Base + belongs_to :user + has_many :answers + has_many :comments + has_many :votes +end diff --git a/kiwi/db/migrate/20160122211518_create_questions.rb b/kiwi/db/migrate/20160122211518_create_questions.rb new file mode 100644 index 0000000..021b88c --- /dev/null +++ b/kiwi/db/migrate/20160122211518_create_questions.rb @@ -0,0 +1,12 @@ +class CreateQuestions < ActiveRecord::Migration + def change + create_table :questions do |t| + t.string :title + t.text :content + t.integer :user_id + t.integer :best_answer_id + + t.timestamps null: false + end + end +end diff --git a/kiwi/test/fixtures/questions.yml b/kiwi/test/fixtures/questions.yml new file mode 100644 index 0000000..f34db3e --- /dev/null +++ b/kiwi/test/fixtures/questions.yml @@ -0,0 +1,13 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + title: MyString + content: MyText + user_id: 1 + best_answer_id: 1 + +two: + title: MyString + content: MyText + user_id: 1 + best_answer_id: 1 diff --git a/kiwi/test/models/question_test.rb b/kiwi/test/models/question_test.rb new file mode 100644 index 0000000..88f6ea7 --- /dev/null +++ b/kiwi/test/models/question_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class QuestionTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end From 9e7a2d3db9ca59f78acced00a8aebbd9aaef77fb Mon Sep 17 00:00:00 2001 From: Nicola Beuscher Date: Fri, 22 Jan 2016 16:27:22 -0500 Subject: [PATCH 006/152] Add database constraints and model validations --- kiwi/app/models/question.rb | 3 ++ .../20160122211518_create_questions.rb | 6 ++-- kiwi/db/schema.rb | 30 +++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 kiwi/db/schema.rb diff --git a/kiwi/app/models/question.rb b/kiwi/app/models/question.rb index 753902e..2f76e4c 100644 --- a/kiwi/app/models/question.rb +++ b/kiwi/app/models/question.rb @@ -3,4 +3,7 @@ class Question < ActiveRecord::Base has_many :answers has_many :comments has_many :votes + + validates_presence_of :title, :content, :user_id + end diff --git a/kiwi/db/migrate/20160122211518_create_questions.rb b/kiwi/db/migrate/20160122211518_create_questions.rb index 021b88c..d1fef24 100644 --- a/kiwi/db/migrate/20160122211518_create_questions.rb +++ b/kiwi/db/migrate/20160122211518_create_questions.rb @@ -1,9 +1,9 @@ class CreateQuestions < ActiveRecord::Migration def change create_table :questions do |t| - t.string :title - t.text :content - t.integer :user_id + t.string :title, null: false + t.text :content, null: false + t.integer :user_id, null: false, index: true, foreign_key: true t.integer :best_answer_id t.timestamps null: false diff --git a/kiwi/db/schema.rb b/kiwi/db/schema.rb new file mode 100644 index 0000000..08fb330 --- /dev/null +++ b/kiwi/db/schema.rb @@ -0,0 +1,30 @@ +# encoding: UTF-8 +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 20160122211518) do + + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" + + create_table "questions", force: :cascade do |t| + t.string "title", null: false + t.text "content", null: false + t.integer "user_id", null: false + t.integer "best_answer_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "questions", ["user_id"], name: "index_questions_on_user_id", using: :btree + +end From 649d4edb47d1f48dadd9d809bfedb7a5e9feb0d2 Mon Sep 17 00:00:00 2001 From: Nicola Beuscher Date: Fri, 22 Jan 2016 16:38:23 -0500 Subject: [PATCH 007/152] Fix assocations for polymorphism --- kiwi/app/models/question.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kiwi/app/models/question.rb b/kiwi/app/models/question.rb index 2f76e4c..96fb3ce 100644 --- a/kiwi/app/models/question.rb +++ b/kiwi/app/models/question.rb @@ -1,8 +1,8 @@ class Question < ActiveRecord::Base belongs_to :user has_many :answers - has_many :comments - has_many :votes + has_many :comments as: :commentable + has_many :votes as: :votable validates_presence_of :title, :content, :user_id From cfcb12ef03e31e75c903939bfb3b18c9f1f0c98d Mon Sep 17 00:00:00 2001 From: Luis Fernando Plaz Date: Fri, 22 Jan 2016 16:16:55 -0500 Subject: [PATCH 008/152] Generate Migrations Votes Comments --- kiwi/db/migrate/20160122211402_create_comments.rb | 6 ++++++ kiwi/db/migrate/20160122211417_create_votes.rb | 6 ++++++ 2 files changed, 12 insertions(+) create mode 100644 kiwi/db/migrate/20160122211402_create_comments.rb create mode 100644 kiwi/db/migrate/20160122211417_create_votes.rb diff --git a/kiwi/db/migrate/20160122211402_create_comments.rb b/kiwi/db/migrate/20160122211402_create_comments.rb new file mode 100644 index 0000000..50db114 --- /dev/null +++ b/kiwi/db/migrate/20160122211402_create_comments.rb @@ -0,0 +1,6 @@ +class CreateComments < ActiveRecord::Migration + def change + create_table :comments do |t| + end + end +end diff --git a/kiwi/db/migrate/20160122211417_create_votes.rb b/kiwi/db/migrate/20160122211417_create_votes.rb new file mode 100644 index 0000000..519789e --- /dev/null +++ b/kiwi/db/migrate/20160122211417_create_votes.rb @@ -0,0 +1,6 @@ +class CreateVotes < ActiveRecord::Migration + def change + create_table :votes do |t| + end + end +end From f8ef1e2606072cfbc242b438cad72f78d4fdc588 Mon Sep 17 00:00:00 2001 From: Luis Fernando Plaz Date: Fri, 22 Jan 2016 16:18:48 -0500 Subject: [PATCH 009/152] Create migration for comments --- kiwi/db/migrate/20160122211743_create_comments.rb | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 kiwi/db/migrate/20160122211743_create_comments.rb diff --git a/kiwi/db/migrate/20160122211743_create_comments.rb b/kiwi/db/migrate/20160122211743_create_comments.rb new file mode 100644 index 0000000..ec17c41 --- /dev/null +++ b/kiwi/db/migrate/20160122211743_create_comments.rb @@ -0,0 +1,8 @@ +class CreateComments < ActiveRecord::Migration + def change + create_table :comments do |t| + + t.timestamps null: false + end + end +end From 83043f46cd823ac43f188883d0ca186333927179 Mon Sep 17 00:00:00 2001 From: Luis Fernando Plaz Date: Fri, 22 Jan 2016 16:19:20 -0500 Subject: [PATCH 010/152] Add migration for votes --- kiwi/db/migrate/20160122211756_create_votes.rb | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 kiwi/db/migrate/20160122211756_create_votes.rb diff --git a/kiwi/db/migrate/20160122211756_create_votes.rb b/kiwi/db/migrate/20160122211756_create_votes.rb new file mode 100644 index 0000000..9f13209 --- /dev/null +++ b/kiwi/db/migrate/20160122211756_create_votes.rb @@ -0,0 +1,8 @@ +class CreateVotes < ActiveRecord::Migration + def change + create_table :votes do |t| + + t.timestamps null: false + end + end +end From 6d294b5429cf1989f5f30a0f39cf0367a68bdf16 Mon Sep 17 00:00:00 2001 From: Luis Fernando Plaz Date: Fri, 22 Jan 2016 16:19:42 -0500 Subject: [PATCH 011/152] Add Models for Votes Comments --- kiwi/app/models/comment.rb | 2 ++ kiwi/app/models/vote.rb | 2 ++ kiwi/db/migrate/20160122211402_create_comments.rb | 6 ------ kiwi/db/migrate/20160122211417_create_votes.rb | 6 ------ kiwi/test/fixtures/comments.yml | 11 +++++++++++ kiwi/test/fixtures/votes.yml | 11 +++++++++++ kiwi/test/models/comment_test.rb | 7 +++++++ kiwi/test/models/vote_test.rb | 7 +++++++ 8 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 kiwi/app/models/comment.rb create mode 100644 kiwi/app/models/vote.rb delete mode 100644 kiwi/db/migrate/20160122211402_create_comments.rb delete mode 100644 kiwi/db/migrate/20160122211417_create_votes.rb create mode 100644 kiwi/test/fixtures/comments.yml create mode 100644 kiwi/test/fixtures/votes.yml create mode 100644 kiwi/test/models/comment_test.rb create mode 100644 kiwi/test/models/vote_test.rb diff --git a/kiwi/app/models/comment.rb b/kiwi/app/models/comment.rb new file mode 100644 index 0000000..45b2d38 --- /dev/null +++ b/kiwi/app/models/comment.rb @@ -0,0 +1,2 @@ +class Comment < ActiveRecord::Base +end diff --git a/kiwi/app/models/vote.rb b/kiwi/app/models/vote.rb new file mode 100644 index 0000000..3b2b5a2 --- /dev/null +++ b/kiwi/app/models/vote.rb @@ -0,0 +1,2 @@ +class Vote < ActiveRecord::Base +end diff --git a/kiwi/db/migrate/20160122211402_create_comments.rb b/kiwi/db/migrate/20160122211402_create_comments.rb deleted file mode 100644 index 50db114..0000000 --- a/kiwi/db/migrate/20160122211402_create_comments.rb +++ /dev/null @@ -1,6 +0,0 @@ -class CreateComments < ActiveRecord::Migration - def change - create_table :comments do |t| - end - end -end diff --git a/kiwi/db/migrate/20160122211417_create_votes.rb b/kiwi/db/migrate/20160122211417_create_votes.rb deleted file mode 100644 index 519789e..0000000 --- a/kiwi/db/migrate/20160122211417_create_votes.rb +++ /dev/null @@ -1,6 +0,0 @@ -class CreateVotes < ActiveRecord::Migration - def change - create_table :votes do |t| - end - end -end diff --git a/kiwi/test/fixtures/comments.yml b/kiwi/test/fixtures/comments.yml new file mode 100644 index 0000000..937a0c0 --- /dev/null +++ b/kiwi/test/fixtures/comments.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/kiwi/test/fixtures/votes.yml b/kiwi/test/fixtures/votes.yml new file mode 100644 index 0000000..937a0c0 --- /dev/null +++ b/kiwi/test/fixtures/votes.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/kiwi/test/models/comment_test.rb b/kiwi/test/models/comment_test.rb new file mode 100644 index 0000000..b6d6131 --- /dev/null +++ b/kiwi/test/models/comment_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class CommentTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/kiwi/test/models/vote_test.rb b/kiwi/test/models/vote_test.rb new file mode 100644 index 0000000..f31f992 --- /dev/null +++ b/kiwi/test/models/vote_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class VoteTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end From f43a713d2c683f491f8a310ee81d158e48a620be Mon Sep 17 00:00:00 2001 From: Luis Fernando Plaz Date: Fri, 22 Jan 2016 16:40:03 -0500 Subject: [PATCH 012/152] Add comment model associatons and validations --- kiwi/app/models/comment.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/kiwi/app/models/comment.rb b/kiwi/app/models/comment.rb index 45b2d38..55031a9 100644 --- a/kiwi/app/models/comment.rb +++ b/kiwi/app/models/comment.rb @@ -1,2 +1,7 @@ class Comment < ActiveRecord::Base + belongs_to :user + belongs_to :commentable, polymorphic: true + + validates :content, presence: true, length: { maximum: 500 } + end From 15f646408ef21afdd3a9db2eb11352aaac7ecbd6 Mon Sep 17 00:00:00 2001 From: Luis Fernando Plaz Date: Fri, 22 Jan 2016 16:41:16 -0500 Subject: [PATCH 013/152] Add votes model associatons and validations --- kiwi/app/models/vote.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/kiwi/app/models/vote.rb b/kiwi/app/models/vote.rb index 3b2b5a2..40d7f04 100644 --- a/kiwi/app/models/vote.rb +++ b/kiwi/app/models/vote.rb @@ -1,2 +1,8 @@ class Vote < ActiveRecord::Base + belongs_to :user + belongs_to :votable, polymorphic: true + + validates :direction, presence: true + validates_inclusion_of :direction, :in => [-1, 1], :allow_nil => true + end From d2cb1309742779acc90d8417612f48a672fb0cad Mon Sep 17 00:00:00 2001 From: Luis Fernando Plaz Date: Fri, 22 Jan 2016 16:41:47 -0500 Subject: [PATCH 014/152] Complete migrations information comment and votes --- kiwi/db/migrate/20160122211743_create_comments.rb | 3 +++ kiwi/db/migrate/20160122211756_create_votes.rb | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/kiwi/db/migrate/20160122211743_create_comments.rb b/kiwi/db/migrate/20160122211743_create_comments.rb index ec17c41..f9f789b 100644 --- a/kiwi/db/migrate/20160122211743_create_comments.rb +++ b/kiwi/db/migrate/20160122211743_create_comments.rb @@ -1,6 +1,9 @@ class CreateComments < ActiveRecord::Migration def change create_table :comments do |t| + t.string :content, null: false, limit: 200 + t.integer :commentable_id, polymorphic: true, index: true null: false + t.string :commentable_type, polymorphic: true, null:false t.timestamps null: false end diff --git a/kiwi/db/migrate/20160122211756_create_votes.rb b/kiwi/db/migrate/20160122211756_create_votes.rb index 9f13209..c4203fa 100644 --- a/kiwi/db/migrate/20160122211756_create_votes.rb +++ b/kiwi/db/migrate/20160122211756_create_votes.rb @@ -1,6 +1,10 @@ class CreateVotes < ActiveRecord::Migration def change create_table :votes do |t| + t.integer :user_id, index: true, null: false + t.integer :votable_id, polymorphic: true, index: true, null: false + t.string :votable_type, polymorphic: true, null: false + t.integer :direction null: false t.timestamps null: false end From b0d8fa79f8d963293d8a05a4e95d39c31506fd7a Mon Sep 17 00:00:00 2001 From: Jeff George Date: Fri, 22 Jan 2016 16:36:10 -0500 Subject: [PATCH 015/152] Create User model and users_table migration --- kiwi/app/models/user.rb | 13 +++++++++++++ .../db/migrate/20160122211310_create_users_table.rb | 11 +++++++++++ kiwi/db/schema.rb | 11 +++++++++++ 3 files changed, 35 insertions(+) create mode 100644 kiwi/app/models/user.rb create mode 100644 kiwi/db/migrate/20160122211310_create_users_table.rb diff --git a/kiwi/app/models/user.rb b/kiwi/app/models/user.rb new file mode 100644 index 0000000..dc97690 --- /dev/null +++ b/kiwi/app/models/user.rb @@ -0,0 +1,13 @@ +class User < ActiveRecord::Base + has_secure_password + has_many :questions + has_many :answers + has_many :comments + has_many :votes + + validates :name, presence: true, length: {in: 8..20} + validates :email, presence: true + + validates_format_of :email, with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create + +end diff --git a/kiwi/db/migrate/20160122211310_create_users_table.rb b/kiwi/db/migrate/20160122211310_create_users_table.rb new file mode 100644 index 0000000..a29b402 --- /dev/null +++ b/kiwi/db/migrate/20160122211310_create_users_table.rb @@ -0,0 +1,11 @@ +class CreateUsersTable < ActiveRecord::Migration + def change + create_table :users_tables do |t| + t.string :name, null: false, unique: true, limit: 20 + t.string :email, null: false, unique: true + t.string :password_digest, null: false + + t.timestamps null: false + end + end +end diff --git a/kiwi/db/schema.rb b/kiwi/db/schema.rb index 08fb330..4e36040 100644 --- a/kiwi/db/schema.rb +++ b/kiwi/db/schema.rb @@ -11,8 +11,10 @@ # # It's strongly recommended that you check this file into your version control system. + ActiveRecord::Schema.define(version: 20160122211518) do + # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -27,4 +29,13 @@ add_index "questions", ["user_id"], name: "index_questions_on_user_id", using: :btree + + create_table "users_tables", force: :cascade do |t| + t.string "name", limit: 20, null: false + t.string "email", null: false + t.string "password_digest", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + end From 1db87481d8b84daf37786a75a12dd3b722142d4a Mon Sep 17 00:00:00 2001 From: Jeff George Date: Fri, 22 Jan 2016 17:15:55 -0500 Subject: [PATCH 016/152] Resolve merge conflict and fixed typos in migrations and models --- kiwi/app/models/answer.rb | 2 +- kiwi/app/models/question.rb | 4 +- .../migrate/20160122211234_create_answers.rb | 4 +- ...able.rb => 20160122211310_create_users.rb} | 4 +- .../migrate/20160122211743_create_comments.rb | 2 +- .../db/migrate/20160122211756_create_votes.rb | 2 +- kiwi/db/schema.rb | 40 ++++++++++++++++--- 7 files changed, 44 insertions(+), 14 deletions(-) rename kiwi/db/migrate/{20160122211310_create_users_table.rb => 20160122211310_create_users.rb} (71%) diff --git a/kiwi/app/models/answer.rb b/kiwi/app/models/answer.rb index ffea432..61140c1 100644 --- a/kiwi/app/models/answer.rb +++ b/kiwi/app/models/answer.rb @@ -1,4 +1,4 @@ -class Answers < ApplicationController +class Answer < ActiveRecord::Base belongs_to :user belongs_to :question diff --git a/kiwi/app/models/question.rb b/kiwi/app/models/question.rb index 96fb3ce..8ac844f 100644 --- a/kiwi/app/models/question.rb +++ b/kiwi/app/models/question.rb @@ -1,8 +1,8 @@ class Question < ActiveRecord::Base belongs_to :user has_many :answers - has_many :comments as: :commentable - has_many :votes as: :votable + has_many :comments, as: :commentable + has_many :votes, as: :votable validates_presence_of :title, :content, :user_id diff --git a/kiwi/db/migrate/20160122211234_create_answers.rb b/kiwi/db/migrate/20160122211234_create_answers.rb index 2f011d3..0aa7edd 100644 --- a/kiwi/db/migrate/20160122211234_create_answers.rb +++ b/kiwi/db/migrate/20160122211234_create_answers.rb @@ -2,8 +2,8 @@ class CreateAnswers < ActiveRecord::Migration def change create_table :answers do |t| t.text :content, presence: true - t.references :user, index: true, foreign_key: true, presence: true - t.references :question, index: true, foreign_key: true, presence: true + t.integer :user_id, index: true, foreign_key: true, presence: true + t.integer :question_id, index: true, foreign_key: true, presence: true t.timestamps null: false end diff --git a/kiwi/db/migrate/20160122211310_create_users_table.rb b/kiwi/db/migrate/20160122211310_create_users.rb similarity index 71% rename from kiwi/db/migrate/20160122211310_create_users_table.rb rename to kiwi/db/migrate/20160122211310_create_users.rb index a29b402..b3f5a08 100644 --- a/kiwi/db/migrate/20160122211310_create_users_table.rb +++ b/kiwi/db/migrate/20160122211310_create_users.rb @@ -1,6 +1,6 @@ -class CreateUsersTable < ActiveRecord::Migration +class CreateUsers < ActiveRecord::Migration def change - create_table :users_tables do |t| + create_table :users do |t| t.string :name, null: false, unique: true, limit: 20 t.string :email, null: false, unique: true t.string :password_digest, null: false diff --git a/kiwi/db/migrate/20160122211743_create_comments.rb b/kiwi/db/migrate/20160122211743_create_comments.rb index f9f789b..d55ea8d 100644 --- a/kiwi/db/migrate/20160122211743_create_comments.rb +++ b/kiwi/db/migrate/20160122211743_create_comments.rb @@ -2,7 +2,7 @@ class CreateComments < ActiveRecord::Migration def change create_table :comments do |t| t.string :content, null: false, limit: 200 - t.integer :commentable_id, polymorphic: true, index: true null: false + t.integer :commentable_id, polymorphic: true, index: true, null: false t.string :commentable_type, polymorphic: true, null:false t.timestamps null: false diff --git a/kiwi/db/migrate/20160122211756_create_votes.rb b/kiwi/db/migrate/20160122211756_create_votes.rb index c4203fa..c25f890 100644 --- a/kiwi/db/migrate/20160122211756_create_votes.rb +++ b/kiwi/db/migrate/20160122211756_create_votes.rb @@ -4,7 +4,7 @@ def change t.integer :user_id, index: true, null: false t.integer :votable_id, polymorphic: true, index: true, null: false t.string :votable_type, polymorphic: true, null: false - t.integer :direction null: false + t.integer :direction, null: false t.timestamps null: false end diff --git a/kiwi/db/schema.rb b/kiwi/db/schema.rb index 4e36040..999b32b 100644 --- a/kiwi/db/schema.rb +++ b/kiwi/db/schema.rb @@ -11,13 +11,32 @@ # # It's strongly recommended that you check this file into your version control system. - -ActiveRecord::Schema.define(version: 20160122211518) do - +ActiveRecord::Schema.define(version: 20160122211756) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + create_table "answers", force: :cascade do |t| + t.text "content" + t.integer "user_id" + t.integer "question_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "answers", ["question_id"], name: "index_answers_on_question_id", using: :btree + add_index "answers", ["user_id"], name: "index_answers_on_user_id", using: :btree + + create_table "comments", force: :cascade do |t| + t.string "content", limit: 200, null: false + t.integer "commentable_id", null: false + t.string "commentable_type", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "comments", ["commentable_id"], name: "index_comments_on_commentable_id", using: :btree + create_table "questions", force: :cascade do |t| t.string "title", null: false t.text "content", null: false @@ -29,8 +48,7 @@ add_index "questions", ["user_id"], name: "index_questions_on_user_id", using: :btree - - create_table "users_tables", force: :cascade do |t| + create_table "users", force: :cascade do |t| t.string "name", limit: 20, null: false t.string "email", null: false t.string "password_digest", null: false @@ -38,4 +56,16 @@ t.datetime "updated_at", null: false end + create_table "votes", force: :cascade do |t| + t.integer "user_id", null: false + t.integer "votable_id", null: false + t.string "votable_type", null: false + t.integer "direction", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "votes", ["user_id"], name: "index_votes_on_user_id", using: :btree + add_index "votes", ["votable_id"], name: "index_votes_on_votable_id", using: :btree + end From 66860446f42450b0306eeddc2da866f98c74b843 Mon Sep 17 00:00:00 2001 From: Nicola Beuscher Date: Fri, 22 Jan 2016 17:32:24 -0500 Subject: [PATCH 017/152] Add main and normalize css files --- kiwi/app/assets/stylesheets/main.css | 0 kiwi/app/assets/stylesheets/normalize.css | 396 ++++++++++++++++++++++ 2 files changed, 396 insertions(+) create mode 100644 kiwi/app/assets/stylesheets/main.css create mode 100644 kiwi/app/assets/stylesheets/normalize.css diff --git a/kiwi/app/assets/stylesheets/main.css b/kiwi/app/assets/stylesheets/main.css new file mode 100644 index 0000000..e69de29 diff --git a/kiwi/app/assets/stylesheets/normalize.css b/kiwi/app/assets/stylesheets/normalize.css new file mode 100644 index 0000000..a9c6f52 --- /dev/null +++ b/kiwi/app/assets/stylesheets/normalize.css @@ -0,0 +1,396 @@ +/*! normalize.css v2.1.0 | MIT License | git.io/normalize */ + +/* ========================================================================== + HTML5 display definitions + ========================================================================== */ + +/** + * Correct `block` display not defined in IE 8/9. + */ + +article, +aside, +details, +figcaption, +figure, +footer, +header, +hgroup, +main, +nav, +section, +summary { + display: block; +} + +/** + * Correct `inline-block` display not defined in IE 8/9. + */ + +audio, +canvas, +video { + display: inline-block; +} + +/** + * Prevent modern browsers from displaying `audio` without controls. + * Remove excess height in iOS 5 devices. + */ + +audio:not([controls]) { + display: none; + height: 0; +} + +/** + * Address styling not present in IE 8/9. + */ + +[hidden] { + display: none; +} + +/* ========================================================================== + Base + ========================================================================== */ + +/** + * 1. Set default font family to sans-serif. + * 2. Prevent iOS text size adjust after orientation change, without disabling + * user zoom. + */ + +html { + font-family: sans-serif; /* 1 */ + -webkit-text-size-adjust: 100%; /* 2 */ + -ms-text-size-adjust: 100%; /* 2 */ +} + +/** + * Remove default margin. + */ + +body { + margin: 0; +} + +/* ========================================================================== + Links + ========================================================================== */ + +/** + * Address `outline` inconsistency between Chrome and other browsers. + */ + +a:focus { + outline: thin dotted; +} + +/** + * Improve readability when focused and also mouse hovered in all browsers. + */ + +a:active, +a:hover { + outline: 0; +} + +/* ========================================================================== + Typography + ========================================================================== */ + +/** + * Address variable `h1` font-size and margin within `section` and `article` + * contexts in Firefox 4+, Safari 5, and Chrome. + */ + +h1 { + font-size: 2em; + margin: 0.67em 0; +} + +/** + * Address styling not present in IE 8/9, Safari 5, and Chrome. + */ + +abbr[title] { + border-bottom: 1px dotted; +} + +/** + * Address style set to `bolder` in Firefox 4+, Safari 5, and Chrome. + */ + +b, +strong { + font-weight: bold; +} + +/** + * Address styling not present in Safari 5 and Chrome. + */ + +dfn { + font-style: italic; +} + +/** + * Address differences between Firefox and other browsers. + */ + +hr { + -moz-box-sizing: content-box; + box-sizing: content-box; + height: 0; +} + +/** + * Address styling not present in IE 8/9. + */ + +mark { + background: #ff0; + color: #000; +} + +/** + * Correct font family set oddly in Safari 5 and Chrome. + */ + +code, +kbd, +pre, +samp { + font-family: monospace, serif; + font-size: 1em; +} + +/** + * Improve readability of pre-formatted text in all browsers. + */ + +pre { + white-space: pre-wrap; +} + +/** + * Set consistent quote types. + */ + +q { + quotes: "\201C" "\201D" "\2018" "\2019"; +} + +/** + * Address inconsistent and variable font size in all browsers. + */ + +small { + font-size: 80%; +} + +/** + * Prevent `sub` and `sup` affecting `line-height` in all browsers. + */ + +sub, +sup { + font-size: 75%; + line-height: 0; + position: relative; + vertical-align: baseline; +} + +sup { + top: -0.5em; +} + +sub { + bottom: -0.25em; +} + +/* ========================================================================== + Embedded content + ========================================================================== */ + +/** + * Remove border when inside `a` element in IE 8/9. + */ + +img { + border: 0; +} + +/** + * Correct overflow displayed oddly in IE 9. + */ + +svg:not(:root) { + overflow: hidden; +} + +/* ========================================================================== + Figures + ========================================================================== */ + +/** + * Address margin not present in IE 8/9 and Safari 5. + */ + +figure { + margin: 0; +} + +/* ========================================================================== + Forms + ========================================================================== */ + +/** + * Define consistent border, margin, and padding. + */ + +fieldset { + border: 1px solid #c0c0c0; + margin: 0 2px; + padding: 0.35em 0.625em 0.75em; +} + +/** + * 1. Correct `color` not being inherited in IE 8/9. + * 2. Remove padding so people aren't caught out if they zero out fieldsets. + */ + +legend { + border: 0; /* 1 */ + padding: 0; /* 2 */ +} + +/** + * 1. Correct font family not being inherited in all browsers. + * 2. Correct font size not being inherited in all browsers. + * 3. Address margins set differently in Firefox 4+, Safari 5, and Chrome. + */ + +button, +input, +select, +textarea { + font-family: inherit; /* 1 */ + font-size: 100%; /* 2 */ + margin: 0; /* 3 */ +} + +/** + * Address Firefox 4+ setting `line-height` on `input` using `!important` in + * the UA stylesheet. + */ + +button, +input { + line-height: normal; +} + +/** + * Address inconsistent `text-transform` inheritance for `button` and `select`. + * All other form control elements do not inherit `text-transform` values. + * Correct `button` style inheritance in Chrome, Safari 5+, and IE 8+. + * Correct `select` style inheritance in Firefox 4+ and Opera. + */ + +button, +select { + text-transform: none; +} + +/** + * 1. Avoid the WebKit bug in Android 4.0.* where (2) destroys native `audio` + * and `video` controls. + * 2. Correct inability to style clickable `input` types in iOS. + * 3. Improve usability and consistency of cursor style between image-type + * `input` and others. + */ + +button, +html input[type="button"], /* 1 */ +input[type="reset"], +input[type="submit"] { + -webkit-appearance: button; /* 2 */ + cursor: pointer; /* 3 */ +} + +/** + * Re-set default cursor for disabled elements. + */ + +button[disabled], +html input[disabled] { + cursor: default; +} + +/** + * 1. Address box sizing set to `content-box` in IE 8/9. + * 2. Remove excess padding in IE 8/9. + */ + +input[type="checkbox"], +input[type="radio"] { + box-sizing: border-box; /* 1 */ + padding: 0; /* 2 */ +} + +/** + * 1. Address `appearance` set to `searchfield` in Safari 5 and Chrome. + * 2. Address `box-sizing` set to `border-box` in Safari 5 and Chrome + * (include `-moz` to future-proof). + */ + +input[type="search"] { + -webkit-appearance: textfield; /* 1 */ + -moz-box-sizing: content-box; + -webkit-box-sizing: content-box; /* 2 */ + box-sizing: content-box; +} + +/** + * Remove inner padding and search cancel button in Safari 5 and Chrome + * on OS X. + */ + +input[type="search"]::-webkit-search-cancel-button, +input[type="search"]::-webkit-search-decoration { + -webkit-appearance: none; +} + +/** + * Remove inner padding and border in Firefox 4+. + */ + +button::-moz-focus-inner, +input::-moz-focus-inner { + border: 0; + padding: 0; +} + +/** + * 1. Remove default vertical scrollbar in IE 8/9. + * 2. Improve readability and alignment in all browsers. + */ + +textarea { + overflow: auto; /* 1 */ + vertical-align: top; /* 2 */ +} + +/* ========================================================================== + Tables + ========================================================================== */ + +/** + * Remove most spacing between table cells. + */ + +table { + border-collapse: collapse; + border-spacing: 0; +} From 30a70990a1af2c902f957882700497ec1e40a1c1 Mon Sep 17 00:00:00 2001 From: Nicola Beuscher Date: Fri, 22 Jan 2016 18:24:18 -0500 Subject: [PATCH 018/152] Write seeds for users, questions, answers, and comments --- kiwi/db/seeds.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/kiwi/db/seeds.rb b/kiwi/db/seeds.rb index 4edb1e8..35e2602 100644 --- a/kiwi/db/seeds.rb +++ b/kiwi/db/seeds.rb @@ -5,3 +5,23 @@ # # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) # Mayor.create(name: 'Emanuel', city: cities.first) + +nicola = User.create(name: "nicola_dbc", email: "nicola@example.com", password: "password") +luis = User.create(name: "luis_dbc", email: "luis@example.com", password: "password") +zach = User.create(name: "zach_dbc", email: "zach@example.com", password: "password") +jeff = User.create(name: "jeff_dbc", email: "jeff@example.com", password: "password") + +rails_q = Question.create(title: "How do I build a Rails app?", content: "Wondering how to do this", user: nicola) +sinatra_q =Question.create(title: "How do I build a Sinatra app?", content: "Wondering how to do this", user: luis) +mvc_q = Question.create(title: "What is MVC?", content: "design patterns, yo", user: zach) +css_q = Question.create(title: "What are the best CSS styles?", content: "Should I style things?", user: jeff) + +answer = Answer.create(content:"It's super easy just follow the rails guide.", user: luis, question: rails_q) +Answer.create(content:"Just ask someone from phase 2.", user: jeff, question: sinatra_q) +Answer.create(content:"Use the sinatra gem and make restful routes.", user: luis, question: sinatra_q) +Answer.create(content:"Model, view, controller, duh.", user: nicola, question: mvc_q) + +Comment.create(content:"Can you explain what you mean by that?", commentable_type: "Question", commentable_id: rails_q.id, user: luis) +Comment.create(content:"I don't understand the question", commentable_type: "Question", commentable_id: css_q.id, user: nicola) +Comment.create(content:"This is a great answer.", commentable_type: "Answer", commentable_id: answer.id, user: jeff) +Comment.create(content:"This should be voted best answer for sure.", commentable_type: "Answer", commentable_id: answer.id, user: zach) From 907b998bc93a9890a0075b60510cdcf7fa259bb1 Mon Sep 17 00:00:00 2001 From: Nicola Beuscher Date: Fri, 22 Jan 2016 18:28:42 -0500 Subject: [PATCH 019/152] Write migration file to add user id to comments table --- kiwi/db/migrate/20160122232556_add_user_id_to_comments.rb | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 kiwi/db/migrate/20160122232556_add_user_id_to_comments.rb diff --git a/kiwi/db/migrate/20160122232556_add_user_id_to_comments.rb b/kiwi/db/migrate/20160122232556_add_user_id_to_comments.rb new file mode 100644 index 0000000..8d0dc4e --- /dev/null +++ b/kiwi/db/migrate/20160122232556_add_user_id_to_comments.rb @@ -0,0 +1,5 @@ +class AddUserIdToComments < ActiveRecord::Migration + def change + add_column :comments, :user_id, :integer, null: false, index: true, foreign_key: true + end +end From 3cef0c7604652a4634fc09bd872700f76c2f1ab7 Mon Sep 17 00:00:00 2001 From: Nicola Beuscher Date: Fri, 22 Jan 2016 18:29:49 -0500 Subject: [PATCH 020/152] New schema file created from added migration --- kiwi/db/schema.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kiwi/db/schema.rb b/kiwi/db/schema.rb index 999b32b..27db35c 100644 --- a/kiwi/db/schema.rb +++ b/kiwi/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: 20160122211756) do +ActiveRecord::Schema.define(version: 20160122232556) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -33,6 +33,7 @@ t.string "commentable_type", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "user_id", null: false end add_index "comments", ["commentable_id"], name: "index_comments_on_commentable_id", using: :btree From 947cb4a735ca9de99339a476c75db5818fcb9c92 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Fri, 22 Jan 2016 17:30:21 -0500 Subject: [PATCH 021/152] Add std_format_date helper to application_helper.rb --- kiwi/app/helpers/application_helper.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/kiwi/app/helpers/application_helper.rb b/kiwi/app/helpers/application_helper.rb index de6be79..b9cc4a0 100644 --- a/kiwi/app/helpers/application_helper.rb +++ b/kiwi/app/helpers/application_helper.rb @@ -1,2 +1,8 @@ module ApplicationHelper + + def std_format_date date + return "" unless date + date.strftime(" at %l:%M %P on %b %e, %Y") + end + end From 757c2cb59fe03f6c35da999791fab15849cbe6be Mon Sep 17 00:00:00 2001 From: Jeff George Date: Fri, 22 Jan 2016 18:03:16 -0500 Subject: [PATCH 022/152] Add current_user helpers to application_controller --- kiwi/app/controllers/application_controller.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/kiwi/app/controllers/application_controller.rb b/kiwi/app/controllers/application_controller.rb index d83690e..ee974a6 100644 --- a/kiwi/app/controllers/application_controller.rb +++ b/kiwi/app/controllers/application_controller.rb @@ -2,4 +2,20 @@ class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception + + helper_method :current_user + + def current_user + @user ||= User.find session[:user_id] if session[:user_id] + end + + def authorize_user! + redirect_to new_admin_session_path unless current_user.present? + end + + def login user + session[:user_id] = user.id + end + + end From 2a8daad497daef6967d1553882f58578f45c3d85 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Fri, 22 Jan 2016 18:05:55 -0500 Subject: [PATCH 023/152] Implement sessions_controller --- kiwi/app/controllers/sessions_controller.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 kiwi/app/controllers/sessions_controller.rb diff --git a/kiwi/app/controllers/sessions_controller.rb b/kiwi/app/controllers/sessions_controller.rb new file mode 100644 index 0000000..337a60b --- /dev/null +++ b/kiwi/app/controllers/sessions_controller.rb @@ -0,0 +1,17 @@ +class Admin::SessionsController < ApplicationController + + def new + end + + def create + @user = User.find_by_email params[:email] + if @user && @user.authenticate(params[:password]) + login @user + redirect_to root_path + else + flash[:notice] = "Bad email/password combination" + redirect_to new_admin_session_path + end + end + +end \ No newline at end of file From 994e9a7505d7f201abb49aff9f90ebf6fa78db4c Mon Sep 17 00:00:00 2001 From: Jeff George Date: Fri, 22 Jan 2016 18:21:34 -0500 Subject: [PATCH 024/152] Edit invalid login info flash notice --- kiwi/app/controllers/sessions_controller.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kiwi/app/controllers/sessions_controller.rb b/kiwi/app/controllers/sessions_controller.rb index 337a60b..62739af 100644 --- a/kiwi/app/controllers/sessions_controller.rb +++ b/kiwi/app/controllers/sessions_controller.rb @@ -1,4 +1,4 @@ -class Admin::SessionsController < ApplicationController +class SessionsController < ApplicationController def new end @@ -9,8 +9,8 @@ def create login @user redirect_to root_path else - flash[:notice] = "Bad email/password combination" - redirect_to new_admin_session_path + flash[:notice] = "Invalid email or password" + redirect_to new_session_path end end From e0f1559433ac0f7213722af81278102333e2851f Mon Sep 17 00:00:00 2001 From: Jeff George Date: Fri, 22 Jan 2016 18:21:57 -0500 Subject: [PATCH 025/152] Create users_controller.rb --- kiwi/app/controllers/users_controller.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 kiwi/app/controllers/users_controller.rb diff --git a/kiwi/app/controllers/users_controller.rb b/kiwi/app/controllers/users_controller.rb new file mode 100644 index 0000000..e345a53 --- /dev/null +++ b/kiwi/app/controllers/users_controller.rb @@ -0,0 +1,22 @@ +class UsersController < ApplicationController + + def new + @user = User.new + end + + def show + @user = User.find_by(id: current_user.id) + end + + def create + user = User.new(user_params) + if user.save + login user + redirect_to questions_path + else + flash[:notice] = "Invalid email or password" + render new_user_path + end + end + +end \ No newline at end of file From e214f9406d6c0e32aef441caae42270923e89172 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Fri, 22 Jan 2016 18:23:46 -0500 Subject: [PATCH 026/152] Implement sessions#new form --- kiwi/app/views/sessions/new.html.erb | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 kiwi/app/views/sessions/new.html.erb diff --git a/kiwi/app/views/sessions/new.html.erb b/kiwi/app/views/sessions/new.html.erb new file mode 100644 index 0000000..4f81506 --- /dev/null +++ b/kiwi/app/views/sessions/new.html.erb @@ -0,0 +1,5 @@ +<%= form_tag admin_sessions_path do %> + <%= email_field_tag :email, nil, :placeholder => "Email" %> + <%= password_field_tag :password, nil, :placeholder => "Password" %> + <%= submit_tag "Log In" %> +<% end %> \ No newline at end of file From b593d2fa10fd9ac98762f775c4168fcd01e97f66 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Fri, 22 Jan 2016 18:34:48 -0500 Subject: [PATCH 027/152] Implement users#new form --- kiwi/app/views/users/new.html.erb | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 kiwi/app/views/users/new.html.erb diff --git a/kiwi/app/views/users/new.html.erb b/kiwi/app/views/users/new.html.erb new file mode 100644 index 0000000..061e4a0 --- /dev/null +++ b/kiwi/app/views/users/new.html.erb @@ -0,0 +1,11 @@ +<%= form_for User.new do |f| %> + <%= f.label :username %> + <%= f.text_field :username %> + <%= f.label :email %> + <%= f.text_field :email %> + <%= f.label :password %> + <%= f.password_field :password %> + <%#= f.label :password_confirmation %> + <%#= f.password_field :password_confirmation %> + <%= f.submit 'Create Account' %> +<% end %> \ No newline at end of file From d66f7c889b4a9b7dc47583fcc5333ae8a734bb28 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Fri, 22 Jan 2016 19:24:10 -0500 Subject: [PATCH 028/152] Implement sessions#new and users#new forms and routes --- kiwi/app/controllers/sessions_controller.rb | 8 +++++++- kiwi/app/controllers/users_controller.rb | 13 ++++++++++--- kiwi/app/models/user.rb | 1 + kiwi/app/views/sessions/new.html.erb | 2 +- kiwi/app/views/users/new.html.erb | 4 ++-- kiwi/config/routes.rb | 12 ++++++++++++ 6 files changed, 33 insertions(+), 7 deletions(-) diff --git a/kiwi/app/controllers/sessions_controller.rb b/kiwi/app/controllers/sessions_controller.rb index 62739af..6048a49 100644 --- a/kiwi/app/controllers/sessions_controller.rb +++ b/kiwi/app/controllers/sessions_controller.rb @@ -4,7 +4,8 @@ def new end def create - @user = User.find_by_email params[:email] + binding.pry + @user = User.find_by(email: params[:email]) if @user && @user.authenticate(params[:password]) login @user redirect_to root_path @@ -14,4 +15,9 @@ def create end end + def destroy + sessions.clear + redirect_to root_path + end + end \ No newline at end of file diff --git a/kiwi/app/controllers/users_controller.rb b/kiwi/app/controllers/users_controller.rb index e345a53..a3d004f 100644 --- a/kiwi/app/controllers/users_controller.rb +++ b/kiwi/app/controllers/users_controller.rb @@ -9,9 +9,10 @@ def show end def create - user = User.new(user_params) - if user.save - login user + binding.pry + @user = User.new(user_params) + if @user.save + login @user redirect_to questions_path else flash[:notice] = "Invalid email or password" @@ -19,4 +20,10 @@ def create end end + private + + def user_params + user_params = params.require(:user).permit(:name, :email, :password) + end + end \ No newline at end of file diff --git a/kiwi/app/models/user.rb b/kiwi/app/models/user.rb index dc97690..4e62509 100644 --- a/kiwi/app/models/user.rb +++ b/kiwi/app/models/user.rb @@ -10,4 +10,5 @@ class User < ActiveRecord::Base validates_format_of :email, with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create + end diff --git a/kiwi/app/views/sessions/new.html.erb b/kiwi/app/views/sessions/new.html.erb index 4f81506..6f83be2 100644 --- a/kiwi/app/views/sessions/new.html.erb +++ b/kiwi/app/views/sessions/new.html.erb @@ -1,4 +1,4 @@ -<%= form_tag admin_sessions_path do %> +<%= form_tag sessions_path do %> <%= email_field_tag :email, nil, :placeholder => "Email" %> <%= password_field_tag :password, nil, :placeholder => "Password" %> <%= submit_tag "Log In" %> diff --git a/kiwi/app/views/users/new.html.erb b/kiwi/app/views/users/new.html.erb index 061e4a0..a6df976 100644 --- a/kiwi/app/views/users/new.html.erb +++ b/kiwi/app/views/users/new.html.erb @@ -1,6 +1,6 @@ <%= form_for User.new do |f| %> - <%= f.label :username %> - <%= f.text_field :username %> + <%= f.label :name %> + <%= f.text_field :name %> <%= f.label :email %> <%= f.text_field :email %> <%= f.label :password %> diff --git a/kiwi/config/routes.rb b/kiwi/config/routes.rb index 3f66539..3bba5ac 100644 --- a/kiwi/config/routes.rb +++ b/kiwi/config/routes.rb @@ -1,4 +1,16 @@ Rails.application.routes.draw do + + ############# USER AND SESSIONS ROUTES ################# + + # These can be deleted if they have been added elsewhere by the routes team. + + resources :users, only: [ :new, :show, :create ] + resources :sessions, only: [ :new, :create, :destroy ] + + + ########### END USERS AND SESSIONS ROUTES ################ + + # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". From 830abbb1a0a81f576991c35b8b02b04179f447ea Mon Sep 17 00:00:00 2001 From: Luis Fernando Plaz Date: Fri, 22 Jan 2016 17:55:11 -0500 Subject: [PATCH 029/152] Add routes for users, questions & answers --- kiwi/config/routes.rb | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/kiwi/config/routes.rb b/kiwi/config/routes.rb index 3bba5ac..020c3b3 100644 --- a/kiwi/config/routes.rb +++ b/kiwi/config/routes.rb @@ -1,16 +1,22 @@ Rails.application.routes.draw do - ############# USER AND SESSIONS ROUTES ################# + resources :users do + resources :question, only: [:new, :edit] + end - # These can be deleted if they have been added elsewhere by the routes team. + resources :question do + resources :answer, only: [:new, :show, :delete, :edit] + resources :comment, only: [:new, :show] + end + + resources :answer do + resources :comment, only: [:new, :show] + end resources :users, only: [ :new, :show, :create ] resources :sessions, only: [ :new, :create, :destroy ] - ########### END USERS AND SESSIONS ROUTES ################ - - # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". From 847b418bac8fdd994e266dd7064f40f69eabdf1f Mon Sep 17 00:00:00 2001 From: Luis Fernando Plaz Date: Fri, 22 Jan 2016 18:38:24 -0500 Subject: [PATCH 030/152] Include controller methods for questions --- kiwi/app/controllers/questions_controller.rb | 45 ++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 kiwi/app/controllers/questions_controller.rb diff --git a/kiwi/app/controllers/questions_controller.rb b/kiwi/app/controllers/questions_controller.rb new file mode 100644 index 0000000..0fd6542 --- /dev/null +++ b/kiwi/app/controllers/questions_controller.rb @@ -0,0 +1,45 @@ +class QuestionsController < ApplicationController + + # before_filter + + def index + @questions = Question.all + end + + def new + @question = Question.new + end + + def create + @question = Question.new(question_params) + if @question.save + redirect_to question_path + else + render :new + end + end + + def show + @question = Question.find_by(id: params[:id]) + end + + def edit + @question = Question.find_by(id: params[:id]) + if @question.update_attributes(question_params) + redirect_to question_path + else + render :edit + end + end + + def delete + @question = Question.find_by(id: params[:id]) + @question.destroy + end + + private + + def question_params + params.require(:question).permit(:title,:content,:user_id,:best_answer_id) + end +end From 4c77c661d33af2042f734fddc045a124439b581b Mon Sep 17 00:00:00 2001 From: Luis Fernando Plaz Date: Fri, 22 Jan 2016 18:39:01 -0500 Subject: [PATCH 031/152] Add index and new views for questions --- kiwi/app/views/questions/index.html.erb | 15 +++++++++++++++ kiwi/app/views/questions/new.html.erb | 23 +++++++++++++++++++++++ kiwi/app/views/questions/show.html.erb | 0 3 files changed, 38 insertions(+) create mode 100644 kiwi/app/views/questions/index.html.erb create mode 100644 kiwi/app/views/questions/new.html.erb create mode 100644 kiwi/app/views/questions/show.html.erb diff --git a/kiwi/app/views/questions/index.html.erb b/kiwi/app/views/questions/index.html.erb new file mode 100644 index 0000000..5d58760 --- /dev/null +++ b/kiwi/app/views/questions/index.html.erb @@ -0,0 +1,15 @@ +

LIST OF QUESTIONS

+ +<%= link_to "NEW QUESTION", new_question_path %> + +

QUESTIONS

+ +
+
    + <% @questions.each do |question| %> +
  • <%= link_to question.title question_path %>
  • + <% end %> +
+
+ + diff --git a/kiwi/app/views/questions/new.html.erb b/kiwi/app/views/questions/new.html.erb new file mode 100644 index 0000000..a378fa3 --- /dev/null +++ b/kiwi/app/views/questions/new.html.erb @@ -0,0 +1,23 @@ +

POST A NEW QUESTION

+ +<%= form_for @question do |f| %> +
+
+ <%= f.label :title %> + <%= f.text_field :title %> +
+ +
+ <%= f.label :content %> + <%= f.text_area :content %> +
+ + <%= f.submit 'POST YOUR QUESTION' %> +
+ +<% end %> + + + + + diff --git a/kiwi/app/views/questions/show.html.erb b/kiwi/app/views/questions/show.html.erb new file mode 100644 index 0000000..e69de29 From 2fdb95fc5d4aa1ca4248db42c2bbdcbd9c022394 Mon Sep 17 00:00:00 2001 From: Luis Fernando Plaz Date: Fri, 22 Jan 2016 18:39:34 -0500 Subject: [PATCH 032/152] Minor CSS adjustments for questions form --- kiwi/app/assets/stylesheets/questions.scss | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 kiwi/app/assets/stylesheets/questions.scss diff --git a/kiwi/app/assets/stylesheets/questions.scss b/kiwi/app/assets/stylesheets/questions.scss new file mode 100644 index 0000000..1945f86 --- /dev/null +++ b/kiwi/app/assets/stylesheets/questions.scss @@ -0,0 +1,13 @@ +// Place all the styles related to the question controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ + +#form { +display:inline; +} + +#input-options { + + display:block; + padding:5px; +} From 45f4d4f462e833901cd5cd86708fc41d57ff4869 Mon Sep 17 00:00:00 2001 From: Luis Fernando Plaz Date: Fri, 22 Jan 2016 18:39:59 -0500 Subject: [PATCH 033/152] Included files during controller generation --- kiwi/app/assets/javascripts/questions.js | 2 ++ kiwi/app/helpers/questions_helper.rb | 2 ++ kiwi/config/routes.rb | 21 +++++++++++-------- .../controllers/question_controller_test.rb | 7 +++++++ 4 files changed, 23 insertions(+), 9 deletions(-) create mode 100644 kiwi/app/assets/javascripts/questions.js create mode 100644 kiwi/app/helpers/questions_helper.rb create mode 100644 kiwi/test/controllers/question_controller_test.rb diff --git a/kiwi/app/assets/javascripts/questions.js b/kiwi/app/assets/javascripts/questions.js new file mode 100644 index 0000000..dee720f --- /dev/null +++ b/kiwi/app/assets/javascripts/questions.js @@ -0,0 +1,2 @@ +// Place all the behaviors and hooks related to the matching controller here. +// All this logic will automatically be available in application.js. diff --git a/kiwi/app/helpers/questions_helper.rb b/kiwi/app/helpers/questions_helper.rb new file mode 100644 index 0000000..2eaab4a --- /dev/null +++ b/kiwi/app/helpers/questions_helper.rb @@ -0,0 +1,2 @@ +module QuestionsHelper +end diff --git a/kiwi/config/routes.rb b/kiwi/config/routes.rb index 020c3b3..38eb2ab 100644 --- a/kiwi/config/routes.rb +++ b/kiwi/config/routes.rb @@ -1,22 +1,25 @@ Rails.application.routes.draw do - resources :users do - resources :question, only: [:new, :edit] - end + # resources :users do + # resources :questions, only: [:new, :edit, :index] + # end - resources :question do - resources :answer, only: [:new, :show, :delete, :edit] - resources :comment, only: [:new, :show] + resources :questions do + # resources :answers, only: [:new, :show, :delete, :edit] + # resources :comments, only: [:new, :show] end - resources :answer do - resources :comment, only: [:new, :show] - end + # resources :answers do + # resources :comments, only: [:new, :show] + # end resources :users, only: [ :new, :show, :create ] resources :sessions, only: [ :new, :create, :destroy ] + root 'questions#index' + + # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". diff --git a/kiwi/test/controllers/question_controller_test.rb b/kiwi/test/controllers/question_controller_test.rb new file mode 100644 index 0000000..0d272cf --- /dev/null +++ b/kiwi/test/controllers/question_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class QuestionControllerTest < ActionController::TestCase + # test "the truth" do + # assert true + # end +end From 6ad6f90934ced1ea19154ed0551a869617d6af31 Mon Sep 17 00:00:00 2001 From: Luis Fernando Plaz Date: Fri, 22 Jan 2016 19:31:21 -0500 Subject: [PATCH 034/152] Include :answers and :users in Question query --- kiwi/app/controllers/questions_controller.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kiwi/app/controllers/questions_controller.rb b/kiwi/app/controllers/questions_controller.rb index 0fd6542..85054e6 100644 --- a/kiwi/app/controllers/questions_controller.rb +++ b/kiwi/app/controllers/questions_controller.rb @@ -12,6 +12,7 @@ def new def create @question = Question.new(question_params) + binding.pry if @question.save redirect_to question_path else @@ -20,7 +21,7 @@ def create end def show - @question = Question.find_by(id: params[:id]) + @question = Question.includes(:answers, :user).find(params[:id]) end def edit From ab5dcc5f529ff20d417ff6c1a79d5255ea9bc517 Mon Sep 17 00:00:00 2001 From: Luis Fernando Plaz Date: Fri, 22 Jan 2016 19:31:52 -0500 Subject: [PATCH 035/152] Reorganize divs & associations --- kiwi/app/views/questions/index.html.erb | 3 ++- kiwi/app/views/questions/new.html.erb | 30 ++++++++++++------------- kiwi/app/views/questions/show.html.erb | 15 +++++++++++++ 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/kiwi/app/views/questions/index.html.erb b/kiwi/app/views/questions/index.html.erb index 5d58760..893ac61 100644 --- a/kiwi/app/views/questions/index.html.erb +++ b/kiwi/app/views/questions/index.html.erb @@ -7,7 +7,8 @@
    <% @questions.each do |question| %> -
  • <%= link_to question.title question_path %>
  • + +
  • <%= link_to question.title, question_path(question) %>
  • <% end %>
diff --git a/kiwi/app/views/questions/new.html.erb b/kiwi/app/views/questions/new.html.erb index a378fa3..30d997f 100644 --- a/kiwi/app/views/questions/new.html.erb +++ b/kiwi/app/views/questions/new.html.erb @@ -1,20 +1,18 @@

POST A NEW QUESTION

- -<%= form_for @question do |f| %> -
-
- <%= f.label :title %> - <%= f.text_field :title %> -
- -
- <%= f.label :content %> - <%= f.text_area :content %> -
- - <%= f.submit 'POST YOUR QUESTION' %> -
- + <%= form_for @question do |f| %> +
+
+ <%= f.label :title %> + <%= f.text_field :title %> +
+ +
+ <%= f.label :content %> + <%= f.text_area :content %> +
+ + <%= f.submit 'POST YOUR QUESTION' %> +
<% end %> diff --git a/kiwi/app/views/questions/show.html.erb b/kiwi/app/views/questions/show.html.erb index e69de29..1855d0c 100644 --- a/kiwi/app/views/questions/show.html.erb +++ b/kiwi/app/views/questions/show.html.erb @@ -0,0 +1,15 @@ +
+ <%= @question.title %> +
+ <%= @question.content%> .....by <%= @question.user.name %> +
+ +
+ <% @question.answers.each do |answer|%> +
+ <%= answer.content %> +
+ <% end %> +
+ + From 891973cde54a84a5a4fe043a460dfc326bcc974d Mon Sep 17 00:00:00 2001 From: Luis Fernando Plaz Date: Fri, 22 Jan 2016 19:32:12 -0500 Subject: [PATCH 036/152] Change css setting --- kiwi/app/assets/stylesheets/main.css | 26 +++++++++++++++++++++ kiwi/app/assets/stylesheets/questions.scss | 8 ------- kiwi/app/views/layouts/application.html.erb | 3 +++ 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/kiwi/app/assets/stylesheets/main.css b/kiwi/app/assets/stylesheets/main.css index e69de29..e8a4bd5 100644 --- a/kiwi/app/assets/stylesheets/main.css +++ b/kiwi/app/assets/stylesheets/main.css @@ -0,0 +1,26 @@ +* { + font-family: 'Open Sans Condensed', sans-serif; +} +#form { + display:inline; +} + +#input-options { + + display:block; + padding:5px; +} + +.container{ + display:block; + padding:10px; + border:1px solid black; + margin:0 auto; + width: 50%; +} + +.web-title { + padding:10px; + margin:0 auto; + text-align: center; +} diff --git a/kiwi/app/assets/stylesheets/questions.scss b/kiwi/app/assets/stylesheets/questions.scss index 1945f86..75b9c0b 100644 --- a/kiwi/app/assets/stylesheets/questions.scss +++ b/kiwi/app/assets/stylesheets/questions.scss @@ -2,12 +2,4 @@ // They will automatically be included in application.css. // You can use Sass (SCSS) here: http://sass-lang.com/ -#form { -display:inline; -} -#input-options { - - display:block; - padding:5px; -} diff --git a/kiwi/app/views/layouts/application.html.erb b/kiwi/app/views/layouts/application.html.erb index 2df3bda..8bc8e3b 100644 --- a/kiwi/app/views/layouts/application.html.erb +++ b/kiwi/app/views/layouts/application.html.erb @@ -1,11 +1,14 @@ + Kiwi <%= stylesheet_link_tag 'application', media: 'all' %> <%= javascript_include_tag 'application' %> <%= csrf_meta_tags %> + +

KIWI-OVERFLOW

<%= yield %> From 58a27213cf89f9eeefdf058fcbc2e68c4fd3c77a Mon Sep 17 00:00:00 2001 From: Nicola Beuscher Date: Fri, 22 Jan 2016 18:52:06 -0500 Subject: [PATCH 037/152] Add basic nav bar to layout --- kiwi/app/views/layouts/application.html.erb | 22 +++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/kiwi/app/views/layouts/application.html.erb b/kiwi/app/views/layouts/application.html.erb index 8bc8e3b..17bd128 100644 --- a/kiwi/app/views/layouts/application.html.erb +++ b/kiwi/app/views/layouts/application.html.erb @@ -10,8 +10,26 @@

KIWI-OVERFLOW

- -<%= yield %> +
+

Kiwi Overflow

+ +
+
+ <%= yield %> +
+
+

2016 © Jeff, Luis, Zach, and Nicola

+
From 1b2d3b8731b1321d07006b1b20022688a32b06ff Mon Sep 17 00:00:00 2001 From: Nicola Beuscher Date: Fri, 22 Jan 2016 20:08:13 -0500 Subject: [PATCH 038/152] Fix login, add nav bar, remove binding prys --- kiwi/app/controllers/application_controller.rb | 5 ++++- kiwi/app/controllers/questions_controller.rb | 1 - kiwi/app/controllers/sessions_controller.rb | 5 ++--- kiwi/app/views/layouts/application.html.erb | 9 ++++----- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/kiwi/app/controllers/application_controller.rb b/kiwi/app/controllers/application_controller.rb index ee974a6..6b27db6 100644 --- a/kiwi/app/controllers/application_controller.rb +++ b/kiwi/app/controllers/application_controller.rb @@ -3,7 +3,7 @@ class ApplicationController < ActionController::Base # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception - helper_method :current_user + helper_method :current_user, :logged_in? def current_user @user ||= User.find session[:user_id] if session[:user_id] @@ -17,5 +17,8 @@ def login user session[:user_id] = user.id end + def logged_in? + !!current_user + end end diff --git a/kiwi/app/controllers/questions_controller.rb b/kiwi/app/controllers/questions_controller.rb index 85054e6..926e52f 100644 --- a/kiwi/app/controllers/questions_controller.rb +++ b/kiwi/app/controllers/questions_controller.rb @@ -12,7 +12,6 @@ def new def create @question = Question.new(question_params) - binding.pry if @question.save redirect_to question_path else diff --git a/kiwi/app/controllers/sessions_controller.rb b/kiwi/app/controllers/sessions_controller.rb index 6048a49..8fffd7f 100644 --- a/kiwi/app/controllers/sessions_controller.rb +++ b/kiwi/app/controllers/sessions_controller.rb @@ -4,7 +4,6 @@ def new end def create - binding.pry @user = User.find_by(email: params[:email]) if @user && @user.authenticate(params[:password]) login @user @@ -16,8 +15,8 @@ def create end def destroy - sessions.clear + session.clear redirect_to root_path end -end \ No newline at end of file +end diff --git a/kiwi/app/views/layouts/application.html.erb b/kiwi/app/views/layouts/application.html.erb index 17bd128..2f8cfcd 100644 --- a/kiwi/app/views/layouts/application.html.erb +++ b/kiwi/app/views/layouts/application.html.erb @@ -7,8 +7,6 @@ <%= javascript_include_tag 'application' %> <%= csrf_meta_tags %> - -

KIWI-OVERFLOW

Kiwi Overflow

@@ -16,11 +14,12 @@
    <% if logged_in? %>
  • Welcome, <%= current_user.name %>
  • +
  • <%= link_to("Log Out", session_path(current_user), method: :delete) %>
  • <% else %> -
  • <%= link_to "Register", register_path %>
  • -
  • <%= link_to "Login", login_path %>
  • -
  • <%= link_to "All Questions", questions_path %>
  • +
  • <%= link_to "Register", new_user_path %>
  • +
  • <%= link_to "Login", new_session_path %>
  • <% end %> +
  • <%= link_to "All Questions", questions_path %>
From deaf06d9de746d50053e553c72cedb2f0fea46de Mon Sep 17 00:00:00 2001 From: Jeff George Date: Fri, 22 Jan 2016 20:43:44 -0500 Subject: [PATCH 039/152] Add line to assign current_user to question.user in questions#create --- kiwi/app/controllers/questions_controller.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kiwi/app/controllers/questions_controller.rb b/kiwi/app/controllers/questions_controller.rb index 926e52f..ccbe93c 100644 --- a/kiwi/app/controllers/questions_controller.rb +++ b/kiwi/app/controllers/questions_controller.rb @@ -11,7 +11,9 @@ def new end def create + binding.pry @question = Question.new(question_params) + @question.user = current_user if @question.save redirect_to question_path else From 87ac10380de48b5bec388cfc03feedea2d8a3e67 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Fri, 22 Jan 2016 20:59:20 -0500 Subject: [PATCH 040/152] Fix bug in questions#create: pass new question id as parameter with redirect --- kiwi/app/controllers/questions_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kiwi/app/controllers/questions_controller.rb b/kiwi/app/controllers/questions_controller.rb index ccbe93c..50c44d1 100644 --- a/kiwi/app/controllers/questions_controller.rb +++ b/kiwi/app/controllers/questions_controller.rb @@ -15,7 +15,7 @@ def create @question = Question.new(question_params) @question.user = current_user if @question.save - redirect_to question_path + redirect_to question_path(id: @question.id) else render :new end From eb765ab0ceb17ebb55532f3a818924c5a0919b86 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Fri, 22 Jan 2016 21:28:17 -0500 Subject: [PATCH 041/152] Reformat questions#show view --- kiwi/app/views/questions/show.html.erb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/kiwi/app/views/questions/show.html.erb b/kiwi/app/views/questions/show.html.erb index 1855d0c..06a519b 100644 --- a/kiwi/app/views/questions/show.html.erb +++ b/kiwi/app/views/questions/show.html.erb @@ -1,9 +1,11 @@
- <%= @question.title %> -
- <%= @question.content%> .....by <%= @question.user.name %> +

<%= @question.title %>

+

<%= @question.content%>

+

Asked by <%= @question.user.name %> on <%= std_format_date(@question.created_at) %>

+<%= link_to "Edit my question", edit_question_path %> +
<% @question.answers.each do |answer|%>
From 5742be25bf9d526ab2a278c2aaf616d6bf75626d Mon Sep 17 00:00:00 2001 From: Jeff George Date: Fri, 22 Jan 2016 21:28:51 -0500 Subject: [PATCH 042/152] Implement questions#edit and #update routes and view --- kiwi/app/controllers/questions_controller.rb | 4 ++++ kiwi/app/views/questions/edit.html.erb | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 kiwi/app/views/questions/edit.html.erb diff --git a/kiwi/app/controllers/questions_controller.rb b/kiwi/app/controllers/questions_controller.rb index 50c44d1..2f9c3d2 100644 --- a/kiwi/app/controllers/questions_controller.rb +++ b/kiwi/app/controllers/questions_controller.rb @@ -27,6 +27,10 @@ def show def edit @question = Question.find_by(id: params[:id]) + end + + def update + @question = Question.find_by(id: params[:id]) if @question.update_attributes(question_params) redirect_to question_path else diff --git a/kiwi/app/views/questions/edit.html.erb b/kiwi/app/views/questions/edit.html.erb new file mode 100644 index 0000000..57eb2fb --- /dev/null +++ b/kiwi/app/views/questions/edit.html.erb @@ -0,0 +1,17 @@ +

Edit Your Question

+ <%= form_for @question do |f| %> +
+
+ <%= f.label :title %> + <%= f.text_field :title %> +
+ +
+ <%= f.label :content %> + <%= f.text_area :content %> +
+ + <%= f.submit 'Save your question' %> +
+<% end %> + From 35d9d435ca04c70b482fd88b1b78339e672b6621 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Fri, 22 Jan 2016 21:32:49 -0500 Subject: [PATCH 043/152] Add delete question link to questions#show view; debug questions#destroy route --- kiwi/app/controllers/questions_controller.rb | 3 ++- kiwi/app/views/questions/show.html.erb | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/kiwi/app/controllers/questions_controller.rb b/kiwi/app/controllers/questions_controller.rb index 2f9c3d2..247c5c4 100644 --- a/kiwi/app/controllers/questions_controller.rb +++ b/kiwi/app/controllers/questions_controller.rb @@ -38,9 +38,10 @@ def update end end - def delete + def destroy @question = Question.find_by(id: params[:id]) @question.destroy + redirect_to questions_path end private diff --git a/kiwi/app/views/questions/show.html.erb b/kiwi/app/views/questions/show.html.erb index 06a519b..0044c67 100644 --- a/kiwi/app/views/questions/show.html.erb +++ b/kiwi/app/views/questions/show.html.erb @@ -5,6 +5,7 @@
<%= link_to "Edit my question", edit_question_path %> +<%= link_to "Delete my question", question_path, method: :delete %>
<% @question.answers.each do |answer|%> From 71a5794e9e83f38962c2634746fa92f9987331ae Mon Sep 17 00:00:00 2001 From: Jeff George Date: Fri, 22 Jan 2016 21:38:41 -0500 Subject: [PATCH 044/152] Implement partial for questions#new and #edit --- kiwi/app/views/questions/_form.html.erb | 15 +++++++++++++++ kiwi/app/views/questions/edit.html.erb | 17 ++--------------- kiwi/app/views/questions/new.html.erb | 18 ++---------------- 3 files changed, 19 insertions(+), 31 deletions(-) create mode 100644 kiwi/app/views/questions/_form.html.erb diff --git a/kiwi/app/views/questions/_form.html.erb b/kiwi/app/views/questions/_form.html.erb new file mode 100644 index 0000000..622234e --- /dev/null +++ b/kiwi/app/views/questions/_form.html.erb @@ -0,0 +1,15 @@ +<%= form_for @question do |f| %> +
+
+ <%= f.label :title %> + <%= f.text_field :title %> +
+ +
+ <%= f.label :content %> + <%= f.text_area :content %> +
+ + <%= f.submit 'Save your question' %> +
+<% end %> diff --git a/kiwi/app/views/questions/edit.html.erb b/kiwi/app/views/questions/edit.html.erb index 57eb2fb..5278231 100644 --- a/kiwi/app/views/questions/edit.html.erb +++ b/kiwi/app/views/questions/edit.html.erb @@ -1,17 +1,4 @@ -

Edit Your Question

- <%= form_for @question do |f| %> -
-
- <%= f.label :title %> - <%= f.text_field :title %> -
+

Edit Your Question

-
- <%= f.label :content %> - <%= f.text_area :content %> -
- - <%= f.submit 'Save your question' %> -
-<% end %> +<%= render partial: "form" %> diff --git a/kiwi/app/views/questions/new.html.erb b/kiwi/app/views/questions/new.html.erb index 30d997f..7b3f89d 100644 --- a/kiwi/app/views/questions/new.html.erb +++ b/kiwi/app/views/questions/new.html.erb @@ -1,20 +1,6 @@ -

POST A NEW QUESTION

- <%= form_for @question do |f| %> -
-
- <%= f.label :title %> - <%= f.text_field :title %> -
- -
- <%= f.label :content %> - <%= f.text_area :content %> -
- - <%= f.submit 'POST YOUR QUESTION' %> -
-<% end %> +

Post a new question

+<%= render partial: "form" %> From d374e727d7c977c778863efb25b362d905092cec Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sat, 23 Jan 2016 13:58:40 -0500 Subject: [PATCH 045/152] Hide new question link on questions/index from non-logged-in visitors --- kiwi/app/views/questions/index.html.erb | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/kiwi/app/views/questions/index.html.erb b/kiwi/app/views/questions/index.html.erb index 893ac61..6664fd1 100644 --- a/kiwi/app/views/questions/index.html.erb +++ b/kiwi/app/views/questions/index.html.erb @@ -1,16 +1,15 @@ -

LIST OF QUESTIONS

- -<%= link_to "NEW QUESTION", new_question_path %> -

QUESTIONS

-
    - <% @questions.each do |question| %> - -
  • <%= link_to question.title, question_path(question) %>
  • - <% end %> -
+
    + <% @questions.each do |question| %> +
  • <%= link_to question.title, question_path(question) %>
  • + <% end %> +
+<% if logged_in? %> + <%= link_to "Post a New Question", new_question_path %> +<% end %> + From 87674392e134927c61ba8804402e2fdd3fc8b620 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sat, 23 Jan 2016 14:05:20 -0500 Subject: [PATCH 046/152] Remove binding.pry in questions_controller --- kiwi/app/controllers/questions_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kiwi/app/controllers/questions_controller.rb b/kiwi/app/controllers/questions_controller.rb index 247c5c4..7aca559 100644 --- a/kiwi/app/controllers/questions_controller.rb +++ b/kiwi/app/controllers/questions_controller.rb @@ -11,7 +11,7 @@ def new end def create - binding.pry + # binding.pry @question = Question.new(question_params) @question.user = current_user if @question.save From d0b2a786ba46a0ca1d34ef5c9fceb7abd6931ab5 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sat, 23 Jan 2016 14:06:16 -0500 Subject: [PATCH 047/152] Hide edit and delete questions links on qeustions/show view from all users except question owner --- kiwi/app/views/questions/show.html.erb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/kiwi/app/views/questions/show.html.erb b/kiwi/app/views/questions/show.html.erb index 0044c67..151f7c1 100644 --- a/kiwi/app/views/questions/show.html.erb +++ b/kiwi/app/views/questions/show.html.erb @@ -4,8 +4,10 @@

Asked by <%= @question.user.name %> on <%= std_format_date(@question.created_at) %>

-<%= link_to "Edit my question", edit_question_path %> -<%= link_to "Delete my question", question_path, method: :delete %> +<% if current_user == @question.user %> + <%= link_to "Edit my question", edit_question_path %> + <%= link_to "Delete my question", question_path, method: :delete %> +<% end %>
<% @question.answers.each do |answer|%> From 9793f83dd25b29a86c301f8f9ebb9d1977f8df09 Mon Sep 17 00:00:00 2001 From: deweydell Date: Sat, 23 Jan 2016 13:40:24 -0500 Subject: [PATCH 048/152] Update readme to include link to our trello board --- kiwi/README.rdoc | 27 ++------------------------- 1 file changed, 2 insertions(+), 25 deletions(-) diff --git a/kiwi/README.rdoc b/kiwi/README.rdoc index dd4e97e..00593b8 100644 --- a/kiwi/README.rdoc +++ b/kiwi/README.rdoc @@ -1,28 +1,5 @@ == README -This README would normally document whatever steps are necessary to get the -application up and running. +This is a Ruby on Rails app built by @webdevjeffus, @deweydell, @luisplaz, and @zlschatz which is an imitation of Stack Overflow. -Things you may want to cover: - -* Ruby version - -* System dependencies - -* Configuration - -* Database creation - -* Database initialization - -* How to run the test suite - -* Services (job queues, cache servers, search engines, etc.) - -* Deployment instructions - -* ... - - -Please feel free to use a different markup language if you do not plan to run -rake doc:app. +Our trello board has images of our schema and working agreement: https://trello.com/b/TKvtoxBm/kiwi-overflow From 6bec1e8e259424dc93af92c7be0491dae9f785a1 Mon Sep 17 00:00:00 2001 From: deweydell Date: Sat, 23 Jan 2016 14:48:58 -0500 Subject: [PATCH 049/152] Add font and css for main index page and nav bar --- kiwi/app/assets/stylesheets/main.css | 93 +++++++++++++++++++++++++--- 1 file changed, 83 insertions(+), 10 deletions(-) diff --git a/kiwi/app/assets/stylesheets/main.css b/kiwi/app/assets/stylesheets/main.css index e8a4bd5..2911fdc 100644 --- a/kiwi/app/assets/stylesheets/main.css +++ b/kiwi/app/assets/stylesheets/main.css @@ -1,26 +1,99 @@ -* { - font-family: 'Open Sans Condensed', sans-serif; +body { + font-family: 'Roboto', sans-serif; } + +header, footer { + margin: 0 auto; + width: 50%; + text-align: center; + + margin-bottom: 1em; +} + +header { + background-color: #E0EEE0; + padding: 0px 20px 20px 20px; +} + +nav { + margin: 0 auto; +} + +#kiwi { + height: 100px; + width: 100px; + margin-top: 1em; +} + +#logo { + font-weight: 900; + margin-bottom: 1em; +} + +nav ul { + display: inline; +} + +nav li { + display: inline; + list-style: none; + font-weight: 400; + padding: 0 10px 0 10px; +} + +main { + margin: 0px auto; + width: 50%; + padding: 0px 20px 20px 20px; + border: 1px dotted black; +} + +.index-question-container { + border-bottom: 1px solid gray; + padding-bottom: 1em; + margin-bottom: 1em; +} + #form { display:inline; } #input-options { - display:block; padding:5px; } -.container{ - display:block; - padding:10px; - border:1px solid black; - margin:0 auto; - width: 50%; -} .web-title { padding:10px; margin:0 auto; text-align: center; } + + + +ul { + padding-left: 0; +} + + +a { + text-decoration: none; + color: #66CD00; +} + +a:hover { + text-decoration: underline; + color: green; +} + +textarea { + margin-top: 3%; + margin-bottom: 3%; + width: 100%; + height: 200px; +} + +p { + font-weight: 300; +} \ No newline at end of file From 6a2319d626d08eea4f7b99f88f603326ee689df0 Mon Sep 17 00:00:00 2001 From: deweydell Date: Sat, 23 Jan 2016 14:49:27 -0500 Subject: [PATCH 050/152] Add font link and reformat layout for navbar --- kiwi/app/views/layouts/application.html.erb | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/kiwi/app/views/layouts/application.html.erb b/kiwi/app/views/layouts/application.html.erb index 2f8cfcd..c03ef25 100644 --- a/kiwi/app/views/layouts/application.html.erb +++ b/kiwi/app/views/layouts/application.html.erb @@ -1,23 +1,24 @@ - - Kiwi + + Kiwi Overflow <%= stylesheet_link_tag 'application', media: 'all' %> <%= javascript_include_tag 'application' %> <%= csrf_meta_tags %>
-

Kiwi Overflow

-
+
<%= f.submit 'Create Account' %>
From a10188d3ea1e9c011777336596a11b68f3827fe4 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sat, 23 Jan 2016 15:15:13 -0500 Subject: [PATCH 058/152] Create comments_controller; begin writing routes --- kiwi/app/controllers/comments_controller.rb | 37 +++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 kiwi/app/controllers/comments_controller.rb diff --git a/kiwi/app/controllers/comments_controller.rb b/kiwi/app/controllers/comments_controller.rb new file mode 100644 index 0000000..ee0f807 --- /dev/null +++ b/kiwi/app/controllers/comments_controller.rb @@ -0,0 +1,37 @@ +class CommentsController < ApplicationController + + def new + @comment = Comment.new + end + + def create + @comment = Comment.new(comment_params) + @comment.user = current_user + if @comment.save + redirect_to + else + render :new + end + end + + def edit + @comment = Comment.find_by(id: params[:id]) + end + + def update + @comment = Comment.find_by(id: params[:id]) + if @comment.update_attributes(comment_params) + redirect_to + + end + + def destroy + end + + private + + def comment_params + params.require(:comment).permit() + end + +end \ No newline at end of file From 5c3fa2d5a5da592f8f4824275df8caaafb5d8ff9 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sat, 23 Jan 2016 15:45:31 -0500 Subject: [PATCH 059/152] In questions/_form, change id='input-option' to class --- kiwi/app/views/questions/_form.html.erb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kiwi/app/views/questions/_form.html.erb b/kiwi/app/views/questions/_form.html.erb index 28621a3..d56650b 100644 --- a/kiwi/app/views/questions/_form.html.erb +++ b/kiwi/app/views/questions/_form.html.erb @@ -1,11 +1,11 @@ <%= form_for @question do |f| %>
-
+
<%= f.label :title %> <%= f.text_field :title %>
-
+
<%= f.label :content %> <%= f.text_area :content %>
From e890ef6b3ef36de28b2bd909612689761036bda0 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sat, 23 Jan 2016 16:37:39 -0500 Subject: [PATCH 060/152] Remove binding.pry from users#create --- kiwi/app/controllers/users_controller.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/kiwi/app/controllers/users_controller.rb b/kiwi/app/controllers/users_controller.rb index a3d004f..44455ca 100644 --- a/kiwi/app/controllers/users_controller.rb +++ b/kiwi/app/controllers/users_controller.rb @@ -9,7 +9,6 @@ def show end def create - binding.pry @user = User.new(user_params) if @user.save login @user From 51f58de31a7ac2ca61575f8e674e04f118a3be6e Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sat, 23 Jan 2016 17:05:58 -0500 Subject: [PATCH 061/152] Add temporary link to comments#new --- kiwi/app/views/questions/show.html.erb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kiwi/app/views/questions/show.html.erb b/kiwi/app/views/questions/show.html.erb index 5c6b561..4f33e8a 100644 --- a/kiwi/app/views/questions/show.html.erb +++ b/kiwi/app/views/questions/show.html.erb @@ -46,4 +46,5 @@
<% end %> - + +<%= link_to "Comment on the Question", new_question_comment_path(@question.id) %> From 833c04f122b290041a46c417e3ca07b2db581f37 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sat, 23 Jan 2016 17:06:52 -0500 Subject: [PATCH 062/152] Implement comments#new route through to form view --- kiwi/app/controllers/comments_controller.rb | 17 ++++++++++++----- kiwi/app/views/comments/_form.html.erb | 14 ++++++++++++++ kiwi/app/views/comments/edit.html.erb | 4 ++++ kiwi/app/views/comments/new.html.erb | 6 ++++++ kiwi/config/routes.rb | 4 ++-- 5 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 kiwi/app/views/comments/_form.html.erb create mode 100644 kiwi/app/views/comments/edit.html.erb create mode 100644 kiwi/app/views/comments/new.html.erb diff --git a/kiwi/app/controllers/comments_controller.rb b/kiwi/app/controllers/comments_controller.rb index ee0f807..6803fc7 100644 --- a/kiwi/app/controllers/comments_controller.rb +++ b/kiwi/app/controllers/comments_controller.rb @@ -1,14 +1,16 @@ class CommentsController < ApplicationController def new - @comment = Comment.new + @question = Question.find_by(id: params[:question_id]) + @comment = @question.comments.new end def create - @comment = Comment.new(comment_params) + @commentable = Question.find_by(id: params[:id]) + @comment = @commentable.comments.new(comment_params) @comment.user = current_user if @comment.save - redirect_to + redirect_to question_path(id: @commentable_id) else render :new end @@ -22,16 +24,21 @@ def update @comment = Comment.find_by(id: params[:id]) if @comment.update_attributes(comment_params) redirect_to - + else + render :edit + end end def destroy + @comment = Comment.find_by(id: params[:id]) + @comment.destroy + redirect_to questions_path end private def comment_params - params.require(:comment).permit() + params.require(:comment).permit(:content, :commentable_type, :commentable_id, :user_id) end end \ No newline at end of file diff --git a/kiwi/app/views/comments/_form.html.erb b/kiwi/app/views/comments/_form.html.erb new file mode 100644 index 0000000..52d0732 --- /dev/null +++ b/kiwi/app/views/comments/_form.html.erb @@ -0,0 +1,14 @@ +<% binding.pry %> + +<%= form_for [@question, @question.comments.build] do |f| %> +
+ +
+ <%= f.label :content %> + <%= f.text_area :content %> +
+ + <%= f.submit 'Save your comment' %> + +
+<% end %> \ No newline at end of file diff --git a/kiwi/app/views/comments/edit.html.erb b/kiwi/app/views/comments/edit.html.erb new file mode 100644 index 0000000..cca6126 --- /dev/null +++ b/kiwi/app/views/comments/edit.html.erb @@ -0,0 +1,4 @@ +

Edit your comment

+ +<%= render partial: "form" %> + diff --git a/kiwi/app/views/comments/new.html.erb b/kiwi/app/views/comments/new.html.erb new file mode 100644 index 0000000..3a2b5a5 --- /dev/null +++ b/kiwi/app/views/comments/new.html.erb @@ -0,0 +1,6 @@ +

Add your comment

+ +<% binding.pry %> + +<%= render partial: "form" %> + diff --git a/kiwi/config/routes.rb b/kiwi/config/routes.rb index 38eb2ab..4545e87 100644 --- a/kiwi/config/routes.rb +++ b/kiwi/config/routes.rb @@ -4,9 +4,9 @@ # resources :questions, only: [:new, :edit, :index] # end - resources :questions do + resources :questions do # resources :answers, only: [:new, :show, :delete, :edit] - # resources :comments, only: [:new, :show] + resources :comments, only: [:new, :create, :edit, :update, :destroy] end # resources :answers do From 8dfd0ed6a5a21e9dc6cab8980b6fa775c8096840 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sat, 23 Jan 2016 17:10:14 -0500 Subject: [PATCH 063/152] Remove bindings from new comments view and _form --- kiwi/app/views/comments/_form.html.erb | 2 -- kiwi/app/views/comments/new.html.erb | 2 -- 2 files changed, 4 deletions(-) diff --git a/kiwi/app/views/comments/_form.html.erb b/kiwi/app/views/comments/_form.html.erb index 52d0732..4069a3a 100644 --- a/kiwi/app/views/comments/_form.html.erb +++ b/kiwi/app/views/comments/_form.html.erb @@ -1,5 +1,3 @@ -<% binding.pry %> - <%= form_for [@question, @question.comments.build] do |f| %>
diff --git a/kiwi/app/views/comments/new.html.erb b/kiwi/app/views/comments/new.html.erb index 3a2b5a5..7ede03a 100644 --- a/kiwi/app/views/comments/new.html.erb +++ b/kiwi/app/views/comments/new.html.erb @@ -1,6 +1,4 @@

Add your comment

-<% binding.pry %> - <%= render partial: "form" %> From 3fec32c94a8a1598a4140f19a7b7382e403059f1 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sat, 23 Jan 2016 17:20:13 -0500 Subject: [PATCH 064/152] Implement comments#create route; working for questions, not tested for answers --- kiwi/app/controllers/comments_controller.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/kiwi/app/controllers/comments_controller.rb b/kiwi/app/controllers/comments_controller.rb index 6803fc7..a9ac792 100644 --- a/kiwi/app/controllers/comments_controller.rb +++ b/kiwi/app/controllers/comments_controller.rb @@ -6,11 +6,16 @@ def new end def create - @commentable = Question.find_by(id: params[:id]) - @comment = @commentable.comments.new(comment_params) - @comment.user = current_user + binding.pry + if params[:question_id] + question = Question.find_by(id: params[:question_id]) + @comment = question.comments.new(user: current_user, content: params[:comment][:content]) + else + answer = Answer.find_by(id: params[:answer_id]) + @comment = answer.comments.new(user: current_user, content: params[:comment][:content]) + end if @comment.save - redirect_to question_path(id: @commentable_id) + redirect_to question_path(id: @comment.commentable_id) else render :new end From a1c1fdcc5c7ccc36c3f65e55bf72f765edc2c02a Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sat, 23 Jan 2016 17:40:40 -0500 Subject: [PATCH 065/152] Add link to comments#new from questions#show --- kiwi/app/controllers/comments_controller.rb | 39 +++++++++++---------- kiwi/app/views/questions/show.html.erb | 9 ++--- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/kiwi/app/controllers/comments_controller.rb b/kiwi/app/controllers/comments_controller.rb index a9ac792..c32383e 100644 --- a/kiwi/app/controllers/comments_controller.rb +++ b/kiwi/app/controllers/comments_controller.rb @@ -6,7 +6,6 @@ def new end def create - binding.pry if params[:question_id] question = Question.find_by(id: params[:question_id]) @comment = question.comments.new(user: current_user, content: params[:comment][:content]) @@ -21,24 +20,26 @@ def create end end - def edit - @comment = Comment.find_by(id: params[:id]) - end - - def update - @comment = Comment.find_by(id: params[:id]) - if @comment.update_attributes(comment_params) - redirect_to - else - render :edit - end - end - - def destroy - @comment = Comment.find_by(id: params[:id]) - @comment.destroy - redirect_to questions_path - end + # NOT YET WORKING + # def edit + # @comment = Comment.find_by(id: params[:id]) + # end + + # def update + # @comment = Comment.find_by(id: params[:id]) + # if @comment.update_attributes(comment_params) + # redirect_to + # else + # render :edit + # end + # end + + # def destroy + # @comment = Comment.find_by(id: params[:id]) + # commentable_id = @comment.commentable.id + # @comment.destroy + # redirect_to questions_path(commentable_id) + # end private diff --git a/kiwi/app/views/questions/show.html.erb b/kiwi/app/views/questions/show.html.erb index 4f33e8a..0817a5d 100644 --- a/kiwi/app/views/questions/show.html.erb +++ b/kiwi/app/views/questions/show.html.erb @@ -6,14 +6,14 @@
Comments:
<% @question.comments.each do |comment| %> -

<%= comment.content %> - <%= comment.user.name %> on <%= std_format_date(comment.created_at) %>

+

<%= comment.content %> • <%= comment.user.name %> on <%= std_format_date(comment.created_at) %>

<% end %>
<% end %>
    -
  • <%= link_to "Add Comment", new_question_path %>
  • +
  • <%= link_to "Add Comment", new_question_comment_path(@question.id) %>
  • <%= link_to "Add Answer", new_question_path %>
@@ -35,7 +35,7 @@
Comments:
<% answer.comments.each do |comment| %> -

<%= comment.content %> - +

<%= comment.content %> • <%= comment.user.name %> on <%= std_format_date(comment.created_at) %>

<% end %>
@@ -45,6 +45,3 @@ <% end %>
<% end %> - - -<%= link_to "Comment on the Question", new_question_comment_path(@question.id) %> From e320aa09c1152d4958ac6ab55c5da2cfdc2f70e5 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sat, 23 Jan 2016 18:26:09 -0500 Subject: [PATCH 066/152] Remove binding.pry in questions#create --- kiwi/app/controllers/questions_controller.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/kiwi/app/controllers/questions_controller.rb b/kiwi/app/controllers/questions_controller.rb index 7aca559..fd3d828 100644 --- a/kiwi/app/controllers/questions_controller.rb +++ b/kiwi/app/controllers/questions_controller.rb @@ -11,7 +11,6 @@ def new end def create - # binding.pry @question = Question.new(question_params) @question.user = current_user if @question.save From 16477ecb67e803fceae6c323bd9ce3d0b4a56ec9 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sat, 23 Jan 2016 18:26:45 -0500 Subject: [PATCH 067/152] Add non-nested routes for comments#edit, #update, and #destroy --- kiwi/config/routes.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/kiwi/config/routes.rb b/kiwi/config/routes.rb index 4545e87..4e0d908 100644 --- a/kiwi/config/routes.rb +++ b/kiwi/config/routes.rb @@ -6,13 +6,15 @@ resources :questions do # resources :answers, only: [:new, :show, :delete, :edit] - resources :comments, only: [:new, :create, :edit, :update, :destroy] + resources :comments, only: [:new, :create] end # resources :answers do - # resources :comments, only: [:new, :show] + # resources :comments, only: [:new, :show] # end + resources :comments, only: [ :edit, :update, :destroy ] + resources :users, only: [ :new, :show, :create ] resources :sessions, only: [ :new, :create, :destroy ] From b27dc330b985a29db56a470532b3a44f7669cbf3 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sat, 23 Jan 2016 18:27:41 -0500 Subject: [PATCH 068/152] Implement comments#destroy --- kiwi/app/controllers/comments_controller.rb | 16 ++++++++++------ kiwi/app/views/questions/show.html.erb | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/kiwi/app/controllers/comments_controller.rb b/kiwi/app/controllers/comments_controller.rb index c32383e..49f2b45 100644 --- a/kiwi/app/controllers/comments_controller.rb +++ b/kiwi/app/controllers/comments_controller.rb @@ -34,12 +34,16 @@ def create # end # end - # def destroy - # @comment = Comment.find_by(id: params[:id]) - # commentable_id = @comment.commentable.id - # @comment.destroy - # redirect_to questions_path(commentable_id) - # end + def destroy + @comment = Comment.find_by(id: params[:id]) + commentable = @comment.commentable + @comment.destroy + if commentable.is_a? Question + redirect_to question_path(commentable.id) + else + redirect_to question_path(commentable.question.id) + end + end private diff --git a/kiwi/app/views/questions/show.html.erb b/kiwi/app/views/questions/show.html.erb index 0817a5d..e0adb45 100644 --- a/kiwi/app/views/questions/show.html.erb +++ b/kiwi/app/views/questions/show.html.erb @@ -6,7 +6,7 @@
Comments:
<% @question.comments.each do |comment| %> -

<%= comment.content %> • <%= comment.user.name %> on <%= std_format_date(comment.created_at) %>

+

<%= comment.content %> • <%= comment.user.name %> on <%= std_format_date(comment.created_at) %> • <%= link_to "Delete",comment_path(comment.id), method: :delete %>

<% end %>
<% end %> From c95f50fd5da8384284673d8e449eeded1bf49929 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sat, 23 Jan 2016 18:40:43 -0500 Subject: [PATCH 069/152] Begin work on comments#edit --- kiwi/app/controllers/comments_controller.rb | 2 ++ kiwi/app/views/questions/edit.html.erb | 12 +++++++++++- kiwi/app/views/questions/show.html.erb | 2 +- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/kiwi/app/controllers/comments_controller.rb b/kiwi/app/controllers/comments_controller.rb index 49f2b45..a908c5f 100644 --- a/kiwi/app/controllers/comments_controller.rb +++ b/kiwi/app/controllers/comments_controller.rb @@ -14,6 +14,7 @@ def create @comment = answer.comments.new(user: current_user, content: params[:comment][:content]) end if @comment.save + # We may need a conditional here once we have comments to answers as well redirect_to question_path(id: @comment.commentable_id) else render :new @@ -22,6 +23,7 @@ def create # NOT YET WORKING # def edit + # binding.pry # @comment = Comment.find_by(id: params[:id]) # end diff --git a/kiwi/app/views/questions/edit.html.erb b/kiwi/app/views/questions/edit.html.erb index 5278231..da9af0c 100644 --- a/kiwi/app/views/questions/edit.html.erb +++ b/kiwi/app/views/questions/edit.html.erb @@ -1,4 +1,14 @@

Edit Your Question

-<%= render partial: "form" %> +<%= form_for [@comment] do |f| %> +
+
+ <%= f.label :content %> + <%= f.text_area :content %> +
+ + <%= f.submit 'Save your comment' %> + +
+<% end %> \ No newline at end of file diff --git a/kiwi/app/views/questions/show.html.erb b/kiwi/app/views/questions/show.html.erb index e0adb45..d6e14ab 100644 --- a/kiwi/app/views/questions/show.html.erb +++ b/kiwi/app/views/questions/show.html.erb @@ -6,7 +6,7 @@
Comments:
<% @question.comments.each do |comment| %> -

<%= comment.content %> • <%= comment.user.name %> on <%= std_format_date(comment.created_at) %> • <%= link_to "Delete",comment_path(comment.id), method: :delete %>

+

<%= comment.content %> • <%= comment.user.name %> on <%= std_format_date(comment.created_at) %> • <%= link_to "Edit", edit_comment_path %> • <%= link_to "Delete",comment_path(comment.id), method: :delete %>

<% end %>
<% end %> From 8efe7cc2cbeb7f64e71aa79315e994da375d9d8c Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sat, 23 Jan 2016 19:02:34 -0500 Subject: [PATCH 070/152] Repair questions/edit view effed-up in previous merge. Sorry. --- kiwi/app/views/questions/edit.html.erb | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/kiwi/app/views/questions/edit.html.erb b/kiwi/app/views/questions/edit.html.erb index da9af0c..aa0b8fc 100644 --- a/kiwi/app/views/questions/edit.html.erb +++ b/kiwi/app/views/questions/edit.html.erb @@ -1,14 +1,3 @@

Edit Your Question

-<%= form_for [@comment] do |f| %> -
- -
- <%= f.label :content %> - <%= f.text_area :content %> -
- - <%= f.submit 'Save your comment' %> - -
-<% end %> \ No newline at end of file +<%= render partial: "form" %> \ No newline at end of file From 26aac071f101155ce35b8f10f8de487e90d60fcc Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sat, 23 Jan 2016 19:20:03 -0500 Subject: [PATCH 071/152] Implement comments#edit and #update routes --- kiwi/app/controllers/comments_controller.rb | 28 ++++++++++----------- kiwi/app/views/comments/edit.html.erb | 12 ++++++++- kiwi/app/views/questions/show.html.erb | 2 +- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/kiwi/app/controllers/comments_controller.rb b/kiwi/app/controllers/comments_controller.rb index a908c5f..d5155b3 100644 --- a/kiwi/app/controllers/comments_controller.rb +++ b/kiwi/app/controllers/comments_controller.rb @@ -21,20 +21,20 @@ def create end end - # NOT YET WORKING - # def edit - # binding.pry - # @comment = Comment.find_by(id: params[:id]) - # end - - # def update - # @comment = Comment.find_by(id: params[:id]) - # if @comment.update_attributes(comment_params) - # redirect_to - # else - # render :edit - # end - # end + def edit + @comment = Comment.find_by(id: params[:id]) + binding.pry + end + + def update + @comment = Comment.find_by(id: params[:id]) + if @comment.update_attributes(comment_params) + # We may need a conditional here once we have comments to answers as well + redirect_to question_path(id: @comment.commentable_id) + else + render :edit + end + end def destroy @comment = Comment.find_by(id: params[:id]) diff --git a/kiwi/app/views/comments/edit.html.erb b/kiwi/app/views/comments/edit.html.erb index cca6126..197332b 100644 --- a/kiwi/app/views/comments/edit.html.erb +++ b/kiwi/app/views/comments/edit.html.erb @@ -1,4 +1,14 @@

Edit your comment

-<%= render partial: "form" %> +<%= form_for @comment do |f| %> +
+
+ <%= f.label :content %> + <%= f.text_area :content %> +
+ + <%= f.submit 'Save your comment' %> + +
+<% end %> \ No newline at end of file diff --git a/kiwi/app/views/questions/show.html.erb b/kiwi/app/views/questions/show.html.erb index d6e14ab..db65132 100644 --- a/kiwi/app/views/questions/show.html.erb +++ b/kiwi/app/views/questions/show.html.erb @@ -6,7 +6,7 @@
Comments:
<% @question.comments.each do |comment| %> -

<%= comment.content %> • <%= comment.user.name %> on <%= std_format_date(comment.created_at) %> • <%= link_to "Edit", edit_comment_path %> • <%= link_to "Delete",comment_path(comment.id), method: :delete %>

+

<%= comment.content %> • <%= comment.user.name %> on <%= std_format_date(comment.created_at) %> • <%= link_to "Edit", edit_comment_path(comment.id) %> • <%= link_to "Delete",comment_path(comment.id), method: :delete %>

<% end %>
<% end %> From 77781855c9e5a50c14d618631a6e4ba9fff27069 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sat, 23 Jan 2016 19:34:58 -0500 Subject: [PATCH 072/152] Attempted to recast comments/_form to work for both comments#new and #edit; failed. --- kiwi/app/controllers/comments_controller.rb | 1 + kiwi/app/views/comments/_form.html.erb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/kiwi/app/controllers/comments_controller.rb b/kiwi/app/controllers/comments_controller.rb index d5155b3..ae02542 100644 --- a/kiwi/app/controllers/comments_controller.rb +++ b/kiwi/app/controllers/comments_controller.rb @@ -23,6 +23,7 @@ def create def edit @comment = Comment.find_by(id: params[:id]) + @question = Question.find_by(id: @comment.commentable_id) binding.pry end diff --git a/kiwi/app/views/comments/_form.html.erb b/kiwi/app/views/comments/_form.html.erb index 4069a3a..7c19d36 100644 --- a/kiwi/app/views/comments/_form.html.erb +++ b/kiwi/app/views/comments/_form.html.erb @@ -1,4 +1,4 @@ -<%= form_for [@question, @question.comments.build] do |f| %> +<%= form_for [@question, @comment] do |f| %>
From 0f3871094d58349b5ae4b112dfdb66de19c8f5d8 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sat, 23 Jan 2016 19:45:28 -0500 Subject: [PATCH 073/152] Add conditionals in questions#show to hide links from wrong users --- kiwi/app/views/questions/show.html.erb | 34 ++++++++++++++------------ 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/kiwi/app/views/questions/show.html.erb b/kiwi/app/views/questions/show.html.erb index db65132..94d2eb8 100644 --- a/kiwi/app/views/questions/show.html.erb +++ b/kiwi/app/views/questions/show.html.erb @@ -6,16 +6,18 @@
Comments:
<% @question.comments.each do |comment| %> -

<%= comment.content %> • <%= comment.user.name %> on <%= std_format_date(comment.created_at) %> • <%= link_to "Edit", edit_comment_path(comment.id) %> • <%= link_to "Delete",comment_path(comment.id), method: :delete %>

+

<%= comment.content %> • <%= comment.user.name %> on <%= std_format_date(comment.created_at) %> • <% if current_user == comment.user %><%= link_to "Edit", edit_comment_path(comment.id) %> • <%= link_to "Delete",comment_path(comment.id), method: :delete %><% end %>

<% end %>
<% end %>
-
    -
  • <%= link_to "Add Comment", new_question_comment_path(@question.id) %>
  • -
  • <%= link_to "Add Answer", new_question_path %>
  • -
+<% if logged_in? %> +
    +
  • <%= link_to "Add Comment", new_question_comment_path(@question.id) %>
  • +
  • <%= link_to "Add Answer", new_question_path %>
  • +
+<% end %>
    <% if current_user == @question.user %> @@ -31,16 +33,18 @@

    Answered by <%= answer.user.name %> on <%= std_format_date(answer.created_at) %>

    <%= answer.content %>

    - <% if !answer.comments.empty? %> -
    Comments:
    -
    - <% answer.comments.each do |comment| %> -

    <%= comment.content %> • - <%= comment.user.name %> on <%= std_format_date(comment.created_at) %>

    - <% end %> -
    - <% end %> - <%= link_to "Add Comment", new_question_path %> + <% if !answer.comments.empty? %> +
    Comments:
    +
    + <% answer.comments.each do |comment| %> +

    <%= comment.content %> • + <%= comment.user.name %> on <%= std_format_date(comment.created_at) %>

    + <% end %> +
    + <% end %> + <% if logged_in? %> + <%= link_to "Add Comment", new_question_path %> + <% end %>
    <% end %>
From 16e3d005d11c57599881d2d66dbacbd3736c441a Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sat, 23 Jan 2016 19:46:13 -0500 Subject: [PATCH 074/152] Remove binding.pry from comments#edit --- kiwi/app/controllers/comments_controller.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/kiwi/app/controllers/comments_controller.rb b/kiwi/app/controllers/comments_controller.rb index ae02542..871b96b 100644 --- a/kiwi/app/controllers/comments_controller.rb +++ b/kiwi/app/controllers/comments_controller.rb @@ -24,7 +24,6 @@ def create def edit @comment = Comment.find_by(id: params[:id]) @question = Question.find_by(id: @comment.commentable_id) - binding.pry end def update From 9eca68960e29463f4c050772094c01a7b685611c Mon Sep 17 00:00:00 2001 From: deweydell Date: Sat, 23 Jan 2016 20:00:40 -0500 Subject: [PATCH 075/152] Add register, login, and logout paths to routes and layout --- kiwi/app/views/layouts/application.html.erb | 4 ++-- kiwi/config/routes.rb | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/kiwi/app/views/layouts/application.html.erb b/kiwi/app/views/layouts/application.html.erb index 6a59441..553f2d7 100644 --- a/kiwi/app/views/layouts/application.html.erb +++ b/kiwi/app/views/layouts/application.html.erb @@ -18,8 +18,8 @@
  • <%= link_to "Post a Question", new_question_path %>
  • |
  • <%= link_to("Log Out", session_path(current_user), method: :delete) %>
  • || <% else %> -
  • <%= link_to "Register", new_user_path %>
  • | -
  • <%= link_to "Login", new_session_path %>
  • || +
  • <%= link_to "Register", register_path %>
  • | +
  • <%= link_to "Login", login_path %>
  • || <% end %>
  • <%= link_to "All Questions", questions_path %>
  • diff --git a/kiwi/config/routes.rb b/kiwi/config/routes.rb index 4e0d908..dce7672 100644 --- a/kiwi/config/routes.rb +++ b/kiwi/config/routes.rb @@ -21,6 +21,10 @@ root 'questions#index' + get 'login' => 'sessions#new' + delete 'logout' => 'sessions#destroy' + get 'register' => 'users#new' + # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". From 5ef77a7ed3163be3a842bc1b65891fee5e1b644c Mon Sep 17 00:00:00 2001 From: deweydell Date: Sat, 23 Jan 2016 20:18:59 -0500 Subject: [PATCH 076/152] Add css and labels for votes, answers, views on index --- kiwi/app/assets/stylesheets/main.css | 20 +++++++++++++++++++- kiwi/app/views/questions/index.html.erb | 17 ++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/kiwi/app/assets/stylesheets/main.css b/kiwi/app/assets/stylesheets/main.css index 005240a..933ebd9 100644 --- a/kiwi/app/assets/stylesheets/main.css +++ b/kiwi/app/assets/stylesheets/main.css @@ -118,4 +118,22 @@ label{ text-align: right; margin-right: 1.5em; display: block -} \ No newline at end of file +} + +.question-stats { + display: inline; + margin-right: 2em; +} + +.votes, .answers, .views { + display: inline; + font-weight: 900; + text-align: center; +} +.votes-label, .answers-label, .views-label { + display: inline; + margin-right: 1em; +} + + + diff --git a/kiwi/app/views/questions/index.html.erb b/kiwi/app/views/questions/index.html.erb index 0a7ef21..f2dc8fb 100644 --- a/kiwi/app/views/questions/index.html.erb +++ b/kiwi/app/views/questions/index.html.erb @@ -3,7 +3,22 @@ <% @questions.each do |question| %>
    +
    + +
    0
    +
    votes
    + +
    + <%= question.answers.count %> +
    +
    answers
    + +
    0
    +
    views
    + +
    + + <%= link_to question.title, question_path(question) %> - <%= link_to question.title, question_path(question) %>
    <% end %> \ No newline at end of file From e0605f4a577ed486e8be8bc1d6b899603a4c2b2a Mon Sep 17 00:00:00 2001 From: deweydell Date: Sat, 23 Jan 2016 20:44:25 -0500 Subject: [PATCH 077/152] Add rspec-rails and shoulda-matchers gems to gemfile --- kiwi/Gemfile | 2 ++ kiwi/Gemfile.lock | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/kiwi/Gemfile b/kiwi/Gemfile index 55500b2..a8832dd 100644 --- a/kiwi/Gemfile +++ b/kiwi/Gemfile @@ -41,6 +41,8 @@ group :development, :test do gem 'database_cleaner' gem 'better_errors' gem 'faker' + gem 'rspec-rails' + gem 'shoulda-matchers' end group :development do diff --git a/kiwi/Gemfile.lock b/kiwi/Gemfile.lock index 305ec2b..51cdd10 100644 --- a/kiwi/Gemfile.lock +++ b/kiwi/Gemfile.lock @@ -58,6 +58,7 @@ GEM concurrent-ruby (1.0.0) database_cleaner (1.5.1) debug_inspector (0.0.2) + diff-lcs (1.2.5) erubis (2.7.0) execjs (2.6.0) factory_girl (4.5.0) @@ -127,6 +128,23 @@ GEM rake (10.5.0) rdoc (4.2.1) json (~> 1.4) + rspec-core (3.4.1) + 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.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.4.0) + rspec-rails (3.4.0) + actionpack (>= 3.0, < 4.3) + activesupport (>= 3.0, < 4.3) + railties (>= 3.0, < 4.3) + rspec-core (~> 3.4.0) + rspec-expectations (~> 3.4.0) + rspec-mocks (~> 3.4.0) + rspec-support (~> 3.4.0) + rspec-support (3.4.1) sass (3.4.21) sass-rails (5.0.4) railties (>= 4.0.0, < 5.0) @@ -137,6 +155,8 @@ GEM sdoc (0.4.1) json (~> 1.7, >= 1.7.7) rdoc (~> 4.0) + shoulda-matchers (2.8.0) + activesupport (>= 3.0.0) slop (3.6.0) sprockets (3.5.2) concurrent-ruby (~> 1.0) @@ -179,8 +199,10 @@ DEPENDENCIES pry-nav pry-rails rails (= 4.2.5) + rspec-rails sass-rails (~> 5.0) sdoc (~> 0.4.0) + shoulda-matchers uglifier (>= 1.3.0) web-console (~> 2.0) From 85dd70f8715ab9a46c71a254bf3d0709bd749306 Mon Sep 17 00:00:00 2001 From: deweydell Date: Sat, 23 Jan 2016 20:49:09 -0500 Subject: [PATCH 078/152] Add format documentation to .rspec file so tests are easy to read --- kiwi/.rspec | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 kiwi/.rspec diff --git a/kiwi/.rspec b/kiwi/.rspec new file mode 100644 index 0000000..f8f74f2 --- /dev/null +++ b/kiwi/.rspec @@ -0,0 +1,3 @@ +--color +--require spec_helper +--format documentation \ No newline at end of file From 70a83d3db7752e5200390909005d0b3dbbb6d287 Mon Sep 17 00:00:00 2001 From: deweydell Date: Sat, 23 Jan 2016 20:51:31 -0500 Subject: [PATCH 079/152] Generate spec helper and rails helper files --- kiwi/spec/rails_helper.rb | 57 ++++++++++++++++++++++++ kiwi/spec/spec_helper.rb | 92 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 kiwi/spec/rails_helper.rb create mode 100644 kiwi/spec/spec_helper.rb diff --git a/kiwi/spec/rails_helper.rb b/kiwi/spec/rails_helper.rb new file mode 100644 index 0000000..6f1ab14 --- /dev/null +++ b/kiwi/spec/rails_helper.rb @@ -0,0 +1,57 @@ +# This file is copied to spec/ when you run 'rails generate rspec:install' +ENV['RAILS_ENV'] ||= 'test' +require File.expand_path('../../config/environment', __FILE__) +# Prevent database truncation if the environment is production +abort("The Rails environment is running in production mode!") if Rails.env.production? +require 'spec_helper' +require 'rspec/rails' +# Add additional requires below this line. Rails is not loaded until this point! + +# Requires supporting ruby files with custom matchers and macros, etc, in +# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are +# run as spec files by default. This means that files in spec/support that end +# in _spec.rb will both be required and run as specs, causing the specs to be +# run twice. It is recommended that you do not name files matching this glob to +# end with _spec.rb. You can configure this pattern with the --pattern +# option on the command line or in ~/.rspec, .rspec or `.rspec-local`. +# +# The following line is provided for convenience purposes. It has the downside +# of increasing the boot-up time by auto-requiring all files in the support +# directory. Alternatively, in the individual `*_spec.rb` files, manually +# require only the support files necessary. +# +# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } + +# Checks for pending migration and applies them before tests are run. +# If you are not using ActiveRecord, you can remove this line. +ActiveRecord::Migration.maintain_test_schema! + +RSpec.configure do |config| + # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures + config.fixture_path = "#{::Rails.root}/spec/fixtures" + + # If you're not using ActiveRecord, or you'd prefer not to run each of your + # examples within a transaction, remove the following line or assign false + # instead of true. + config.use_transactional_fixtures = true + + # RSpec Rails can automatically mix in different behaviours to your tests + # based on their file location, for example enabling you to call `get` and + # `post` in specs under `spec/controllers`. + # + # You can disable this behaviour by removing the line below, and instead + # explicitly tag your specs with their type, e.g.: + # + # RSpec.describe UsersController, :type => :controller do + # # ... + # end + # + # The different available types are documented in the features, such as in + # https://relishapp.com/rspec/rspec-rails/docs + config.infer_spec_type_from_file_location! + + # Filter lines from Rails gems in backtraces. + config.filter_rails_from_backtrace! + # arbitrary gems may also be filtered via: + # config.filter_gems_from_backtrace("gem name") +end diff --git a/kiwi/spec/spec_helper.rb b/kiwi/spec/spec_helper.rb new file mode 100644 index 0000000..61e2738 --- /dev/null +++ b/kiwi/spec/spec_helper.rb @@ -0,0 +1,92 @@ +# This file was generated by the `rails generate rspec:install` command. Conventionally, all +# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. +# The generated `.rspec` file contains `--require spec_helper` which will cause +# this file to always be loaded, without a need to explicitly require it in any +# files. +# +# Given that it is always loaded, you are encouraged to keep this file as +# light-weight as possible. Requiring heavyweight dependencies from this file +# will add to the boot time of your test suite on EVERY test run, even for an +# individual file that may not need all of that loaded. Instead, consider making +# a separate helper file that requires the additional dependencies and performs +# the additional setup, and require it from the spec files that actually need +# it. +# +# The `.rspec` file also contains a few flags that are not defaults but that +# users commonly want. +# +# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + # rspec-expectations config goes here. You can use an alternate + # assertion/expectation library such as wrong or the stdlib/minitest + # assertions if you prefer. + config.expect_with :rspec do |expectations| + # This option will default to `true` in RSpec 4. It makes the `description` + # and `failure_message` of custom matchers include text for helper methods + # defined using `chain`, e.g.: + # be_bigger_than(2).and_smaller_than(4).description + # # => "be bigger than 2 and smaller than 4" + # ...rather than: + # # => "be bigger than 2" + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with :rspec do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = true + end + +# The settings below are suggested to provide a good initial experience +# with RSpec, but feel free to customize to your heart's content. +=begin + # These two settings work together to allow you to limit a spec run + # to individual examples or groups you care about by tagging them with + # `:focus` metadata. When nothing is tagged with `:focus`, all examples + # get run. + config.filter_run :focus + config.run_all_when_everything_filtered = true + + # Allows RSpec to persist some state between runs in order to support + # the `--only-failures` and `--next-failure` CLI options. We recommend + # you configure your source control system to ignore this file. + config.example_status_persistence_file_path = "spec/examples.txt" + + # Limits the available syntax to the non-monkey patched syntax that is + # recommended. For more details, see: + # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ + # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode + config.disable_monkey_patching! + + # Many RSpec users commonly either run the entire suite or an individual + # file, and it's useful to allow more verbose output when running an + # individual spec file. + if config.files_to_run.one? + # Use the documentation formatter for detailed output, + # unless a formatter has already been configured + # (e.g. via a command-line flag). + config.default_formatter = 'doc' + end + + # Print the 10 slowest examples and example groups at the + # end of the spec run, to help surface which specs are running + # particularly slow. + config.profile_examples = 10 + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = :random + + # Seed global randomization in this process using the `--seed` CLI option. + # Setting this allows you to use `--seed` to deterministically reproduce + # test failures related to randomization by passing the same `--seed` value + # as the one that triggered the failure. + Kernel.srand config.seed +=end +end From 656b5017186da44fb4a3433be8a10e1325358a0a Mon Sep 17 00:00:00 2001 From: deweydell Date: Sat, 23 Jan 2016 20:53:42 -0500 Subject: [PATCH 080/152] Create controller folder and questions controller spec file --- kiwi/spec/controllers/questions_controller_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 kiwi/spec/controllers/questions_controller_spec.rb diff --git a/kiwi/spec/controllers/questions_controller_spec.rb b/kiwi/spec/controllers/questions_controller_spec.rb new file mode 100644 index 0000000..e15a103 --- /dev/null +++ b/kiwi/spec/controllers/questions_controller_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe QuestionsController do + +end \ No newline at end of file From 1649a816047bb71a1eb94ee3e8368a851d6f949f Mon Sep 17 00:00:00 2001 From: deweydell Date: Sat, 23 Jan 2016 20:59:05 -0500 Subject: [PATCH 081/152] Create factories file with user and question --- kiwi/spec/factories.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 kiwi/spec/factories.rb diff --git a/kiwi/spec/factories.rb b/kiwi/spec/factories.rb new file mode 100644 index 0000000..58ff2ee --- /dev/null +++ b/kiwi/spec/factories.rb @@ -0,0 +1,12 @@ +FactoryGirl.define do + factory :user do + sequence(:name) {|n| "user#{n}" } + sequence(:email) {|n| "email#{n}@gmail.com" } + password "password" + end + + factory :question do + title { Faker::Lorem.sentence } + content { Faker::Lorem.paragraph } + end +end \ No newline at end of file From d10448c749a67d6056a2e62b7bd90b1b88057186 Mon Sep 17 00:00:00 2001 From: deweydell Date: Sat, 23 Jan 2016 21:13:26 -0500 Subject: [PATCH 082/152] Require rails_helper instead of spec_helper in QuestionsController spec --- kiwi/spec/controllers/questions_controller_spec.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/kiwi/spec/controllers/questions_controller_spec.rb b/kiwi/spec/controllers/questions_controller_spec.rb index e15a103..6ac8527 100644 --- a/kiwi/spec/controllers/questions_controller_spec.rb +++ b/kiwi/spec/controllers/questions_controller_spec.rb @@ -1,5 +1,13 @@ -require 'spec_helper' +require 'rails_helper' + + describe QuestionsController do + + it "#index" do + get :index + expect(assigns(:questions)).to eq Question.all + end + + -describe QuestionsController do end \ No newline at end of file From 17c1c05028b9a8de96a2e58164336a019bd0381e Mon Sep 17 00:00:00 2001 From: deweydell Date: Sat, 23 Jan 2016 21:21:00 -0500 Subject: [PATCH 083/152] Add work on questions controller spec for create route --- .../spec/controllers/questions_controller_spec.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/kiwi/spec/controllers/questions_controller_spec.rb b/kiwi/spec/controllers/questions_controller_spec.rb index 6ac8527..26821b0 100644 --- a/kiwi/spec/controllers/questions_controller_spec.rb +++ b/kiwi/spec/controllers/questions_controller_spec.rb @@ -7,7 +7,22 @@ expect(assigns(:questions)).to eq Question.all end + it "#new" do + get :new + end + + context "#create" do + it "creates a question with valid params" do + post :create, question: {title: "valid", content: "this is content"} + expect(Question.find(assigns[:question].id)).to be true + end + + it "doesn't create a question when params are invalid" do + post :create, question: {title: "invalid", content: ""} + expect(Question.find(assigns[:question].id)).to be false + end + end end \ No newline at end of file From fa3a8af6cb504e70d2eca3c6e076c7810d26a077 Mon Sep 17 00:00:00 2001 From: deweydell Date: Sat, 23 Jan 2016 22:02:12 -0500 Subject: [PATCH 084/152] Move some gems to specific test area per everyday rails article --- kiwi/Gemfile | 11 +++++++---- kiwi/Gemfile.lock | 5 ++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/kiwi/Gemfile b/kiwi/Gemfile index a8832dd..86ad64e 100644 --- a/kiwi/Gemfile +++ b/kiwi/Gemfile @@ -35,16 +35,19 @@ group :development, :test do gem 'byebug' gem 'pry-rails' gem 'pry-nav' - gem 'factory_girl' - gem 'launchy' - gem 'capybara' + gem 'factory_girl_rails' gem 'database_cleaner' gem 'better_errors' - gem 'faker' gem 'rspec-rails' gem 'shoulda-matchers' end +group :test do + gem 'launchy' + gem 'capybara' + gem 'faker' +end + group :development do # Access an IRB console on exception pages or by using <%= console %> in views gem 'web-console', '~> 2.0' diff --git a/kiwi/Gemfile.lock b/kiwi/Gemfile.lock index 51cdd10..d28deff 100644 --- a/kiwi/Gemfile.lock +++ b/kiwi/Gemfile.lock @@ -63,6 +63,9 @@ GEM execjs (2.6.0) factory_girl (4.5.0) activesupport (>= 3.0.0) + factory_girl_rails (4.5.0) + factory_girl (~> 4.5.0) + railties (>= 3.0.0) faker (1.6.1) i18n (~> 0.5) globalid (0.3.6) @@ -190,7 +193,7 @@ DEPENDENCIES byebug capybara database_cleaner - factory_girl + factory_girl_rails faker jbuilder (~> 2.0) jquery-rails From df815c3dd1607fbe7faba6a50bd20ae0a58daac2 Mon Sep 17 00:00:00 2001 From: deweydell Date: Sat, 23 Jan 2016 22:02:28 -0500 Subject: [PATCH 085/152] Require capybara in rails helper --- kiwi/spec/rails_helper.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/kiwi/spec/rails_helper.rb b/kiwi/spec/rails_helper.rb index 6f1ab14..4a83ee8 100644 --- a/kiwi/spec/rails_helper.rb +++ b/kiwi/spec/rails_helper.rb @@ -5,6 +5,7 @@ abort("The Rails environment is running in production mode!") if Rails.env.production? require 'spec_helper' require 'rspec/rails' +require "capybara/rspec" # Add additional requires below this line. Rails is not loaded until this point! # Requires supporting ruby files with custom matchers and macros, etc, in From 0f452ff66959cf4f875c3ca195c34a617ecdb3d6 Mon Sep 17 00:00:00 2001 From: deweydell Date: Sat, 23 Jan 2016 22:17:41 -0500 Subject: [PATCH 086/152] Write get index spec for questions controller --- .../controllers/questions_controller_spec.rb | 22 +++++-------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/kiwi/spec/controllers/questions_controller_spec.rb b/kiwi/spec/controllers/questions_controller_spec.rb index 26821b0..68829ea 100644 --- a/kiwi/spec/controllers/questions_controller_spec.rb +++ b/kiwi/spec/controllers/questions_controller_spec.rb @@ -2,26 +2,14 @@ describe QuestionsController do - it "#index" do - get :index - expect(assigns(:questions)).to eq Question.all - end - - it "#new" do - get :new - end + describe 'GET #index' do - context "#create" do - - it "creates a question with valid params" do - post :create, question: {title: "valid", content: "this is content"} - expect(Question.find(assigns[:question].id)).to be true + it "populates an array of questions" do + question = FactoryGirl.create(:question) + get :index + expect(assigns(:questions)).to eq([question]) end - it "doesn't create a question when params are invalid" do - post :create, question: {title: "invalid", content: ""} - expect(Question.find(assigns[:question].id)).to be false - end end From 7340e09a0bea0068f908c5a1b64d085e26785eb8 Mon Sep 17 00:00:00 2001 From: deweydell Date: Sat, 23 Jan 2016 22:18:38 -0500 Subject: [PATCH 087/152] Rewrite factory for question --- kiwi/spec/factories.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/kiwi/spec/factories.rb b/kiwi/spec/factories.rb index 58ff2ee..d7bb509 100644 --- a/kiwi/spec/factories.rb +++ b/kiwi/spec/factories.rb @@ -1,12 +1,12 @@ +#this may not be the correct way to generate FactoryGirl objects...still in progress + FactoryGirl.define do - factory :user do - sequence(:name) {|n| "user#{n}" } - sequence(:email) {|n| "email#{n}@gmail.com" } - password "password" - end - factory :question do - title { Faker::Lorem.sentence } - content { Faker::Lorem.paragraph } + factory :question do |f| + f.title { Faker::Lorem.sentence } + f.content { Faker::Lorem.paragraph } + f.user_id { rand(1..10) } end -end \ No newline at end of file + +end + From eb0161c403f7d389dd927d693279f37bc03985ed Mon Sep 17 00:00:00 2001 From: deweydell Date: Sat, 23 Jan 2016 22:18:49 -0500 Subject: [PATCH 088/152] Write basic specs for question model --- kiwi/spec/models/question_spec.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 kiwi/spec/models/question_spec.rb diff --git a/kiwi/spec/models/question_spec.rb b/kiwi/spec/models/question_spec.rb new file mode 100644 index 0000000..7530185 --- /dev/null +++ b/kiwi/spec/models/question_spec.rb @@ -0,0 +1,22 @@ +require 'rails_helper' + +describe Question do + let(:question) { FactoryGirl.create(:question) } + + it "has a valid factory" do + expect(question).to be_valid + end + + it "is invalid without a title" do + expect(FactoryGirl.build(:question, title: nil)).to_not be_valid + end + + it "is invalid without a content" do + expect(FactoryGirl.build(:question, content: nil)).to_not be_valid + end + + it "is invalid without a user_id" do + expect(FactoryGirl.build(:question, user_id: nil)).to_not be_valid + end + +end \ No newline at end of file From f3569704352cc67fe1eb58e9ddaf6f9173ba2fbe Mon Sep 17 00:00:00 2001 From: deweydell Date: Sat, 23 Jan 2016 22:49:10 -0500 Subject: [PATCH 089/152] Add belongs to user test to question model spec --- kiwi/spec/models/question_spec.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kiwi/spec/models/question_spec.rb b/kiwi/spec/models/question_spec.rb index 7530185..0466fe7 100644 --- a/kiwi/spec/models/question_spec.rb +++ b/kiwi/spec/models/question_spec.rb @@ -3,6 +3,8 @@ describe Question do let(:question) { FactoryGirl.create(:question) } + it { should belong_to(:user) } + it "has a valid factory" do expect(question).to be_valid end From a2fdc87d5f3a685f7d140f08fd90021ac355b120 Mon Sep 17 00:00:00 2001 From: deweydell Date: Sat, 23 Jan 2016 22:49:45 -0500 Subject: [PATCH 090/152] Add get #show and post #create tests to QuestionsController spec --- .../controllers/questions_controller_spec.rb | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/kiwi/spec/controllers/questions_controller_spec.rb b/kiwi/spec/controllers/questions_controller_spec.rb index 68829ea..56ea054 100644 --- a/kiwi/spec/controllers/questions_controller_spec.rb +++ b/kiwi/spec/controllers/questions_controller_spec.rb @@ -10,7 +10,59 @@ expect(assigns(:questions)).to eq([question]) end + it "renders the :index view" do + get :index + expect(response).to render_template :index + end + + end + + describe "GET #show" do + + it "assigns the requested question to @question" do + question = FactoryGirl.create(:question) + get :show, id: question + expect(assigns(:question)).to eq(question) + end + + it "renders the #show view" do + get :show, id: FactoryGirl.create(:question) + expect(response).to render_template :show + end + + end + +#Still Working on pending tests below + + describe "POST create" do + + context "with valid attributes" do + + xit "creates a new question" do + post :create, question: {title: "Title", Content: "Content", user_id: 1 } + expect(assigns(:question)).to change(Question, :count).by(1) + end + + xit "redirects to the new question" do + post :create, question: FactoryGirl.attributes_for(:question) + expect(response).to redirect_to Question.last + end + end + context "with invalid attributes" do + xit "does not save the new question" do + expect{ + post :create, question: FactoryGirl.attributes_for(:question) + }.to_not change(Question, :count) + end + + xit "re-renders the new method" do + post :create, question: FactoryGirl.attributes_for(:question) + expect(response).to render_template :new + end + end + end + end \ No newline at end of file From 3871293014632500b3356544716694008ca219ab Mon Sep 17 00:00:00 2001 From: Zach Schatz Date: Sat, 23 Jan 2016 15:18:05 -0500 Subject: [PATCH 091/152] Create answers controller, setup view to add answers form --- kiwi/app/controllers/answers_controller.rb | 7 +++++++ kiwi/app/views/questions/show.html.erb | 15 +++++++++++++++ kiwi/config/routes.rb | 8 +++++--- 3 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 kiwi/app/controllers/answers_controller.rb diff --git a/kiwi/app/controllers/answers_controller.rb b/kiwi/app/controllers/answers_controller.rb new file mode 100644 index 0000000..452b115 --- /dev/null +++ b/kiwi/app/controllers/answers_controller.rb @@ -0,0 +1,7 @@ +class AnswersController < ApplicationController + +def new + binding.pry +end + +end \ No newline at end of file diff --git a/kiwi/app/views/questions/show.html.erb b/kiwi/app/views/questions/show.html.erb index 94d2eb8..f29bd3a 100644 --- a/kiwi/app/views/questions/show.html.erb +++ b/kiwi/app/views/questions/show.html.erb @@ -49,3 +49,18 @@ <% end %> <% end %> + + +
    + +
    +<%= form_for[@question, @answer] %> +
    +
    + <%= f.text_area :content %> +
    + + <%= f.submit 'Post Answer' %> +
    +<% end %> +
    diff --git a/kiwi/config/routes.rb b/kiwi/config/routes.rb index dce7672..1dd0567 100644 --- a/kiwi/config/routes.rb +++ b/kiwi/config/routes.rb @@ -4,9 +4,11 @@ # resources :questions, only: [:new, :edit, :index] # end - resources :questions do - # resources :answers, only: [:new, :show, :delete, :edit] - resources :comments, only: [:new, :create] + + resources :questions do + resources :answers, only: [:new] + resources :comments, only: [:new, :create] + # resources :comments, only: [:new, :show] end # resources :answers do From 37a74355b99dd444fd6c38b2baee843507badb20 Mon Sep 17 00:00:00 2001 From: Zach Schatz Date: Sat, 23 Jan 2016 17:24:07 -0500 Subject: [PATCH 092/152] Include answers controller to create answer, and add new answer to question show --- kiwi/app/controllers/answers_controller.rb | 16 ++++++++++++++++ kiwi/app/controllers/questions_controller.rb | 1 + 2 files changed, 17 insertions(+) diff --git a/kiwi/app/controllers/answers_controller.rb b/kiwi/app/controllers/answers_controller.rb index 452b115..c8abffc 100644 --- a/kiwi/app/controllers/answers_controller.rb +++ b/kiwi/app/controllers/answers_controller.rb @@ -1,7 +1,23 @@ class AnswersController < ApplicationController def new + @answer = Answer.new +end + +def create binding.pry + @answer = Answer.new(answer_params) + if @answer.save + redirect_to question_path(id: @answer.question_id) + else + alert("An Error has Occurred!") + end +end + +private + +def answer_params + params.require(:answer).permit(:content,:user_id,:question_id) end end \ No newline at end of file diff --git a/kiwi/app/controllers/questions_controller.rb b/kiwi/app/controllers/questions_controller.rb index fd3d828..71b0565 100644 --- a/kiwi/app/controllers/questions_controller.rb +++ b/kiwi/app/controllers/questions_controller.rb @@ -22,6 +22,7 @@ def create def show @question = Question.includes(:answers, :user).find(params[:id]) + @answer = Answer.new end def edit From 566925879c4636cd84ef9522832e0e41015c8186 Mon Sep 17 00:00:00 2001 From: Zach Schatz Date: Sat, 23 Jan 2016 17:24:30 -0500 Subject: [PATCH 093/152] Revise show page to include form --- kiwi/app/views/answers/new.html.erb | 1 + kiwi/app/views/questions/show.html.erb | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 kiwi/app/views/answers/new.html.erb diff --git a/kiwi/app/views/answers/new.html.erb b/kiwi/app/views/answers/new.html.erb new file mode 100644 index 0000000..596b876 --- /dev/null +++ b/kiwi/app/views/answers/new.html.erb @@ -0,0 +1 @@ +new.html.erb \ No newline at end of file diff --git a/kiwi/app/views/questions/show.html.erb b/kiwi/app/views/questions/show.html.erb index f29bd3a..628cce8 100644 --- a/kiwi/app/views/questions/show.html.erb +++ b/kiwi/app/views/questions/show.html.erb @@ -53,13 +53,17 @@
    +

    Your Answer

    -<%= form_for[@question, @answer] %> +<%= form_for [@question, @question.answers.build] do |f| %>
    <%= f.text_area :content %> + <%= f.hidden_field :question_id %> + <% if logged_in? %> + <%= f.hidden_field :user_id, :value => current_user.id %> + <% end %>
    - <%= f.submit 'Post Answer' %>
    <% end %> From 4112f8d08d9fbc787c9a2e5e354f710cba66c7c2 Mon Sep 17 00:00:00 2001 From: Zach Schatz Date: Sat, 23 Jan 2016 17:24:48 -0500 Subject: [PATCH 094/152] Edit routes --- kiwi/config/routes.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/kiwi/config/routes.rb b/kiwi/config/routes.rb index 1dd0567..64f531e 100644 --- a/kiwi/config/routes.rb +++ b/kiwi/config/routes.rb @@ -6,10 +6,9 @@ resources :questions do - resources :answers, only: [:new] resources :comments, only: [:new, :create] - # resources :comments, only: [:new, :show] - end + resources :answers, only: [:create] + end # resources :answers do # resources :comments, only: [:new, :show] From 64e7da3f90d78050be7f2e65703d1030d28fd919 Mon Sep 17 00:00:00 2001 From: Zach Schatz Date: Sun, 24 Jan 2016 11:26:49 -0500 Subject: [PATCH 095/152] Redirect to login_path if user is not logged in --- kiwi/app/controllers/answers_controller.rb | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/kiwi/app/controllers/answers_controller.rb b/kiwi/app/controllers/answers_controller.rb index c8abffc..0e07916 100644 --- a/kiwi/app/controllers/answers_controller.rb +++ b/kiwi/app/controllers/answers_controller.rb @@ -1,16 +1,12 @@ class AnswersController < ApplicationController - -def new - @answer = Answer.new -end - +add_flash_types(:alert) def create - binding.pry @answer = Answer.new(answer_params) if @answer.save redirect_to question_path(id: @answer.question_id) else - alert("An Error has Occurred!") + #error to be handled by ajax + redirect_to login_path end end From 99067af7543a82b57a68ed962a0f780a1886a56d Mon Sep 17 00:00:00 2001 From: Zach Schatz Date: Sun, 24 Jan 2016 11:40:54 -0500 Subject: [PATCH 096/152] Create answer form partial & remove answer link on show page --- kiwi/app/views/answers/_form.html.erb | 12 ++++++++++++ kiwi/app/views/questions/show.html.erb | 14 +------------- 2 files changed, 13 insertions(+), 13 deletions(-) create mode 100644 kiwi/app/views/answers/_form.html.erb diff --git a/kiwi/app/views/answers/_form.html.erb b/kiwi/app/views/answers/_form.html.erb new file mode 100644 index 0000000..413c6e1 --- /dev/null +++ b/kiwi/app/views/answers/_form.html.erb @@ -0,0 +1,12 @@ +<%= form_for [@question, @question.answers.build] do |f| %> +
    +
    + <%= f.text_area :content %> + <%= f.hidden_field :question_id %> + <% if logged_in? %> + <%= f.hidden_field :user_id, :value => current_user.id %> + <% end %> +
    + <%= f.submit 'Post Answer' %> +
    +<% end %> diff --git a/kiwi/app/views/questions/show.html.erb b/kiwi/app/views/questions/show.html.erb index 628cce8..e4f84ac 100644 --- a/kiwi/app/views/questions/show.html.erb +++ b/kiwi/app/views/questions/show.html.erb @@ -15,7 +15,6 @@ <% if logged_in? %>
    • <%= link_to "Add Comment", new_question_comment_path(@question.id) %>
    • -
    • <%= link_to "Add Answer", new_question_path %>
    <% end %> @@ -55,16 +54,5 @@

    Your Answer

    -<%= form_for [@question, @question.answers.build] do |f| %> -
    -
    - <%= f.text_area :content %> - <%= f.hidden_field :question_id %> - <% if logged_in? %> - <%= f.hidden_field :user_id, :value => current_user.id %> - <% end %> -
    - <%= f.submit 'Post Answer' %> -
    -<% end %> +<%= render "answers/form" %>
    From 7a7c31cf05126e11fa7746eb360d6fae55f01581 Mon Sep 17 00:00:00 2001 From: Zach Schatz Date: Sun, 24 Jan 2016 14:05:29 -0500 Subject: [PATCH 097/152] Working to implement votes for question --- kiwi/app/assets/javascripts/vote.js | 8 ++++++++ kiwi/app/assets/stylesheets/main.css | 20 +++++++++++++++++++- kiwi/app/controllers/votes_controller.rb | 20 ++++++++++++++++++++ kiwi/app/models/concerns/votable.rb | 14 ++++++++++++++ kiwi/app/models/question.rb | 3 +++ kiwi/app/models/vote.rb | 2 ++ kiwi/app/views/questions/show.html.erb | 7 +++++++ kiwi/config/routes.rb | 1 + 8 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 kiwi/app/assets/javascripts/vote.js create mode 100644 kiwi/app/controllers/votes_controller.rb create mode 100644 kiwi/app/models/concerns/votable.rb diff --git a/kiwi/app/assets/javascripts/vote.js b/kiwi/app/assets/javascripts/vote.js new file mode 100644 index 0000000..8499c93 --- /dev/null +++ b/kiwi/app/assets/javascripts/vote.js @@ -0,0 +1,8 @@ +$(document).on("ready", function(){ + + // $('#up-vote').on("click", function(event){ + // event.preventDefault(); + // }) + + +}) \ No newline at end of file diff --git a/kiwi/app/assets/stylesheets/main.css b/kiwi/app/assets/stylesheets/main.css index 933ebd9..b4d68d1 100644 --- a/kiwi/app/assets/stylesheets/main.css +++ b/kiwi/app/assets/stylesheets/main.css @@ -73,7 +73,7 @@ main { .answer { padding: 1em; margin-bottom: 1em; - background-color: #E0EEE0; + background-color: #E0EEE0; } form, #new-user { @@ -135,5 +135,23 @@ label{ margin-right: 1em; } +.vote-container { + height: 4em; + width: 2em; + border: 1px solid red; + float: left; + margin-right: 1em; + font-size: .5em; +} + +.vote-attributes{ + list-style: none; + text-align: center; + width: 100%; + height: 100%; + margin: auto 0; + padding: 0; + /*list-style-position: inside;*/ +} diff --git a/kiwi/app/controllers/votes_controller.rb b/kiwi/app/controllers/votes_controller.rb new file mode 100644 index 0000000..361a039 --- /dev/null +++ b/kiwi/app/controllers/votes_controller.rb @@ -0,0 +1,20 @@ +class VotesController < ApplicationController + + def create + binding.pry + @vote = Vote.new(vote_params) + if @vote.save + redirect_to question_path(id: @vote.question_id) + else + redirect_to login_path + end + end + +private + +def vote_params + params.require(:vote).permit(:direction,:user_id, :question_id) +end + + +end \ No newline at end of file diff --git a/kiwi/app/models/concerns/votable.rb b/kiwi/app/models/concerns/votable.rb new file mode 100644 index 0000000..a4c3c11 --- /dev/null +++ b/kiwi/app/models/concerns/votable.rb @@ -0,0 +1,14 @@ +module Votable + + extend ActiveSupport::Concern + + included do + has_many :votes, as: :votable + end + + attr_accessor :votable_attributes + # accepts_nested_attributes_for :votable + +end + +ActiveRecord::Base.send(:include, Votable) \ No newline at end of file diff --git a/kiwi/app/models/question.rb b/kiwi/app/models/question.rb index 8ac844f..e1201f4 100644 --- a/kiwi/app/models/question.rb +++ b/kiwi/app/models/question.rb @@ -1,4 +1,7 @@ class Question < ActiveRecord::Base + + include Votable + belongs_to :user has_many :answers has_many :comments, as: :commentable diff --git a/kiwi/app/models/vote.rb b/kiwi/app/models/vote.rb index 40d7f04..f06297b 100644 --- a/kiwi/app/models/vote.rb +++ b/kiwi/app/models/vote.rb @@ -1,4 +1,6 @@ class Vote < ActiveRecord::Base + include Votable + belongs_to :user belongs_to :votable, polymorphic: true diff --git a/kiwi/app/views/questions/show.html.erb b/kiwi/app/views/questions/show.html.erb index e4f84ac..b997711 100644 --- a/kiwi/app/views/questions/show.html.erb +++ b/kiwi/app/views/questions/show.html.erb @@ -1,5 +1,12 @@

    <%= @question.title %>

    +
    +
      +
    • <%= link_to '▲'.html_safe, question_votes_path(@question.id, vote: {direction: '1', question_id: @question.id, user_id: current_user.id}), method: :post %>
    • +
    • <%= @question.votes.count %>
    • +
    • <%= link_to '▼'.html_safe %>
    • +
    +

    Asked by <%= @question.user.name %> on <%= std_format_date(@question.created_at) %>

    <%= @question.content %>

    <% if !@question.comments.empty? %> diff --git a/kiwi/config/routes.rb b/kiwi/config/routes.rb index 64f531e..16cfb59 100644 --- a/kiwi/config/routes.rb +++ b/kiwi/config/routes.rb @@ -8,6 +8,7 @@ resources :questions do resources :comments, only: [:new, :create] resources :answers, only: [:create] + resources :votes, only: [ :create, :destroy ] end # resources :answers do From b8b8601be9b36ce4c58a856debd48ec775d3bc8c Mon Sep 17 00:00:00 2001 From: Nicola Beuscher Date: Sun, 24 Jan 2016 14:23:02 -0500 Subject: [PATCH 098/152] Remove test directory files since we are using rspec --- kiwi/test/controllers/.keep | 0 kiwi/test/controllers/question_controller_test.rb | 7 ------- kiwi/test/fixtures/.keep | 0 kiwi/test/fixtures/comments.yml | 11 ----------- kiwi/test/fixtures/questions.yml | 13 ------------- kiwi/test/fixtures/votes.yml | 11 ----------- kiwi/test/helpers/.keep | 0 kiwi/test/integration/.keep | 0 kiwi/test/mailers/.keep | 0 kiwi/test/models/.keep | 0 kiwi/test/models/comment_test.rb | 7 ------- kiwi/test/models/question_test.rb | 7 ------- kiwi/test/models/vote_test.rb | 7 ------- kiwi/test/test_helper.rb | 10 ---------- 14 files changed, 73 deletions(-) delete mode 100644 kiwi/test/controllers/.keep delete mode 100644 kiwi/test/controllers/question_controller_test.rb delete mode 100644 kiwi/test/fixtures/.keep delete mode 100644 kiwi/test/fixtures/comments.yml delete mode 100644 kiwi/test/fixtures/questions.yml delete mode 100644 kiwi/test/fixtures/votes.yml delete mode 100644 kiwi/test/helpers/.keep delete mode 100644 kiwi/test/integration/.keep delete mode 100644 kiwi/test/mailers/.keep delete mode 100644 kiwi/test/models/.keep delete mode 100644 kiwi/test/models/comment_test.rb delete mode 100644 kiwi/test/models/question_test.rb delete mode 100644 kiwi/test/models/vote_test.rb delete mode 100644 kiwi/test/test_helper.rb diff --git a/kiwi/test/controllers/.keep b/kiwi/test/controllers/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/kiwi/test/controllers/question_controller_test.rb b/kiwi/test/controllers/question_controller_test.rb deleted file mode 100644 index 0d272cf..0000000 --- a/kiwi/test/controllers/question_controller_test.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'test_helper' - -class QuestionControllerTest < ActionController::TestCase - # test "the truth" do - # assert true - # end -end diff --git a/kiwi/test/fixtures/.keep b/kiwi/test/fixtures/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/kiwi/test/fixtures/comments.yml b/kiwi/test/fixtures/comments.yml deleted file mode 100644 index 937a0c0..0000000 --- a/kiwi/test/fixtures/comments.yml +++ /dev/null @@ -1,11 +0,0 @@ -# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html - -# This model initially had no columns defined. If you add columns to the -# model remove the '{}' from the fixture names and add the columns immediately -# below each fixture, per the syntax in the comments below -# -one: {} -# column: value -# -two: {} -# column: value diff --git a/kiwi/test/fixtures/questions.yml b/kiwi/test/fixtures/questions.yml deleted file mode 100644 index f34db3e..0000000 --- a/kiwi/test/fixtures/questions.yml +++ /dev/null @@ -1,13 +0,0 @@ -# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html - -one: - title: MyString - content: MyText - user_id: 1 - best_answer_id: 1 - -two: - title: MyString - content: MyText - user_id: 1 - best_answer_id: 1 diff --git a/kiwi/test/fixtures/votes.yml b/kiwi/test/fixtures/votes.yml deleted file mode 100644 index 937a0c0..0000000 --- a/kiwi/test/fixtures/votes.yml +++ /dev/null @@ -1,11 +0,0 @@ -# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html - -# This model initially had no columns defined. If you add columns to the -# model remove the '{}' from the fixture names and add the columns immediately -# below each fixture, per the syntax in the comments below -# -one: {} -# column: value -# -two: {} -# column: value diff --git a/kiwi/test/helpers/.keep b/kiwi/test/helpers/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/kiwi/test/integration/.keep b/kiwi/test/integration/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/kiwi/test/mailers/.keep b/kiwi/test/mailers/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/kiwi/test/models/.keep b/kiwi/test/models/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/kiwi/test/models/comment_test.rb b/kiwi/test/models/comment_test.rb deleted file mode 100644 index b6d6131..0000000 --- a/kiwi/test/models/comment_test.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'test_helper' - -class CommentTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end -end diff --git a/kiwi/test/models/question_test.rb b/kiwi/test/models/question_test.rb deleted file mode 100644 index 88f6ea7..0000000 --- a/kiwi/test/models/question_test.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'test_helper' - -class QuestionTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end -end diff --git a/kiwi/test/models/vote_test.rb b/kiwi/test/models/vote_test.rb deleted file mode 100644 index f31f992..0000000 --- a/kiwi/test/models/vote_test.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'test_helper' - -class VoteTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end -end diff --git a/kiwi/test/test_helper.rb b/kiwi/test/test_helper.rb deleted file mode 100644 index 92e39b2..0000000 --- a/kiwi/test/test_helper.rb +++ /dev/null @@ -1,10 +0,0 @@ -ENV['RAILS_ENV'] ||= 'test' -require File.expand_path('../../config/environment', __FILE__) -require 'rails/test_help' - -class ActiveSupport::TestCase - # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. - fixtures :all - - # Add more helper methods to be used by all tests here... -end From f65b3248d5b7b1414dda1558d9285bb030b95e1f Mon Sep 17 00:00:00 2001 From: Nicola Beuscher Date: Sun, 24 Jan 2016 14:51:00 -0500 Subject: [PATCH 099/152] Move faker gem into development and test group --- kiwi/Gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kiwi/Gemfile b/kiwi/Gemfile index 86ad64e..6803041 100644 --- a/kiwi/Gemfile +++ b/kiwi/Gemfile @@ -40,12 +40,12 @@ group :development, :test do gem 'better_errors' gem 'rspec-rails' gem 'shoulda-matchers' + gem 'faker' end group :test do gem 'launchy' gem 'capybara' - gem 'faker' end group :development do From cbb5742267bf11fd9c0b51097d0b7f48f7e60833 Mon Sep 17 00:00:00 2001 From: Nicola Beuscher Date: Sun, 24 Jan 2016 14:51:33 -0500 Subject: [PATCH 100/152] Create longer content for questions and more answers in seeds --- kiwi/db/seeds.rb | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/kiwi/db/seeds.rb b/kiwi/db/seeds.rb index 35e2602..59f54b3 100644 --- a/kiwi/db/seeds.rb +++ b/kiwi/db/seeds.rb @@ -10,18 +10,26 @@ luis = User.create(name: "luis_dbc", email: "luis@example.com", password: "password") zach = User.create(name: "zach_dbc", email: "zach@example.com", password: "password") jeff = User.create(name: "jeff_dbc", email: "jeff@example.com", password: "password") +hacker_bot = User.create(name: "hacker_bot", email: "hacker_bot@example.com", password: "password") -rails_q = Question.create(title: "How do I build a Rails app?", content: "Wondering how to do this", user: nicola) -sinatra_q =Question.create(title: "How do I build a Sinatra app?", content: "Wondering how to do this", user: luis) -mvc_q = Question.create(title: "What is MVC?", content: "design patterns, yo", user: zach) -css_q = Question.create(title: "What are the best CSS styles?", content: "Should I style things?", user: jeff) +rails_q = Question.create(title: "How do I build a Rails app?", content: Faker::Lorem.paragraph(10), user: nicola) +sinatra_q =Question.create(title: "How do I build a Sinatra app?", content: Faker::Lorem.paragraph(10), user: luis) +mvc_q = Question.create(title: "What is MVC?", content: Faker::Lorem.paragraph(10), user: zach) +css_q = Question.create(title: "What are the best CSS styles?", content: Faker::Lorem.paragraph(10), user: jeff) answer = Answer.create(content:"It's super easy just follow the rails guide.", user: luis, question: rails_q) Answer.create(content:"Just ask someone from phase 2.", user: jeff, question: sinatra_q) -Answer.create(content:"Use the sinatra gem and make restful routes.", user: luis, question: sinatra_q) -Answer.create(content:"Model, view, controller, duh.", user: nicola, question: mvc_q) +Answer.create(content:"Use the sinatra gem and make restful routes.", user: nicola, question: sinatra_q) +Answer.create(content:"Model, view, controller, duh.", user: zach, question: mvc_q) + +Answer.create(content: Faker::Hacker.say_something_smart, user: hacker_bot, question: rails_q) +Answer.create(content: Faker::Hacker.say_something_smart, user: hacker_bot, question: sinatra_q) +Answer.create(content: Faker::Hacker.say_something_smart, user: hacker_bot, question: mvc_q) +Answer.create(content: Faker::Hacker.say_something_smart, user: hacker_bot, question: css_q) Comment.create(content:"Can you explain what you mean by that?", commentable_type: "Question", commentable_id: rails_q.id, user: luis) Comment.create(content:"I don't understand the question", commentable_type: "Question", commentable_id: css_q.id, user: nicola) Comment.create(content:"This is a great answer.", commentable_type: "Answer", commentable_id: answer.id, user: jeff) Comment.create(content:"This should be voted best answer for sure.", commentable_type: "Answer", commentable_id: answer.id, user: zach) + + From 18897f7225f4a8a142bda4a7eb691b57e47afa52 Mon Sep 17 00:00:00 2001 From: Nicola Beuscher Date: Sun, 24 Jan 2016 14:52:14 -0500 Subject: [PATCH 101/152] Make question show page easier to read with more css --- kiwi/app/assets/stylesheets/main.css | 13 ++++++++--- kiwi/app/views/questions/show.html.erb | 30 ++++++++++++++------------ 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/kiwi/app/assets/stylesheets/main.css b/kiwi/app/assets/stylesheets/main.css index 933ebd9..43b636b 100644 --- a/kiwi/app/assets/stylesheets/main.css +++ b/kiwi/app/assets/stylesheets/main.css @@ -70,10 +70,14 @@ main { border-bottom: 1px solid gray; } +.answers-list { + margin-top: 4em; +} + .answer { padding: 1em; margin-bottom: 1em; - background-color: #E0EEE0; + background-color: #E0EEE0; } form, #new-user { @@ -99,7 +103,6 @@ textarea { .comment { width: 100%; - text-indent: 2em; color: gray; } @@ -135,5 +138,9 @@ label{ margin-right: 1em; } - +.user-time { + font-size: 13px; + color: green; + font-weight: 300; +} diff --git a/kiwi/app/views/questions/show.html.erb b/kiwi/app/views/questions/show.html.erb index e4f84ac..5eefc53 100644 --- a/kiwi/app/views/questions/show.html.erb +++ b/kiwi/app/views/questions/show.html.erb @@ -1,12 +1,20 @@

    <%= @question.title %>

    -

    Asked by <%= @question.user.name %> on <%= std_format_date(@question.created_at) %>

    + Asked by <%= @question.user.name %> <%= std_format_date(@question.created_at) %>

    <%= @question.content %>

    + +
      + <% if current_user == @question.user %> +
    • <%= link_to "Edit my question", edit_question_path %>
    • +
    • <%= link_to "Delete my question", question_path, method: :delete %>
    • + <% end %> +
    + <% if !@question.comments.empty? %> -
    Comments:
    -
    +
    +
    Comments:
    <% @question.comments.each do |comment| %> -

    <%= comment.content %> • <%= comment.user.name %> on <%= std_format_date(comment.created_at) %> • <% if current_user == comment.user %><%= link_to "Edit", edit_comment_path(comment.id) %> • <%= link_to "Delete",comment_path(comment.id), method: :delete %><% end %>

    +

    <%= comment.content %> • <%= comment.user.name %> <%= std_format_date(comment.created_at) %> <% if current_user == comment.user %><%= link_to "Edit", edit_comment_path(comment.id) %> • <%= link_to "Delete",comment_path(comment.id), method: :delete %><% end %>

    <% end %>
    <% end %> @@ -18,26 +26,20 @@ <% end %> -
      - <% if current_user == @question.user %> -
    • <%= link_to "Edit my question", edit_question_path %>
    • -
    • <%= link_to "Delete my question", question_path, method: :delete %>
    • - <% end %> -
    <% if !@question.answers.empty? %> -

    Answers: <%=@question.answers.count %>

    +

    Answers: <%=@question.answers.count %>

    <% @question.answers.each do |answer|%>
    -

    Answered by <%= answer.user.name %> on <%= std_format_date(answer.created_at) %>

    + Answered by <%= answer.user.name %> <%= std_format_date(answer.created_at) %>

    <%= answer.content %>

    <% if !answer.comments.empty? %>
    Comments:
    <% answer.comments.each do |comment| %>

    <%= comment.content %> • - <%= comment.user.name %> on <%= std_format_date(comment.created_at) %>

    + <%= comment.user.name %> <%= std_format_date(comment.created_at) %>

    <% end %>
    <% end %> @@ -52,7 +54,7 @@
    -

    Your Answer

    +

    Add Your Answer:

    <%= render "answers/form" %>
    From 9c7c13d62561dfa86b6b5b6eb0134e39579f3924 Mon Sep 17 00:00:00 2001 From: Luis Fernando Plaz Date: Sun, 24 Jan 2016 15:42:56 -0500 Subject: [PATCH 102/152] Create working upvote button --- kiwi/app/controllers/votes_controller.rb | 27 ++++++++++++++++++------ kiwi/app/models/concerns/votable.rb | 18 ++++++++-------- kiwi/app/models/question.rb | 2 +- kiwi/app/models/vote.rb | 2 +- kiwi/app/views/questions/show.html.erb | 3 ++- 5 files changed, 33 insertions(+), 19 deletions(-) diff --git a/kiwi/app/controllers/votes_controller.rb b/kiwi/app/controllers/votes_controller.rb index 361a039..ee13273 100644 --- a/kiwi/app/controllers/votes_controller.rb +++ b/kiwi/app/controllers/votes_controller.rb @@ -1,20 +1,33 @@ class VotesController < ApplicationController + before_action :load_votable def create - binding.pry - @vote = Vote.new(vote_params) - if @vote.save - redirect_to question_path(id: @vote.question_id) + @vote = current_user.votes.new(vote_params) + if !!params[:question_id] + @vote.votable_type = "Question" + @vote.votable_id = params[:question_id] + @vote.save + redirect_to question_path(id: @vote.votable_id) + elsif !!params[:answer_id] + @vote.votable_type = "Answer" + @vote.votable_id = params[:answer_id] else - redirect_to login_path + @vote.votable_type = "Comment" + @vote.votable_id = params[:comment_id] end end private def vote_params - params.require(:vote).permit(:direction,:user_id, :question_id) + params.require(:vote).permit(:direction,:user_id) +end + +def load_votable + # binding.pry + resource, id = request.path.split('/')[1,2] + @votable = resource.singularize.classify.constantize.find(id) end -end \ No newline at end of file +end diff --git a/kiwi/app/models/concerns/votable.rb b/kiwi/app/models/concerns/votable.rb index a4c3c11..5266853 100644 --- a/kiwi/app/models/concerns/votable.rb +++ b/kiwi/app/models/concerns/votable.rb @@ -1,14 +1,14 @@ -module Votable +# module Votable - extend ActiveSupport::Concern +# extend ActiveSupport::Concern - included do - has_many :votes, as: :votable - end +# included do +# has_many :votes, as: :votable +# end - attr_accessor :votable_attributes - # accepts_nested_attributes_for :votable +# attr_accessor :votable_attributes +# # accepts_nested_attributes_for :votable -end +# end -ActiveRecord::Base.send(:include, Votable) \ No newline at end of file +# ActiveRecord::Base.send(:include, Votable) diff --git a/kiwi/app/models/question.rb b/kiwi/app/models/question.rb index e1201f4..a3479bb 100644 --- a/kiwi/app/models/question.rb +++ b/kiwi/app/models/question.rb @@ -1,6 +1,6 @@ class Question < ActiveRecord::Base - include Votable + # include Votable belongs_to :user has_many :answers diff --git a/kiwi/app/models/vote.rb b/kiwi/app/models/vote.rb index f06297b..fed4a27 100644 --- a/kiwi/app/models/vote.rb +++ b/kiwi/app/models/vote.rb @@ -1,5 +1,5 @@ class Vote < ActiveRecord::Base - include Votable + # include Votable belongs_to :user belongs_to :votable, polymorphic: true diff --git a/kiwi/app/views/questions/show.html.erb b/kiwi/app/views/questions/show.html.erb index a1e8c91..1e3818b 100644 --- a/kiwi/app/views/questions/show.html.erb +++ b/kiwi/app/views/questions/show.html.erb @@ -3,11 +3,12 @@
      -
    • <%= link_to '▲'.html_safe, question_votes_path(@question.id, vote: {direction: '1', question_id: @question.id, user_id: current_user.id}), method: :post %>
    • +
    • <%= link_to '▲'.html_safe, question_votes_path(@question, vote: {direction: '1'}), method: :post %>
    • <%= @question.votes.count %>
    • <%= link_to '▼'.html_safe %>
    + Asked by <%= @question.user.name %> <%= std_format_date(@question.created_at) %>

    <%= @question.content %>

    From 196bfc9cc78d3fc925cea74bb45b1a1671aea762 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sun, 24 Jan 2016 15:40:59 -0500 Subject: [PATCH 103/152] Implement new_answer_comment_path --- kiwi/app/controllers/comments_controller.rb | 10 ++++++++-- kiwi/app/views/comments/_form.html.erb | 2 +- kiwi/app/views/questions/show.html.erb | 2 +- kiwi/config/routes.rb | 4 ++++ 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/kiwi/app/controllers/comments_controller.rb b/kiwi/app/controllers/comments_controller.rb index 871b96b..549d91c 100644 --- a/kiwi/app/controllers/comments_controller.rb +++ b/kiwi/app/controllers/comments_controller.rb @@ -1,8 +1,14 @@ class CommentsController < ApplicationController def new - @question = Question.find_by(id: params[:question_id]) - @comment = @question.comments.new + # binding.pry + if params[:question_id] + @commentable = Question.find_by(id: params[:question_id]) + @comment = @commentable.comments.new + else + @commentable = Answer.find_by(id: params[:answer_id]) + @comment = @commentable.comments.new + end end def create diff --git a/kiwi/app/views/comments/_form.html.erb b/kiwi/app/views/comments/_form.html.erb index 7c19d36..d409596 100644 --- a/kiwi/app/views/comments/_form.html.erb +++ b/kiwi/app/views/comments/_form.html.erb @@ -1,4 +1,4 @@ -<%= form_for [@question, @comment] do |f| %> +<%= form_for [@commentable, @comment] do |f| %>
    diff --git a/kiwi/app/views/questions/show.html.erb b/kiwi/app/views/questions/show.html.erb index 1e3818b..e802cf4 100644 --- a/kiwi/app/views/questions/show.html.erb +++ b/kiwi/app/views/questions/show.html.erb @@ -54,7 +54,7 @@
    <% end %> <% if logged_in? %> - <%= link_to "Add Comment", new_question_path %> + <%= link_to "Add Comment", new_answer_comment_path(answer.id) %> <% end %>
    <% end %> diff --git a/kiwi/config/routes.rb b/kiwi/config/routes.rb index 16cfb59..530ed97 100644 --- a/kiwi/config/routes.rb +++ b/kiwi/config/routes.rb @@ -11,6 +11,10 @@ resources :votes, only: [ :create, :destroy ] end + resources :answers do + resources :comments, only: [:new, :create] + end + # resources :answers do # resources :comments, only: [:new, :show] # end From 8bc456ff4d6f3488cdebc290c6cb7750c5aaaf7c Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sun, 24 Jan 2016 15:43:16 -0500 Subject: [PATCH 104/152] Remove comments, bindings from comment_controller --- kiwi/app/controllers/comments_controller.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/kiwi/app/controllers/comments_controller.rb b/kiwi/app/controllers/comments_controller.rb index 549d91c..a5393a9 100644 --- a/kiwi/app/controllers/comments_controller.rb +++ b/kiwi/app/controllers/comments_controller.rb @@ -1,7 +1,6 @@ class CommentsController < ApplicationController def new - # binding.pry if params[:question_id] @commentable = Question.find_by(id: params[:question_id]) @comment = @commentable.comments.new @@ -20,7 +19,6 @@ def create @comment = answer.comments.new(user: current_user, content: params[:comment][:content]) end if @comment.save - # We may need a conditional here once we have comments to answers as well redirect_to question_path(id: @comment.commentable_id) else render :new @@ -35,7 +33,6 @@ def edit def update @comment = Comment.find_by(id: params[:id]) if @comment.update_attributes(comment_params) - # We may need a conditional here once we have comments to answers as well redirect_to question_path(id: @comment.commentable_id) else render :edit From ebc20ea4df2152bd19e88b82734bdd6e2640ee6c Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sun, 24 Jan 2016 15:46:36 -0500 Subject: [PATCH 105/152] Add links to edit and delete answer comments on questions#show --- kiwi/app/views/questions/show.html.erb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kiwi/app/views/questions/show.html.erb b/kiwi/app/views/questions/show.html.erb index e802cf4..ac50019 100644 --- a/kiwi/app/views/questions/show.html.erb +++ b/kiwi/app/views/questions/show.html.erb @@ -24,7 +24,7 @@
    Comments:
    <% @question.comments.each do |comment| %> -

    <%= comment.content %> • <%= comment.user.name %> <%= std_format_date(comment.created_at) %> <% if current_user == comment.user %><%= link_to "Edit", edit_comment_path(comment.id) %> • <%= link_to "Delete",comment_path(comment.id), method: :delete %><% end %>

    +

    <%= comment.content %> • <%= comment.user.name %> <%= std_format_date(comment.created_at) %> <% if current_user == comment.user %> • <%= link_to "Edit", edit_comment_path(comment.id) %> • <%= link_to "Delete",comment_path(comment.id), method: :delete %><% end %>

    <% end %>
    <% end %> @@ -49,7 +49,7 @@
    <% answer.comments.each do |comment| %>

    <%= comment.content %> • - <%= comment.user.name %> <%= std_format_date(comment.created_at) %>

    + <%= comment.user.name %> <%= std_format_date(comment.created_at) %><% if current_user == comment.user %> • <%= link_to "Edit", edit_comment_path(comment.id) %> • <%= link_to "Delete",comment_path(comment.id), method: :delete %><% end %>

    <% end %>
    <% end %> From 747894a55203b3e88eb75a0d73ae709ee70f7e39 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sun, 24 Jan 2016 15:49:17 -0500 Subject: [PATCH 106/152] Add confirm alert to delete comments links --- kiwi/app/views/questions/show.html.erb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kiwi/app/views/questions/show.html.erb b/kiwi/app/views/questions/show.html.erb index ac50019..cc5ffc9 100644 --- a/kiwi/app/views/questions/show.html.erb +++ b/kiwi/app/views/questions/show.html.erb @@ -24,7 +24,7 @@
    Comments:
    <% @question.comments.each do |comment| %> -

    <%= comment.content %> • <%= comment.user.name %> <%= std_format_date(comment.created_at) %> <% if current_user == comment.user %> • <%= link_to "Edit", edit_comment_path(comment.id) %> • <%= link_to "Delete",comment_path(comment.id), method: :delete %><% end %>

    +

    <%= comment.content %> • <%= comment.user.name %> <%= std_format_date(comment.created_at) %> <% if current_user == comment.user %> • <%= link_to "Edit", edit_comment_path(comment.id) %> • <%= link_to "Delete",comment_path(comment.id), method: :delete, data: {confirm: 'Are you sure?'} %><% end %>

    <% end %>
    <% end %> @@ -49,7 +49,7 @@
    <% answer.comments.each do |comment| %>

    <%= comment.content %> • - <%= comment.user.name %> <%= std_format_date(comment.created_at) %><% if current_user == comment.user %> • <%= link_to "Edit", edit_comment_path(comment.id) %> • <%= link_to "Delete",comment_path(comment.id), method: :delete %><% end %>

    + <%= comment.user.name %> <%= std_format_date(comment.created_at) %><% if current_user == comment.user %> • <%= link_to "Edit", edit_comment_path(comment.id) %> • <%= link_to "Delete",comment_path(comment.id), method: :delete, data: {confirm: 'Are you sure?'} %><% end %>

    <% end %>
    <% end %> From b227a5acb8d212a17d0c8c8d360aeb8721a7cc64 Mon Sep 17 00:00:00 2001 From: Nicola Beuscher Date: Sun, 24 Jan 2016 15:50:20 -0500 Subject: [PATCH 107/152] Write factory for answer --- kiwi/spec/factories.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/kiwi/spec/factories.rb b/kiwi/spec/factories.rb index d7bb509..06128f5 100644 --- a/kiwi/spec/factories.rb +++ b/kiwi/spec/factories.rb @@ -8,5 +8,8 @@ f.user_id { rand(1..10) } end + factory :answer do |f| + f.content { Faker::Lorem.paragraph } + f.question_id { rand(1..10) } end From 00af283203c89a780c2ebd64b86f824d8a6d361b Mon Sep 17 00:00:00 2001 From: Nicola Beuscher Date: Sun, 24 Jan 2016 16:13:30 -0500 Subject: [PATCH 108/152] Create user factory, add basic answer model specs --- .../controllers/answers_controller_spec.rb | 0 .../controllers/questions_controller_spec.rb | 17 ++++++++------ kiwi/spec/factories.rb | 10 +++++++-- kiwi/spec/models/answer_spec.rb | 22 +++++++++++++++++++ kiwi/spec/models/question_spec.rb | 5 +++-- 5 files changed, 43 insertions(+), 11 deletions(-) create mode 100644 kiwi/spec/controllers/answers_controller_spec.rb create mode 100644 kiwi/spec/models/answer_spec.rb diff --git a/kiwi/spec/controllers/answers_controller_spec.rb b/kiwi/spec/controllers/answers_controller_spec.rb new file mode 100644 index 0000000..e69de29 diff --git a/kiwi/spec/controllers/questions_controller_spec.rb b/kiwi/spec/controllers/questions_controller_spec.rb index 56ea054..b5b089e 100644 --- a/kiwi/spec/controllers/questions_controller_spec.rb +++ b/kiwi/spec/controllers/questions_controller_spec.rb @@ -2,10 +2,11 @@ describe QuestionsController do - describe 'GET #index' do + describe 'GET #index' do it "populates an array of questions" do - question = FactoryGirl.create(:question) + user = FactoryGirl.create(:user) + question = FactoryGirl.create(:question, user: user) get :index expect(assigns(:questions)).to eq([question]) end @@ -20,13 +21,15 @@ describe "GET #show" do it "assigns the requested question to @question" do - question = FactoryGirl.create(:question) + user = FactoryGirl.create(:user) + question = FactoryGirl.create(:question, user: user) get :show, id: question expect(assigns(:question)).to eq(question) end it "renders the #show view" do - get :show, id: FactoryGirl.create(:question) + user = FactoryGirl.create(:user) + get :show, id: FactoryGirl.create(:question, user: user) expect(response).to render_template :show end @@ -38,7 +41,7 @@ context "with valid attributes" do - xit "creates a new question" do + xit "creates a new question" do post :create, question: {title: "Title", Content: "Content", user_id: 1 } expect(assigns(:question)).to change(Question, :count).by(1) end @@ -61,8 +64,8 @@ post :create, question: FactoryGirl.attributes_for(:question) expect(response).to render_template :new end - end + end end -end \ No newline at end of file +end diff --git a/kiwi/spec/factories.rb b/kiwi/spec/factories.rb index 06128f5..8abbafc 100644 --- a/kiwi/spec/factories.rb +++ b/kiwi/spec/factories.rb @@ -2,14 +2,20 @@ FactoryGirl.define do + factory :user do |f| + f.name "my_username" + f.email { Faker::Internet.safe_email } + f.password { Faker::Internet.password } + end + factory :question do |f| f.title { Faker::Lorem.sentence } f.content { Faker::Lorem.paragraph } - f.user_id { rand(1..10) } end factory :answer do |f| f.content { Faker::Lorem.paragraph } - f.question_id { rand(1..10) } + end + end diff --git a/kiwi/spec/models/answer_spec.rb b/kiwi/spec/models/answer_spec.rb new file mode 100644 index 0000000..250a13d --- /dev/null +++ b/kiwi/spec/models/answer_spec.rb @@ -0,0 +1,22 @@ +require 'rails_helper' + +describe Answer do + let(:user) { FactoryGirl.create(:user) } + let(:question) { FactoryGirl.create(:question, user: user) } + let(:answer) { FactoryGirl.create(:answer, question: question, user: user) } + + it { should belong_to(:question) } + + it "has a valid factory" do + expect(answer).to be_valid + end + + it "is invalid without a content" do + expect(FactoryGirl.build(:answer, content: nil)).to_not be_valid + end + + it "is invalid without a question_id" do + expect(FactoryGirl.build(:answer, question_id: nil)).to_not be_valid + end + +end diff --git a/kiwi/spec/models/question_spec.rb b/kiwi/spec/models/question_spec.rb index 0466fe7..bd7abbe 100644 --- a/kiwi/spec/models/question_spec.rb +++ b/kiwi/spec/models/question_spec.rb @@ -1,7 +1,8 @@ require 'rails_helper' describe Question do - let(:question) { FactoryGirl.create(:question) } + let(:user) { FactoryGirl.create(:user) } + let(:question) { FactoryGirl.create(:question, user: user) } it { should belong_to(:user) } @@ -21,4 +22,4 @@ expect(FactoryGirl.build(:question, user_id: nil)).to_not be_valid end -end \ No newline at end of file +end From 360ef4be18040eadb0f1d69dd59bcc6c4d3ee822 Mon Sep 17 00:00:00 2001 From: Luis Fernando Plaz Date: Sun, 24 Jan 2016 15:51:48 -0500 Subject: [PATCH 109/152] Make changes to votable_type assignment --- kiwi/app/controllers/votes_controller.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/kiwi/app/controllers/votes_controller.rb b/kiwi/app/controllers/votes_controller.rb index ee13273..0a3e7db 100644 --- a/kiwi/app/controllers/votes_controller.rb +++ b/kiwi/app/controllers/votes_controller.rb @@ -1,5 +1,5 @@ class VotesController < ApplicationController - before_action :load_votable + before_action :vote_type def create @vote = current_user.votes.new(vote_params) @@ -23,10 +23,13 @@ def vote_params params.require(:vote).permit(:direction,:user_id) end -def load_votable - # binding.pry - resource, id = request.path.split('/')[1,2] - @votable = resource.singularize.classify.constantize.find(id) +def vote_type + + + + + + end From bbe051238ab88d874603c7ff22f04a684012acf6 Mon Sep 17 00:00:00 2001 From: Luis Fernando Plaz Date: Sun, 24 Jan 2016 17:07:55 -0500 Subject: [PATCH 110/152] Fix answers resources routes to nest comments and votes --- kiwi/app/controllers/votes_controller.rb | 24 +++++++++++++++--------- kiwi/app/models/answer.rb | 5 +++++ kiwi/app/models/question.rb | 5 +++++ kiwi/app/views/questions/show.html.erb | 11 +++++++++-- kiwi/config/routes.rb | 10 ++++------ 5 files changed, 38 insertions(+), 17 deletions(-) diff --git a/kiwi/app/controllers/votes_controller.rb b/kiwi/app/controllers/votes_controller.rb index 0a3e7db..960c74c 100644 --- a/kiwi/app/controllers/votes_controller.rb +++ b/kiwi/app/controllers/votes_controller.rb @@ -1,5 +1,5 @@ class VotesController < ApplicationController - before_action :vote_type + # before_action :vote_type def create @vote = current_user.votes.new(vote_params) @@ -11,9 +11,14 @@ def create elsif !!params[:answer_id] @vote.votable_type = "Answer" @vote.votable_id = params[:answer_id] + @answer = Answer.find_by(id: params[:answer_id]) + @vote.save + redirect_to question_path(id: @answer.question_id) else @vote.votable_type = "Comment" @vote.votable_id = params[:comment_id] + @vote.save + redirect_to question_path(id: @comment.question_id) end end @@ -23,14 +28,15 @@ def vote_params params.require(:vote).permit(:direction,:user_id) end -def vote_type - - - - - - -end +# def vote_type +# if !!params[:question_id] +# @vote_type = {votable_type: "Question" , votable_id: params[:question_id]} +# elsif !!params[:answer_id] +# @vote_type = {votable_type: "Answer" , votable_id: params[:answer_id]} +# else +# @vote_type = {votable_type: "Comment" , votable_id: params[:comment_id]} +# end +# end end diff --git a/kiwi/app/models/answer.rb b/kiwi/app/models/answer.rb index 61140c1..19833e4 100644 --- a/kiwi/app/models/answer.rb +++ b/kiwi/app/models/answer.rb @@ -6,4 +6,9 @@ class Answer < ActiveRecord::Base has_many :votes, as: :votable validates_presence_of :content, :user, :question + + def sum_of_votes + self.votes.inject(0){|sum, vote| sum += vote.direction} + end + end diff --git a/kiwi/app/models/question.rb b/kiwi/app/models/question.rb index a3479bb..b36f82c 100644 --- a/kiwi/app/models/question.rb +++ b/kiwi/app/models/question.rb @@ -9,4 +9,9 @@ class Question < ActiveRecord::Base validates_presence_of :title, :content, :user_id + + def sum_of_votes + self.votes.inject(0){|sum, vote| sum += vote.direction} + end + end diff --git a/kiwi/app/views/questions/show.html.erb b/kiwi/app/views/questions/show.html.erb index cc5ffc9..e101075 100644 --- a/kiwi/app/views/questions/show.html.erb +++ b/kiwi/app/views/questions/show.html.erb @@ -4,8 +4,8 @@
    • <%= link_to '▲'.html_safe, question_votes_path(@question, vote: {direction: '1'}), method: :post %>
    • -
    • <%= @question.votes.count %>
    • -
    • <%= link_to '▼'.html_safe %>
    • +
    • <%= @question.sum_of_votes %>
    • +
    • <%= link_to '▼'.html_safe, question_votes_path(@question, vote: {direction: '-1'}), method: :post %>
    @@ -42,6 +42,13 @@

    Answers: <%=@question.answers.count %>

    <% @question.answers.each do |answer|%>
    +
    +
      +
    • <%= link_to '▲'.html_safe, answer_votes_path(answer, vote: {direction: '1'}), method: :post %>
    • +
    • <%= answer.sum_of_votes %>
    • +
    • <%= link_to '▼'.html_safe, answer_votes_path(answer, vote: {direction: '-1'}), method: :post %>
    • +
    +
    Answered by <%= answer.user.name %> <%= std_format_date(answer.created_at) %>

    <%= answer.content %>

    <% if !answer.comments.empty? %> diff --git a/kiwi/config/routes.rb b/kiwi/config/routes.rb index 530ed97..5759aea 100644 --- a/kiwi/config/routes.rb +++ b/kiwi/config/routes.rb @@ -11,13 +11,11 @@ resources :votes, only: [ :create, :destroy ] end - resources :answers do - resources :comments, only: [:new, :create] - end + resources :answers do + resources :comments, only: [:new, :create] + resources :votes, only: [:create, :destroy] + end - # resources :answers do - # resources :comments, only: [:new, :show] - # end resources :comments, only: [ :edit, :update, :destroy ] From c633f1cc13a7313b9ad5e6d30bc249bf463ebe1e Mon Sep 17 00:00:00 2001 From: Luis Fernando Plaz Date: Sun, 24 Jan 2016 17:06:00 -0500 Subject: [PATCH 111/152] Implement vote count on questions index page --- kiwi/app/views/questions/index.html.erb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kiwi/app/views/questions/index.html.erb b/kiwi/app/views/questions/index.html.erb index f2dc8fb..76340e5 100644 --- a/kiwi/app/views/questions/index.html.erb +++ b/kiwi/app/views/questions/index.html.erb @@ -4,8 +4,8 @@ <% @questions.each do |question| %>
    - -
    0
    + +
    <%= question.sum_of_votes %>
    votes
    @@ -21,4 +21,4 @@ <%= link_to question.title, question_path(question) %>
    - <% end %> \ No newline at end of file + <% end %> From ba50eb3a81b87858b60c224a2f9975f6d1a32884 Mon Sep 17 00:00:00 2001 From: Luis Fernando Plaz Date: Sun, 24 Jan 2016 17:39:05 -0500 Subject: [PATCH 112/152] Start method to avoid user revotes --- kiwi/app/controllers/votes_controller.rb | 4 ++-- kiwi/app/models/answer.rb | 6 ++---- kiwi/app/models/comment.rb | 2 ++ kiwi/app/models/concerns/votable.rb | 20 ++++++++++---------- kiwi/app/models/question.rb | 9 +-------- kiwi/app/models/vote.rb | 1 - 6 files changed, 17 insertions(+), 25 deletions(-) diff --git a/kiwi/app/controllers/votes_controller.rb b/kiwi/app/controllers/votes_controller.rb index 960c74c..0fef45e 100644 --- a/kiwi/app/controllers/votes_controller.rb +++ b/kiwi/app/controllers/votes_controller.rb @@ -1,9 +1,9 @@ class VotesController < ApplicationController - # before_action :vote_type def create @vote = current_user.votes.new(vote_params) - if !!params[:question_id] + binding.pry + if !!params[:question_id] && Question.find(params[:question_id]).voted? @vote.votable_type = "Question" @vote.votable_id = params[:question_id] @vote.save diff --git a/kiwi/app/models/answer.rb b/kiwi/app/models/answer.rb index 19833e4..12d8e7b 100644 --- a/kiwi/app/models/answer.rb +++ b/kiwi/app/models/answer.rb @@ -1,4 +1,6 @@ class Answer < ActiveRecord::Base + include Votable + belongs_to :user belongs_to :question @@ -7,8 +9,4 @@ class Answer < ActiveRecord::Base validates_presence_of :content, :user, :question - def sum_of_votes - self.votes.inject(0){|sum, vote| sum += vote.direction} - end - end diff --git a/kiwi/app/models/comment.rb b/kiwi/app/models/comment.rb index 55031a9..c1f2180 100644 --- a/kiwi/app/models/comment.rb +++ b/kiwi/app/models/comment.rb @@ -1,4 +1,6 @@ class Comment < ActiveRecord::Base + include Votable + belongs_to :user belongs_to :commentable, polymorphic: true diff --git a/kiwi/app/models/concerns/votable.rb b/kiwi/app/models/concerns/votable.rb index 5266853..5dd85fe 100644 --- a/kiwi/app/models/concerns/votable.rb +++ b/kiwi/app/models/concerns/votable.rb @@ -1,14 +1,14 @@ -# module Votable +module Votable -# extend ActiveSupport::Concern + helper_method :current_user -# included do -# has_many :votes, as: :votable -# end + def sum_of_votes + self.votes.inject(0){|sum, vote| sum += vote.direction} + end -# attr_accessor :votable_attributes -# # accepts_nested_attributes_for :votable + def voted? + binding.pry + self.votes.find_by(user_id: user_id) + end -# end - -# ActiveRecord::Base.send(:include, Votable) +end diff --git a/kiwi/app/models/question.rb b/kiwi/app/models/question.rb index b36f82c..c451d01 100644 --- a/kiwi/app/models/question.rb +++ b/kiwi/app/models/question.rb @@ -1,6 +1,5 @@ class Question < ActiveRecord::Base - - # include Votable + include Votable belongs_to :user has_many :answers @@ -8,10 +7,4 @@ class Question < ActiveRecord::Base has_many :votes, as: :votable validates_presence_of :title, :content, :user_id - - - def sum_of_votes - self.votes.inject(0){|sum, vote| sum += vote.direction} - end - end diff --git a/kiwi/app/models/vote.rb b/kiwi/app/models/vote.rb index fed4a27..547d0e9 100644 --- a/kiwi/app/models/vote.rb +++ b/kiwi/app/models/vote.rb @@ -1,5 +1,4 @@ class Vote < ActiveRecord::Base - # include Votable belongs_to :user belongs_to :votable, polymorphic: true From 6bf44ee81cfdca5fdba4a632d972f918b6fe4de5 Mon Sep 17 00:00:00 2001 From: Luis Fernando Plaz Date: Sun, 24 Jan 2016 18:18:39 -0500 Subject: [PATCH 113/152] Fix merging problems --- kiwi/app/views/questions/index.html.erb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kiwi/app/views/questions/index.html.erb b/kiwi/app/views/questions/index.html.erb index 76340e5..3728d6b 100644 --- a/kiwi/app/views/questions/index.html.erb +++ b/kiwi/app/views/questions/index.html.erb @@ -1,6 +1,8 @@

    All Questions

    +<% binding.pry %> + <% @questions.each do |question| %>
    From 693a18760f87684ecfe5a0d21544cb0a4cf0376e Mon Sep 17 00:00:00 2001 From: Luis Fernando Plaz Date: Sun, 24 Jan 2016 18:11:03 -0500 Subject: [PATCH 114/152] Fix votable module --- kiwi/app/assets/stylesheets/main.css | 14 ++++++++ kiwi/app/controllers/questions_controller.rb | 12 ++++++- kiwi/app/models/concerns/votable.rb | 3 -- kiwi/app/views/questions/index.html.erb | 37 ++++++++++++-------- 4 files changed, 47 insertions(+), 19 deletions(-) diff --git a/kiwi/app/assets/stylesheets/main.css b/kiwi/app/assets/stylesheets/main.css index bda69b4..9b78ba0 100644 --- a/kiwi/app/assets/stylesheets/main.css +++ b/kiwi/app/assets/stylesheets/main.css @@ -163,3 +163,17 @@ label{ font-weight: 300; } +.sort-by-menu{ + display:inline; +} + +.sort-by-nav{ + display:block-inline; + text-align:right; +} + +.sort-by-options{ + display:inline; + text-align:right; +} + diff --git a/kiwi/app/controllers/questions_controller.rb b/kiwi/app/controllers/questions_controller.rb index 71b0565..91da0b9 100644 --- a/kiwi/app/controllers/questions_controller.rb +++ b/kiwi/app/controllers/questions_controller.rb @@ -4,6 +4,16 @@ class QuestionsController < ApplicationController def index @questions = Question.all + if params[:sort] == "trending" + + elsif params[:sort] == "most-recent" + @question = @questions.order(:created_at) + elsif params[:sort] == "rate" + binding.pry + @question = @questions.sort_by { |question| question.votes.count }.reverse + else + + end end def new @@ -30,7 +40,7 @@ def edit end def update - @question = Question.find_by(id: params[:id]) + @question = Question.find_by(id: params[:id]) if @question.update_attributes(question_params) redirect_to question_path else diff --git a/kiwi/app/models/concerns/votable.rb b/kiwi/app/models/concerns/votable.rb index 5dd85fe..002cb2d 100644 --- a/kiwi/app/models/concerns/votable.rb +++ b/kiwi/app/models/concerns/votable.rb @@ -1,7 +1,4 @@ module Votable - - helper_method :current_user - def sum_of_votes self.votes.inject(0){|sum, vote| sum += vote.direction} end diff --git a/kiwi/app/views/questions/index.html.erb b/kiwi/app/views/questions/index.html.erb index 3728d6b..308b792 100644 --- a/kiwi/app/views/questions/index.html.erb +++ b/kiwi/app/views/questions/index.html.erb @@ -1,26 +1,33 @@

    All Questions

    -<% binding.pry %> +
    +
      +

      Sort By

      +
    • <%= link_to "Trending", root_path(sort: "trending") %>
    • | +
    • <%= link_to "Most Recent", root_path(sort: "most-recent") %>
    • | +
    • <%= link_to "Highest-Voted", root_path(sort: "rate")%>
    • +
    +
    <% @questions.each do |question| %> -
    -
    +
    +
    -
    <%= question.sum_of_votes %>
    -
    votes
    - -
    - <%= question.answers.count %> -
    -
    answers
    - -
    0
    -
    views
    +
    <%= question.sum_of_votes %>
    +
    votes
    +
    + <%= question.answers.count %>
    +
    answers
    - <%= link_to question.title, question_path(question) %> +
    0
    +
    views
    - <% end %> + + <%= link_to question.title, question_path(question) %> + +
    +<% end %> From 830df83b82b6f18008698333dd5269fe19697f66 Mon Sep 17 00:00:00 2001 From: Luis Fernando Plaz Date: Sun, 24 Jan 2016 18:16:02 -0500 Subject: [PATCH 115/152] Add Sorting filter in question controller --- kiwi/app/controllers/questions_controller.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/kiwi/app/controllers/questions_controller.rb b/kiwi/app/controllers/questions_controller.rb index 91da0b9..96e01ea 100644 --- a/kiwi/app/controllers/questions_controller.rb +++ b/kiwi/app/controllers/questions_controller.rb @@ -1,7 +1,5 @@ class QuestionsController < ApplicationController - # before_filter - def index @questions = Question.all if params[:sort] == "trending" @@ -9,8 +7,7 @@ def index elsif params[:sort] == "most-recent" @question = @questions.order(:created_at) elsif params[:sort] == "rate" - binding.pry - @question = @questions.sort_by { |question| question.votes.count }.reverse + @question = @questions.sort_by { |question| question.sum_of_votes }.reverse else end From 284a4f6fc0d796779761035649d383abe27ee06c Mon Sep 17 00:00:00 2001 From: Nicola Beuscher Date: Sun, 24 Jan 2016 17:26:56 -0500 Subject: [PATCH 116/152] Update readme in main kiwi overflow dir --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index daabbf6..2c754f6 100644 --- a/README.md +++ b/README.md @@ -1 +1,5 @@ -# kiwi-overflow \ No newline at end of file +# Kiwi Overflow + +This is a Ruby on Rails app built by @webdevjeffus, @deweydell, @luisplaz, and @zlschatz which is an imitation of Stack Overflow. + +[Our trello board has images of our schema and working agreement](https://trello.com/b/TKvtoxBm/kiwi-overflow) From 13645d6f9c439b0681544a99bab296bb83e43795 Mon Sep 17 00:00:00 2001 From: Nicola Beuscher Date: Sun, 24 Jan 2016 17:38:11 -0500 Subject: [PATCH 117/152] Update vote button css and add answers js file --- kiwi/.DS_Store | Bin 0 -> 6148 bytes kiwi/app/assets/javascripts/answers.js | 5 +++++ kiwi/app/assets/stylesheets/main.css | 8 ++++++-- kiwi/app/views/questions/show.html.erb | 2 +- kiwi/public/.DS_Store | Bin 0 -> 6148 bytes 5 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 kiwi/.DS_Store create mode 100644 kiwi/app/assets/javascripts/answers.js create mode 100644 kiwi/public/.DS_Store diff --git a/kiwi/.DS_Store b/kiwi/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..693b89f07f26af5a6474345e0177a162a7c55a61 GIT binary patch literal 6148 zcmeHKOHRWu6dX5IDHMq=S!T%!i5rBf1+idLE&%<2s(w{k#4^X=P#l2Uz`SP*#V%|s zgx-_vN$h#{c-q7<0N2@$E`TM7q>+5cqHqKXA#{|RvjD8 z@Qh8&Q{}G}WEN!r8(3boLj4UwZWE za5^)`2{XHJLUG~f;c}(J6&|fN2h4%kfz02wW&iK}e*TY>?9Lo82mX}_b3pu*w|Rs{>y!dc~3e literal 0 HcmV?d00001 diff --git a/kiwi/app/assets/javascripts/answers.js b/kiwi/app/assets/javascripts/answers.js new file mode 100644 index 0000000..d6e46b8 --- /dev/null +++ b/kiwi/app/assets/javascripts/answers.js @@ -0,0 +1,5 @@ +$(document).on("ready", function(){ + + $('') + +}); diff --git a/kiwi/app/assets/stylesheets/main.css b/kiwi/app/assets/stylesheets/main.css index 9b78ba0..d503ffe 100644 --- a/kiwi/app/assets/stylesheets/main.css +++ b/kiwi/app/assets/stylesheets/main.css @@ -141,10 +141,10 @@ label{ .vote-container { height: 4em; width: 2em; - border: 1px solid red; float: left; margin-right: 1em; - font-size: .5em; + font-size: 1em; + margin-bottom: 10em; } .vote-attributes{ @@ -177,3 +177,7 @@ label{ text-align:right; } +ul li { + list-style: none; +} + diff --git a/kiwi/app/views/questions/show.html.erb b/kiwi/app/views/questions/show.html.erb index e101075..12cb0c2 100644 --- a/kiwi/app/views/questions/show.html.erb +++ b/kiwi/app/views/questions/show.html.erb @@ -11,7 +11,7 @@ Asked by <%= @question.user.name %> <%= std_format_date(@question.created_at) %> -

    <%= @question.content %>

    +

    <%= @question.content %>

      <% if current_user == @question.user %> diff --git a/kiwi/public/.DS_Store b/kiwi/public/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 Date: Sun, 24 Jan 2016 17:47:10 -0500 Subject: [PATCH 118/152] Update seeds file so that questions are 3 sentences each --- kiwi/app/assets/stylesheets/main.css | 4 +++- kiwi/db/seeds.rb | 8 ++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/kiwi/app/assets/stylesheets/main.css b/kiwi/app/assets/stylesheets/main.css index d503ffe..4e89bfc 100644 --- a/kiwi/app/assets/stylesheets/main.css +++ b/kiwi/app/assets/stylesheets/main.css @@ -144,7 +144,6 @@ label{ float: left; margin-right: 1em; font-size: 1em; - margin-bottom: 10em; } .vote-attributes{ @@ -181,3 +180,6 @@ ul li { list-style: none; } +#question-content { +} + diff --git a/kiwi/db/seeds.rb b/kiwi/db/seeds.rb index 59f54b3..5c5c596 100644 --- a/kiwi/db/seeds.rb +++ b/kiwi/db/seeds.rb @@ -12,10 +12,10 @@ jeff = User.create(name: "jeff_dbc", email: "jeff@example.com", password: "password") hacker_bot = User.create(name: "hacker_bot", email: "hacker_bot@example.com", password: "password") -rails_q = Question.create(title: "How do I build a Rails app?", content: Faker::Lorem.paragraph(10), user: nicola) -sinatra_q =Question.create(title: "How do I build a Sinatra app?", content: Faker::Lorem.paragraph(10), user: luis) -mvc_q = Question.create(title: "What is MVC?", content: Faker::Lorem.paragraph(10), user: zach) -css_q = Question.create(title: "What are the best CSS styles?", content: Faker::Lorem.paragraph(10), user: jeff) +rails_q = Question.create(title: "How do I build a Rails app?", content: Faker::Lorem.paragraph(3), user: nicola) +sinatra_q =Question.create(title: "How do I build a Sinatra app?", content: Faker::Lorem.paragraph(3), user: luis) +mvc_q = Question.create(title: "What is MVC?", content: Faker::Lorem.paragraph(3), user: zach) +css_q = Question.create(title: "What are the best CSS styles?", content: Faker::Lorem.paragraph(3), user: jeff) answer = Answer.create(content:"It's super easy just follow the rails guide.", user: luis, question: rails_q) Answer.create(content:"Just ask someone from phase 2.", user: jeff, question: sinatra_q) From d221b03fa2211db08c0be6609ddbb8c138157db0 Mon Sep 17 00:00:00 2001 From: Nicola Beuscher Date: Sun, 24 Jan 2016 17:50:31 -0500 Subject: [PATCH 119/152] Prevent default of add answer form --- kiwi/app/assets/javascripts/answers.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/kiwi/app/assets/javascripts/answers.js b/kiwi/app/assets/javascripts/answers.js index d6e46b8..689acde 100644 --- a/kiwi/app/assets/javascripts/answers.js +++ b/kiwi/app/assets/javascripts/answers.js @@ -1,5 +1,7 @@ $(document).on("ready", function(){ - $('') + $('#answers-form').on('submit', function(){ + event.preventDefault(); + }); }); From a5f6b5251a548290f4e953247ca37ff14d060068 Mon Sep 17 00:00:00 2001 From: Nicola Beuscher Date: Sun, 24 Jan 2016 18:14:16 -0500 Subject: [PATCH 120/152] Work on ajax request for post answers form --- kiwi/app/assets/javascripts/answers.js | 13 ++++++++- kiwi/app/controllers/answers_controller.rb | 34 +++++++++++++--------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/kiwi/app/assets/javascripts/answers.js b/kiwi/app/assets/javascripts/answers.js index 689acde..d652ff0 100644 --- a/kiwi/app/assets/javascripts/answers.js +++ b/kiwi/app/assets/javascripts/answers.js @@ -1,7 +1,18 @@ $(document).on("ready", function(){ - $('#answers-form').on('submit', function(){ + $('#answers-form').on('submit', function(event){ event.preventDefault(); + + $.ajax({ + url: $('#answers-form form').attr("action"), + type: "POST", + data: $(this).serialize() + }).done(function(response) { + console.log("Success!") + $('.answers-list').append(response); + }).fail(function(response) { + console.log("FAILURE") + }); }); }); diff --git a/kiwi/app/controllers/answers_controller.rb b/kiwi/app/controllers/answers_controller.rb index 0e07916..7e327ee 100644 --- a/kiwi/app/controllers/answers_controller.rb +++ b/kiwi/app/controllers/answers_controller.rb @@ -1,19 +1,25 @@ class AnswersController < ApplicationController -add_flash_types(:alert) -def create - @answer = Answer.new(answer_params) - if @answer.save - redirect_to question_path(id: @answer.question_id) - else - #error to be handled by ajax - redirect_to login_path + add_flash_types(:alert) + + def create + @answer = Answer.new(answer_params) + binding.pry + if @answer.save + if request.xhr? + render question_path(id: @answer.question_id), layout: false, locals: { answer: @answer } + else + redirect_to question_path(id: @answer.question_id) + end + else + @errors = Answer.errors.full_messages + redirect_to question_path(id: @answer.question_id) + end end -end -private + private -def answer_params - params.require(:answer).permit(:content,:user_id,:question_id) -end + def answer_params + params.require(:answer).permit(:content,:user_id,:question_id) + end -end \ No newline at end of file +end From a0394651b93e12982ecd8a792a7c20158fdf93a0 Mon Sep 17 00:00:00 2001 From: Nicola Beuscher Date: Sun, 24 Jan 2016 18:36:33 -0500 Subject: [PATCH 121/152] Progress on ajax, comments out JS so it all still works. --- kiwi/app/assets/javascripts/answers.js | 30 +++++++++++----------- kiwi/app/controllers/answers_controller.rb | 11 +++----- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/kiwi/app/assets/javascripts/answers.js b/kiwi/app/assets/javascripts/answers.js index d652ff0..fb1f104 100644 --- a/kiwi/app/assets/javascripts/answers.js +++ b/kiwi/app/assets/javascripts/answers.js @@ -1,18 +1,18 @@ -$(document).on("ready", function(){ +// $(document).on("ready", function(){ - $('#answers-form').on('submit', function(event){ - event.preventDefault(); +// $('#answers-form').on('submit', function(event){ +// event.preventDefault(); +// $.ajax({ +// url: $('#answers-form form').attr("action"), +// type: "POST", +// data: $('#answers-form form').serialize() - $.ajax({ - url: $('#answers-form form').attr("action"), - type: "POST", - data: $(this).serialize() - }).done(function(response) { - console.log("Success!") - $('.answers-list').append(response); - }).fail(function(response) { - console.log("FAILURE") - }); - }); +// }).done(function(response) { +// console.log("Success!") +// $('.answers-list').append(response); +// }).fail(function(response) { +// console.log("FAILURE") +// }); +// }); -}); +// }); diff --git a/kiwi/app/controllers/answers_controller.rb b/kiwi/app/controllers/answers_controller.rb index 7e327ee..804d3b6 100644 --- a/kiwi/app/controllers/answers_controller.rb +++ b/kiwi/app/controllers/answers_controller.rb @@ -3,16 +3,11 @@ class AnswersController < ApplicationController def create @answer = Answer.new(answer_params) - binding.pry if @answer.save - if request.xhr? - render question_path(id: @answer.question_id), layout: false, locals: { answer: @answer } - else - redirect_to question_path(id: @answer.question_id) - end - else - @errors = Answer.errors.full_messages redirect_to question_path(id: @answer.question_id) + else + #error to be handled by ajax + redirect_to login_path end end From b3fbc88d54de4866d632e83162369878ad14414b Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sun, 24 Jan 2016 16:35:56 -0500 Subject: [PATCH 122/152] Implement comments_controller_spec with first test --- kiwi/app/controllers/comments_controller.rb | 4 ++-- .../controllers/comments_controller_spec.rb | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 kiwi/spec/controllers/comments_controller_spec.rb diff --git a/kiwi/app/controllers/comments_controller.rb b/kiwi/app/controllers/comments_controller.rb index a5393a9..3267f30 100644 --- a/kiwi/app/controllers/comments_controller.rb +++ b/kiwi/app/controllers/comments_controller.rb @@ -1,13 +1,13 @@ class CommentsController < ApplicationController def new + binding.pry if params[:question_id] @commentable = Question.find_by(id: params[:question_id]) - @comment = @commentable.comments.new else @commentable = Answer.find_by(id: params[:answer_id]) - @comment = @commentable.comments.new end + @comment = @commentable.comments.new end def create diff --git a/kiwi/spec/controllers/comments_controller_spec.rb b/kiwi/spec/controllers/comments_controller_spec.rb new file mode 100644 index 0000000..d147845 --- /dev/null +++ b/kiwi/spec/controllers/comments_controller_spec.rb @@ -0,0 +1,21 @@ +require 'rails_helper' + +describe CommentsController do + + + describe "#new" do + it "renders a form to create a new comment on a question" do + question = FactoryGirl.create(:question) + get :new, question_id: question.id + expect(response).to render_template :new + end + end + + # describe "#new" do + # it "renders a form to create a new comment on an answer" do + # answer = + # end + + + +end From 407adf257a2c330f3ea440804b8073751e03c6a1 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sun, 24 Jan 2016 17:20:57 -0500 Subject: [PATCH 123/152] Add FactoryGirl objects to comments_controller_spec --- kiwi/spec/controllers/comments_controller_spec.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/kiwi/spec/controllers/comments_controller_spec.rb b/kiwi/spec/controllers/comments_controller_spec.rb index d147845..ddb7600 100644 --- a/kiwi/spec/controllers/comments_controller_spec.rb +++ b/kiwi/spec/controllers/comments_controller_spec.rb @@ -2,6 +2,10 @@ describe CommentsController do + let(:user) { FactoryGirl.create :user } + let(:question) { FactoryGirl.create :question } + let(:answer) { FactoryGirl.create :answer} + describe "#new" do it "renders a form to create a new comment on a question" do From fccf496fcc8ff1e33ea7cc1d3413706fcee01ddd Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sun, 24 Jan 2016 18:51:49 -0500 Subject: [PATCH 124/152] Add factory for comments in factories.rb --- kiwi/spec/factories.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/kiwi/spec/factories.rb b/kiwi/spec/factories.rb index 8abbafc..90785c8 100644 --- a/kiwi/spec/factories.rb +++ b/kiwi/spec/factories.rb @@ -15,7 +15,11 @@ factory :answer do |f| f.content { Faker::Lorem.paragraph } - end + end + + factory :comment do |f| + f.content {Faker::Lorem.paragraph} + end end From 2ae9bf035834f74ed30cc291f16a3b3d2092b92f Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sun, 24 Jan 2016 18:52:53 -0500 Subject: [PATCH 125/152] Implement 4 tests in comments_controller_spec.rb --- kiwi/app/controllers/comments_controller.rb | 1 - .../controllers/comments_controller_spec.rb | 44 +++++++++++++++---- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/kiwi/app/controllers/comments_controller.rb b/kiwi/app/controllers/comments_controller.rb index 3267f30..3b7ea35 100644 --- a/kiwi/app/controllers/comments_controller.rb +++ b/kiwi/app/controllers/comments_controller.rb @@ -1,7 +1,6 @@ class CommentsController < ApplicationController def new - binding.pry if params[:question_id] @commentable = Question.find_by(id: params[:question_id]) else diff --git a/kiwi/spec/controllers/comments_controller_spec.rb b/kiwi/spec/controllers/comments_controller_spec.rb index ddb7600..b29da2d 100644 --- a/kiwi/spec/controllers/comments_controller_spec.rb +++ b/kiwi/spec/controllers/comments_controller_spec.rb @@ -2,24 +2,50 @@ describe CommentsController do - let(:user) { FactoryGirl.create :user } - let(:question) { FactoryGirl.create :question } - let(:answer) { FactoryGirl.create :answer} + let(:user1) { FactoryGirl.create :user } + let(:user2) { FactoryGirl.create :user } + let(:question) { FactoryGirl.create :question, user: user1 } + let(:answer) { FactoryGirl.create :answer, question: question, user: user2} describe "#new" do it "renders a form to create a new comment on a question" do - question = FactoryGirl.create(:question) get :new, question_id: question.id expect(response).to render_template :new end - end - # describe "#new" do - # it "renders a form to create a new comment on an answer" do - # answer = - # end + it "renders a form to create a new comment on an answer" do + get :new, answer_id: answer.id + expect(response).to render_template :new + end + end + describe "#create" do + context "with valid attributes" do + + it "creates a new comment to a question" do + expect do + post :create, comment: { + content: "This is a comment.", + commentable_id: question.id, + commentable_type: question.class, + user: user2 + } + end + end + + it "creates a new comment to an answer" do + expect do + post :create, comment: { + content: "This is a comment.", + commentable_id: answer.id, + commentable_type: answer.class, + user: user2 + } + end + end + end + end end From 232c3c0a137768520f4b2f78ffd129693836bf94 Mon Sep 17 00:00:00 2001 From: deweydell Date: Sun, 24 Jan 2016 21:46:45 -0500 Subject: [PATCH 126/152] Create _answer_show partial to render in question view --- kiwi/app/views/answers/_answer_show.html.erb | 26 ++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 kiwi/app/views/answers/_answer_show.html.erb diff --git a/kiwi/app/views/answers/_answer_show.html.erb b/kiwi/app/views/answers/_answer_show.html.erb new file mode 100644 index 0000000..1162fcb --- /dev/null +++ b/kiwi/app/views/answers/_answer_show.html.erb @@ -0,0 +1,26 @@ +
      +
      +
        +
      • <%= link_to '▲'.html_safe, answer_votes_path(answer, vote: {direction: '1'}), method: :post %>
      • +
      • <%= answer.sum_of_votes %>
      • +
      • <%= link_to '▼'.html_safe, answer_votes_path(answer, vote: {direction: '-1'}), method: :post %>
      • +
      +
      + + Answered by <%= answer.user.name %> <%= std_format_date(answer.created_at) %> +

      <%= answer.content %>

      + + <% if !answer.comments.empty? %> +
      Comments:
      +
      + <% answer.comments.each do |comment| %> +

      <%= comment.content %> • + <%= comment.user.name %> <%= std_format_date(comment.created_at) %><% if current_user == comment.user %> • <%= link_to "Edit", edit_comment_path(comment.id) %> • <%= link_to "Delete",comment_path(comment.id), method: :delete, data: {confirm: 'Are you sure?'} %><% end %>

      + <% end %> +
      + <% end %> + + <% if logged_in? %> + <%= link_to "Add Comment", new_answer_comment_path(answer.id) %> + <% end %> +
      From d5e8b7b9d2a65154a6ba6cf8b3738e537c902090 Mon Sep 17 00:00:00 2001 From: deweydell Date: Sun, 24 Jan 2016 21:47:07 -0500 Subject: [PATCH 127/152] Finish writing js ajax request for posting answer to question --- kiwi/app/assets/javascripts/answers.js | 32 +++++++++++++------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/kiwi/app/assets/javascripts/answers.js b/kiwi/app/assets/javascripts/answers.js index fb1f104..fb764de 100644 --- a/kiwi/app/assets/javascripts/answers.js +++ b/kiwi/app/assets/javascripts/answers.js @@ -1,18 +1,18 @@ -// $(document).on("ready", function(){ +$(document).on("ready", function(){ -// $('#answers-form').on('submit', function(event){ -// event.preventDefault(); -// $.ajax({ -// url: $('#answers-form form').attr("action"), -// type: "POST", -// data: $('#answers-form form').serialize() + $('#answers-form form').on('submit', function(event){ + event.preventDefault(); + var valuesToSubmit = $(this).serialize(); + $.ajax({ + url: $('#answers-form form').attr("action"), + type: "POST", + data: valuesToSubmit, + }).done(function(response) { + console.log("Success!") + $('.answers-list').append(response); + }).fail(function(response) { + console.log("FAILURE") + }); + }); -// }).done(function(response) { -// console.log("Success!") -// $('.answers-list').append(response); -// }).fail(function(response) { -// console.log("FAILURE") -// }); -// }); - -// }); +}); From 4d3f962ced671c232319cc13908545b1a73affb8 Mon Sep 17 00:00:00 2001 From: deweydell Date: Sun, 24 Jan 2016 21:47:21 -0500 Subject: [PATCH 128/152] Remove binding.pry --- kiwi/app/controllers/votes_controller.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/kiwi/app/controllers/votes_controller.rb b/kiwi/app/controllers/votes_controller.rb index 0fef45e..28547a2 100644 --- a/kiwi/app/controllers/votes_controller.rb +++ b/kiwi/app/controllers/votes_controller.rb @@ -2,7 +2,6 @@ class VotesController < ApplicationController def create @vote = current_user.votes.new(vote_params) - binding.pry if !!params[:question_id] && Question.find(params[:question_id]).voted? @vote.votable_type = "Question" @vote.votable_id = params[:question_id] From 169718ad07a2ae329e2c466b58acd2a9af2424d9 Mon Sep 17 00:00:00 2001 From: deweydell Date: Sun, 24 Jan 2016 21:47:28 -0500 Subject: [PATCH 129/152] Remove Binding.pry --- kiwi/app/models/concerns/votable.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/kiwi/app/models/concerns/votable.rb b/kiwi/app/models/concerns/votable.rb index 002cb2d..3adb3d1 100644 --- a/kiwi/app/models/concerns/votable.rb +++ b/kiwi/app/models/concerns/votable.rb @@ -4,7 +4,6 @@ def sum_of_votes end def voted? - binding.pry self.votes.find_by(user_id: user_id) end From f3482562f852e089869b5346ee6d6d1b4fe64aa9 Mon Sep 17 00:00:00 2001 From: deweydell Date: Sun, 24 Jan 2016 21:47:53 -0500 Subject: [PATCH 130/152] Move answer html from question page into partial --- kiwi/app/views/questions/show.html.erb | 29 ++++---------------------- 1 file changed, 4 insertions(+), 25 deletions(-) diff --git a/kiwi/app/views/questions/show.html.erb b/kiwi/app/views/questions/show.html.erb index 12cb0c2..0d68fd4 100644 --- a/kiwi/app/views/questions/show.html.erb +++ b/kiwi/app/views/questions/show.html.erb @@ -41,34 +41,13 @@

      Answers: <%=@question.answers.count %>

      <% @question.answers.each do |answer|%> -
      -
      -
        -
      • <%= link_to '▲'.html_safe, answer_votes_path(answer, vote: {direction: '1'}), method: :post %>
      • -
      • <%= answer.sum_of_votes %>
      • -
      • <%= link_to '▼'.html_safe, answer_votes_path(answer, vote: {direction: '-1'}), method: :post %>
      • -
      -
      - Answered by <%= answer.user.name %> <%= std_format_date(answer.created_at) %> -

      <%= answer.content %>

      - <% if !answer.comments.empty? %> -
      Comments:
      -
      - <% answer.comments.each do |comment| %> -

      <%= comment.content %> • - <%= comment.user.name %> <%= std_format_date(comment.created_at) %><% if current_user == comment.user %> • <%= link_to "Edit", edit_comment_path(comment.id) %> • <%= link_to "Delete",comment_path(comment.id), method: :delete, data: {confirm: 'Are you sure?'} %><% end %>

      - <% end %> -
      - <% end %> - <% if logged_in? %> - <%= link_to "Add Comment", new_answer_comment_path(answer.id) %> - <% end %> -
      + + <%= render partial: "answers/answer_show", layout: false, locals: { answer: answer} %> + <% end %> -
      +
    <% end %> -

    Add Your Answer:

    From 04f2f4dafec4175afffa5d12d3e09077f7bf9038 Mon Sep 17 00:00:00 2001 From: deweydell Date: Sun, 24 Jan 2016 21:48:18 -0500 Subject: [PATCH 131/152] Account for xhr request for posting answer --- kiwi/app/controllers/answers_controller.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/kiwi/app/controllers/answers_controller.rb b/kiwi/app/controllers/answers_controller.rb index 804d3b6..2277635 100644 --- a/kiwi/app/controllers/answers_controller.rb +++ b/kiwi/app/controllers/answers_controller.rb @@ -3,11 +3,16 @@ class AnswersController < ApplicationController def create @answer = Answer.new(answer_params) - if @answer.save - redirect_to question_path(id: @answer.question_id) + if request.xhr? + @answer.save + render partial: "answers/answer_show", layout: false, locals: { answer: @answer} else - #error to be handled by ajax - redirect_to login_path + if @answer.save + redirect_to question_path(id: @answer.question_id) + else + @errors = Answer.errors.full_messages + redirect_to question_path(id: @answer.question_id) + end end end From bcec917272264fde1875bd2f46a705325be985aa Mon Sep 17 00:00:00 2001 From: deweydell Date: Sun, 24 Jan 2016 21:49:58 -0500 Subject: [PATCH 132/152] Add error handling for non-ajax requests to post answer --- kiwi/app/views/answers/_form.html.erb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/kiwi/app/views/answers/_form.html.erb b/kiwi/app/views/answers/_form.html.erb index 413c6e1..2ec76ad 100644 --- a/kiwi/app/views/answers/_form.html.erb +++ b/kiwi/app/views/answers/_form.html.erb @@ -10,3 +10,9 @@ <%= f.submit 'Post Answer' %> <% end %> + +<% if @errors %> + <% @errors.each do |msg| %> +

    <%= msg %>

    + <% end %> +<% end %> \ No newline at end of file From 47790ccd945cef6e444a7c751b8f0548caed6cab Mon Sep 17 00:00:00 2001 From: deweydell Date: Sun, 24 Jan 2016 22:17:39 -0500 Subject: [PATCH 133/152] Add JS to clear text area after submit answer post form --- kiwi/app/assets/javascripts/answers.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kiwi/app/assets/javascripts/answers.js b/kiwi/app/assets/javascripts/answers.js index fb764de..a612856 100644 --- a/kiwi/app/assets/javascripts/answers.js +++ b/kiwi/app/assets/javascripts/answers.js @@ -8,10 +8,10 @@ $(document).on("ready", function(){ type: "POST", data: valuesToSubmit, }).done(function(response) { - console.log("Success!") + $('#answers-form form textarea').val(''); $('.answers-list').append(response); }).fail(function(response) { - console.log("FAILURE") + console.log("FAILURE"); }); }); From e2653092e0e34763b5f201de4fee7ebb067b4c26 Mon Sep 17 00:00:00 2001 From: deweydell Date: Sun, 24 Jan 2016 22:18:28 -0500 Subject: [PATCH 134/152] Remove error handling, not sure how to do it. --- kiwi/app/controllers/answers_controller.rb | 7 ++++--- kiwi/app/views/answers/_form.html.erb | 6 ------ 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/kiwi/app/controllers/answers_controller.rb b/kiwi/app/controllers/answers_controller.rb index 2277635..80ab5a2 100644 --- a/kiwi/app/controllers/answers_controller.rb +++ b/kiwi/app/controllers/answers_controller.rb @@ -4,13 +4,14 @@ class AnswersController < ApplicationController def create @answer = Answer.new(answer_params) if request.xhr? - @answer.save - render partial: "answers/answer_show", layout: false, locals: { answer: @answer} + if @answer.save + render partial: "answers/answer_show", layout: false, locals: { answer: @answer} + end else if @answer.save redirect_to question_path(id: @answer.question_id) else - @errors = Answer.errors.full_messages + @errors = @answer.errors.full_messages redirect_to question_path(id: @answer.question_id) end end diff --git a/kiwi/app/views/answers/_form.html.erb b/kiwi/app/views/answers/_form.html.erb index 2ec76ad..66a45ae 100644 --- a/kiwi/app/views/answers/_form.html.erb +++ b/kiwi/app/views/answers/_form.html.erb @@ -9,10 +9,4 @@
    <%= f.submit 'Post Answer' %> -<% end %> - -<% if @errors %> - <% @errors.each do |msg| %> -

    <%= msg %>

    - <% end %> <% end %> \ No newline at end of file From 89762845dc904eb1ab1af8472b207f9847d3fbf2 Mon Sep 17 00:00:00 2001 From: luisplaz Date: Sun, 24 Jan 2016 23:12:42 -0500 Subject: [PATCH 135/152] Finish Order Filter --- kiwi/app/controllers/questions_controller.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/kiwi/app/controllers/questions_controller.rb b/kiwi/app/controllers/questions_controller.rb index 96e01ea..25375f2 100644 --- a/kiwi/app/controllers/questions_controller.rb +++ b/kiwi/app/controllers/questions_controller.rb @@ -1,15 +1,14 @@ class QuestionsController < ApplicationController def index - @questions = Question.all - if params[:sort] == "trending" + if params[:sort] == "trend" - elsif params[:sort] == "most-recent" - @question = @questions.order(:created_at) + elsif params[:sort] == "recent" + @questions = Question.all.order(:created_at) elsif params[:sort] == "rate" - @question = @questions.sort_by { |question| question.sum_of_votes }.reverse + @questions = Question.all.sort_by { |question| question.sum_of_votes }.reverse else - + @questions = Question.all end end From fe3b20db5e25d9e13e18e06e61c84a7c9598e151 Mon Sep 17 00:00:00 2001 From: luisplaz Date: Sun, 24 Jan 2016 23:13:33 -0500 Subject: [PATCH 136/152] Add trial ajax for view count update --- kiwi/app/assets/javascripts/questions.js | 12 ++++++++ kiwi/app/views/questions/index.html.erb | 36 +++++++++++------------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/kiwi/app/assets/javascripts/questions.js b/kiwi/app/assets/javascripts/questions.js index dee720f..2353bc8 100644 --- a/kiwi/app/assets/javascripts/questions.js +++ b/kiwi/app/assets/javascripts/questions.js @@ -1,2 +1,14 @@ // Place all the behaviors and hooks related to the matching controller here. // All this logic will automatically be available in application.js. +// $(document).on("ready", function(){ +// $('.question-container a').on( "click", function(e) { +// e.preventDefault(); +// var start = $(this).prev().children('.views')[0].innerHTML; +// $(this).prev().children('.views')[0].innerHTML = start+1; +// }); + +// }); + + + +// \ No newline at end of file diff --git a/kiwi/app/views/questions/index.html.erb b/kiwi/app/views/questions/index.html.erb index 308b792..b23e7c1 100644 --- a/kiwi/app/views/questions/index.html.erb +++ b/kiwi/app/views/questions/index.html.erb @@ -10,24 +10,20 @@
    -<% @questions.each do |question| %> -
    -
    - -
    <%= question.sum_of_votes %>
    -
    votes
    - -
    - <%= question.answers.count %> -
    -
    answers
    - -
    0
    -
    views
    - -
    - +<% if @questions %> + <% @questions.each do |question| %> +
    +
    +
    <%= question.sum_of_votes %>
    +
    votes
    +
    + <%= question.answers.count %> +
    +
    answers
    +
    0
    +
    views
    +
    <%= link_to question.title, question_path(question) %> - -
    -<% end %> +
    + <% end %> + <% end %> From 5cceade020ae26cb70b6a8184e8b13d10fd55b70 Mon Sep 17 00:00:00 2001 From: Zach Schatz Date: Sun, 24 Jan 2016 22:22:16 -0500 Subject: [PATCH 137/152] User can only vote once per question and/or answer --- kiwi/app/controllers/votes_controller.rb | 37 ++++++++++++++++++------ 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/kiwi/app/controllers/votes_controller.rb b/kiwi/app/controllers/votes_controller.rb index 28547a2..6477c9b 100644 --- a/kiwi/app/controllers/votes_controller.rb +++ b/kiwi/app/controllers/votes_controller.rb @@ -2,22 +2,41 @@ class VotesController < ApplicationController def create @vote = current_user.votes.new(vote_params) - if !!params[:question_id] && Question.find(params[:question_id]).voted? - @vote.votable_type = "Question" - @vote.votable_id = params[:question_id] - @vote.save - redirect_to question_path(id: @vote.votable_id) + if !!params[:question_id] + @question = Question.find(params[:question_id]) + if !@question.votes.find_by(user_id: current_user) + @vote.votable_type = "Question" + @vote.votable_id = params[:question_id] + @vote.save + redirect_to question_path(id: @vote.votable_id) + else + @question.votes.find_by(user_id: current_user).destroy() + @vote.votable_type = "Question" + @vote.votable_id = params[:question_id] + @vote.save + redirect_to question_path(id: @vote.votable_id) + end elsif !!params[:answer_id] - @vote.votable_type = "Answer" - @vote.votable_id = params[:answer_id] @answer = Answer.find_by(id: params[:answer_id]) - @vote.save - redirect_to question_path(id: @answer.question_id) + if !@answer.votes.find_by(user_id: current_user) + @vote.votable_type = "Answer" + @vote.votable_id = params[:answer_id] + @vote.save + redirect_to question_path(id: @answer.question_id) + else + @answer.votes.find_by(user_id: current_user).destroy() + @vote.votable_type = "Answer" + @vote.votable_id = params[:answer_id] + @vote.save + redirect_to question_path(id: @answer.question_id) + end + else @vote.votable_type = "Comment" @vote.votable_id = params[:comment_id] @vote.save redirect_to question_path(id: @comment.question_id) + # end end end From 58042f25e86a224238fbd2c34c05a2939b6c6b37 Mon Sep 17 00:00:00 2001 From: Zach Schatz Date: Sun, 24 Jan 2016 23:50:20 -0500 Subject: [PATCH 138/152] Remove comment voting from Votes Controller --- kiwi/app/controllers/votes_controller.rb | 7 ------- 1 file changed, 7 deletions(-) diff --git a/kiwi/app/controllers/votes_controller.rb b/kiwi/app/controllers/votes_controller.rb index 6477c9b..0b1b88a 100644 --- a/kiwi/app/controllers/votes_controller.rb +++ b/kiwi/app/controllers/votes_controller.rb @@ -30,13 +30,6 @@ def create @vote.save redirect_to question_path(id: @answer.question_id) end - - else - @vote.votable_type = "Comment" - @vote.votable_id = params[:comment_id] - @vote.save - redirect_to question_path(id: @comment.question_id) - # end end end From d2e88ae8e7d8d7b9a42d3df59071c4f696f59f6f Mon Sep 17 00:00:00 2001 From: Zach Schatz Date: Sun, 24 Jan 2016 23:54:01 -0500 Subject: [PATCH 139/152] Indent fix --- kiwi/app/controllers/votes_controller.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/kiwi/app/controllers/votes_controller.rb b/kiwi/app/controllers/votes_controller.rb index 0b1b88a..9d6ad24 100644 --- a/kiwi/app/controllers/votes_controller.rb +++ b/kiwi/app/controllers/votes_controller.rb @@ -33,11 +33,11 @@ def create end end -private + private -def vote_params - params.require(:vote).permit(:direction,:user_id) -end + def vote_params + params.require(:vote).permit(:direction,:user_id) + end # def vote_type # if !!params[:question_id] From c5e7adf8497a03897b8f41932052398f135e7699 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sun, 24 Jan 2016 23:42:47 -0500 Subject: [PATCH 140/152] Add best_answer_path to routes --- kiwi/config/routes.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/kiwi/config/routes.rb b/kiwi/config/routes.rb index 5759aea..461b3fc 100644 --- a/kiwi/config/routes.rb +++ b/kiwi/config/routes.rb @@ -14,6 +14,7 @@ resources :answers do resources :comments, only: [:new, :create] resources :votes, only: [:create, :destroy] + patch 'best', on: :member end From 0c8417f60a94d0b9b199cdd93f097e3339639a9e Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sun, 24 Jan 2016 23:44:05 -0500 Subject: [PATCH 141/152] Add bottom margin to .vote-container in main.css --- kiwi/app/assets/stylesheets/main.css | 1 + 1 file changed, 1 insertion(+) diff --git a/kiwi/app/assets/stylesheets/main.css b/kiwi/app/assets/stylesheets/main.css index 4e89bfc..3db6b37 100644 --- a/kiwi/app/assets/stylesheets/main.css +++ b/kiwi/app/assets/stylesheets/main.css @@ -143,6 +143,7 @@ label{ width: 2em; float: left; margin-right: 1em; + margin-bottom: 0.5em; font-size: 1em; } From 18e7f830ae45074f20c2c523c77c57bef8965968 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Sun, 24 Jan 2016 23:55:44 -0500 Subject: [PATCH 142/152] Implement best_answer_path and answers#best route; user can choose best answer --- kiwi/app/controllers/answers_controller.rb | 7 ++++++ kiwi/app/views/answers/_answer_show.html.erb | 23 +++++++++++++++----- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/kiwi/app/controllers/answers_controller.rb b/kiwi/app/controllers/answers_controller.rb index 80ab5a2..3884c52 100644 --- a/kiwi/app/controllers/answers_controller.rb +++ b/kiwi/app/controllers/answers_controller.rb @@ -17,6 +17,13 @@ def create end end + def best + # binding.pry + @answer = Answer.find_by(id: params[:id]) + @answer.question.update(best_answer_id: @answer.id) + redirect_to question_path(id: @answer.question_id) + end + private def answer_params diff --git a/kiwi/app/views/answers/_answer_show.html.erb b/kiwi/app/views/answers/_answer_show.html.erb index 1162fcb..d8432c7 100644 --- a/kiwi/app/views/answers/_answer_show.html.erb +++ b/kiwi/app/views/answers/_answer_show.html.erb @@ -4,6 +4,16 @@
  • <%= link_to '▲'.html_safe, answer_votes_path(answer, vote: {direction: '1'}), method: :post %>
  • <%= answer.sum_of_votes %>
  • <%= link_to '▼'.html_safe, answer_votes_path(answer, vote: {direction: '-1'}), method: :post %>
  • + <%# binding.pry %> + <% if @question.best_answer_id == answer.id %> +
  • BEST!
  • + <% else %> + <% if @question.user == current_user && !@question.best_answer_id %> +
  • + <%= link_to "Best?", best_answer_path(answer.id), method: :patch %> +
  • + <% end %> + <% end %>
    @@ -11,16 +21,17 @@

    <%= answer.content %>

    <% if !answer.comments.empty? %> -
    Comments:
    +
    Comments:
    <% answer.comments.each do |comment| %>

    <%= comment.content %> • - <%= comment.user.name %> <%= std_format_date(comment.created_at) %><% if current_user == comment.user %> • <%= link_to "Edit", edit_comment_path(comment.id) %> • <%= link_to "Delete",comment_path(comment.id), method: :delete, data: {confirm: 'Are you sure?'} %><% end %>

    - <% end %> + <%= comment.user.name %> <%= std_format_date(comment.created_at) %><% if current_user == comment.user %> • <%= link_to "Edit", edit_comment_path(comment.id) %> • <%= link_to "Delete",comment_path(comment.id), method: :delete, data: {confirm: 'Are you sure?'} %><% end %> +

    + <% end %>
    - <% end %> + <% end %> - <% if logged_in? %> + <% if logged_in? %> <%= link_to "Add Comment", new_answer_comment_path(answer.id) %> - <% end %> + <% end %>
    From 610489382b9c81d1d389810a643d6215070e8b36 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Mon, 25 Jan 2016 00:18:44 -0500 Subject: [PATCH 143/152] Add best_answer getter method to Question model --- kiwi/app/models/question.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/kiwi/app/models/question.rb b/kiwi/app/models/question.rb index c451d01..d14701b 100644 --- a/kiwi/app/models/question.rb +++ b/kiwi/app/models/question.rb @@ -7,4 +7,9 @@ class Question < ActiveRecord::Base has_many :votes, as: :votable validates_presence_of :title, :content, :user_id + + def best_answer + Answer.find_by(id: self.best_answer_id) + end + end From d1ce905b5bdefad5f67f61785d2de29db18b7acf Mon Sep 17 00:00:00 2001 From: Jeff George Date: Mon, 25 Jan 2016 00:19:37 -0500 Subject: [PATCH 144/152] Pull best answer out of order and display as first answer in list --- kiwi/app/views/questions/show.html.erb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/kiwi/app/views/questions/show.html.erb b/kiwi/app/views/questions/show.html.erb index 0d68fd4..62bbaa2 100644 --- a/kiwi/app/views/questions/show.html.erb +++ b/kiwi/app/views/questions/show.html.erb @@ -40,17 +40,21 @@ <% if !@question.answers.empty? %>

    Answers: <%=@question.answers.count %>

    + <% if @question.best_answer_id %> + <%= render partial: "answers/answer_show", layout: false, locals: { answer: @question.best_answer} %> + <% end %> <% @question.answers.each do |answer|%> - - <%= render partial: "answers/answer_show", layout: false, locals: { answer: answer} %> + <% if answer.id != @question.best_answer_id %> + <%= render partial: "answers/answer_show", layout: false, locals: { answer: answer} %> + <% end %> <% end %> -
    +
    <% end %>

    Add Your Answer:

    -<%= render "answers/form" %> + <%= render "answers/form" %>
    From 3318368332c103b0c90a6b5e1061a0bd8d8bec06 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Mon, 25 Jan 2016 00:43:08 -0500 Subject: [PATCH 145/152] Implement sorting of answers on questions#show view, with best answer first --- kiwi/app/controllers/questions_controller.rb | 1 + kiwi/app/models/question.rb | 4 ++ kiwi/app/views/questions/show.html.erb | 51 ++++++++++---------- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/kiwi/app/controllers/questions_controller.rb b/kiwi/app/controllers/questions_controller.rb index 25375f2..a99f407 100644 --- a/kiwi/app/controllers/questions_controller.rb +++ b/kiwi/app/controllers/questions_controller.rb @@ -28,6 +28,7 @@ def create def show @question = Question.includes(:answers, :user).find(params[:id]) + @answers = @question.sort_answers_by_votes @answer = Answer.new end diff --git a/kiwi/app/models/question.rb b/kiwi/app/models/question.rb index d14701b..860be6b 100644 --- a/kiwi/app/models/question.rb +++ b/kiwi/app/models/question.rb @@ -12,4 +12,8 @@ def best_answer Answer.find_by(id: self.best_answer_id) end + def sort_answers_by_votes + self.answers.sort_by { |answer| answer.sum_of_votes }.reverse + end + end diff --git a/kiwi/app/views/questions/show.html.erb b/kiwi/app/views/questions/show.html.erb index 62bbaa2..067f326 100644 --- a/kiwi/app/views/questions/show.html.erb +++ b/kiwi/app/views/questions/show.html.erb @@ -1,33 +1,33 @@

    <%= @question.title %>

    -
    -
      -
    • <%= link_to '▲'.html_safe, question_votes_path(@question, vote: {direction: '1'}), method: :post %>
    • -
    • <%= @question.sum_of_votes %>
    • -
    • <%= link_to '▼'.html_safe, question_votes_path(@question, vote: {direction: '-1'}), method: :post %>
    • -
    -
    +
    +
      +
    • <%= link_to '▲'.html_safe, question_votes_path(@question, vote: {direction: '1'}), method: :post %>
    • +
    • <%= @question.sum_of_votes %>
    • +
    • <%= link_to '▼'.html_safe, question_votes_path(@question, vote: {direction: '-1'}), method: :post %>
    • +
    +
    - Asked by <%= @question.user.name %> <%= std_format_date(@question.created_at) %> + Asked by <%= @question.user.name %> <%= std_format_date(@question.created_at) %> -

    <%= @question.content %>

    +

    <%= @question.content %>

    -
      - <% if current_user == @question.user %> -
    • <%= link_to "Edit my question", edit_question_path %>
    • -
    • <%= link_to "Delete my question", question_path, method: :delete %>
    • - <% end %> -
    +
      + <% if current_user == @question.user %> +
    • <%= link_to "Edit my question", edit_question_path %>
    • +
    • <%= link_to "Delete my question", question_path, method: :delete %>
    • + <% end %> +
    - <% if !@question.comments.empty? %> -
    -
    Comments:
    - <% @question.comments.each do |comment| %> -

    <%= comment.content %> • <%= comment.user.name %> <%= std_format_date(comment.created_at) %> <% if current_user == comment.user %> • <%= link_to "Edit", edit_comment_path(comment.id) %> • <%= link_to "Delete",comment_path(comment.id), method: :delete, data: {confirm: 'Are you sure?'} %><% end %>

    - <% end %> -
    + <% if !@question.comments.empty? %> +
    +
    Comments:
    + <% @question.comments.each do |comment| %> +

    <%= comment.content %> • <%= comment.user.name %> <%= std_format_date(comment.created_at) %> <% if current_user == comment.user %> • <%= link_to "Edit", edit_comment_path(comment.id) %> • <%= link_to "Delete",comment_path(comment.id), method: :delete, data: {confirm: 'Are you sure?'} %><% end %>

    <% end %> +
    + <% end %>
    <% if logged_in? %> @@ -37,14 +37,13 @@ <% end %> -<% if !@question.answers.empty? %> +<% if !@answers.empty? %>
    -

    Answers: <%=@question.answers.count %>

    +

    Answers: <%= @answers.count %>

    <% if @question.best_answer_id %> <%= render partial: "answers/answer_show", layout: false, locals: { answer: @question.best_answer} %> <% end %> - <% @question.answers.each do |answer|%> - + <% @answers.each do |answer|%> <% if answer.id != @question.best_answer_id %> <%= render partial: "answers/answer_show", layout: false, locals: { answer: answer} %> <% end %> From bc8878de8e9c5a1f19bdb7420a9b5f49a5f84d6f Mon Sep 17 00:00:00 2001 From: Jeff George Date: Mon, 25 Jan 2016 00:51:35 -0500 Subject: [PATCH 146/152] Sort comments by created_at on questions#show --- kiwi/app/views/answers/_answer_show.html.erb | 2 +- kiwi/app/views/questions/show.html.erb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/kiwi/app/views/answers/_answer_show.html.erb b/kiwi/app/views/answers/_answer_show.html.erb index d8432c7..f841208 100644 --- a/kiwi/app/views/answers/_answer_show.html.erb +++ b/kiwi/app/views/answers/_answer_show.html.erb @@ -23,7 +23,7 @@ <% if !answer.comments.empty? %>
    Comments:
    - <% answer.comments.each do |comment| %> + <% answer.comments.order(:created_at).each do |comment| %>

    <%= comment.content %> • <%= comment.user.name %> <%= std_format_date(comment.created_at) %><% if current_user == comment.user %> • <%= link_to "Edit", edit_comment_path(comment.id) %> • <%= link_to "Delete",comment_path(comment.id), method: :delete, data: {confirm: 'Are you sure?'} %><% end %>

    diff --git a/kiwi/app/views/questions/show.html.erb b/kiwi/app/views/questions/show.html.erb index 067f326..15ac5a3 100644 --- a/kiwi/app/views/questions/show.html.erb +++ b/kiwi/app/views/questions/show.html.erb @@ -23,7 +23,7 @@ <% if !@question.comments.empty? %>
    Comments:
    - <% @question.comments.each do |comment| %> + <% @question.comments.order(:created_at).each do |comment| %>

    <%= comment.content %> • <%= comment.user.name %> <%= std_format_date(comment.created_at) %> <% if current_user == comment.user %> • <%= link_to "Edit", edit_comment_path(comment.id) %> • <%= link_to "Delete",comment_path(comment.id), method: :delete, data: {confirm: 'Are you sure?'} %><% end %>

    <% end %>
    From 4db0dc697cdf63e71da0faf01c94adb6ae0d0837 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Mon, 25 Jan 2016 01:52:25 -0500 Subject: [PATCH 147/152] Fix bug with answer comments in comments#create --- kiwi/app/controllers/comments_controller.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/kiwi/app/controllers/comments_controller.rb b/kiwi/app/controllers/comments_controller.rb index 3b7ea35..8ba1325 100644 --- a/kiwi/app/controllers/comments_controller.rb +++ b/kiwi/app/controllers/comments_controller.rb @@ -18,7 +18,11 @@ def create @comment = answer.comments.new(user: current_user, content: params[:comment][:content]) end if @comment.save - redirect_to question_path(id: @comment.commentable_id) + if @comment.commentable_type == Question + redirect_to question_path(id: @comment.commentable_id) + else + redirect_to question_path(id: @comment.commentable.question_id) + end else render :new end From 02294dc82d29ae775685b880c66c5b1a38a9a3af Mon Sep 17 00:00:00 2001 From: Jeff George Date: Mon, 25 Jan 2016 01:59:23 -0500 Subject: [PATCH 148/152] Fix bug with answer comments in comments#update --- kiwi/app/controllers/comments_controller.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/kiwi/app/controllers/comments_controller.rb b/kiwi/app/controllers/comments_controller.rb index 8ba1325..5c1f79a 100644 --- a/kiwi/app/controllers/comments_controller.rb +++ b/kiwi/app/controllers/comments_controller.rb @@ -18,7 +18,7 @@ def create @comment = answer.comments.new(user: current_user, content: params[:comment][:content]) end if @comment.save - if @comment.commentable_type == Question + if @comment.commentable_type == "Question" redirect_to question_path(id: @comment.commentable_id) else redirect_to question_path(id: @comment.commentable.question_id) @@ -36,7 +36,11 @@ def edit def update @comment = Comment.find_by(id: params[:id]) if @comment.update_attributes(comment_params) - redirect_to question_path(id: @comment.commentable_id) + if @comment.commentable_type == "Question" + redirect_to question_path(id: @comment.commentable_id) + else + redirect_to question_path(id: @comment.commentable.question_id) + end else render :edit end From ab7c4a55ecf7ba7a5abc52cbc0b1ae3b34b74fd9 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Mon, 25 Jan 2016 02:11:44 -0500 Subject: [PATCH 149/152] Implement #redirect_to_question_path in comments_controller; refactor comments#create, #update and #destroy --- kiwi/app/controllers/comments_controller.rb | 33 +++++++++------------ 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/kiwi/app/controllers/comments_controller.rb b/kiwi/app/controllers/comments_controller.rb index 5c1f79a..679eefa 100644 --- a/kiwi/app/controllers/comments_controller.rb +++ b/kiwi/app/controllers/comments_controller.rb @@ -18,11 +18,7 @@ def create @comment = answer.comments.new(user: current_user, content: params[:comment][:content]) end if @comment.save - if @comment.commentable_type == "Question" - redirect_to question_path(id: @comment.commentable_id) - else - redirect_to question_path(id: @comment.commentable.question_id) - end + redirect_to_question_path else render :new end @@ -36,11 +32,7 @@ def edit def update @comment = Comment.find_by(id: params[:id]) if @comment.update_attributes(comment_params) - if @comment.commentable_type == "Question" - redirect_to question_path(id: @comment.commentable_id) - else - redirect_to question_path(id: @comment.commentable.question_id) - end + redirect_to_question_path else render :edit end @@ -48,19 +40,22 @@ def update def destroy @comment = Comment.find_by(id: params[:id]) - commentable = @comment.commentable @comment.destroy - if commentable.is_a? Question - redirect_to question_path(commentable.id) - else - redirect_to question_path(commentable.question.id) - end + redirect_to_question_path end private - def comment_params - params.require(:comment).permit(:content, :commentable_type, :commentable_id, :user_id) - end + def comment_params + params.require(:comment).permit(:content, :commentable_type, :commentable_id, :user_id) + end + + def redirect_to_question_path + if @comment.commentable_type == "Question" + redirect_to question_path(id: @comment.commentable_id) + else + redirect_to question_path(id: @comment.commentable.question_id) + end + end end \ No newline at end of file From f3c16cdead5c474b9c4a9643fc74121a12c654f9 Mon Sep 17 00:00:00 2001 From: Jeff George Date: Mon, 25 Jan 2016 02:51:58 -0500 Subject: [PATCH 150/152] Fix indenting and missing div tag in questions/index view --- kiwi/app/assets/stylesheets/main.css | 4 ++++ kiwi/app/views/questions/index.html.erb | 28 +++++++++++++------------ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/kiwi/app/assets/stylesheets/main.css b/kiwi/app/assets/stylesheets/main.css index 3db6b37..78d160b 100644 --- a/kiwi/app/assets/stylesheets/main.css +++ b/kiwi/app/assets/stylesheets/main.css @@ -128,6 +128,10 @@ label{ margin-right: 2em; } +.question-links { + display: inline; +} + .votes, .answers, .views { display: inline; font-weight: 900; diff --git a/kiwi/app/views/questions/index.html.erb b/kiwi/app/views/questions/index.html.erb index b23e7c1..fa7a71e 100644 --- a/kiwi/app/views/questions/index.html.erb +++ b/kiwi/app/views/questions/index.html.erb @@ -12,18 +12,20 @@ <% if @questions %> <% @questions.each do |question| %> -
    -
    -
    <%= question.sum_of_votes %>
    -
    votes
    -
    - <%= question.answers.count %> +
    +
    +
    <%= question.sum_of_votes %>
    +
    votes
    + +
    <%= question.answers.count %>
    +
    answers
    + +
    0
    +
    views
    -
    answers
    -
    0
    -
    views
    -
    - <%= link_to question.title, question_path(question) %> -
    - <% end %> + +
    <% end %> +<% end %> From 37bbd0d9479be270e395d7259f1ece48204159ed Mon Sep 17 00:00:00 2001 From: Jeff George Date: Mon, 25 Jan 2016 03:10:46 -0500 Subject: [PATCH 151/152] Add views column to questions table --- .../20160125075431_add_views_column_to_questions.rb | 5 +++++ kiwi/db/schema.rb | 13 +++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) create mode 100644 kiwi/db/migrate/20160125075431_add_views_column_to_questions.rb diff --git a/kiwi/db/migrate/20160125075431_add_views_column_to_questions.rb b/kiwi/db/migrate/20160125075431_add_views_column_to_questions.rb new file mode 100644 index 0000000..e61fd80 --- /dev/null +++ b/kiwi/db/migrate/20160125075431_add_views_column_to_questions.rb @@ -0,0 +1,5 @@ +class AddViewsColumnToQuestions < ActiveRecord::Migration + def change + add_column( :questions, :views, :integer, default: 0 ) + end +end diff --git a/kiwi/db/schema.rb b/kiwi/db/schema.rb index 27db35c..74b798d 100644 --- a/kiwi/db/schema.rb +++ b/kiwi/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: 20160122232556) do +ActiveRecord::Schema.define(version: 20160125075431) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -39,12 +39,13 @@ add_index "comments", ["commentable_id"], name: "index_comments_on_commentable_id", using: :btree create_table "questions", force: :cascade do |t| - t.string "title", null: false - t.text "content", null: false - t.integer "user_id", null: false + t.string "title", null: false + t.text "content", null: false + t.integer "user_id", null: false t.integer "best_answer_id" - 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 add_index "questions", ["user_id"], name: "index_questions_on_user_id", using: :btree From 7b3117c39a64ff2f5898a7ecced059dd95f5ebac Mon Sep 17 00:00:00 2001 From: Jeff George Date: Mon, 25 Jan 2016 03:11:36 -0500 Subject: [PATCH 152/152] Implement display of views count on questions/index view --- kiwi/app/controllers/questions_controller.rb | 2 ++ kiwi/app/views/questions/index.html.erb | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/kiwi/app/controllers/questions_controller.rb b/kiwi/app/controllers/questions_controller.rb index a99f407..7d3296e 100644 --- a/kiwi/app/controllers/questions_controller.rb +++ b/kiwi/app/controllers/questions_controller.rb @@ -28,6 +28,8 @@ def create def show @question = Question.includes(:answers, :user).find(params[:id]) + @question.increment(:views) + @question.save @answers = @question.sort_answers_by_votes @answer = Answer.new end diff --git a/kiwi/app/views/questions/index.html.erb b/kiwi/app/views/questions/index.html.erb index fa7a71e..c872ad3 100644 --- a/kiwi/app/views/questions/index.html.erb +++ b/kiwi/app/views/questions/index.html.erb @@ -20,7 +20,7 @@
    <%= question.answers.count %>
    answers
    -
    0
    +
    <%= question.views %>
    views