Skip to content
Merged
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
3 changes: 3 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
class ApplicationController < ActionController::Base
def current_user
User.first
end
end
21 changes: 21 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class CommentsController < ApplicationController
def new
@comment = Comment.new
end

def create
@comment = current_user.comments.new(comment_params)
@comment.post_id = params[:post_id]
if @comment.save
redirect_to user_post_path(user_id: current_user.id, id: @comment.post.id)
else
render :new, alert: 'Error occurred, Post not saved'
end
end

private

def comment_params
params.require(:comment).permit(:text)
end
end
19 changes: 19 additions & 0 deletions app/controllers/likes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class LikesController < ApplicationController
def create
@post = Post.find(params[:post_id])
@like = current_user.likes.new(like_params)
redirect_to user_post_path(user_id: current_user.id, id: @post.id) if @like.save
Comment on lines +3 to +5

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Kudos on using current_user here 👏🏻

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @ShoiraTa
I will refactor my code

end

# def destroy
# @like = current_user.likes.find(params[:id])
# @like.destroy
# redirect_to @post
# end

private

def like_params
params.require(:like).permit(:post_id, :user_id)
end
end
22 changes: 21 additions & 1 deletion app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,32 @@ def index
end

def show
@post = @user.posts.find(params[:id])
@post = Post.find(params[:id])
@user = current_user
end

def new
@post = Post.new
end

def create
@user = current_user
@post = @user.posts.new(post_params)

if @post.save
redirect_to user_post_path(user_id: @user.id, id: @post.id)
else
render :new, alert: 'Error occurred, Post not saved'
end
end

private

def set_user
@user = User.find(params[:user_id])
end

def post_params
params.require(:post).permit(:title, :text)
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/helpers/likes_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module LikesHelper
end
4 changes: 3 additions & 1 deletion app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
class Comment < ApplicationRecord
belongs_to :post
belongs_to :user
validates :text, presence: true
validates :text, length: { maximum: 100 }

after_save :updates_comments_counter
def updates_comments_counter
post.increment!(:commentsCounter)
post.increment!(:comments_counter)
end
end
2 changes: 1 addition & 1 deletion app/models/like.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ class Like < ApplicationRecord
after_save :update_likes_counter

def update_likes_counter
post.increment!(:likesCounter)
post.increment!(:likes_counter)
end
end
6 changes: 3 additions & 3 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class Post < ApplicationRecord

validates :title, presence: true
validates :title, length: { maximum: 250 }
validates :commentsCounter, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
validates :likesCounter, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
validates :comments_counter, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
validates :likes_counter, numericality: { only_integer: true, greater_than_or_equal_to: 0 }

after_save :updates_post_counter

Expand All @@ -17,6 +17,6 @@ def recent_comments
private

def updates_post_counter
user.increment!(:postsCounter)
user.increment!(:posts_counter)
end
end
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class User < ApplicationRecord
has_many :likes

validates :name, presence: true
validates :postsCounter, numericality: { greater_than_or_equal_to: 0 }
validates :posts_counter, numericality: { greater_than_or_equal_to: 0 }
def recent_posts
posts.limit(3).order(created_at: :desc)
end
Expand Down
2 changes: 2 additions & 0 deletions app/views/comments/create.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Comments#create</h1>
<p>Find me in app/views/comments/create.html.erb</p>
2 changes: 2 additions & 0 deletions app/views/comments/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Comments#index</h1>
<p>Find me in app/views/comments/index.html.erb</p>
10 changes: 10 additions & 0 deletions app/views/comments/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<%= form_with method: :post,url:user_post_comments_path , model: @comment do |form| %>
<div>
<%= form.label :text %><br>
<%= form.text_area :text %>
</div>

<div>
<%= form.submit 'comment' %>
</div>
<% end %>
2 changes: 2 additions & 0 deletions app/views/likes/create.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Likes#create</h1>
<p>Find me in app/views/likes/create.html.erb</p>
17 changes: 17 additions & 0 deletions app/views/posts/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@


<%= form_with method: :post,url:user_posts_path , model: @post do |form| %>
<div>
<%= form.label :title %><br>
<%= form.text_field :title %>
</div>

<div>
<%= form.label :text %><br>
<%= form.text_area :text %>
</div>

<div>
<%= form.submit %>
</div>
<% end %>
4 changes: 2 additions & 2 deletions app/views/posts/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
<%end%>
</ul>
<div>
<h5>Number of Likes:</h5> <%= post.likesCounter%>
<h5>Number of comments:</h5> <%= post.commentsCounter%>
<h5>Number of Likes:</h5> <%= post.likes_counter%>
<h5>Number of comments:</h5> <%= post.comments_counter%>
</div>


Expand Down
3 changes: 3 additions & 0 deletions app/views/posts/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h2>new post </h2>

<%= render "form", post: @post %>
6 changes: 5 additions & 1 deletion app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<h2><%= @post.title %> </h2>

<p><%= @post.text%> </p>
<%= link_to 'Comment', new_user_post_comment_path(user_id:@user.id,post_id:@post.id ) %>

<%= button_to "Like",user_post_likes_path(user_id:@user.id,post_id:@post.id ), params:{like:{post_id:@post.id } }, method: :post %>

<% @post.comments.each do |comment|%>
<li> <%= comment.text %> </li>

<%end%>
<%end%>

2 changes: 1 addition & 1 deletion app/views/users/_user.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<li>
<div>
<img src="<%= user.photo%>" width=100 height=100/></div> <div><%= user.name%> <h5>Number of posts:</h5> <%= user.postsCounter%> </div> </li>
<img src="<%= user.photo%>" width=100 height=100/></div> <div><%= user.name%> <h5>Number of posts:</h5> <%= user.posts_counter%> </div> </li>
2 changes: 0 additions & 2 deletions app/views/users/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<h2> These are all your users</h2>


<ul>
<% @users.each do |user| %>
<%= render user %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<h5> Post #<%= post.id %> </h5>
<h3> <%= post.title %></h3>
<p> <%= post.text %> </p>
<div> <h4>Comments: </h4><%post.commentsCounter%> <h4>Likes: </h4><%post.likesCounter%> </div>
<div> <h4>Comments: </h4><%post.comments_counter%> <h4>Likes: </h4><%post.likes_counter%> </div>

</div>

Expand Down
8 changes: 6 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
Rails.application.routes.draw do
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# put '/post/:id/like' to: 'posts#like', as: 'like'

resources :users, only: [:index, :show] do
resources :posts, only: [:index, :show]
resources :posts, only:[:index, :show, :new, :create] do
resources :comments, only: [:new, :create]
resources :likes,only:[:create, :destroy]
end
end
end
6 changes: 6 additions & 0 deletions db/migrate/20220827133017_add_default_value_to_posts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddDefaultValueToPosts < ActiveRecord::Migration[7.0]
def change
remove_column :posts, :commentsCounter, :integer
add_column :posts, :comments_counter, :integer, default: 0
end
end
6 changes: 6 additions & 0 deletions db/migrate/20220827134133_change_column_in_posts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ChangeColumnInPosts < ActiveRecord::Migration[7.0]
def change
remove_column :posts, :likesCounter, :integer
add_column :posts, :likes_counter, :integer, default: 0
end
end
6 changes: 6 additions & 0 deletions db/migrate/20220827134513_change_column_name_in_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ChangeColumnNameInUsers < ActiveRecord::Migration[7.0]
def change
remove_column :users, :postsCounter, :integer
add_column :users, :posts_counter, :integer, default: 0
end
end
8 changes: 4 additions & 4 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
15 changes: 15 additions & 0 deletions spec/helpers/likes_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 LikesHelper. For example:
#
# describe LikesHelper 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 LikesHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end
24 changes: 24 additions & 0 deletions spec/requests/comments_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'rails_helper'

RSpec.describe 'Comments', type: :request do
describe 'GET /index' do
it 'returns http success' do
get '/comments/index'
expect(response).to have_http_status(:success)
end
end

describe 'GET /new' do
it 'returns http success' do
get '/comments/new'
expect(response).to have_http_status(:success)
end
end

describe 'GET /create' do
it 'returns http success' do
get '/comments/create'
expect(response).to have_http_status(:success)
end
end
end
10 changes: 10 additions & 0 deletions spec/requests/likes_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'rails_helper'

RSpec.describe 'Likes', type: :request do
describe 'GET /create' do
it 'returns http success' do
get '/likes/create'
expect(response).to have_http_status(:success)
end
end
end
5 changes: 5 additions & 0 deletions spec/views/comments/create.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe 'comments/create.html.erb', type: :view do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/views/comments/index.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe 'comments/index.html.erb', type: :view do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/views/comments/new.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe 'comments/new.html.erb', type: :view do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/views/likes/create.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe 'likes/create.html.erb', type: :view do
pending "add some examples to (or delete) #{__FILE__}"
end