Skip to content

Commit

Permalink
Create image_posting_function of micropost
Browse files Browse the repository at this point in the history
  • Loading branch information
hogemax committed Mar 12, 2018
1 parent b0243fb commit 52a4dbd
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@
/tmp
/vendor/bundle
/database.sqlite
/public/uploads/tmp/
/public/uploads/micropost/image/
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ gem 'jbuilder', '1.5.3'
group :doc do
gem 'sdoc', '0.4.1', require: false
end
gem 'carrierwave'
gem 'rmagick'
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ GEM
bootstrap-sass (3.3.1.0)
sass (~> 3.2)
builder (3.1.4)
carrierwave (1.2.2)
activemodel (>= 4.0.0)
activesupport (>= 4.0.0)
mime-types (>= 1.16)
coffee-rails (4.0.1)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.0)
Expand Down Expand Up @@ -87,6 +91,7 @@ GEM
rdoc (4.3.0)
responders (1.1.2)
railties (>= 3.2, < 4.2)
rmagick (2.16.0)
sass (3.2.19)
sass-rails (4.0.3)
railties (>= 4.0.0, < 5.0)
Expand Down Expand Up @@ -129,12 +134,14 @@ PLATFORMS

DEPENDENCIES
bootstrap-sass (= 3.3.1)
carrierwave
coffee-rails (= 4.0.1)
devise (= 3.4.1)
faker (= 1.1.2)
jbuilder (= 1.5.3)
jquery-rails (= 3.1.2)
rails (= 4.0.5)
rmagick
sass-rails (= 4.0.3)
sdoc (= 0.4.1)
sprockets (= 2.11.0)
Expand Down
4 changes: 4 additions & 0 deletions app/assets/stylesheets/custom.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ input {
li {
padding: 10px 0;
border-top: 1px solid #e8e8e8;

.image img {
width: 100%;
}
}
}
.content {
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/microposts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def destroy
private

def micropost_params
params.require(:micropost).permit(:content)
params.require(:micropost).permit(:content, :image)
end

def correct_user
Expand Down
1 change: 1 addition & 0 deletions app/models/micropost.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Micropost < ActiveRecord::Base
mount_uploader :image, ImageUploader
belongs_to :user
default_scope -> { order('created_at DESC') }
validates :content, presence: true, length: { maximum: 140 }
Expand Down
63 changes: 63 additions & 0 deletions app/uploaders/image_uploader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
class ImageUploader < 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]
#process :resize_to_fit => [200, 200]
process :resize_to_limit => [300, 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

#version :profile do
# process :resize_to_fill => [150, 150, gravity = ::Magick::CenterGravity]
#end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
# def extension_whitelist
# %w(jpg jpeg gif png)
# end

# jpg,jpeg,gif,pngしか受け付けない
def extension_white_list
%w(jpg jpeg gif png)
end

#ファイルサイズ制限
def size_range
1..9.megabytes
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
3 changes: 3 additions & 0 deletions app/views/microposts/_micropost.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<li>
<span class="content"><%= micropost.content %></span>
<% if micropost.image? %>
<div class="image"><%= image_tag micropost.image_url %></div>
<% end %>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
Expand Down
3 changes: 3 additions & 0 deletions app/views/shared/_feed_item.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<%= link_to feed_item.user.name, feed_item.user %>
</span>
<span class="content"><%= feed_item.content %></span>
<% if feed_item.image? %>
<div><%= image_tag feed_item.image_url(:thumb) %></div>
<% end %>
<span class="timestamp">
Posted <%= time_ago_in_words(feed_item.created_at) %> ago.
</span>
Expand Down
4 changes: 4 additions & 0 deletions app/views/shared/_micropost_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
<div class="field">
<%= f.text_area :content, placeholder: "Compose new micropost..." %>
</div>
<div class=field>
<%= f.label :image %><br>
<%= f.file_field :image %>
</div>
<%= f.submit "Post", class: "btn btn-lg btn-primary" %>
<% end %>
5 changes: 5 additions & 0 deletions db/migrate/20180311182848_add_images_to_microposts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddImagesToMicroposts < ActiveRecord::Migration
def change
add_column :microposts, :image, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20180306140953) do
ActiveRecord::Schema.define(version: 20180311182848) do

create_table "microposts", force: true do |t|
t.string "content"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "image"
end

add_index "microposts", ["user_id", "created_at"], name: "index_microposts_on_user_id_and_created_at"
Expand Down

0 comments on commit 52a4dbd

Please sign in to comment.