Skip to content

Commit 233f5bb

Browse files
committed
Add user microposts
1 parent d291f5e commit 233f5bb

34 files changed

+598
-109
lines changed

Gemfile

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
33

44
ruby "3.1.1"
55

6-
gem "rails", "7.0.2.3"
7-
gem "bcrypt", "3.1.16"
8-
gem "faker", "2.20.0"
9-
gem "will_paginate", "3.3.1"
10-
gem "bootstrap-will_paginate", "1.0.0"
11-
gem "bootstrap-sass", "3.4.1"
12-
gem "sassc-rails", "2.1.2"
13-
gem "sprockets-rails", "3.4.2"
14-
gem "importmap-rails", "1.0.3"
15-
gem "turbo-rails", "1.0.1"
16-
gem "stimulus-rails", "1.0.4"
17-
gem "jbuilder", "2.11.5"
18-
gem "puma", "5.6.4"
19-
gem "bootsnap", "1.11.1", require: false
6+
gem "rails", "7.0.2.3"
7+
gem "image_processing", "1.12.2"
8+
gem "active_storage_validations", "0.9.7"
9+
gem "bcrypt", "3.1.16"
10+
gem "faker", "2.20.0"
11+
gem "will_paginate", "3.3.1"
12+
gem "bootstrap-will_paginate", "1.0.0"
13+
gem "bootstrap-sass", "3.4.1"
14+
gem "sassc-rails", "2.1.2"
15+
gem "sprockets-rails", "3.4.2"
16+
gem "importmap-rails", "1.0.3"
17+
gem "turbo-rails", "1.0.1"
18+
gem "stimulus-rails", "1.0.4"
19+
gem "jbuilder", "2.11.5"
20+
gem "puma", "5.6.4"
21+
gem "bootsnap", "1.11.1", require: false
2022

2123
group :development, :test do
2224
gem "debug", "1.4.0", platforms: %i[ mri mingw x64_mingw ]
@@ -39,7 +41,8 @@ group :test do
3941
end
4042

4143
group :production do
42-
gem "pg", "1.3.3"
44+
gem "pg", "1.3.3"
45+
gem "aws-sdk-s3", "1.113.0", require: false
4346
end
4447

4548
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem.

Gemfile.lock

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ GEM
4646
erubi (~> 1.4)
4747
rails-dom-testing (~> 2.0)
4848
rails-html-sanitizer (~> 1.1, >= 1.2.0)
49+
active_storage_validations (0.9.7)
50+
activejob (>= 5.2.0)
51+
activemodel (>= 5.2.0)
52+
activestorage (>= 5.2.0)
53+
activesupport (>= 5.2.0)
4954
activejob (7.0.2.3)
5055
activesupport (= 7.0.2.3)
5156
globalid (>= 0.3.6)
@@ -71,6 +76,22 @@ GEM
7176
ansi (1.5.0)
7277
autoprefixer-rails (10.4.2.0)
7378
execjs (~> 2)
79+
aws-eventstream (1.2.0)
80+
aws-partitions (1.574.0)
81+
aws-sdk-core (3.130.0)
82+
aws-eventstream (~> 1, >= 1.0.2)
83+
aws-partitions (~> 1, >= 1.525.0)
84+
aws-sigv4 (~> 1.1)
85+
jmespath (~> 1.0)
86+
aws-sdk-kms (1.55.0)
87+
aws-sdk-core (~> 3, >= 3.127.0)
88+
aws-sigv4 (~> 1.1)
89+
aws-sdk-s3 (1.113.0)
90+
aws-sdk-core (~> 3, >= 3.127.0)
91+
aws-sdk-kms (~> 1)
92+
aws-sigv4 (~> 1.4)
93+
aws-sigv4 (1.4.0)
94+
aws-eventstream (~> 1, >= 1.0.2)
7495
bcrypt (3.1.16)
7596
bindex (0.8.1)
7697
bootsnap (1.11.1)
@@ -121,6 +142,9 @@ GEM
121142
minitest (>= 3.0)
122143
i18n (1.10.0)
123144
concurrent-ruby (~> 1.0)
145+
image_processing (1.12.2)
146+
mini_magick (>= 4.9.5, < 5)
147+
ruby-vips (>= 2.0.17, < 3)
124148
importmap-rails (1.0.3)
125149
actionpack (>= 6.0.0)
126150
railties (>= 6.0.0)
@@ -130,6 +154,7 @@ GEM
130154
jbuilder (2.11.5)
131155
actionview (>= 5.0.0)
132156
activesupport (>= 5.0.0)
157+
jmespath (1.6.1)
133158
listen (3.7.1)
134159
rb-fsevent (~> 0.10, >= 0.10.3)
135160
rb-inotify (~> 0.9, >= 0.9.10)
@@ -142,6 +167,7 @@ GEM
142167
marcel (1.0.2)
143168
matrix (0.4.2)
144169
method_source (1.0.0)
170+
mini_magick (4.11.0)
145171
mini_mime (1.1.2)
146172
minitest (5.15.0)
147173
minitest-reporters (1.5.0)
@@ -221,6 +247,8 @@ GEM
221247
io-console (~> 0.5)
222248
rexml (3.2.5)
223249
ruby-progressbar (1.11.0)
250+
ruby-vips (2.1.4)
251+
ffi (~> 1.12)
224252
rubyzip (2.3.2)
225253
sassc (2.4.0)
226254
ffi (~> 1.9)
@@ -275,6 +303,8 @@ PLATFORMS
275303
x86_64-darwin-19
276304

277305
DEPENDENCIES
306+
active_storage_validations (= 0.9.7)
307+
aws-sdk-s3 (= 1.113.0)
278308
bcrypt (= 3.1.16)
279309
bootsnap (= 1.11.1)
280310
bootstrap-sass (= 3.4.1)
@@ -284,6 +314,7 @@ DEPENDENCIES
284314
faker (= 2.20.0)
285315
guard (= 2.18.0)
286316
guard-minitest (= 2.4.6)
317+
image_processing (= 1.12.2)
287318
importmap-rails (= 1.0.3)
288319
jbuilder (= 2.11.5)
289320
minitest (= 5.15.0)

app/assets/stylesheets/custom.scss

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,51 @@ input {
224224
padding: 10px 0;
225225
border-bottom: 1px solid $gray-lighter;
226226
}
227+
}
228+
229+
/* microposts */
230+
231+
.microposts {
232+
list-style: none;
233+
padding: 0;
234+
li {
235+
padding: 10px 0;
236+
border-top: 1px solid #e8e8e8;
237+
}
238+
.user {
239+
margin-top: 5em;
240+
padding-top: 0;
241+
}
242+
.content {
243+
display: block;
244+
margin-left: 60px;
245+
img {
246+
display: block;
247+
padding: 5px 0;
248+
}
249+
}
250+
.timestamp {
251+
color: $gray-light;
252+
display: block;
253+
margin-left: 60px;
254+
}
255+
.gravatar {
256+
float: left;
257+
margin-right: 10px;
258+
margin-top: 5px;
259+
}
260+
}
261+
262+
aside {
263+
textarea {
264+
height: 100px;
265+
margin-bottom: 5px;
266+
}
267+
}
268+
269+
span.image {
270+
margin-top: 10px;
271+
input {
272+
border: 0;
273+
}
227274
}
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
class ApplicationController < ActionController::Base
22
include SessionsHelper
3-
end
3+
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
12+
end
13+
end
14+
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class MicropostsController < ApplicationController
2+
before_action :logged_in_user, only: [:create, :destroy]
3+
before_action :correct_user, only: :destroy
4+
5+
def create
6+
@micropost = current_user.microposts.build(micropost_params)
7+
@micropost.image.attach(params[:micropost][:image])
8+
if @micropost.save
9+
flash[:success] = "Micropost created!"
10+
redirect_to root_url
11+
else
12+
@feed_items = current_user.feed.paginate(page: params[:page])
13+
render 'static_pages/home', status: :unprocessable_entity
14+
end
15+
end
16+
17+
def destroy
18+
@micropost.destroy
19+
flash[:success] = "Micropost deleted"
20+
if request.referrer.nil? || request.referrer == microposts_url
21+
redirect_to root_url
22+
else
23+
redirect_to request.referrer, status: :see_other
24+
end
25+
end
26+
27+
private
28+
29+
def micropost_params
30+
params.require(:micropost).permit(:content, :image)
31+
end
32+
33+
def correct_user
34+
@micropost = current_user.microposts.find_by(id: params[:id])
35+
redirect_to root_url if @micropost.nil?
36+
end
37+
end
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
class StaticPagesController < ApplicationController
2+
23
def home
4+
if logged_in?
5+
@micropost = current_user.microposts.build
6+
@feed_items = current_user.feed.paginate(page: params[:page])
7+
end
38
end
49

510
def help
611
end
712

813
def about
914
end
10-
15+
1116
def contact
1217
end
1318
end

app/controllers/users_controller.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def index
99

1010
def show
1111
@user = User.find(params[:id])
12+
@microposts = @user.microposts.paginate(page: params[:page])
1213
end
1314

1415
def new
@@ -55,15 +56,6 @@ def user_params
5556

5657
# Before filters
5758

58-
# Confirms a logged-in user.
59-
def logged_in_user
60-
unless logged_in?
61-
store_location
62-
flash[:danger] = "Please log in."
63-
redirect_to login_url
64-
end
65-
end
66-
6759
# Confirms the correct user.
6860
def correct_user
6961
@user = User.find(params[:id])

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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
import "@hotwired/turbo-rails"
44
import "controllers"
55
import "custom/menu"
6+
import "custom/image_upload"

app/javascript/custom/image_upload.js

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

0 commit comments

Comments
 (0)