Skip to content

Commit

Permalink
added image download via Carrierwave to user profile
Browse files Browse the repository at this point in the history
  • Loading branch information
strix3000 committed Jan 30, 2012
1 parent e1f92fc commit 9aedee7
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ gem 'sorcery'
gem 'therubyracer'
gem 'sorcery'
gem 'carrierwave'
gem 'rmagick'

# Gems used only for assets and not required
# in production environments by default.
group :assets do
Expand Down
5 changes: 3 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
class User < ActiveRecord::Base
attr_accessible :email, :password, :password_confirmation
attr_accessible :email, :password, :password_confirmation, :image
authenticates_with_sorcery!

validates_length_of :password, :minimum => 3, :message => "password must be at least 3 characters long", :if => :password
validates_confirmation_of :password, :message => "should match confirmation", :if => :password
mount_uploader :image, ImageUploader
end
7 changes: 4 additions & 3 deletions app/models/user.rb~
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
class User < ActiveRecord::Base
attr_accessible :email, :password, :password_confirmation
attr_accessible :email, :password, :password_confirmation, :image
authenticates_with_sorcery!

validates :password, :length => { :minimum => 3, maximum => 20 }, :confirmation => true

validates_length_of :password, :minimum => 3, :message => "password must be at least 3 characters long", :if => :password
validates_confirmation_of :password, :message => "should match confirmation", :if => :password
mount_uploader :image, Image_uploader
end
48 changes: 48 additions & 0 deletions app/uploaders/avatar_uploader.rb~
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# encoding: utf-8

class AvatarUploader < 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
# "/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 :scale => [50, 50]
# end

# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
# def extension_white_list
# %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
48 changes: 48 additions & 0 deletions app/uploaders/image_uploader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# encoding: utf-8

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
# "/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 => [200, 75]
end

# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
# def extension_white_list
# %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
48 changes: 48 additions & 0 deletions app/uploaders/image_uploader.rb~
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# encoding: utf-8

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
# "/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 => [200, 75]
end

# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
# def extension_white_list
# %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
6 changes: 5 additions & 1 deletion app/views/users/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
</div>
<% end %>

<div class="field">
<%= f.file_field :image %><br />
</div>

<div class="field">
<%= f.label :username %><br />
<%= f.text_field :username %>
Expand All @@ -25,7 +29,7 @@
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
<%= f.password_field :password_confirmation %>
</div>
</div>
<div class="actions">
Expand Down
8 changes: 8 additions & 0 deletions app/views/users/_form.html.erb~
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
</div>
</div>
<div class="actions">
<%= f.submit %>
Expand Down
5 changes: 5 additions & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<p id="notice"><%= notice %></p>

<p>
<b>Image:</b>
<%= image_tag @user.image_url(:thumb) if @user.image? %>
</p>

<p>
<b>Username:</b>
<%= @user.username %>
Expand Down
5 changes: 5 additions & 0 deletions app/views/users/show.html.erb~
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<p id="notice"><%= notice %></p>

<p>
<b>Image:</b>
<%= image_tag @user.image_url(:thumb)%>
</p>

<p>
<b>Username:</b>
<%= @user.username %>
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20120130150725_add_image_to_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddImageToUser < ActiveRecord::Migration
def change
add_column :users, :image, :string

end
end

0 comments on commit 9aedee7

Please sign in to comment.