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

Show 20 articles #15

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
d136375
Added Users model and a create user page.
paulmckissock Jun 10, 2024
8412d05
Added a login and sign up screen. Created sessions_controller. Made i…
paulmckissock Jun 11, 2024
f9f928e
Migrated db so each article belongs to a user. Changed seed so it cre…
paulmckissock Jun 11, 2024
499e219
moved signup button to application.html.erb. Added a home button to a…
paulmckissock Jun 11, 2024
6fd339f
Created users factory. Modified Articles factory so that it's associa…
paulmckissock Jun 11, 2024
695fd6d
users factory
paulmckissock Jun 11, 2024
95e83de
Made each article be created by a random user
paulmckissock Jun 11, 2024
f75a068
Made it direct to root path if the user wasn't valid when signing up.
paulmckissock Jun 12, 2024
b7afa5c
standardrb
paulmckissock Jun 12, 2024
0370bba
added a line saying who you are logged in as
paulmckissock Jun 12, 2024
0f47a38
Added :create to the before_action :authorized in ArticlesController
paulmckissock Jun 12, 2024
1010827
test files that still need work
paulmckissock Jun 12, 2024
27ef7e6
Wrote/fixed tests in every controller, changed sessions route to logo…
paulmckissock Jun 13, 2024
bae59b5
standardrb
paulmckissock Jun 13, 2024
08c4ee5
Added model tests. Fixed user.rb model so it has many articles.
paulmckissock Jun 13, 2024
4dc3289
Made only the 20 most recent Articles show. Added paginate for the ar…
paulmckissock Jun 13, 2024
992c414
standardrb
paulmckissock Jun 13, 2024
17750a9
Added requirement that User email is in correct format. Fixed indenta…
paulmckissock Jun 13, 2024
d539fdf
Created add_user_id_to_articles.rb
paulmckissock Jun 13, 2024
964a1db
Removed password paramater from user generation in seeds.rb
paulmckissock Jun 13, 2024
2b39acc
fixed add_user_id_to_articles.rb
paulmckissock Jun 13, 2024
e9fc384
Made change email button and page
paulmckissock Jun 13, 2024
41e84e5
Created change password screen and button. Made a link to show curren…
paulmckissock Jun 13, 2024
0a696a0
standardrb
paulmckissock Jun 13, 2024
1e10f9f
Removed back button.
paulmckissock Jun 13, 2024
0a53b08
Added contexts to the controller tests. Added methods to test_helper.
paulmckissock Jun 14, 2024
46de58a
merged users into show 20-articles
paulmckissock Jun 14, 2024
96177e1
changed seeds.rb
paulmckissock Jun 14, 2024
838ca69
fixed seeds.rb
paulmckissock Jun 14, 2024
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
7 changes: 7 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,17 @@ gem "jbuilder"
# Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword]
# gem "bcrypt", "~> 3.1.7"

gem "kaminari"

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem "tzinfo-data", platforms: %i[mswin mswin64 mingw x64_mingw jruby]

# Reduces boot times through caching; required in config/boot.rb
gem "bootsnap", require: false

# Authentication
gem "bcrypt", "~> 3.1.7"

# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"

Expand Down Expand Up @@ -72,6 +77,8 @@ group :test do
gem "capybara"
gem "selenium-webdriver"
gem "rails-controller-testing"
gem "minitest-rails"
gem "minitest-spec-rails"
end

gem "dockerfile-rails", ">= 1.6", group: :development
23 changes: 23 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ GEM
sshkit (>= 1.6.1, != 1.7.0)
ast (2.4.2)
base64 (0.2.0)
bcrypt (3.1.20)
bigdecimal (3.1.8)
bindex (0.8.1)
bootsnap (1.18.3)
Expand Down Expand Up @@ -139,6 +140,18 @@ GEM
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
json (2.7.2)
kaminari (1.2.2)
activesupport (>= 4.1.0)
kaminari-actionview (= 1.2.2)
kaminari-activerecord (= 1.2.2)
kaminari-core (= 1.2.2)
kaminari-actionview (1.2.2)
actionview
kaminari-core (= 1.2.2)
kaminari-activerecord (1.2.2)
activerecord
kaminari-core (= 1.2.2)
kaminari-core (1.2.2)
language_server-protocol (3.17.0.3)
lint_roller (1.1.0)
loofah (2.22.0)
Expand All @@ -153,6 +166,12 @@ GEM
matrix (0.4.2)
mini_mime (1.1.5)
minitest (5.23.1)
minitest-rails (7.1.0)
minitest (~> 5.20)
railties (~> 7.1.0)
minitest-spec-rails (7.2.0)
minitest (>= 5.0)
railties (>= 4.1)
msgpack (1.7.2)
mutex_m (0.2.0)
net-imap (0.4.12)
Expand Down Expand Up @@ -332,6 +351,7 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
bcrypt (~> 3.1.7)
bootsnap
capistrano (~> 3.10)
capistrano-passenger
Expand All @@ -342,6 +362,9 @@ DEPENDENCIES
factory_bot_rails
importmap-rails
jbuilder
kaminari
minitest-rails
minitest-spec-rails
puma (>= 5.0)
rails (~> 7.1.3, >= 7.1.3.4)
rails-controller-testing
Expand Down
19 changes: 19 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
class ApplicationController < ActionController::Base
helper_method :logged_in?, :current_user, :home?

def current_user
if session[:user_id]
@user = User.find(session[:user_id])
end
end

def logged_in?
!!current_user
end

def authorized
redirect_to login_path unless logged_in?
end

def home?
request.path == root_path
end
end
4 changes: 3 additions & 1 deletion app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class ArticlesController < ApplicationController
before_action :authorized, only: [:new, :create]
def index
@articles = Article.all
@articles = Article.order(created_at: :desc).page(params[:page]).per(20)
end

def show
Expand All @@ -13,6 +14,7 @@ def new

def create
@article = Article.new(article_params)
@article.user = current_user

if @article.save
redirect_to @article
Expand Down
19 changes: 19 additions & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class SessionsController < ApplicationController
def new
end

def create
@user = User.find_by(name: params[:name])
if @user&.authenticate(params[:password])
session[:user_id] = @user.id
redirect_to @user
else
redirect_to root_path
end
end

def destroy
session[:user_id] = nil
redirect_to login_path
end
end
60 changes: 60 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
class UsersController < ApplicationController
before_action :authorized, only: [:show]
def new
@user = User.new
end

def create
@user = User.create(user_params)
if @user.valid?
@user.save
redirect_to @user
else
redirect_to root_path
end
end

def show
@user = User.find(params[:id])
end

def edit_email
@user = User.find(params[:id])
end

def update_email
@user = User.find(params[:id])
if @user.update(email_params)
redirect_to @user
else
render :edit_email
end
end

def edit_password
@user = User.find(params[:id])
end

def update_password
@user = User.find(params[:id])
if @user.update(password_params)
redirect_to @user
else
render :edit_password
end
end

private

def user_params
params.require(:user).permit(:name, :password, :password_confirmation, :email)
end

def email_params
params.require(:user).permit(:email)
end

def password_params
params.require(:user).permit(:password, :password_confirmation)
end
end
1 change: 1 addition & 0 deletions app/models/article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# - link: must be present

class Article < ApplicationRecord
belongs_to :user
validates :title, presence: true
validates :link, presence: true
end
7 changes: 7 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class User < ApplicationRecord
has_secure_password
has_many :articles

validates :name, presence: true, uniqueness: true
validates :email, presence: true, uniqueness: true, format: {with: URI::MailTo::EMAIL_REGEXP, message: "must be a valid email address"}
end
4 changes: 3 additions & 1 deletion app/views/articles/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
<ul>
<% @articles.each do |article| %>
<li>
<%= link_to article.title, article.link %> - Posted on <%= article.created_at.strftime("%B %d, %Y at %I:%M %p") %>
<%= link_to article.title, article.link %> - Posted by <%= article.user.name %> on <%= article.created_at.strftime("%B %d, %Y at %I:%M %p") %>
<div>
<%= link_to "comment", article %>
</div>
</li>
<% end %>
</ul>

<%= paginate @articles %>
15 changes: 14 additions & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,23 @@
<%= csp_meta_tag %>

<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>

</head>

<body>
<%= yield %>

<div>
<% unless home? %>
<%= button_to "Home", root_path, method: :get %>
<% end %>
<% if logged_in? %>
<p>Logged in as <%= link_to current_user.name, user_path(current_user) %></p>
<%= button_to "Logout", logout_path, method: :delete %>
<% else %>
<%= button_to "Login", login_path, method: :get %>
<%= button_to "Sign Up", signup_path, method: :get %>
<% end %>

</div>
</body>
</html>
7 changes: 7 additions & 0 deletions app/views/sessions/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<%= form_tag login_path do %>
<%= label_tag "Username" %>
<%= text_field_tag :name, nil, placeholder: "Username" %>
<%= label_tag "Password" %>
<%= password_field_tag :password, nil, placeholder: "Password" %>
<%= submit_tag "Log In"%>
<% end %>
13 changes: 13 additions & 0 deletions app/views/users/edit_email.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h1>Edit Email</h1>

<%= form_with(model: @user, url: update_email_user_path(@user), method: :patch) do |form| %>
<div>
<%= form.label :email %><br>
<%= form.email_field :email %>
</div>

<div>
<%= form.submit "Update Email" %>
</div>
<% end %>

17 changes: 17 additions & 0 deletions app/views/users/edit_password.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<h1>Edit Password</h1>

<%= form_for @user, url: update_password_user_path(@user), method: :patch do |f| %>
<div class="field">
<div>
<%= f.label :password %>
</div>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br>
<%= f.password_field :password_confirmation %>
</div>
<div class="actions">
<%= f.submit "Update Password" %>
</div>
<% end %>
11 changes: 11 additions & 0 deletions app/views/users/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%= form_for @user do |f| %>
<%= f.label :name %>
<%= f.text_field :name, placeholder: "Username" %>
<%= f.label :email %>
<%= f.text_field :email, placeholder: "Email" %>
<%= f.label :password %>
<%= f.password_field :password, placeholder: "Password" %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, placeholder: "Confirm Password" %>
<%= f.submit "Create Account" %>
<% end %>
4 changes: 4 additions & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h1><%= @user.name %></h1>
<p>Email: <%= @user.email %></p>
<%= button_to "Change Email", edit_email_user_path(@user), method: :get %>
<%= button_to "Change Password", edit_password_user_path(@user), method: :get %>
10 changes: 9 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Rails.application.routes.draw do
root "articles#index"
resources :articles
resources :users
get "/signup", to: "users#new"
get "/login", to: "sessions#new"
post "/login", to: "sessions#create"
delete "/logout", to: "sessions#destroy"

# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
get "/users/:id/edit_email", to: "users#edit_email", as: "edit_email_user"
patch "/users/:id/update_email", to: "users#update_email", as: "update_email_user"
get "/users/:id/edit_password", to: "users#edit_password", as: "edit_password_user"
patch "/users/:id/update_password", to: "users#update_password", as: "update_password_user"
end
11 changes: 11 additions & 0 deletions db/migrate/20240611125900_create_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateUsers < ActiveRecord::Migration[7.1]
def change
create_table :users do |t|
t.string :name, null: false
t.string :email, null: false
t.string :password_digest, null: false

t.timestamps
end
end
end
6 changes: 6 additions & 0 deletions db/migrate/20240613193343_add_user_id_to_articles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddUserIdToArticles < ActiveRecord::Migration[7.1]
def change
add_column :articles, :user_id, :integer, null: false
add_index :articles, :user_id
end
end
12 changes: 11 additions & 1 deletion db/schema.rb

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

Loading