Skip to content

Commit d42ea79

Browse files
committed
Add user microposts
1 parent 4a847ec commit d42ea79

37 files changed

+518
-20
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
44
ruby "3.4.3"
55

66
gem "rails", "8.0.2"
7+
gem "active_storage_validations", "3.0.2"
78
gem "jsbundling-rails", "1.3.1"
89
gem "bcrypt", "3.1.13"
910
gem "faker", "3.5.2"
@@ -42,4 +43,5 @@ end
4243

4344
group :production do
4445
gem "pg", "1.5.9"
46+
gem "aws-sdk-s3", "1.199.0", require: false
4547
end

Gemfile.lock

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ GEM
4444
erubi (~> 1.11)
4545
rails-dom-testing (~> 2.2)
4646
rails-html-sanitizer (~> 1.6)
47+
active_storage_validations (3.0.2)
48+
activejob (>= 6.1.4)
49+
activemodel (>= 6.1.4)
50+
activestorage (>= 6.1.4)
51+
activesupport (>= 6.1.4)
52+
marcel (>= 1.0.3)
4753
activejob (8.0.2)
4854
activesupport (= 8.0.2)
4955
globalid (>= 0.3.6)
@@ -75,6 +81,25 @@ GEM
7581
addressable (2.8.7)
7682
public_suffix (>= 2.0.2, < 7.0)
7783
ansi (1.5.0)
84+
aws-eventstream (1.4.0)
85+
aws-partitions (1.1162.0)
86+
aws-sdk-core (3.232.0)
87+
aws-eventstream (~> 1, >= 1.3.0)
88+
aws-partitions (~> 1, >= 1.992.0)
89+
aws-sigv4 (~> 1.9)
90+
base64
91+
bigdecimal
92+
jmespath (~> 1, >= 1.6.1)
93+
logger
94+
aws-sdk-kms (1.112.0)
95+
aws-sdk-core (~> 3, >= 3.231.0)
96+
aws-sigv4 (~> 1.5)
97+
aws-sdk-s3 (1.199.0)
98+
aws-sdk-core (~> 3, >= 3.231.0)
99+
aws-sdk-kms (~> 1)
100+
aws-sigv4 (~> 1.5)
101+
aws-sigv4 (1.12.1)
102+
aws-eventstream (~> 1, >= 1.0.2)
78103
base64 (0.3.0)
79104
bcrypt (3.1.13)
80105
bcrypt_pbkdf (1.1.1)
@@ -149,6 +174,7 @@ GEM
149174
jbuilder (2.13.0)
150175
actionview (>= 5.0.0)
151176
activesupport (>= 5.0.0)
177+
jmespath (1.6.2)
152178
jsbundling-rails (1.3.1)
153179
railties (>= 6.0.0)
154180
kamal (2.7.0)
@@ -353,6 +379,8 @@ PLATFORMS
353379
x86_64-linux-musl
354380

355381
DEPENDENCIES
382+
active_storage_validations (= 3.0.2)
383+
aws-sdk-s3 (= 1.199.0)
356384
bcrypt (= 3.1.13)
357385
bootsnap (= 1.18.6)
358386
capybara (= 3.40.0)

app/assets/stylesheets/application.bootstrap.scss

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ $gray-medium-light: #eaeaea;
1212
/* universal */
1313

1414
body > div {
15-
padding-top: 30px;
15+
padding-top: 90px;
1616
}
1717

1818
section {
@@ -215,3 +215,44 @@ input {
215215
border-bottom: 1px solid $gray-500;
216216
}
217217
}
218+
/* microposts */
219+
.microposts {
220+
list-style: none;
221+
padding: 0;
222+
li {
223+
padding: 10px 0;
224+
border-top: 1px solid #e8e8e8;
225+
}
226+
.user {
227+
margin-top: 5em;
228+
padding-top: 0;
229+
}
230+
.content {
231+
display: block;
232+
margin-left: 60px;
233+
img {
234+
display: block;
235+
padding: 5px 0;
236+
}
237+
}
238+
.timestamp {
239+
color: $gray-500;
240+
display: block;
241+
margin-left: 60px;
242+
}
243+
.gravatar {
244+
float: left;
245+
margin-right: 10px;
246+
margin-top: 5px;
247+
}
248+
}
249+
aside textarea {
250+
height: 100px;
251+
margin-bottom: 5px;
252+
}
253+
span.image {
254+
margin-top: 10px;
255+
}
256+
input {
257+
border: 0;
258+
}
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
class ApplicationController < ActionController::Base
22
include SessionsHelper
3-
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
4-
allow_browser versions: :modern
53

6-
def hello
7-
render html: "hello, world!"
4+
private
5+
6+
# Confirms a logged-in user.
7+
def logged_in_user
8+
unless logged_in?
9+
store_location
10+
flash[:danger] = "Please log in."
11+
redirect_to login_url, status: :see_other
12+
end
813
end
914
end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class MicropostsController < ApplicationController
2+
before_action :logged_in_user, only: [:create, :destroy]
3+
before_action :correct_user,
4+
only: :destroy
5+
6+
def create
7+
@micropost = current_user.microposts.build(micropost_params)
8+
@micropost.image.attach(params[:micropost][:image])
9+
if @micropost.save
10+
flash[:success] = "Micropost created!"
11+
redirect_to root_url
12+
else
13+
@feed_items = current_user.feed.paginate(page: params[:page])
14+
render "static_pages/home", status: :unprocessable_entity
15+
end
16+
end
17+
18+
def destroy
19+
@micropost.destroy
20+
flash[:success] = "Micropost deleted"
21+
if request.referrer.nil? || request.referrer == microposts_url
22+
redirect_to root_url, status: :see_other
23+
else
24+
redirect_to request.referrer, status: :see_other
25+
end
26+
end
27+
28+
private
29+
30+
def micropost_params
31+
params.require(:micropost).permit(:content, :image)
32+
end
33+
34+
def correct_user
35+
@micropost = current_user.microposts.find_by(id: params[:id])
36+
redirect_to root_url, status: :see_other if @micropost.nil?
37+
end
38+
end

app/controllers/static_pages_controller.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
class StaticPagesController < ApplicationController
22
def home
3+
if logged_in?
4+
@micropost = current_user.microposts.build
5+
@feed_items = current_user.feed.paginate(page: params[:page])
6+
end
37
end
48

59
def help
@@ -9,6 +13,5 @@ def about
913
end
1014

1115
def contact
12-
1316
end
1417
end

app/controllers/users_controller.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class UsersController < ApplicationController
55

66
def show
77
@user = User.find(params[:id])
8+
@microposts = @user.microposts.paginate(page: params[:page])
89
end
910

1011
def new
@@ -35,9 +36,6 @@ def update
3536
end
3637
end
3738

38-
def show
39-
@user = User.find(params[:id])
40-
end
4139

4240
def index
4341
@users = User.paginate(page: params[:page])

app/helpers/microposts_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module MicropostsHelper
2+
end

app/javascript/application.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
// Entry point for the build script in your package.json
22
import * as bootstrap from "bootstrap";
3-

app/javascript/image_upload.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Prevent uploading of big images.
2+
document.addEventListener("DOMContentLoaded", function () {
3+
const imageUpload = document.querySelector("#micropost_image");
4+
imageUpload.addEventListener("change", function () {
5+
const size_in_megabytes = this.files[0].size / 1024 / 1024;
6+
if (size_in_megabytes > 5) {
7+
alert("Maximum file size is 5MB. Please choose a smaller file.");
8+
this.value = "";
9+
}
10+
});
11+
});

0 commit comments

Comments
 (0)