Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added avatar.
  • Loading branch information
Giang NGUYEN committed Apr 18, 2012
1 parent b3acf36 commit 36fc64c
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 24 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -11,6 +11,8 @@ gem 'thin'
gem 'squeel'
gem 'cancan'
gem 'simple_form'
gem 'carrierwave'
gem 'rmagick', :require => "RMagick"

group :development, :test do
gem 'sqlite3', '1.3.5'
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Expand Up @@ -42,6 +42,9 @@ GEM
rack-test (>= 0.5.4)
selenium-webdriver (~> 2.0)
xpath (~> 0.1.4)
carrierwave (0.6.2)
activemodel (>= 3.2.0)
activesupport (>= 3.2.0)
childprocess (0.3.1)
ffi (~> 1.0.6)
coffee-rails (3.2.2)
Expand Down Expand Up @@ -116,6 +119,7 @@ GEM
rake (0.9.2.2)
rdoc (3.12)
json (~> 1.4)
rmagick (2.13.1)
rspec (2.9.0)
rspec-core (~> 2.9.0)
rspec-expectations (~> 2.9.0)
Expand Down Expand Up @@ -179,6 +183,7 @@ DEPENDENCIES
bootstrap-will_paginate (= 0.0.5)
cancan
capybara (= 1.1.2)
carrierwave
coffee-rails (= 3.2.2)
factory_girl_rails (= 1.4.0)
faker (= 1.0.1)
Expand All @@ -187,6 +192,7 @@ DEPENDENCIES
jquery-rails
pg (= 0.12.2)
rails (= 3.2.3)
rmagick
rspec-rails (= 2.9.0)
sass-rails (= 3.2.4)
simple_form
Expand Down
8 changes: 5 additions & 3 deletions app/models/user.rb
@@ -1,7 +1,7 @@
class User < ActiveRecord::Base
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

attr_accessible :name, :email, :password, :password_confirmation
attr_accessible :name, :email, :password, :password_confirmation, :avatar, :avatar_cache

has_secure_password

Expand All @@ -20,12 +20,14 @@ class User < ActiveRecord::Base

validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, length: { minimum: 6 }
validates :password_confirmation, presence: true
validates :password, length: { minimum: 6 }, :if => Proc.new { |u| u.password.present? || u.new_record? }
validates :password_confirmation, presence: true, :if => Proc.new { |u| u.password.present? }

# Returns users that have at least 1 published issue.
scope :have_published_issues, lambda { where{id >> Issue.published_issues.map(&:user_id).uniq} }

mount_uploader :avatar, AvatarUploader

def feed
Micropost.from_users_followed_by(self)
end
Expand Down
52 changes: 52 additions & 0 deletions app/uploaders/avatar_uploader.rb
@@ -0,0 +1,52 @@
# encoding: utf-8
class AvatarUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
include CarrierWave::RMagick
# include CarrierWave::MiniMagick

# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
# include Sprockets::Helpers::RailsHelper
# include Sprockets::Helpers::IsolatedHelper

# 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
# # For Rails 3.1+ asset pipeline compatibility:
# # 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_fill => [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
31 changes: 15 additions & 16 deletions app/views/users/edit.html.erb
@@ -1,27 +1,26 @@
<% provide(:title, "Edit user") %>
<% provide(:title, "Edit user") %>
<h1>Update your profile</h1>

<div class="row">
<div class="span6 offset3">
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirm Password" %>
<%= f.password_field :password_confirmation %>
<%= simple_form_for @user do |f| %>
Avatar:
<%- if @user.avatar_url -%>
<%= image_tag(@user.avatar_url(:thumb)) -%>
<%- else -%>
Not set.
<%- end -%>
<%= f.file_field :avatar -%>
<%= f.hidden_field :avatar_cache -%>
<%= f.input :name -%>
<%= f.input :email %>
<%= f.input :password %>
<%= f.input :password_confirmation, :required => false %>
<%= f.submit "Save changes", :class => "btn btn-large btn-primary" %>
<% end %>
<%= gravatar_for @user %>
<a href="http://gravatar.com/emails">change</a>
</div>
</div>
</div>
21 changes: 21 additions & 0 deletions config/initializers/fog.rb
@@ -0,0 +1,21 @@
CarrierWave.configure do |config|
config.cache_dir = "#{Rails.root}/tmp/uploads"

if Rails.env.production?
config.fog_credentials = {
:provider => 'AWS', # required
:aws_access_key_id => 'AKIAJATKGAWCCDNFZN6Q', # required
:aws_secret_access_key => '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', # required
:region => 'us-east-1' # optional, defaults to 'us-east-1'
}
config.storage = :fog
config.permissions = 0666
config.fog_directory = 'assets-banktracker' # required
config.fog_public = true # optional, defaults to true

elsif Rails.env.development?
config.storage = :file
end
end


5 changes: 5 additions & 0 deletions db/migrate/20120418095132_add_avatar_to_user.rb
@@ -0,0 +1,5 @@
class AddAvatarToUser < ActiveRecord::Migration
def change
add_column :users, :avatar, :string
end
end
11 changes: 6 additions & 5 deletions db/schema.rb
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20120417034139) do
ActiveRecord::Schema.define(:version => 20120418095132) do

create_table "cases", :force => true do |t|
t.integer "user_id"
Expand All @@ -34,13 +34,13 @@
create_table "issues", :force => true do |t|
t.integer "user_id"
t.integer "company_id"
t.text "story"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.text "story", :limit => 255
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "status"
t.string "feeling"
t.text "resolution"
t.boolean "published", :default => true
t.boolean "published", :default => true
end

create_table "microposts", :force => true do |t|
Expand Down Expand Up @@ -75,6 +75,7 @@
t.boolean "admin", :default => false
t.integer "company_id"
t.text "story"
t.string "avatar"
end

add_index "users", ["email"], :name => "index_users_on_email", :unique => true
Expand Down
Binary file added public/uploads/user/avatar/101/3mac.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/uploads/user/avatar/101/thumb_3mac.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 36fc64c

Please sign in to comment.