Skip to content

Commit

Permalink
Merge pull request #5 from NenehB/adding_comments
Browse files Browse the repository at this point in the history
Adding comments
  • Loading branch information
Beckyrose200 authored Nov 30, 2021
2 parents a186061 + a0c308f commit 9f2b5f9
Show file tree
Hide file tree
Showing 27 changed files with 205 additions and 54 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ git_source(:github) do |repo_name|
"https://github.com/#{repo_name}.git"
end


gem 'orderly'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.0'
# Use postgresql as the database for Active Record
Expand Down
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ GEM
nokogiri (1.11.7)
mini_portile2 (~> 2.5.0)
racc (~> 1.4)
orderly (0.1.1)
capybara (>= 1.1)
rspec (>= 2.14)
orm_adapter (0.5.0)
pg (1.2.3)
public_suffix (4.0.6)
Expand Down Expand Up @@ -155,6 +158,10 @@ GEM
responders (3.0.1)
actionpack (>= 5.0)
railties (>= 5.0)
rspec (3.10.0)
rspec-core (~> 3.10.0)
rspec-expectations (~> 3.10.0)
rspec-mocks (~> 3.10.0)
rspec-core (3.10.1)
rspec-support (~> 3.10.0)
rspec-expectations (3.10.1)
Expand Down Expand Up @@ -226,6 +233,7 @@ DEPENDENCIES
jbuilder (~> 2.5)
listen (>= 3.0.5, < 3.2)
mimemagic!
orderly
pg
puma (~> 3.12.6)
rails (~> 6.0.0)
Expand Down
Binary file modified app/.DS_Store
Binary file not shown.
Binary file added app/assets/.DS_Store
Binary file not shown.
Binary file added app/assets/images/favicon.ico
Binary file not shown.
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
*= require_tree .
*= require_self
*/

3 changes: 3 additions & 0 deletions app/assets/stylesheets/comments.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the comments controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/
25 changes: 25 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class CommentsController < ApplicationController

def new
@comment = Comment.new
end

def create
@comment = Comment.create(comment: comments_params["comment"], post_id: id_params)
redirect_to posts_url
end

def index
@comment = Comment.new
end

private

def comments_params
params.require(:comment).permit(:comment)
end

def id_params
params.require(:post_id)
end
end
16 changes: 11 additions & 5 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class PostsController < ApplicationController


def new
@post = Post.new
end
Expand All @@ -9,12 +11,16 @@ def create
end

def index
@posts = Post.all
@post = Post.new
@comment = Comment.new
@posts = Post.all.order(created_at: :desc)
@posts_comments = Comment.all.order(created_at: :desc)
end

def update
p post = Post.find_by(id: id_params)
post.update(likes: (post.likes.to_i + 1 ))
post = Post.find_by(id: id_params)
post.increment(:likes)
post.save
redirect_to posts_url
end

Expand All @@ -26,7 +32,7 @@ def post_params
end

def id_params
p params.require(:id)
params.require(:id)
end

end

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
2 changes: 2 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Comment < ApplicationRecord
end
1 change: 0 additions & 1 deletion app/models/post.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
class Post < ApplicationRecord

end

1 change: 1 addition & 0 deletions app/views/comments/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<%= favicon_link_tag asset_path('favicon.ico') %>
<title>Acebook</title>
<%= csrf_meta_tags %>

Expand Down
Empty file added app/views/posts/hello.html.erb
Empty file.
117 changes: 77 additions & 40 deletions app/views/posts/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,54 @@
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

</head>
<body>
</head>
<body>

<nav class="navbar navbar-dark bg-primary">
<div class="container-fluid">
<a class="navbar-brand">Acebook</a>
<form class="d-flex">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</nav>
<nav class="navbar navbar-dark bg-primary">
<div class="container-fluid">
<a class="navbar-brand">Acebook</a>
<form class="d-flex">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</nav>

<ul class="nav justify-content-end">
<li class="nav-item">
<ul class="nav justify-content-end">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
</li>
<li class="nav-item">
<a class="nav-link" href="#">Profile</a>
</li>
<li class="nav-item">
</li>
<li class="nav-item">
<a class="nav-link" href="#">Settings</a>
</li>
<li class="nav-item">
</li>
<li class="nav-item">
<a class="nav-link" href="#">Sign Out</a>
</li>
</ul>
</li>
</ul>

<p1 class="h2 offset-1"> <%= link_to new_post_path do %>
Write a post
<% end %> </p1>

<div class="center"
<div class="mb-3 offset-4" style="text-align: center;">
<label for="post" class="form-label"> <h4>Whats on your mind?</h4></label>
<textarea class="form-control offset-4" id="exampleFormControlTextarea1" rows="4" style="width: 40rem;"></textarea>
<div class="row">
<br>
<div class="container">
<div class = "row">
<div class="center" style="width: 50rem;">
<div class="form-control offset-4" style="text-align: center;">
<label for="post" class="form-label"> <h4>Whats on your mind?</h4></label>
<%= form_for @post, html: {class: 'form-control offsetWidth-8'} do |form| %>
<%= form.label 'message' %>
<%= form.text_field :message %>
<%= form.submit "Submit" %>
<% end %>
<div class="row">
<br>
</div>
</div>
</div>
</div>
</div>

<div class="row">
<br>
</div>

<% @posts.each do |post| %>
Expand All @@ -56,28 +64,57 @@
<div class="card" style="width: 30rem;">
<div class="card-body">
<h5 class="card-title">Users name</h5>
<h6 class="card-subtitle mb-2 text-muted">26/11/2021</h6>
<h6 class="card-subtitle mb-2 text-muted"><%= post.created_at %></h6>
<p class="card-text"><%= post.message %></p>




<%= button_to (post.likes.to_s + ' Likes'), post_path(post.id), :method => :patch %>
</div>


<%= form_for @comment, html: {class: 'form-control offsetWidth-8'} do |form| %>
<%= form.label 'comment' %>
<%= form.text_field :comment %>
<%= hidden_field_tag :post_id, post.id %>
<%= form.submit "Submit comment" %>
<% end %>

<a href="#" class="card-link">Comment</a>
<div class="row">
<br>
</div>
</div>

<% @posts_comments.each do |comments| %>
<% if comments.post_id == post.id %>
<div class="container">
<div class ="row">
<div class="card" style="width: 30rem;">
<p> User Name </p>
<%= comments.created_at %> <br>
<%= comments.comment %>
<div class="row">
<br>
</div>
</div>
</div>
</div>
<% end %>
<div class="row">
<br>
</div>
<% end %>

<div class="row">
<br>
</div>
</div>

<div class="row">
<br>
</div>

</div>
</div>
<% end %>


</body>
</html>
</div>


4 changes: 4 additions & 0 deletions app/views/posts/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<body>






<%= form_for @post do |form| %>
<%= form.label :message %>
<%= form.text_field :message %>
Expand Down
Empty file added app/views/posts/show.html.erb
Empty file.
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
resources :sessions
resources :users
resources :posts

resources :comments
end
1 change: 0 additions & 1 deletion db/migrate/20170526114520_create_posts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ def change
create_table :posts do |t|
t.string :message
t.timestamps

end
end
end
10 changes: 10 additions & 0 deletions db/migrate/20211126120106_new_comment_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class NewCommentTable < ActiveRecord::Migration[6.0]
def change
create_table :comments do |t|
t.integer :user_id
t.integer :post_id
t.string :comment
t.timestamps
end
end
end
13 changes: 12 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,21 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2021_11_25_153348) do

ActiveRecord::Schema.define(version: 2021_11_29_182941) do


# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "comments", force: :cascade do |t|
t.integer "user_id"
t.integer "post_id"
t.string "comment"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end

create_table "posts", force: :cascade do |t|
t.string "message"
t.datetime "created_at", null: false
Expand Down Expand Up @@ -57,4 +67,5 @@
t.datetime "updated_at", precision: 6, null: false
end

add_foreign_key "comments", "posts"
end
16 changes: 12 additions & 4 deletions spec/features/user_can_submit_posts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@
RSpec.feature "Timeline", type: :feature do
scenario "Can submit posts and view them" do
visit "/posts"
click_link "New post"
fill_in "Message", with: "Hello, world!"
click_button "Submit"
expect(page).to have_content("Hello, world!")
end

scenario "User can like a post" do
visit "/posts"
click_link "New post"

fill_in "Message", with: "Hello, world!"
click_button "Submit"
click_button ("Hello, world!")
expect(page).to have_content("1 Like")
click_button ("Likes")
expect(page).to have_button("1 Likes")
end

scenario 'Posts are displayed in descending order' do
visit '/posts'
fill_in "Message", with: "First post"
click_button "Submit"
fill_in "Message", with: "Second post"
click_button "Submit"
expect('Second post').to appear_before('First post')
end
end
12 changes: 12 additions & 0 deletions spec/features/user_can_write_comment_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'rails_helper'

RSpec.feature "Comments", type: :feature do
scenario "User can comment on a post" do
visit "/posts"
fill_in "Message", with: "Hello, world!"
click_button "Submit"
fill_in "Comment", with: "Nice one!"
click_button "Submit comment"
expect(page).to have_content("Nice one!")
end
end
Loading

0 comments on commit 9f2b5f9

Please sign in to comment.