From 1db9ab4b0ef9c7efcfe2c943177aee6091ff8f15 Mon Sep 17 00:00:00 2001 From: ys00065 Date: Thu, 14 Mar 2019 22:14:41 +0000 Subject: [PATCH] added comment to post, on clicking the comment count , it takes to the post show, started adding heart button in post --- Gemfile | 2 ++ Gemfile.lock | 5 ++- app/assets/images/comments_img.png | Bin 0 -> 5183 bytes app/assets/javascripts/comments.coffee | 3 ++ app/controllers/comments_controller.rb | 34 ++++++++++++++++++ app/controllers/posts_controller.rb | 1 + app/helpers/comments_helper.rb | 2 ++ app/models/comment.rb | 4 +++ app/models/post.rb | 6 +++- app/models/post_heart.rb | 6 ++++ app/models/user.rb | 3 +- app/views/comments/_form.html.haml | 19 ++++++++++ app/views/comments/show.html.haml | 1 + app/views/posts/index.html.haml | 5 ++- app/views/posts/show.html.haml | 16 +++++++-- config/routes.rb | 12 ++++--- db/migrate/20190224195706_create_comments.rb | 11 ++++++ .../20190310152745_create_post_hearts.rb | 10 ++++++ ...0310152829_add_deleted_at_to_post_heart.rb | 6 ++++ db/schema.rb | 27 +++++++++++++- spec/controllers/comments_controller_spec.rb | 5 +++ spec/helpers/comments_helper_spec.rb | 15 ++++++++ spec/models/comment_spec.rb | 5 +++ spec/models/post_heart_spec.rb | 5 +++ 24 files changed, 190 insertions(+), 13 deletions(-) create mode 100644 app/assets/images/comments_img.png create mode 100644 app/assets/javascripts/comments.coffee create mode 100644 app/controllers/comments_controller.rb create mode 100644 app/helpers/comments_helper.rb create mode 100644 app/models/comment.rb create mode 100644 app/models/post_heart.rb create mode 100644 app/views/comments/_form.html.haml create mode 100644 app/views/comments/show.html.haml create mode 100644 db/migrate/20190224195706_create_comments.rb create mode 100644 db/migrate/20190310152745_create_post_hearts.rb create mode 100644 db/migrate/20190310152829_add_deleted_at_to_post_heart.rb create mode 100644 spec/controllers/comments_controller_spec.rb create mode 100644 spec/helpers/comments_helper_spec.rb create mode 100644 spec/models/comment_spec.rb create mode 100644 spec/models/post_heart_spec.rb diff --git a/Gemfile b/Gemfile index 4fe5745..839fb9f 100644 --- a/Gemfile +++ b/Gemfile @@ -88,3 +88,5 @@ gem 'annotate' # gem 'erb2haml', :group => :development gem 'haml', '~> 5.0.0.beta.2' + +gem 'paranoia', '~> 2.1', '>= 2.1.5' diff --git a/Gemfile.lock b/Gemfile.lock index e6efb3a..5be6eb2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -141,6 +141,8 @@ GEM nokogiri (1.10.1) mini_portile2 (~> 2.4.0) orm_adapter (0.5.0) + paranoia (2.4.1) + activerecord (>= 4.0, < 5.3) pg (0.19.0) puma (3.12.0) rack (2.0.6) @@ -265,6 +267,7 @@ DEPENDENCIES jbuilder (~> 2.5) jquery-rails listen (~> 3.0.5) + paranoia (~> 2.1, >= 2.1.5) pg (~> 0.19.0) prettify (~> 0.1.0)! puma (~> 3.0) @@ -285,4 +288,4 @@ RUBY VERSION ruby 2.5.0p0 BUNDLED WITH - 1.16.1 + 1.17.3 diff --git a/app/assets/images/comments_img.png b/app/assets/images/comments_img.png new file mode 100644 index 0000000000000000000000000000000000000000..e3006c563a09ecf97126fe8a82b6d38ff780c1e4 GIT binary patch literal 5183 zcmV-F6u|3=P)z1^@s6`(2wD00009a7bBm001r{ z001r{0eGc9b^rhq@kvBMRCt{2on34k*LBDLL$QLWGMJ4*TM{gL$6u9D3)qoS6a^w+ zM?fGa?E_bRhye%fu23m2K|=xoTH7yNKLqJhxe|&3g$3{CAu3=!aM`>hDhiGORX|X~ z94B!h7q&SoQ57Z8+M&vd3`^?vVRoqH?46lAv-7#UKVTqoW_I>!`M+n+IrrRq1wsfo zoZZN9(hUd;K@cPW5`Y{4*>;(BE-e(3ZUBW(02l!D&Y=O&2_ag7Z3-PBi6sK|3V0vOS&=rkO}d3thxfA)cIFTt~DGRivWHO-~xdEB3Terht!H+ z5QH3nO6Mpf+8B->{sX{I0Q`awvX*pX+(@1H1wl{%Oad5BxG9|AJOS`O09pY42|zuK z9U(R17X+aKpb8-0@3RVp0&=A{5&a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fbo5=T8~_v=m}nF;&0Gi>Elf}oLF!PbnVC8dK`$pI)M_Uru7^x{mkWdyn<@zi_+g==K1BAP5qG1=_WwMtwQE8%0rAOP4<+guLl? zgX==I9qUi+X58=R#7(VMa}xe0o))j&>HPby*YjL0KVWXw3v-%dPubt}^fV?XC%s$E z;q-~F>yTxc)z`mB2$}G@$y59QAP7PUzzpr${QP|8Hn8Jl4_TI3S7rc!{e+M;ubY_G z=O=_T0Z_~4N~IFd{%|5?a&pp-@K3P_AMm$1MFo_4|5mHT{eMnmG#U+DxNw2>_A8xv zDNHbnzcbIbM7uUSJIhCZIiaIznxB|*1(ZQqH<*0>9WM}sDu5~4H9q>w2@9VAE&#kn z2>DMx@8Mhg03Zm03V^!D`^?M?N+s$S1c$?fVHi*p#mUcq0&}B@4FLOKuDkyOLdZY* zeV?G>cT9hZq6k%0c?O6R2oK@YxB}o!`$QEM0*N2kp7N-|+}w8uyaRB85J3310px=S zALwH%(oVq;)xTL;S%IP`iLdv`$qoU9e>AA@fj;-)?WC2?(ypzntl-qCQ+)a(#}iG{ zAW4!R;RB}dnezEg^t}l+A*2C^4gdgW&z|LJP>v(bW)q5{_{r)3<`N`*2_FDJ2npqY zIS59aj&iw7+U+*sa4e>#rX2O+a=b#q(rj1{A`Bg%P$-c3`T0mcP)_=3x7(yxEczAw ztuTd8!WKUW@+3Z*Ffa&6tN!NxEaS~OkQ$j>m|IwpIC>DzlMG=akKv5J(lEl{# z3K>n)U>F8eRfVqW&@?R!v0Ef6dW)#UUjxvybVR>1y=G@;QK?iMC(IX%MdWh19i`N= zEF+h5-gd_@3}~9;igt+-cU7#e>p_E5R;~beH)+vt6ZWVXfE*|zy!m`S@TlNNAALj= zMF~;|!jI&STL8`>f)U!jwHkr=Cutu-y "alert"} + %button.close{"data-dismiss" => "alert", :type => "button"} + %span{"aria-hidden" => "true"} × + %span.sr-only Close + %h4 + = pluralize(@post.errors.count, "error") + prohibited this post from being saved: + %ul + - @post.errors.full_messages.each do |msg| + %li= msg + .form-group + = f.label :Comment, class: "col-sm-2 control-label" + .col-sm-10 + = f.text_area :content, class: "form-control" + .form-group + .col-sm-offset-2.col-sm-10 + = f.submit class: "btn btn-primary" diff --git a/app/views/comments/show.html.haml b/app/views/comments/show.html.haml new file mode 100644 index 0000000..cbec711 --- /dev/null +++ b/app/views/comments/show.html.haml @@ -0,0 +1 @@ += @comment.content diff --git a/app/views/posts/index.html.haml b/app/views/posts/index.html.haml index 8e19102..9c411a0 100644 --- a/app/views/posts/index.html.haml +++ b/app/views/posts/index.html.haml @@ -34,10 +34,13 @@ %div{:style => 'border-style: dotted none none none;border-width:1px;padding-top:4px'} %span = image_tag('green-heart.png', class: 'img-circle', size: '20x20') - %span.complementary 1 k + = post[:post_heart_count].to_s %span.twelve_left_margin = image_tag('blue-pulse.png', class: 'img-circle', size: '30x20') %span.complementary 7 k + %span.twelve_left_margin + = link_to image_tag('comments_img.png', class: 'img-circle', size: '30x20') +' ' + post[:comment_count].to_s, 'posts/' + post[:id].to_s + / 01. End div .col-md-2 .col-md-12.wrapper_colour{:style => 'border-radius:0px;'} diff --git a/app/views/posts/show.html.haml b/app/views/posts/show.html.haml index 9ef0087..5b79ff3 100644 --- a/app/views/posts/show.html.haml +++ b/app/views/posts/show.html.haml @@ -12,13 +12,23 @@ /
Title:
- +
Content:
- +
User:
- +
Category:
%p= markdown(@post.content) + - @comments.each do |comment| + .row + .col-xs-6 + = comment[:user_id] + .col-xs-6 + = time_ago_in_words(comment[:created_at]) + .row + .col-xs-12 + = comment[:content] + = render 'comments/form' diff --git a/config/routes.rb b/config/routes.rb index 82e109f..3698f39 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,16 +5,18 @@ collection do get 'search_workout' => 'user_workouts#search_workout' end - end - - resources :workouts + end + + resources :workouts # get 'dashboards/index' get "/dashboards", to: "dashboards#index" resources :meals - resources :posts + resources :posts do + resources :comments + end resources :categories - devise_for :users + devise_for :users resources :todos root to: "dashboards#index" diff --git a/db/migrate/20190224195706_create_comments.rb b/db/migrate/20190224195706_create_comments.rb new file mode 100644 index 0000000..1a0e0ae --- /dev/null +++ b/db/migrate/20190224195706_create_comments.rb @@ -0,0 +1,11 @@ +class CreateComments < ActiveRecord::Migration[5.2] + def change + create_table :comments do |t| + t.text :content + t.references :post, foreign_key: true + t.references :user, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/migrate/20190310152745_create_post_hearts.rb b/db/migrate/20190310152745_create_post_hearts.rb new file mode 100644 index 0000000..22aa0bb --- /dev/null +++ b/db/migrate/20190310152745_create_post_hearts.rb @@ -0,0 +1,10 @@ +class CreatePostHearts < ActiveRecord::Migration[5.2] + def change + create_table :post_hearts do |t| + t.references :post, foreign_key: true + t.references :user, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/migrate/20190310152829_add_deleted_at_to_post_heart.rb b/db/migrate/20190310152829_add_deleted_at_to_post_heart.rb new file mode 100644 index 0000000..b2b5bd7 --- /dev/null +++ b/db/migrate/20190310152829_add_deleted_at_to_post_heart.rb @@ -0,0 +1,6 @@ +class AddDeletedAtToPostHeart < ActiveRecord::Migration[5.2] + def change + add_column :post_hearts, :deleted_at, :datetime + add_index :post_hearts, :deleted_at + end +end diff --git a/db/schema.rb b/db/schema.rb index 4365e2c..2a0b8a0 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_02_06_213404) do +ActiveRecord::Schema.define(version: 2019_03_10_152829) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -22,6 +22,16 @@ t.datetime "updated_at", null: false end + create_table "comments", force: :cascade do |t| + t.text "content" + t.bigint "post_id" + t.bigint "user_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["post_id"], name: "index_comments_on_post_id" + t.index ["user_id"], name: "index_comments_on_user_id" + end + create_table "meals", force: :cascade do |t| t.text "name" t.float "calorie" @@ -35,6 +45,17 @@ t.datetime "updated_at", null: false end + create_table "post_hearts", force: :cascade do |t| + t.bigint "post_id" + t.bigint "user_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.datetime "deleted_at" + t.index ["deleted_at"], name: "index_post_hearts_on_deleted_at" + t.index ["post_id"], name: "index_post_hearts_on_post_id" + t.index ["user_id"], name: "index_post_hearts_on_user_id" + end + create_table "posts", force: :cascade do |t| t.string "title" t.text "content" @@ -102,6 +123,10 @@ t.index ["workout_category_id"], name: "index_workouts_on_workout_category_id", using: :btree end + add_foreign_key "comments", "posts" + add_foreign_key "comments", "users" + add_foreign_key "post_hearts", "posts" + add_foreign_key "post_hearts", "users" add_foreign_key "posts", "categories" add_foreign_key "posts", "users" add_foreign_key "user_workouts", "users" diff --git a/spec/controllers/comments_controller_spec.rb b/spec/controllers/comments_controller_spec.rb new file mode 100644 index 0000000..ebb867f --- /dev/null +++ b/spec/controllers/comments_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe CommentsController, type: :controller do + +end diff --git a/spec/helpers/comments_helper_spec.rb b/spec/helpers/comments_helper_spec.rb new file mode 100644 index 0000000..729cd87 --- /dev/null +++ b/spec/helpers/comments_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the CommentsHelper. For example: +# +# describe CommentsHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe CommentsHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb new file mode 100644 index 0000000..c10688d --- /dev/null +++ b/spec/models/comment_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Comment, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/post_heart_spec.rb b/spec/models/post_heart_spec.rb new file mode 100644 index 0000000..acf03a1 --- /dev/null +++ b/spec/models/post_heart_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe PostHeart, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end