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 @@ /