Browse files

added image download via Carrierwave to user profile

  • Loading branch information...
1 parent e1f92fc commit 9aedee7c7064c612d701cb0d270cb0e5bf75b785 @strix3000 strix3000 committed Jan 30, 2012
View
2 Gemfile
@@ -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
View
5 app/models/user.rb
@@ -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
View
7 app/models/user.rb~
@@ -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
View
48 app/uploaders/avatar_uploader.rb~
@@ -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
View
48 app/uploaders/image_uploader.rb
@@ -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
View
48 app/uploaders/image_uploader.rb~
@@ -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
View
6 app/views/users/_form.html.erb
@@ -12,6 +12,10 @@
<% end %>
<div class="field">
+ <%= f.file_field :image %><br />
+ </div>
+
+ <div class="field">
<%= f.label :username %><br />
<%= f.text_field :username %>
</div>
@@ -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">
View
8 app/views/users/_form.html.erb~
@@ -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 %>
View
5 app/views/users/show.html.erb
@@ -1,6 +1,11 @@
<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 %>
</p>
View
5 app/views/users/show.html.erb~
@@ -1,6 +1,11 @@
<p id="notice"><%= notice %></p>
<p>
+ <b>Image:</b>
+ <%= image_tag @user.image_url(:thumb)%>
+</p>
+
+<p>
<b>Username:</b>
<%= @user.username %>
</p>
View
6 db/migrate/20120130150725_add_image_to_user.rb
@@ -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.