diff --git a/Gemfile b/Gemfile
index 4107249..e253cb3 100644
--- a/Gemfile
+++ b/Gemfile
@@ -34,7 +34,7 @@ gem 'jbuilder'
# gem "kredis"
# Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword]
-# gem "bcrypt", "~> 3.1.7"
+gem "bcrypt", "~> 3.1.7"
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]
@@ -48,14 +48,13 @@ gem 'sassc-rails'
# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"
-gem 'bootstrap', '~> 5.1.3'
+gem 'cancancan'
group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem 'debug', platforms: %i[mri mingw x64_mingw]
- gem 'cowsay'
- gem 'faker'
+ gem 'byebug'
end
group :development do
@@ -68,5 +67,7 @@ group :development do
# Speed up commands on slow machines / big apps [https://github.com/rails/spring]
# gem "spring"
- gem 'byebug'
-end
\ No newline at end of file
+ gem 'cowsay'
+ gem 'faker'
+ gem 'pry-rails', '~> 0.3.9'
+end
diff --git a/Gemfile.lock b/Gemfile.lock
index 386d7bd..b78168b 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -66,17 +66,14 @@ GEM
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
- autoprefixer-rails (10.4.2.0)
- execjs (~> 2)
+ bcrypt (3.1.17)
bindex (0.8.1)
bootsnap (1.11.1)
msgpack (~> 1.2)
- bootstrap (5.1.3)
- autoprefixer-rails (>= 9.1.0)
- popper_js (>= 2.9.3, < 3)
- sassc-rails (>= 2.0.0)
builder (3.2.4)
byebug (11.1.3)
+ cancancan (3.3.0)
+ coderay (1.1.3)
concurrent-ruby (1.1.10)
cowsay (0.3.0)
crass (1.0.6)
@@ -85,7 +82,6 @@ GEM
reline (>= 0.2.7)
digest (3.1.0)
erubi (1.10.0)
- execjs (2.8.1)
faker (2.20.0)
i18n (>= 1.8.11, < 2)
ffi (1.15.5)
@@ -132,7 +128,11 @@ GEM
nokogiri (1.13.3-x86_64-linux)
racc (~> 1.4)
pg (1.3.4)
- popper_js (2.9.3)
+ pry (0.14.1)
+ coderay (~> 1.1)
+ method_source (~> 1.0)
+ pry-rails (0.3.9)
+ pry (>= 0.10.4)
puma (5.6.2)
nio4r (~> 2.0)
racc (1.6.0)
@@ -208,15 +208,17 @@ PLATFORMS
x86_64-linux
DEPENDENCIES
+ bcrypt (~> 3.1.7)
bootsnap
- bootstrap (~> 5.1.3)
byebug
+ cancancan
cowsay
debug
faker
importmap-rails
jbuilder
pg (~> 1.1)
+ pry-rails (~> 0.3.9)
puma (~> 5.0)
rails (~> 7.0.2, >= 7.0.2.3)
sassc-rails
diff --git a/app/assets/stylesheets/user.css b/app/assets/stylesheets/user.css
new file mode 100644
index 0000000..3bbec2e
--- /dev/null
+++ b/app/assets/stylesheets/user.css
@@ -0,0 +1,51 @@
+/* SignUp/SignIn/User form views */
+.user-form,
+.change-password-link,
+.signup-form,
+.signin-form {
+ padding: 15px;
+ margin: 10px 0;
+}
+
+.user-form > .input-group,
+.signup-form > .input-group,
+.signin-form > .input-group {
+ display: flex;
+ flex-direction: column;
+}
+
+.user-form > .input-group > .field_with_errors,
+.signup-form > .input-group > .field_with_errors {
+ display: flex;
+ width: 100%;
+}
+
+.user-form > .input-group > label,
+.signup-form > .input-group > label,
+.signin-form > .input-group > label,
+.user-form > .input-group > .field_with_errors > label,
+.signup-form > .input-group > .field_with_errors > label {
+ margin: 10px 0;
+}
+
+.user-form > .input-group > .field_with_errors > input[type="email"],
+.signup-form > .input-group > .field_with_errors > input[type="email"],
+.signup-form > .input-group > .field_with_errors > input[type="text"] {
+ margin-bottom: 10px;
+ width: 100%;
+}
+
+#user_email,
+#password,
+#new_password_confirmation,
+#user_password_confirmation {
+ margin-bottom: 10px;
+ width: 100%;
+}
+
+.user-form > .input-group > p,
+.signup-form > .input-group > p,
+.signin-form > .input-group > p {
+ margin-bottom: 10px;
+ color: red;
+}
\ No newline at end of file
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 09705d1..316f82b 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -1,2 +1,18 @@
class ApplicationController < ActionController::Base
+
+ private
+ def authenticated_user!
+ redirect_to new_session_path, {alert: "Please sign in first", status: 303} unless user_signed_in?
+ end
+ helper_method :authenticated_user!
+
+ def user_signed_in?
+ current_user.present?
+ end
+ helper_method :user_signed_in?
+
+ def current_user
+ @current_user ||= User.find_by_id session[:user_id]
+ end
+ helper_method :current_user
end
diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb
index 8a10f99..bded9ea 100644
--- a/app/controllers/comments_controller.rb
+++ b/app/controllers/comments_controller.rb
@@ -1,25 +1,37 @@
class CommentsController < ApplicationController
- def create
- @post = Post.find params[:post_id]
- @comment = Comment.new params.require(:comment).permit(:body)
- @comment.post = @post
- if @comment.save
- redirect_to post_path(@post)
- else
- @comments = @post.comments.order(created_at: :desc)
- render 'posts/show', status: 303
- end
- rescue => e
- redirect_to root_path, alert: e.message
- end
-
- def destroy
- @comment = Comment.find params[:id]
- @comment.destroy
- @post = Post.find params[:post_id]
+ before_action :authenticated_user!
+ before_action :find_comment, only: [:destroy]
+ before_action :authorized_user!, only: [:destroy]
+
+ def create
+ @post = Post.find params[:post_id]
+ @comment = Comment.new params.require(:comment).permit(:body)
+ @comment.post = @post
+ @comment.user = current_user
+ if @comment.save
redirect_to post_path(@post), status: 303
- rescue => e
- redirect_to root_path, alert: e.message
+ else
+ @comments = @post.comments.order(created_at: :desc)
+ render 'posts/show', status: 303
end
+ rescue StandardError => e
+ redirect_to root_path, { alert: e.message, status: 303 }
+ end
+
+ def destroy
+ @comment.destroy
+ redirect_to post_path(@comment.post), status: 303
+ rescue StandardError => e
+ redirect_to root_path, { alert: e.message, status: 303 }
+ end
+
+ private
+
+ def find_comment
+ @comment = Comment.find params[:id]
+ end
+
+ def authorized_user!
+ redirect_to post_path(@comment.post), { status: 303, alert: 'Not authorized' } unless can?(:destroy, @comment)
end
-
\ No newline at end of file
+end
\ No newline at end of file
diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb
index 02268a6..54c1a57 100644
--- a/app/controllers/posts_controller.rb
+++ b/app/controllers/posts_controller.rb
@@ -1,55 +1,62 @@
-# postsController
class PostsController < ApplicationController
- def index
- @posts = Post.order(created_at: :desc)
- end
-
- def show
- @post = Post.find params[:id]
- @comment = Comment.new
- @comments = @post.comments.order(created_at: :desc)
- rescue StandardError => e
- redirect_to root_path, alert: e.message
- end
-
- def destroy
- @post = Post.find params[:id]
- @post.destroy
- redirect_to posts_path, { notice: 'Post deleted successfully', status: 303 }
- rescue StandardError => e
- redirect_to root_path, alert: e.message
- end
-
- def new
- @post = Post.new
- end
-
- def create
- @post = Post.new params.require(:post).permit(:title, :body)
- if @post.save
- redirect_to post_path(@post) #{ status: 303, notice: 'Post created successfully' }
- else
- render :new, status: 303
- end
- end
-
- def edit
- @post = Post.find params[:id]
- rescue StandardError => e
- redirect_to root_path, alert: e.message
+ before_action :authenticated_user!, except: %i[index show]
+ before_action :find_post, except: %i[index new create]
+ before_action :authorized_user!, only: %i[edit update destroy]
+
+ def index
+ @posts = Post.order(created_at: :desc)
+ end
+
+ def show
+ @comment = Comment.new
+ @comments = @post.comments.order(created_at: :desc)
+ rescue StandardError => e
+ redirect_to root_path, {alert: e.message, status: 303}
+ end
+
+ def destroy
+ @post.destroy
+ redirect_to root_path, { notice: 'Post deleted successfully', status: 303 }
+ rescue StandardError => e
+ redirect_to root_path, {alert: e.message, status: 303}
+ end
+
+ def new
+ @post = Post.new
+ end
+
+ def create
+ @post = Post.new params.require(:post).permit(:title, :body)
+ @post.user = current_user
+ if @post.save
+ redirect_to post_path(@post) # { status: 303, notice: 'Post created successfully' }
+ else
+ render :new, status: 303
end
-
- def update
- @post = Post.find params[:id]
- puts @post
- if @post.update params.require(:post).permit(:title, :body)
- puts @post
- redirect_to post_path(@post), { status: 303, notice: 'Post updated successfully' }
- else
- render :edit, status: 303
- end
- rescue StandardError => e
- redirect_to posts_path, alert: e.message
+ end
+
+ def edit
+ rescue StandardError => e
+ redirect_to root_path, {alert: e.message, status: 303}
+ end
+
+ def update
+ if @post.update params.require(:post).permit(:title, :body)
+ redirect_to post_path(@post), { status: 303, notice: 'Post updated successfully' }
+ else
+ render :edit, status: 303
end
+ rescue StandardError => e
+ redirect_to root_path, {alert: e.message, status: 303}
+ end
+
+ private
+
+ def find_post
+ @post = Post.find params[:id]
+ end
+
+ def authorized_user!
+ redirect_to post_path(@post), { status: 303, alert: 'Not authorized' } unless can?(:crud, @post)
end
-
\ No newline at end of file
+end
diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb
new file mode 100644
index 0000000..14e3d5d
--- /dev/null
+++ b/app/controllers/sessions_controller.rb
@@ -0,0 +1,21 @@
+class SessionsController < ApplicationController
+ def new
+
+ end
+
+ def create
+ @user = User.find_by_email params[:email]
+ if @user&.authenticate params[:password]
+ session[:user_id]=@user.id
+ redirect_to root_path, status: 303
+ else
+ flash.alert = "User not found"
+ render :new, status: 303
+ end
+ end
+
+ def destroy
+ session[:user_id] = nil
+ redirect_to root_path, status: 303
+ end
+end
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
new file mode 100644
index 0000000..01e2cea
--- /dev/null
+++ b/app/controllers/users_controller.rb
@@ -0,0 +1,70 @@
+class UsersController < ApplicationController
+ before_action :authenticated_user!, except: %i[new create]
+ before_action :find_user, only: %i[edit update]
+ before_action :authorized_user!, only: %i[edit update]
+
+ def new
+ @user = User.new
+ end
+
+ def create
+ @user = User.new params.require(:user).permit(:name, :email, :password, :password_confirmation)
+ if @user.save
+ session[:user_id] = @user.id
+ redirect_to root_path, status: 303
+ else
+ render :new, status: 303
+ end
+ end
+
+ def edit; end
+
+ def update
+ if @user.update params.require(:user).permit(:name, :email, :password, :password_confirmation)
+ redirect_to root_path, status: 303
+ else
+ flash.alert = 'Update failed, please try again'
+ render :edit, status: 303
+ end
+ end
+
+ def change_password
+ @user = User.new
+ end
+
+ def update_password
+ current_password = params[:current_password]
+ new_password = params[:new_password]
+ new_password_confirmation = params[:new_password_confirmation]
+ user = User.find_by_id current_user.id
+ if user&.authenticate current_password
+ if new_password == current_password
+ flash.alert = 'New password must be different'
+ render :change_password, status: 303
+ elsif new_password == new_password_confirmation
+ if user.update password: new_password
+ redirect_to root_path, { notice: 'Password updated', status: 303 }
+ else
+ flash.alert = 'Password update failed, try again'
+ render :change_password, status: 303
+ end
+ else
+ flash.alert = 'New password confirmation does not match'
+ render :change_password, status: 303
+ end
+ else
+ flash.alert = 'Current password does not match'
+ render :change_password, status: 303
+ end
+ end
+
+ private
+
+ def find_user
+ @user = User.find_by_id params[:id]
+ end
+
+ def authorized_user!
+ redirect_to root_path, { status: 303, alert: 'Not authorized' } unless can?(:crud, @user)
+ end
+end
\ No newline at end of file
diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb
deleted file mode 100644
index 0ec9ca5..0000000
--- a/app/helpers/comments_helper.rb
+++ /dev/null
@@ -1,2 +0,0 @@
-module CommentsHelper
-end
diff --git a/app/helpers/posts_helper.rb b/app/helpers/posts_helper.rb
deleted file mode 100644
index a7b8cec..0000000
--- a/app/helpers/posts_helper.rb
+++ /dev/null
@@ -1,2 +0,0 @@
-module PostsHelper
-end
diff --git a/app/models/ability.rb b/app/models/ability.rb
new file mode 100644
index 0000000..5813690
--- /dev/null
+++ b/app/models/ability.rb
@@ -0,0 +1,53 @@
+# frozen_string_literal: true
+
+class Ability
+ include CanCan::Ability
+
+ def initialize(user)
+ # Define abilities for the passed in user here. For example:
+ #
+ # user ||= User.new # guest user (not logged in)
+ # if user.admin?
+ # can :manage, :all
+ # else
+ # can :read, :all
+ # end
+ #
+ # 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/wiki/Defining-Abilities
+ user ||= User.new
+
+ if user.is_admin
+ can :manage, :all
+ else
+ can :read, :all
+ end
+
+ alias_action :edit, :delete, to: :crud
+
+ can :crud, Post do |post|
+ post.user == user
+ end
+
+ can :destroy, Comment do |comment|
+ comment.user == user || comment.post.user == user
+ end
+
+ can :crud, User, id: user.id
+ end
+end
\ No newline at end of file
diff --git a/app/models/comment.rb b/app/models/comment.rb
index cad758d..3b2070e 100644
--- a/app/models/comment.rb
+++ b/app/models/comment.rb
@@ -1,4 +1,5 @@
class Comment < ApplicationRecord
+ belongs_to :user
belongs_to :post
validates :body, presence: true, length: {maximum:255}
diff --git a/app/models/post.rb b/app/models/post.rb
index 63cb440..ad176c2 100644
--- a/app/models/post.rb
+++ b/app/models/post.rb
@@ -1,4 +1,5 @@
class Post < ApplicationRecord
+ belongs_to :user
has_many :comments, dependent: :destroy
validates :title, presence: true, uniqueness: true
diff --git a/app/models/user.rb b/app/models/user.rb
new file mode 100644
index 0000000..02aeff8
--- /dev/null
+++ b/app/models/user.rb
@@ -0,0 +1,9 @@
+class User < ApplicationRecord
+ has_secure_password
+
+ has_many :comments, dependent: :destroy
+ has_many :posts, dependent: :destroy
+
+ validates :email, presence: true, uniqueness: true
+ validates :name, presence: true, length: { minimum: 3 }
+end
\ No newline at end of file
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb
index 32b993e..b24112c 100644
--- a/app/views/layouts/application.html.erb
+++ b/app/views/layouts/application.html.erb
@@ -24,6 +24,21 @@
<%= link_to 'New Post', new_post_path %>
+ <% if user_signed_in? %>
+
+ <%= link_to current_user.name, edit_user_path(current_user.id) %>
+
+
+ <%= button_to 'Sign Out', session_path(current_user.id), method: :delete %>
+
+ <% else %>
+
+ <%= link_to 'Sign In', new_session_path %>
+
+
+ <%= link_to 'Sign Up', new_user_path %>
+
+ <% end %>
diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb
index 698e795..a0a224d 100644
--- a/app/views/posts/show.html.erb
+++ b/app/views/posts/show.html.erb
@@ -3,12 +3,14 @@
<%= @post.title %>
<%= @post.body %>
-
Posted <%= time_ago_in_words(@post.created_at) %> ago
-
-
+ <% if can?(:crud, @post) %>
+
+ <%= button_to 'Edit', edit_post_path(@post), method: :get %>
+ <%= button_to 'Delete',post_path(@post), method: :delete %>
+
+ <% end %>
<% end %>
@@ -17,7 +19,7 @@
<%= form.label :body, "Comment" %>
<%= form.text_area :body, maxlength: "255", rows: "5" %>
<% if @comment.errors.any? %>
- <%= @comment.errors.full_messages.join(", ") %>
+ <%= @comment.errors.full_messages.join(", ").gsub("Body", "Comment") %>
<% end %>
<%= form.submit :Submit %>
@@ -27,7 +29,7 @@
<% @comments&.each do |comment| %>
<% end %>
diff --git a/app/views/sessions/new.html.erb b/app/views/sessions/new.html.erb
new file mode 100644
index 0000000..0d3d4d6
--- /dev/null
+++ b/app/views/sessions/new.html.erb
@@ -0,0 +1,18 @@
+ <%= form_with url: sessions_path do |form| %>
+
+ <% end %>
\ No newline at end of file
diff --git a/app/views/users/change_password.html.erb b/app/views/users/change_password.html.erb
new file mode 100644
index 0000000..3057b6b
--- /dev/null
+++ b/app/views/users/change_password.html.erb
@@ -0,0 +1,22 @@
+ <%= form_with url: update_password_path do |form| %>
+
+ <% end %>
\ No newline at end of file
diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb
new file mode 100644
index 0000000..de6497e
--- /dev/null
+++ b/app/views/users/edit.html.erb
@@ -0,0 +1,23 @@
+ <%= form_with model: @user do |form| %>
+
+ <% end %>
+ <%= link_to 'Change password', change_password_path %>
+
\ No newline at end of file
diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb
new file mode 100644
index 0000000..6bf5029
--- /dev/null
+++ b/app/views/users/new.html.erb
@@ -0,0 +1,30 @@
+ <%= form_with model: @user do |form| %>
+
+ <% end %>
\ No newline at end of file
diff --git a/config/application.rb b/config/application.rb
index 49ff0c6..c78db86 100644
--- a/config/application.rb
+++ b/config/application.rb
@@ -33,5 +33,12 @@ class Application < Rails::Application
# Don't generate system test files.
config.generators.system_tests = nil
+
+ config.generators do |g|
+ # Don't create helper files, instead of --no-helper
+ g.helper = false
+ # Don't create js and css files, instead of --no-assets
+ g.assets = false
+ end
end
end
diff --git a/config/routes.rb b/config/routes.rb
index 089c181..a3d620d 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -5,7 +5,12 @@
# root "articles#index"
root to: 'posts#index'
- resources :posts do
- resources :comments, only: [:create, :destroy]
+ resources :posts, except: [:index] do
+ resources :comments, shallow: true, only: %i[create destroy]
end
-end
+ get '/change_password', to: 'users#change_password', as: :change_password
+ post '/update_password', to: 'users#update_password', as: :update_password
+ resources :users, only: %i[new create edit update]
+ resource :session, only: [:new]
+ resources :sessions, only: %i[create destroy]
+end
\ No newline at end of file
diff --git a/db/migrate/20220323030922_create_users.rb b/db/migrate/20220323030922_create_users.rb
new file mode 100644
index 0000000..ca6e1b4
--- /dev/null
+++ b/db/migrate/20220323030922_create_users.rb
@@ -0,0 +1,11 @@
+class CreateUsers < ActiveRecord::Migration[7.0]
+ def change
+ create_table :users do |t|
+ t.string :name
+ t.string :email, index: { unique: true }
+ t.string :password_digest
+
+ t.timestamps
+ end
+ end
+end
diff --git a/db/migrate/20220323062149_add_user_to_posts.rb b/db/migrate/20220323062149_add_user_to_posts.rb
new file mode 100644
index 0000000..94d796d
--- /dev/null
+++ b/db/migrate/20220323062149_add_user_to_posts.rb
@@ -0,0 +1,5 @@
+class AddUserToPosts < ActiveRecord::Migration[7.0]
+ def change
+ add_reference :posts, :user, null: false, foreign_key: true
+ end
+end
diff --git a/db/migrate/20220323062710_add_user_to_comments.rb b/db/migrate/20220323062710_add_user_to_comments.rb
new file mode 100644
index 0000000..e0fa507
--- /dev/null
+++ b/db/migrate/20220323062710_add_user_to_comments.rb
@@ -0,0 +1,5 @@
+class AddUserToComments < ActiveRecord::Migration[7.0]
+ def change
+ add_reference :comments, :user, null: false, foreign_key: true
+ end
+end
diff --git a/db/migrate/20220326051923_add_is_admin_to_users.rb b/db/migrate/20220326051923_add_is_admin_to_users.rb
new file mode 100644
index 0000000..31b16b3
--- /dev/null
+++ b/db/migrate/20220326051923_add_is_admin_to_users.rb
@@ -0,0 +1,5 @@
+class AddIsAdminToUsers < ActiveRecord::Migration[7.0]
+ def change
+ add_column :users, :is_admin, :boolean, default: false
+ end
+end
\ No newline at end of file
diff --git a/db/schema.rb b/db/schema.rb
index e4be264..60d160d 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: 2022_03_15_002639) do
+ActiveRecord::Schema[7.0].define(version: 2022_03_26_051923) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -19,7 +19,9 @@
t.bigint "post_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
+ t.bigint "user_id", 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 "posts", force: :cascade do |t|
@@ -27,7 +29,21 @@
t.text "body"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
+ t.bigint "user_id", null: false
+ t.index ["user_id"], name: "index_posts_on_user_id"
+ end
+
+ create_table "users", force: :cascade do |t|
+ t.string "name"
+ t.string "email"
+ t.string "password_digest"
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ t.boolean "is_admin", default: false
+ t.index ["email"], name: "index_users_on_email", unique: true
end
add_foreign_key "comments", "posts"
+ add_foreign_key "comments", "users"
+ add_foreign_key "posts", "users"
end
diff --git a/db/seeds.rb b/db/seeds.rb
index d5526c9..e456854 100644
--- a/db/seeds.rb
+++ b/db/seeds.rb
@@ -7,26 +7,46 @@
# Character.create(name: 'Luke', movie: movies.first)
# First destroy all records from table comments due to FK constraint.
-Comment.destroy_all
# Then destroy all records from table posts.
+Comment.destroy_all
Post.destroy_all
+User.destroy_all
# Reset the primary key sequence to 1.
ActiveRecord::Base.connection.reset_pk_sequence!(:posts)
ActiveRecord::Base.connection.reset_pk_sequence!(:comments)
+ActiveRecord::Base.connection.reset_pk_sequence!(:users)
-# Bulk insert of 50 fake posts.
-Post.insert_all(
- 50.times.map do
- {
- title: Faker::Hacker.say_something_smart,
- body: Faker::ChuckNorris.fact,
- created_at: Faker::Time.backward(days: 365),
- updated_at: DateTime.now
- }
- end
+PASSWORD = '123'
+User.create(
+ name: "Ibrahim",
+ email: 'abe@gmail.com',
+ password: PASSWORD
)
-# Show how many fake posts are in the table posts.
-puts Cowsay.say("Generated #{Post.count} posts using Faker.", :frogs)
-puts Cowsay.say("Comments cleared out - #{Comment.count} comments.", :tux)
+5.times do |n|
+ User.create(
+ name: Faker::Name.first_name,
+ email: "user#{n + 1}@user.com",
+ password: PASSWORD
+ )
+end
+
+users = User.all.offset 1
+puts Cowsay.say("Generated #{users.count} users using Faker.", :tux)
+
+ # Bulk insert of 50 fake posts.
+ Post.insert_all(
+ 50.times.map do
+ {
+ title: Faker::Hacker.say_something_smart,
+ body: Faker::ChuckNorris.fact,
+ user_id: users.sample.id,
+ created_at: Faker::Time.backward(days: 365),
+ updated_at: DateTime.now
+ }
+ end
+ )
+
+ # Show how many fake posts are in the table posts.
+ puts Cowsay.say("Generated #{Post.count} posts using Faker.", :frogs)