Permalink
Browse files

added avatar to user form

  • Loading branch information...
1 parent f43be8a commit 43e614c7966fcacb4adefda65f3cb723e56ab950 @orenomba committed Aug 8, 2012
View
@@ -28,7 +28,8 @@ def validate_each(record, attribute, value)
class User < ActiveRecord::Base
authenticates_with_sorcery!
- attr_accessible :name, :email, :password, :password_confirmation, :avatar
+ attr_accessor :remove_avatar
+ attr_accessible :name, :email, :password, :password_confirmation, :avatar, :remove_avatar
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
validates :name, :email, presence: true, uniqueness: true
@@ -42,6 +43,8 @@ class User < ActiveRecord::Base
validates :password, presence: {on: :create}
validates :password, confirmation: true, :if => :password?
+ before_save :should_remove_avatar?, :if => :remove_avatar?
+
class << self
def name?(guess)
guess.strip!
@@ -60,4 +63,14 @@ def name?(guess)
def password?
password.present?
end
+
+ def remove_avatar?
+ not @remove_avatar.to_i.zero?
+ end
+
+ def should_remove_avatar?
+ self.avatar.destroy
+
+ true
+ end
end
@@ -6,6 +6,10 @@
<%= semantic_form_for @user do |form| %>
<%= form.input :avatar %>
+ <% if @user.avatar.file? %>
+ <%= image_tag @user.avatar.url :thumb %>
+ <%= form.input :remove_avatar, as: :boolean %>
+ <% end %>
<%= form.input :name, input_html: name_options %>
<%= form.input :email %>
<%= form.input :password %>
@@ -211,6 +211,7 @@ ja:
labels:
user:
avatar: アバター
+ remove_avatar: アバター削除
name: ユーザー名
email: Eメール
password: パスワード
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -102,6 +102,15 @@
@user.should_not be_valid
end
end
+
+ it "should remove avatar by remove_avatar attribute" do
+ @user.avatar = File.open File.join(File.dirname(__FILE__), "user", "avatar.jpg")
+ @user.save!
+ @user.avatar.should be_file
+
+ @user.update_attributes({"remove_avatar" => "1"})
+ @user.avatar.should_not be_file
+ end
end
describe "name?" do
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -11,29 +11,41 @@
it "should show page" do
page.should have_content "登録"
end
- end
-
- describe "create" do
- before(:each) do
- visit signup_path
- end
-
- it "should show errors" do
- click_button "登録"
- page.should have_selector "div.error", count: 3
- end
- it "should register" do
- fill_in "user_name", with: "test"
- fill_in "user_email", with: "test@domain.local"
- fill_in "user_password", with: "a" * 8
- fill_in "user_password_confirmation", with: "a" * 8
- click_button "登録"
+ describe "create" do
+ before(:each) do
+ visit signup_path
+ end
+
+ it "should show errors" do
+ click_button "登録"
+ page.should have_selector "div.error", count: 3
+ end
- page.should have_content "登録しました"
+ it "should register" do
+ fill_in "user_name", with: "test"
+ fill_in "user_email", with: "test@domain.local"
+ fill_in "user_password", with: "a" * 8
+ fill_in "user_password_confirmation", with: "a" * 8
+ click_button "登録"
+
+ page.should have_content "登録しました"
+ end
+
+ it "should be able to set avatar" do
+ attach_file('user_avatar', File.join(File.dirname(__FILE__), "users", "avatar.jpg"))
+ fill_in "user_name", with: "test"
+ fill_in "user_email", with: "test@domain.local"
+ fill_in "user_password", with: "a" * 8
+ fill_in "user_password_confirmation", with: "a" * 8
+ click_button "登録"
+
+ User.first.avatar.should be_file
+ end
end
end
+
describe "show" do
before(:each) do
@user = FactoryGirl.create :user, name: "test", email: "test@domain.local", password: "a" * 8
@@ -80,9 +92,6 @@
end
describe "update" do
- before(:each) do
- end
-
it "should update" do
lambda{
fill_in "user_email", with: "edit@domain.local"
@@ -103,6 +112,26 @@
@user.reload
}.should_not change(@user, :name).from("test").to("changed")
end
+
+ describe "avatar" do
+ before(:each) do
+ @user.avatar = File.open File.join(File.dirname(__FILE__), "users", "avatar.jpg")
+ @user.save!
+ visit edit_user_path @user
+ end
+
+ it "should be able to remove avatar" do
+ check "user_remove_avatar"
+ click_button "編集"
+
+ # なぜかreloadだとavatarがリロードされない
+ # rails console だとちゃんとリロードされている
+ # TODO @user.reload
+ @user = User.find @user.id
+
+ @user.avatar.should_not be_file
+ end
+ end
end
end
end

0 comments on commit 43e614c

Please sign in to comment.