Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added comment to post, on clicking the comment count , it takes to th… #26

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -54,5 +55,8 @@ end
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'



# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

5 changes: 4 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -321,4 +324,4 @@ RUBY VERSION
ruby 2.5.0p0

BUNDLED WITH
1.16.1
1.17.3
Binary file added app/assets/images/comments_img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions app/assets/javascripts/comments.coffee
Original file line number Diff line number Diff line change
@@ -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/
34 changes: 34 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def destroy
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
@[email protected]
end

# Never trust parameters from the scary internet, only allow the white list through.
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/comments_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module CommentsHelper
end
4 changes: 4 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Comment < ApplicationRecord
belongs_to :post
belongs_to :user
end
6 changes: 5 additions & 1 deletion app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions app/models/post_heart.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class PostHeart < ApplicationRecord
acts_as_paranoid

belongs_to :post
belongs_to :user
end
3 changes: 2 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions app/views/comments/_form.html.haml
Original file line number Diff line number Diff line change
@@ -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"
1 change: 1 addition & 0 deletions app/views/comments/show.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
= @comment.content
5 changes: 4 additions & 1 deletion app/views/posts/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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;'}
Expand Down
16 changes: 13 additions & 3 deletions app/views/posts/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,23 @@
/
<dt>Title:</dt>
<dd><haml_silent></haml_silent></dd>

<dt>Content:</dt>
<dd><haml_silent></haml_silent></dd>

<dt>User:</dt>
<dd><haml_silent></haml_silent></dd>

<dt>Category:</dt>
<dd><haml_silent></haml_silent></dd>
%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'
12 changes: 7 additions & 5 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20190224195706_create_comments.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions db/migrate/20190310152745_create_post_hearts.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions db/migrate/20190310152829_add_deleted_at_to_post_heart.rb
Original file line number Diff line number Diff line change
@@ -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
27 changes: 26 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
5 changes: 5 additions & 0 deletions spec/controllers/comments_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe CommentsController, type: :controller do

end
15 changes: 15 additions & 0 deletions spec/helpers/comments_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions spec/models/comment_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Comment, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/models/post_heart_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe PostHeart, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end