diff --git a/Gemfile b/Gemfile index 3987197..95f8de6 100644 --- a/Gemfile +++ b/Gemfile @@ -22,6 +22,7 @@ gem 'haml', '~> 5.0.0.beta.2' # gem 'haml2erb', '~> 0.2.1' gem 'jbuilder', '~> 2.5' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jquery-rails' # Use jquery as the JavaScript library +gem 'paranoia', '~> 2.1', '>= 2.1.5' # when you called destroy on an Active Record object that it didn't actually destroy gem 'pg', '~> 0.19.0' gem 'prettify', '~> 0.1.0', git: 'https://github.com/rajgng/prettify.git' gem 'puma', '~> 3.0' # Use Puma as the app server @@ -54,5 +55,8 @@ end # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' + + # Use Capistrano for deployment # gem 'capistrano-rails', group: :development + diff --git a/Gemfile.lock b/Gemfile.lock index ce8663a..f765add 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -155,6 +155,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) parallel (1.13.0) parser (2.6.0.0) ast (~> 2.4.0) @@ -299,6 +301,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) @@ -321,4 +324,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 0000000..e3006c5 Binary files /dev/null and b/app/assets/images/comments_img.png differ diff --git a/app/assets/javascripts/comments.coffee b/app/assets/javascripts/comments.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/comments.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb new file mode 100644 index 0000000..7ba1214 --- /dev/null +++ b/app/controllers/comments_controller.rb @@ -0,0 +1,34 @@ +class CommentsController < ApplicationController + before_action :authenticate_user!, except: [:index, :show] + before_action :set_comment, only: [:show, :destroy] + def show + end + def create + @post = Post.find(params[:post_id]) + @comment = @post.comments.create(comment_params) + @comment.user_id = current_user.id + + if @comment.save + redirect_to post_path(@post) + else + render 'new' + end + end + + def destroy + @post = Post.find(params[:post_id]) + @comment = @post.comments.find(params[:id]) + @comment.destroy + redirect_to post_path(@post) + end + + private + + def comment_params + params.require(:comment).permit(:content) + end + # Use callbacks to share common setup or constraints between actions. + def set_comment + @comment = Comment.find(params[:id]) + end +end diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index ea74ad5..328e912 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -70,6 +70,7 @@ def destroy # Use callbacks to share common setup or constraints between actions. def set_post @post = Post.find(params[:id]) + @comments=@post.comments end # Never trust parameters from the scary internet, only allow the white list through. diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb new file mode 100644 index 0000000..0ec9ca5 --- /dev/null +++ b/app/helpers/comments_helper.rb @@ -0,0 +1,2 @@ +module CommentsHelper +end diff --git a/app/models/comment.rb b/app/models/comment.rb new file mode 100644 index 0000000..4a019df --- /dev/null +++ b/app/models/comment.rb @@ -0,0 +1,4 @@ +class Comment < ApplicationRecord + belongs_to :post + belongs_to :user +end diff --git a/app/models/post.rb b/app/models/post.rb index dfe6783..a5fb3fb 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -14,6 +14,8 @@ class Post < ApplicationRecord belongs_to :user belongs_to :category + has_many :comments + has_many :post_heart def self.user_posts(current_user) where(user: current_user) @@ -24,7 +26,9 @@ def self.user_posts(current_user) title: post[:title], content: post[:content].split(" ").first(20).join(" "), email: post.user.email, - created_at: post.created_at + created_at: post.created_at, + comment_count: post.comments.count, + post_heart_count: post.post_heart.count } end end diff --git a/app/models/post_heart.rb b/app/models/post_heart.rb new file mode 100644 index 0000000..c1c59a1 --- /dev/null +++ b/app/models/post_heart.rb @@ -0,0 +1,6 @@ +class PostHeart < ApplicationRecord + acts_as_paranoid + + belongs_to :post + belongs_to :user +end diff --git a/app/models/user.rb b/app/models/user.rb index 15c902f..a298db8 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -20,7 +20,8 @@ class User < ApplicationRecord has_many :user_workouts has_many :workouts, through: :user_workouts - + has_many :comments + has_many :PostHeart # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, diff --git a/app/views/comments/_form.html.haml b/app/views/comments/_form.html.haml new file mode 100644 index 0000000..46bb9d2 --- /dev/null +++ b/app/views/comments/_form.html.haml @@ -0,0 +1,19 @@ += form_for([@post, @post.comments.build], html: { class: "form-horizontal", role: "form" }) do |f| + - if @post.errors.any? + .alert.alert-danger.alert-dismissable{:role => "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 f39b9ee..cd47ec6 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" 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