Skip to content

Commit a0394da

Browse files
Image uploading feature was added
1 parent 8130f8d commit a0394da

File tree

8 files changed

+120
-4
lines changed

8 files changed

+120
-4
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,5 @@ end
3838

3939
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
4040
gem "tzinfo-data", platforms: [:mingw, :mswin, :x64_mingw, :jruby]
41+
42+
gem "active_model_serializers"

Gemfile.lock

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ GEM
3939
erubi (~> 1.4)
4040
rails-dom-testing (~> 2.0)
4141
rails-html-sanitizer (~> 1.1, >= 1.2.0)
42+
active_model_serializers (0.10.13)
43+
actionpack (>= 4.1, < 7.1)
44+
activemodel (>= 4.1, < 7.1)
45+
case_transform (>= 0.2)
46+
jsonapi-renderer (>= 0.1.1.beta1, < 0.3)
4247
activejob (6.1.4.4)
4348
activesupport (= 6.1.4.4)
4449
globalid (>= 0.3.6)
@@ -80,6 +85,8 @@ GEM
8085
rack-test (>= 0.6.3)
8186
regexp_parser (>= 1.5, < 3.0)
8287
xpath (~> 3.2)
88+
case_transform (0.2)
89+
activesupport
8390
childprocess (4.1.0)
8491
concurrent-ruby (1.1.9)
8592
connection_pool (2.2.5)
@@ -98,6 +105,7 @@ GEM
98105
jbuilder (2.11.5)
99106
actionview (>= 5.0.0)
100107
activesupport (>= 5.0.0)
108+
jsonapi-renderer (0.2.2)
101109
launchy (2.5.0)
102110
addressable (~> 2.7)
103111
letter_opener (1.7.0)
@@ -246,6 +254,7 @@ PLATFORMS
246254
x86-mswin32
247255

248256
DEPENDENCIES
257+
active_model_serializers
249258
bootsnap (>= 1.4.4)
250259
byebug
251260
capybara (>= 3.26)

app/controllers/home_controller.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ class HomeController < ApplicationController
44
before_action :set_todo_item, only: [:edit_todo_item]
55

66
def landing
7-
@todos = Todo.all.order(:id)
7+
@todos = Todo.all.order(:id).map { |record| TodoSerializer.new(record) }
88
end
99

1010
def edit_todo_item
11-
@todo_item.update(todo_item_params)
11+
if todo_item_params[:image]
12+
@todo_item.image.attach(todo_item_params[:image])
13+
end
14+
@todo_item.update(todo_item_params.except(:image))
15+
1216
render json: @todo_item
1317
end
1418

@@ -19,7 +23,7 @@ def reset_todo_items
1923
private
2024

2125
def todo_item_params
22-
params.require(:home).permit(:id, :title, :checked)
26+
params.require(:home).permit(:id, :title, :checked, :image)
2327
end
2428

2529
def set_todo_item

app/javascript/components/TodoList/index.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ type TodoItem = {
77
id: number;
88
title: string;
99
checked: boolean;
10+
image: string;
1011
};
1112

1213
type Props = {
@@ -31,11 +32,31 @@ const TodoList: React.FC<Props> = ({ todoItems }) => {
3132
checked: e.target.checked,
3233
}
3334
}).then((res) => {
35+
let foundIndex = todoItems.findIndex(todo => todo.id == res.data.id);
36+
todoItems[foundIndex] = res.data.id;
3437
e.target.checked = res.data.checked;
3538
});
3639

3740
};
3841

42+
const onImageChange = (event, id) => {
43+
const data = new FormData();
44+
data.append("home[image]", event.target.files[0]);
45+
data.append("home[id]", `${id}`);
46+
47+
axios.post('todo/', data, {
48+
headers: {
49+
'Content-Type': 'multipart/form-data'
50+
}})
51+
.then((res) => {
52+
let foundIndex = todoItems.findIndex(x => x.id == res.data.id);
53+
todoItems[foundIndex] = res.data.id;
54+
55+
const image = document.getElementById(`img_${res.data.id}`) as HTMLImageElement | null;
56+
image.src = res.data.image;
57+
});
58+
};
59+
3960
const resetButtonOnClick = (): void => {
4061
axios.post("/reset").then(() => location.reload());
4162
};
@@ -52,6 +73,11 @@ const TodoList: React.FC<Props> = ({ todoItems }) => {
5273
checked={todo.checked}
5374
onChange={(e) => checkBoxOnCheck(e, todo.id)}
5475
/>
76+
77+
<img id={`img_${todo.id}`}
78+
src={todo.image}
79+
style={{maxHeight: '100px', maxWidth: '100px'}}/>
80+
<Form.Control type="file" accept="image/*" multiple={false} onChange={(e) => onImageChange(e, todo.id)} />
5581
</ListGroup.Item>
5682
))}
5783
<ResetButton onClick={resetButtonOnClick}>Reset</ResetButton>

app/models/todo.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
class Todo < ApplicationRecord
2+
has_one_attached :image
23
end

app/serializers/todo_serializer.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class TodoSerializer < ActiveModel::Serializer
2+
include Rails.application.routes.url_helpers
3+
attributes :id, :title, :checked, :image
4+
5+
def image
6+
rails_blob_path(object.image, only_path: true) if object.image.attached?
7+
end
8+
end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# This migration comes from active_storage (originally 20170806125915)
2+
class CreateActiveStorageTables < ActiveRecord::Migration[5.2]
3+
def change
4+
create_table :active_storage_blobs do |t|
5+
t.string :key, null: false
6+
t.string :filename, null: false
7+
t.string :content_type
8+
t.text :metadata
9+
t.string :service_name, null: false
10+
t.bigint :byte_size, null: false
11+
t.string :checksum, null: false
12+
t.datetime :created_at, null: false
13+
14+
t.index [ :key ], unique: true
15+
end
16+
17+
create_table :active_storage_attachments do |t|
18+
t.string :name, null: false
19+
t.references :record, null: false, polymorphic: true, index: false
20+
t.references :blob, null: false
21+
22+
t.datetime :created_at, null: false
23+
24+
t.index [ :record_type, :record_id, :name, :blob_id ], name: "index_active_storage_attachments_uniqueness", unique: true
25+
t.foreign_key :active_storage_blobs, column: :blob_id
26+
end
27+
28+
create_table :active_storage_variant_records do |t|
29+
t.belongs_to :blob, null: false, index: false
30+
t.string :variation_digest, null: false
31+
32+
t.index %i[ blob_id variation_digest ], name: "index_active_storage_variant_records_uniqueness", unique: true
33+
t.foreign_key :active_storage_blobs, column: :blob_id
34+
end
35+
end
36+
end

db/schema.rb

Lines changed: 31 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)