Skip to content

Commit

Permalink
Complete chapter-7
Browse files Browse the repository at this point in the history
  • Loading branch information
JunichiIto committed Jan 2, 2023
1 parent 3947acf commit 1795d35
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@

# Ignore master key for decrypting credentials and more.
/config/master.key

/public/uploads
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,5 @@ group :test do
gem "selenium-webdriver"
gem "webdrivers"
end

gem "carrierwave"
17 changes: 17 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,29 @@ GEM
rack-test (>= 0.6.3)
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
carrierwave (2.2.3)
activemodel (>= 5.0.0)
activesupport (>= 5.0.0)
addressable (~> 2.6)
image_processing (~> 1.1)
marcel (~> 1.0.0)
mini_mime (>= 0.1.3)
ssrf_filter (~> 1.0)
concurrent-ruby (1.1.10)
crass (1.0.6)
date (3.3.3)
debug (1.7.1)
irb (>= 1.5.0)
reline (>= 0.3.1)
erubi (1.12.0)
ffi (1.15.5)
globalid (1.0.0)
activesupport (>= 5.0)
i18n (1.12.0)
concurrent-ruby (~> 1.0)
image_processing (1.12.2)
mini_magick (>= 4.9.5, < 5)
ruby-vips (>= 2.0.17, < 3)
importmap-rails (1.1.5)
actionpack (>= 6.0.0)
railties (>= 6.0.0)
Expand All @@ -112,6 +124,7 @@ GEM
marcel (1.0.2)
matrix (0.4.2)
method_source (1.0.0)
mini_magick (4.12.0)
mini_mime (1.1.2)
mini_portile2 (2.8.1)
minitest (5.17.0)
Expand Down Expand Up @@ -167,6 +180,8 @@ GEM
reline (0.3.2)
io-console (~> 0.5)
rexml (3.2.5)
ruby-vips (2.1.4)
ffi (~> 1.12)
rubyzip (2.3.2)
selenium-webdriver (4.7.1)
rexml (~> 3.2, >= 3.2.5)
Expand All @@ -181,6 +196,7 @@ GEM
sprockets (>= 3.0.0)
sqlite3 (1.5.4)
mini_portile2 (~> 2.8.0)
ssrf_filter (1.1.1)
stimulus-rails (1.2.1)
railties (>= 6.0.0)
thor (1.2.1)
Expand Down Expand Up @@ -214,6 +230,7 @@ PLATFORMS
DEPENDENCIES
bootsnap
capybara
carrierwave
debug
importmap-rails
jbuilder
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/books_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ def set_book

# Only allow a list of trusted parameters through.
def book_params
params.require(:book).permit(:title, :memo, :author)
params.require(:book).permit(:title, :memo, :author, :picture)
end
end
1 change: 1 addition & 0 deletions app/models/book.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class Book < ApplicationRecord
mount_uploader :picture, PictureUploader
end
47 changes: 47 additions & 0 deletions app/uploaders/picture_uploader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class PictureUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick

# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog

# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url(*args)
# # For Rails 3.1+ asset pipeline compatibility:
# # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
#
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end

# Process files as they are uploaded:
# process scale: [200, 300]
#
# def scale(width, height)
# # do something
# end

# Create different versions of your uploaded files:
# version :thumb do
# process resize_to_fit: [50, 50]
# end

# Add an allowlist of extensions which are allowed to be uploaded.
# For images you might use something like this:
# def extension_allowlist
# %w(jpg jpeg gif png)
# end

# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end
end
5 changes: 5 additions & 0 deletions app/views/books/_book.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@
<%= book.author %>
</p>

<p>
<strong>Picture:</strong>
<%= image_tag(book.picture_url) if book.picture.present? %>
</p>

</div>
5 changes: 5 additions & 0 deletions app/views/books/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
<%= form.text_field :author %>
</div>

<div>
<%= form.label :picture, style: "display: block" %>
<%= form.file_field :picture %>
</div>

<div>
<%= form.submit %>
</div>
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20230102000542_add_picture_to_books.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddPictureToBooks < ActiveRecord::Migration[7.0]
def change
add_column :books, :picture, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

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

0 comments on commit 1795d35

Please sign in to comment.