From e9a82270c084518a0eba93dcda54620654d8c0fb Mon Sep 17 00:00:00 2001 From: cherelemma Date: Thu, 13 Jul 2023 21:40:04 +0300 Subject: [PATCH 01/21] install cancancan library --- Gemfile | 2 ++ Gemfile.lock | 2 ++ 2 files changed, 4 insertions(+) diff --git a/Gemfile b/Gemfile index 7bbf738..fc33193 100644 --- a/Gemfile +++ b/Gemfile @@ -78,3 +78,5 @@ group :test do end gem 'devise', '~> 4.9' + +gem "cancancan", "~> 3.5" diff --git a/Gemfile.lock b/Gemfile.lock index 9e6af4b..7393995 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -74,6 +74,7 @@ GEM bootsnap (1.16.0) msgpack (~> 1.2) builder (3.2.4) + cancancan (3.5.0) capybara (3.39.2) addressable matrix @@ -265,6 +266,7 @@ PLATFORMS DEPENDENCIES bootsnap + cancancan (~> 3.5) capybara debug devise (~> 4.9) From 210c90e069fe9f57c66f8e06e9dce2850dbe1a10 Mon Sep 17 00:00:00 2001 From: cherelemma Date: Sat, 15 Jul 2023 21:23:06 +0300 Subject: [PATCH 02/21] install cancancan and generate ability model --- app/models/ability.rb | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 app/models/ability.rb diff --git a/app/models/ability.rb b/app/models/ability.rb new file mode 100644 index 0000000..c26a3e3 --- /dev/null +++ b/app/models/ability.rb @@ -0,0 +1,38 @@ +class Ability + include CanCan::Ability + + def initialize(user) + # Define abilities for the user here. For example: + can :read, :all + + return unless user.present? + + can :manage, User, id: user.id # user can manage only his own profile + can :manage, Post, author_id: user.id # user can manage only his own posts + can :manage, Comment, user_id: user.id # user can manage only his own comments + can :create, Like # user can create likes + + return unless user.role == 'admin' + + can :destroy, Post # admin can delete any post + can :destroy, Comment # admin can delete any comment + + # The first argument to `can` is the action you are giving the user + # permission to do. + # If you pass :manage it will apply to every action. Other common actions + # here are :read, :create, :update and :destroy. + # + # The second argument is the resource the user can perform the action on. + # If you pass :all it will apply to every resource. Otherwise pass a Ruby + # class of the resource. + # + # The third argument is an optional hash of conditions to further filter the + # objects. + # For example, here the user can only update published articles. + # + # can :update, Article, published: true + # + # See the wiki for details: + # https://github.com/CanCanCommunity/cancancan/blob/develop/docs/define_check_abilities.md + end +end From 963a39cc559e63e026f5e4711718235209aa4a0a Mon Sep 17 00:00:00 2001 From: cherelemma Date: Sat, 15 Jul 2023 21:24:03 +0300 Subject: [PATCH 03/21] create migration for role and update schema --- config/initializers/devise.rb | 2 +- db/migrate/20230712183456_add_devise_to_users.rb | 8 ++++---- db/migrate/20230713185223_add_role_to_user.rb | 5 +++++ db/schema.rb | 7 ++++++- 4 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20230713185223_add_role_to_user.rb diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index 6916f65..7f0f95a 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -143,7 +143,7 @@ # without confirming their account. # Default is 0.days, meaning the user cannot access the website without # confirming their account. - # config.allow_unconfirmed_access_for = 2.days + config.allow_unconfirmed_access_for = nil # A period that the user is allowed to confirm their account before their # token becomes invalid. For example, if set to 3.days, the user can confirm diff --git a/db/migrate/20230712183456_add_devise_to_users.rb b/db/migrate/20230712183456_add_devise_to_users.rb index 98ed9dc..d143713 100644 --- a/db/migrate/20230712183456_add_devise_to_users.rb +++ b/db/migrate/20230712183456_add_devise_to_users.rb @@ -22,10 +22,10 @@ def self.up # t.string :last_sign_in_ip ## Confirmable - # t.string :confirmation_token - # t.datetime :confirmed_at - # t.datetime :confirmation_sent_at - # t.string :unconfirmed_email # Only if using reconfirmable + t.string :confirmation_token + t.datetime :confirmed_at + t.datetime :confirmation_sent_at + t.string :unconfirmed_email # Only if using reconfirmable ## Lockable # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts diff --git a/db/migrate/20230713185223_add_role_to_user.rb b/db/migrate/20230713185223_add_role_to_user.rb new file mode 100644 index 0000000..ba3c97c --- /dev/null +++ b/db/migrate/20230713185223_add_role_to_user.rb @@ -0,0 +1,5 @@ +class AddRoleToUser < ActiveRecord::Migration[7.0] + def change + add_column :users, :role, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 1084210..ea67bfb 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[7.0].define(version: 2023_07_12_200923) do +ActiveRecord::Schema[7.0].define(version: 2023_07_13_185223) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -56,6 +56,11 @@ t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" + t.string "confirmation_token" + t.datetime "confirmed_at" + t.datetime "confirmation_sent_at" + t.string "unconfirmed_email" + t.string "role" t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end From 4b12fe9cdc694088ae3ddd260a283ac128e53971 Mon Sep 17 00:00:00 2001 From: Salomon Tshobohwa Date: Sat, 15 Jul 2023 20:27:47 +0200 Subject: [PATCH 04/21] Add destroy method to post controller --- app/controllers/posts_controller.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index c103797..b16045b 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -27,7 +27,16 @@ def create render 'new' end end +def destroy + @post = Post.find(params[:id]) + if can? :destroy, @post + @post.destroy + redirect_to "/users/#{current_user.id}/posts", notice: 'Successfully deleted.' + else + redirect_to user_post_path(@post.author_id, @post), alert: 'Unauthorized action.' + end + end private def post_params From a57b0c13c1dc6625f1b0a0dddc5b23524da02946 Mon Sep 17 00:00:00 2001 From: Salomon Tshobohwa Date: Sat, 15 Jul 2023 20:30:00 +0200 Subject: [PATCH 05/21] Add destroy comment method --- app/controllers/comments_controller.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index cf3ebcb..fc1d81a 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -18,6 +18,18 @@ def create end end + def destroy + @comment = Comment.find(params[:id]) + @post = @comment.post + + if can? :destroy, @comment + @comment.destroy + redirect_to user_post_path(@post.author, @post), notice: 'Comment was successfully deleted.' + else + redirect_to user_posts_path(current_user), alert: 'You are not authorized to delete this comment.' + end + end + private def comment_params From 6afcf6e7fbcddaade80e09f3c950a771c54700fb Mon Sep 17 00:00:00 2001 From: Salomon Tshobohwa Date: Sat, 15 Jul 2023 20:30:24 +0200 Subject: [PATCH 06/21] Fix errors --- Gemfile | 2 +- app/controllers/posts_controller.rb | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index fc33193..b4f8174 100644 --- a/Gemfile +++ b/Gemfile @@ -79,4 +79,4 @@ end gem 'devise', '~> 4.9' -gem "cancancan", "~> 3.5" +gem 'cancancan', '~> 3.5' diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index b16045b..22fc574 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -27,7 +27,8 @@ def create render 'new' end end -def destroy + + def destroy @post = Post.find(params[:id]) if can? :destroy, @post @@ -37,6 +38,7 @@ def destroy redirect_to user_post_path(@post.author_id, @post), alert: 'Unauthorized action.' end end + private def post_params From 8ba00e244f7dd447f50f5a04ccca51391ceb7251 Mon Sep 17 00:00:00 2001 From: Salomon Tshobohwa Date: Sat, 15 Jul 2023 20:31:52 +0200 Subject: [PATCH 07/21] Add destroy routes --- config/routes.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index 06779d6..35ccdcf 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,9 +6,9 @@ root "users#index" resources :users, only: [:index, :show] do - resources :posts, only: [:index, :show, :new, :create] do - resources :comments, only: [:new, :create] + resources :posts, only: [:index, :show, :new, :create, :destroy] do + resources :comments, only: [:new, :create, :destroy] post 'like', on: :member end end -end +end \ No newline at end of file From 36c1b608f6735ecab22887f184158de67c99d14d Mon Sep 17 00:00:00 2001 From: Salomon Tshobohwa Date: Sat, 15 Jul 2023 20:35:11 +0200 Subject: [PATCH 08/21] Add authorization rules for delete post --- app/views/posts/show.html.erb | 83 +++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 37 deletions(-) diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb index 4dc676a..aeed0c0 100644 --- a/app/views/posts/show.html.erb +++ b/app/views/posts/show.html.erb @@ -4,47 +4,56 @@ by <%= @user.name %> -
- Comments: - <% if @post.comments_counter %> - <%= @post.comments_counter %> - <% else %> - 0 - <% end %> - - Likes: - <% if @post.likes_counter %> - <%= @post.likes_counter %> - <% else %> - 0 - <% end %> - +
+

<%= @post.text %>

+
+ Comments: + <% if @post.comments_counter %> + <%= @post.comments_counter %> + <% else %> + 0 + <% end %> + + + +
-

<%= @post.text %>

-
- <% if @current_user %> - <%= form_with(model: @post, url: like_user_post_path(user_id: @current_user.id, id: @post.id), method: :post, local: true) do |form| %> - <%= form.button type: :submit, class: 'like-button' do %> - Like - <% end %> - <% end %> +
+ + <% if @post.comments.length > 0%> +
+

Comments

+ <% @post.comments.each do |comment| %> +
+

<%= comment.user.name %>:

+

<%= comment.text %>

+ + <% if can? :destroy, comment %> + <%= form_with url: "/users/#{@post.author.id}/posts/#{@post.id}/comments/#{comment.id}" , method: :delete do |f|%> + <%= f.submit 'Delete', class: 'short-button' %> + <%end%> + <%end%> +
<% end %>
-
- -
- <% @post.comments.each do |comment| %> -
- <%= comment.user.name %>: - <%= comment.text %> -
- <% end %> -
- -
- -
-

<%= @user.bio %>

-
+ + <% if @user.bio%> +
+

<%= @user.bio %>

+
+ <% end %> - <% @user.recent_posts.each do |post| %> -
+
+ <% @user.recent_posts.each do |post| %> <%= link_to user_post_url(user_id: @user.id, id: post.id), class: 'post-link' do %>

Post #<%= @user.posts.index(post) + 1 %>

<% end %> +

<%= post.text %>

- Comments: - <% if post.comments_counter %> - <%= post.comments_counter %>, + + Comments: + <% if post.comments_counter %> + <%= post.comments_counter %>, <% else %> - 0, + 0, <% end %> - Likes: - <% if post.likes_counter %> - <%= post.likes_counter %> + + Likes: + <% if post.likes_counter %> + <%= post.likes_counter %> <% else %> - 0 + 0 <% end %> + + <% if can? :destroy, post %> + <%= link_to user_post_url(post.author_id, id: post.id), method: :delete, class: 'btn-link' do %> + + <% end %> + <% end %>
-
- <% end %> - <%= link_to user_posts_url(@user.id), class: 'btn-link' do %> - - <% end %> + <% end %> +
+ + <%= link_to user_posts_url(@user.id), class: 'btn-link' do %> + + <% end %> From 1f839fc8de1bfc1fd7225ef498fc03745d607b0f Mon Sep 17 00:00:00 2001 From: cherelemma Date: Sat, 15 Jul 2023 21:41:12 +0300 Subject: [PATCH 12/21] format the index page of the app --- app/models/user.rb | 3 ++- app/views/layouts/application.html.erb | 31 +++++++++++++++++--------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 9605970..231f1cb 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,8 +1,9 @@ class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable - devise :database_authenticatable, :registerable, + devise :database_authenticatable, :registerable, :confirmable, :recoverable, :rememberable, :validatable + has_many :posts, foreign_key: :author_id, dependent: :destroy has_many :comments, foreign_key: :user_id, dependent: :destroy has_many :likes, foreign_key: :user_id, dependent: :destroy diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 5fd34af..af8cf4c 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -11,19 +11,28 @@ - - <% if notice %> -

<%= notice %>

- <% end %> +
+ + + <% if notice %> +

<%= notice %>

+ <% end %> - - <%= yield %> + <%= yield %> +
+ <% else %> + <%= yield %> + <% end %> \ No newline at end of file From 310202c6e313dafadf0a7111c9332e832df0b004 Mon Sep 17 00:00:00 2001 From: cherelemma Date: Sat, 15 Jul 2023 21:41:29 +0300 Subject: [PATCH 13/21] update the stylesheet --- app/assets/stylesheets/application.css | 114 ++++++++++++++++++++++--- 1 file changed, 100 insertions(+), 14 deletions(-) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index f2bd3aa..34496ba 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -4,14 +4,40 @@ box-sizing: border-box; } +.wrapper { + margin: 1rem auto; + gap: 1rem; + box-shadow: 0px 0px 2px 2px rgb(231, 231, 237); +} + +.wrapper, .container { - width: 80vw; + width: 90vw; height: 100vh; display: flex; flex-direction: column; align-items: center; - margin: 2rem 0; + background-color: whitesmoke +} + +.navbar { + width: 100%; + display: flex; + flex-direction: row; + justify-content: space-between; + align-items: center; gap: 1rem; + padding: 1rem; + background-color: #1868f1; + border: 1px solid rgb(128, 128, 128); +} + +.logout { + cursor: pointer; + padding: 5px 10px; + font-size: 15px; + font-weight: bold; + } .user { @@ -21,6 +47,7 @@ justify-content: center; align-items: center; gap: 2rem; + padding: 1rem; } .user-photo { @@ -30,7 +57,7 @@ } .user-info { - width: 46%; + width: 80%; display: flex; flex-direction: row; justify-content: space-between; @@ -45,7 +72,9 @@ } .user-posts { - justify-content: flex-end; + display: flex; + flex-direction: column; + gap: 1rem; align-self: flex-end; padding: 2rem 1rem 1rem 1rem; font-size: small; @@ -64,7 +93,7 @@ a { } .post { - width: 60%; + width: 90%; display: flex; flex-direction: column; border: 3px solid black; @@ -80,25 +109,42 @@ a { padding: 0.5rem 1rem; } +.post-text { + display: grid; + grid-template-columns: 3fr 1fr; + gap: 1rem; +} + .count { display: flex; - flex-direction: row; + flex-direction: column; justify-content: flex-end; + align-items: center; padding: 0.5rem 1rem; - font-size: small; + font-size: medium; + gap: 0.5rem; } .count span { padding: 0 0.25rem; } +.like_count_btn { + display: flex; + flex-direction: row; + gap: 1rem; +} + +.like_count_btn button { + width: auto; +} + .post-comments { - width: 60%; + width: 100%; display: flex; flex-direction: column; padding: 1rem; gap: 0.5rem; - border: 3px solid black; } .btn-link { @@ -109,13 +155,14 @@ button { border-bottom: 5px solid black; border-right: 3px solid black; border-radius: 5px; + padding: 5px; } .btn-link, .btn-link button { cursor: pointer; - padding: 5px; font-size: 15px; + margin: 10px 0; } .post-form, @@ -129,21 +176,43 @@ button { background-color: #f5f5f5; } -.comments, .posts { width: 80%; display: flex; - flex-direction: column; + flex-direction: row; padding: 1rem; gap: 1rem; } +.comments { + display: grid; + grid-template-columns: 0.5fr 2.5fr 0.5fr; +} + +.comments form { + width: auto; + align-self: flex-end; +} + +.post .add-comment { + margin: 0 auto; + align-self: center; +} + +.comment-form textarea { + width: 100%; + height: 100px; + padding: 0.5rem; + border: 1px solid black; + border-radius: 5px; +} + .comment-btn, .like-button { + width: auto; cursor: pointer; padding: 5px 10px; margin-left: 5rem; - font-size: 15px; border-bottom: 5px solid black; border-right: 3px solid black; border-radius: 5px; @@ -220,7 +289,7 @@ ul { .auth_links { display: flex; - flex-direction: row; + flex-direction: column; justify-content: flex-start; align-items: center; gap: 1rem; @@ -228,6 +297,12 @@ ul { margin: auto auto; } +.sign_in_up { + display: flex; + flex-direction: row; + gap: 1rem; +} + .auth_links a { color: blue; padding: 0.5rem; @@ -283,3 +358,14 @@ ul { gap: 1rem; margin: 1rem 0; } + +.like-button { + border: none; + background-color: transparent; + font-size: 2rem; +} + +.short-button { + width: 7vw; + align-items: flex-end; +} \ No newline at end of file From 12c5f96f22747f7fbff773af9f322f249dd7e2ad Mon Sep 17 00:00:00 2001 From: cherelemma Date: Sat, 15 Jul 2023 21:47:48 +0300 Subject: [PATCH 14/21] debug and fix linters error --- app/assets/stylesheets/application.css | 25 ++++++++++++------------- app/models/ability.rb | 6 +++--- app/models/user.rb | 2 +- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 34496ba..f07a351 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -7,7 +7,7 @@ .wrapper { margin: 1rem auto; gap: 1rem; - box-shadow: 0px 0px 2px 2px rgb(231, 231, 237); + box-shadow: 0 0 2px 2px rgb(231, 231, 237); } .wrapper, @@ -17,7 +17,7 @@ display: flex; flex-direction: column; align-items: center; - background-color: whitesmoke + background-color: whitesmoke; } .navbar { @@ -37,7 +37,6 @@ padding: 5px 10px; font-size: 15px; font-weight: bold; - } .user { @@ -135,10 +134,6 @@ a { gap: 1rem; } -.like_count_btn button { - width: auto; -} - .post-comments { width: 100%; display: flex; @@ -158,6 +153,10 @@ button { padding: 5px; } +.like_count_btn button { + width: auto; +} + .btn-link, .btn-link button { cursor: pointer; @@ -189,11 +188,6 @@ button { grid-template-columns: 0.5fr 2.5fr 0.5fr; } -.comments form { - width: auto; - align-self: flex-end; -} - .post .add-comment { margin: 0 auto; align-self: center; @@ -247,6 +241,11 @@ form input { padding: 0.5rem; } +.comments form { + width: auto; + align-self: flex-end; +} + .shared { display: flex; flex-direction: column-reverse; @@ -368,4 +367,4 @@ ul { .short-button { width: 7vw; align-items: flex-end; -} \ No newline at end of file +} diff --git a/app/models/ability.rb b/app/models/ability.rb index c26a3e3..1ccac09 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -7,9 +7,9 @@ def initialize(user) return unless user.present? - can :manage, User, id: user.id # user can manage only his own profile - can :manage, Post, author_id: user.id # user can manage only his own posts - can :manage, Comment, user_id: user.id # user can manage only his own comments + can :manage, User, id: user.id # user can manage only his own profile + can :manage, Post, author_id: user.id # user can manage only his own posts + can :manage, Comment, user_id: user.id # user can manage only his own comments can :create, Like # user can create likes return unless user.role == 'admin' diff --git a/app/models/user.rb b/app/models/user.rb index 231f1cb..3609298 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,7 +3,7 @@ class User < ApplicationRecord # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :confirmable, :recoverable, :rememberable, :validatable - + has_many :posts, foreign_key: :author_id, dependent: :destroy has_many :comments, foreign_key: :user_id, dependent: :destroy has_many :likes, foreign_key: :user_id, dependent: :destroy From f9bdda82f5a82329891807f4b061e68454c96f59 Mon Sep 17 00:00:00 2001 From: cherelemma Date: Sat, 15 Jul 2023 21:57:42 +0300 Subject: [PATCH 15/21] changing db credantials --- config/database.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/config/database.yml b/config/database.yml index 85675c9..dcaf49b 100644 --- a/config/database.yml +++ b/config/database.yml @@ -32,7 +32,7 @@ development: username: postgres # The password associated with the postgres role (username). - password: 243243 + password: postgres#13579 # Connect on a TCP socket. Omitted by default since the client uses a # domain socket that doesn't need configuration. Windows does not have @@ -59,7 +59,7 @@ test: <<: *default database: rails_blog_app username: postgres - password: 243243 + password: postgres#13579 # As with config/credentials.yml, you never want to store sensitive information, # like your database password, in your source code. If your source code is @@ -85,4 +85,4 @@ production: <<: *default database: rails_blog_app username: postgres - password: 243243 + password: postgres#13579 From 3a54d7c2b3f7b154c688dfdb534e44d56487d66a Mon Sep 17 00:00:00 2001 From: cherelemma Date: Sat, 15 Jul 2023 22:40:17 +0300 Subject: [PATCH 16/21] update pages layout and page elements --- app/assets/stylesheets/application.css | 14 ++++-- app/views/posts/index.html.erb | 69 +++++++++++++++----------- app/views/users/show.html.erb | 44 ++++++++-------- 3 files changed, 73 insertions(+), 54 deletions(-) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 67b174e..21c1bc5 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -102,6 +102,7 @@ a { display: flex; flex-direction: column; border: 3px solid black; + margin-bottom: 1rem; } .post h3 { @@ -122,8 +123,8 @@ a { .count { display: flex; - flex-direction: column; - justify-content: flex-end; + flex-direction: row; + justify-content: space-around; align-items: center; padding: 0.5rem 1rem; font-size: medium; @@ -149,10 +150,12 @@ a { } .btn-link { + width: auto; align-self: center; } -button { +button, +form input[type="submit"] { border-bottom: 5px solid black; border-right: 3px solid black; border-radius: 5px; @@ -199,7 +202,8 @@ button { align-self: center; } -.comment-form textarea { +.comment-form textarea, +.post-form textarea { width: 100%; height: 100px; padding: 0.5rem; @@ -358,8 +362,10 @@ ul { } .form-elements { + width: 100%; display: flex; flex-direction: row; + align-items: center; gap: 1rem; margin: 1rem 0; } diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb index 096b324..8699649 100644 --- a/app/views/posts/index.html.erb +++ b/app/views/posts/index.html.erb @@ -26,45 +26,56 @@ - <% @posts.each do |post| %> + <% @user.posts.each do |post| %>
<%= link_to user_post_url(user_id: @user.id, id: post.id), class: 'post-link', id: "post-#{post.id}" do %>

Post #<%= @user.posts.index(post) + 1 %>

<% end %>

<%= post.text %>

- Comments: - <% if post.comments_counter %> - <%= post.comments_counter %> - <% else %> - 0 - <% end %> - - Likes: - <% if post.likes_counter %> - <%= post.likes_counter %> - <% else %> - 0 +
+ <% if can? :destroy, post %> + <%= link_to user_post_url(post.author_id, id: post.id), method: :delete, class: 'btn-link' do %> + + <% end %> <% end %> - +
+
+ Comments: + <% if post.comments_counter %> + <%= post.comments_counter %> + <% else %> + 0 + <% end %> + + Likes: + <% if post.likes_counter %> + <%= post.likes_counter %> + <% else %> + 0 + <% end %> + +
- <% if can? :destroy, post %> - <%= link_to user_post_url(post.author_id, id: post.id), method: :delete, class: 'btn-link' do %> - - <% end %> - <% end %> -
- - <% if post.recent_comments.length > 0 %> -
- <% post.recent_comments.each do |comment| %> -
- <%= comment.author.name %>: - <%= comment.text %> +
+ <% if post.recent_comments.length > 0 %> +
+

Comments

+ <% post.recent_comments.each do |comment| %> +
+

<%= comment.user.name %>:

+

<%= comment.text %>

+ + <% if can? :destroy, comment %> + <%= form_with url: "/users/#{post.author.id}/posts/#{post.id}/comments/#{comment.id}" , method: :delete do |f|%> + <%= f.submit 'Delete', class: 'short-button' %> + <%end%> + <%end%> +
+ <% end %>
<% end %> -
- <% end %> +
<% end %> From 96ef339cc6277f5c814b617a7a850e1f55266c56 Mon Sep 17 00:00:00 2001 From: cherelemma Date: Sat, 15 Jul 2023 22:54:12 +0300 Subject: [PATCH 17/21] add load_and_authorize_resource method at the top of PostsController --- app/controllers/posts_controller.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 4dae4da..2406b0d 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -1,4 +1,7 @@ class PostsController < ApplicationController + + load_and_authorize_resource + def index @user = User.includes(posts: { comments: :author }).find(params[:user_id]) @current_user = current_user From 33cd046c163e849447b742b2d88cf4f60a2cb3d9 Mon Sep 17 00:00:00 2001 From: cherelemma Date: Sat, 15 Jul 2023 22:54:33 +0300 Subject: [PATCH 18/21] styling posts show page --- app/assets/stylesheets/application.css | 10 ++++++++-- app/views/posts/show.html.erb | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 21c1bc5..991943c 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -138,6 +138,7 @@ a { .like_count_btn { display: flex; flex-direction: row; + justify-content: space-around; gap: 1rem; } @@ -162,8 +163,13 @@ form input[type="submit"] { padding: 5px; } -.like_count_btn button { - width: auto; +.like_count_btn form { + width: 3vw; +} + +form input[type="hidden"] { + width: 1px; + height: 1px; } .btn-link, diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb index aeed0c0..fc943f8 100644 --- a/app/views/posts/show.html.erb +++ b/app/views/posts/show.html.erb @@ -23,7 +23,7 @@ <% end %> <%= form_with(model: Like.new, url: like_user_post_path(@post.author, @post)) do |form| %> - <%= form.button type: :submit, class: 'like-button' do %> + <%= form.button type: :submit do %> ♥ <% end %> <% end %> From 1dab854446cb1cdbfd4f6c5d31c2480e31d0c59a Mon Sep 17 00:00:00 2001 From: cherelemma Date: Sat, 15 Jul 2023 23:19:22 +0300 Subject: [PATCH 19/21] fix stylelint and rubucop error --- app/assets/stylesheets/application.css | 25 +++++++++++++++++++++---- app/controllers/posts_controller.rb | 3 +-- app/views/posts/index.html.erb | 7 +++++++ app/views/users/show.html.erb | 7 +++++++ 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 991943c..2406453 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -117,7 +117,7 @@ a { .post-text { display: grid; - grid-template-columns: 3fr 1fr; + grid-template-columns: 2fr 0.5fr; gap: 1rem; } @@ -131,6 +131,16 @@ a { gap: 0.5rem; } +.post-text .count { + display: flex; + flex-direction: column; + justify-content: space-around; + align-items: flex-start; + padding: 0.5rem 2rem 0.5rem 10vh; + font-size: medium; + gap: 0.5rem; +} + .count span { padding: 0 0.25rem; } @@ -138,7 +148,7 @@ a { .like_count_btn { display: flex; flex-direction: row; - justify-content: space-around; + align-items: center; gap: 1rem; } @@ -163,8 +173,11 @@ form input[type="submit"] { padding: 5px; } -.like_count_btn form { - width: 3vw; +.like_count_btn button { + width: 2vw; + height: 2vw; + font-size: x-large; + padding: 0; } form input[type="hidden"] { @@ -257,6 +270,10 @@ form input { padding: 0.5rem; } +.like_count_btn form { + width: 3vw; +} + .comments form { width: auto; align-self: flex-end; diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 2406b0d..caee4ac 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -1,7 +1,6 @@ class PostsController < ApplicationController - load_and_authorize_resource - + def index @user = User.includes(posts: { comments: :author }).find(params[:user_id]) @current_user = current_user diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb index 8699649..a025137 100644 --- a/app/views/posts/index.html.erb +++ b/app/views/posts/index.html.erb @@ -55,6 +55,13 @@ 0 <% end %> + + <%= form_with(model: Like.new, url: like_user_post_path(post.author, post)) do |form| %> + <%= form.button type: :submit do %> + ♥ + <% end %> + <% end %> +
diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 494e7ad..73a822a 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -62,6 +62,13 @@ 0 <% end %> + + <%= form_with(model: Like.new, url: like_user_post_path(post.author, post)) do |form| %> + <%= form.button type: :submit do %> + ♥ + <% end %> + <% end %> +
From 52a9cee949711aeb1b9b550cd7b216a198a61c76 Mon Sep 17 00:00:00 2001 From: cherelemma Date: Sat, 15 Jul 2023 23:39:01 +0300 Subject: [PATCH 20/21] like button styling --- app/assets/stylesheets/application.css | 24 ++++++++++++++++++++++++ app/views/posts/index.html.erb | 2 +- app/views/users/show.html.erb | 2 +- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 2406453..2fbaefe 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -136,6 +136,7 @@ a { flex-direction: column; justify-content: space-around; align-items: flex-start; + text-align: center; padding: 0.5rem 2rem 0.5rem 10vh; font-size: medium; gap: 0.5rem; @@ -403,3 +404,26 @@ ul { width: 7vw; align-items: flex-end; } + +.user-comments-likes { + display: flex; + flex-direction: row; + justify-content: flex-end; + align-items: center; + gap: 1rem; + width: 100%; + margin: 1rem 0; +} + +.count form { + width: auto; +} + +.post .count { + display: grid; + grid-template-columns: 0.5fr 0.5fr; + justify-items: end; + gap: 1rem; + width: 100%; + margin: 1rem 0; +} diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb index a025137..5f71227 100644 --- a/app/views/posts/index.html.erb +++ b/app/views/posts/index.html.erb @@ -40,7 +40,7 @@ <% end %> <% end %> -
+
Comments: <% if post.comments_counter %> <%= post.comments_counter %> diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 73a822a..3dd08be 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -47,7 +47,7 @@ <% end %> <% end %>
-
+
Comments: <% if post.comments_counter %> <%= post.comments_counter %>, From 8ddf8e3e872c9046c352ff1d97f34f3240ccfd9a Mon Sep 17 00:00:00 2001 From: cherelemma Date: Sat, 15 Jul 2023 23:43:31 +0300 Subject: [PATCH 21/21] update post delete method --- app/views/posts/index.html.erb | 2 +- app/views/users/show.html.erb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb index 5f71227..6859ac2 100644 --- a/app/views/posts/index.html.erb +++ b/app/views/posts/index.html.erb @@ -35,7 +35,7 @@
<% if can? :destroy, post %> - <%= link_to user_post_url(post.author_id, id: post.id), method: :delete, class: 'btn-link' do %> + <%= link_to user_post_url(post.author_id, post), method: :delete, class: 'btn-link' do %> <% end %> <% end %> diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 3dd08be..6ec44cd 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -42,7 +42,7 @@
<% if can? :destroy, post %> - <%= link_to user_post_url(post.author_id, id: post.id), method: :delete, class: 'btn-link' do %> + <%= link_to user_post_url(post.author_id, post), method: :delete, class: 'btn-link' do %> <% end %> <% end %>