Skip to content

Commit 781d9ed

Browse files
committed
替换paperclip,使用carrierwave(方便重用上传代码,或者使用七牛/upyun)
1 parent 9033922 commit 781d9ed

13 files changed

+101
-68
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@
1515
/log/*.log
1616
/tmp
1717
/config/config.yml
18-
/public/system
18+
/public/uploads

Gemfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ gem 'bootstrap_helper', '4.2.3'
2727
gem 'nprogress-rails', '~> 0.1.3.0'
2828

2929
# 头像上传
30-
gem 'paperclip', '~> 4.1'
30+
gem 'mini_magick', '3.3'
31+
gem 'carrierwave', '~> 0.10.0'
3132

3233
gem 'sass-rails', '~> 4.0.2'
3334
gem 'uglifier', '2.1.1'

Gemfile.lock

+10-10
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@ GEM
5050
rack (>= 1.0.0)
5151
rack-test (>= 0.5.4)
5252
xpath (~> 2.0)
53+
carrierwave (0.10.0)
54+
activemodel (>= 3.2.0)
55+
activesupport (>= 3.2.0)
56+
json (>= 1.7)
57+
mime-types (>= 1.16)
5358
childprocess (0.5.3)
5459
ffi (~> 1.0, >= 1.0.11)
55-
climate_control (0.0.3)
56-
activesupport (>= 3.0)
57-
cocaine (0.5.4)
58-
climate_control (>= 0.0.3, < 1.0)
5960
coderay (1.1.0)
6061
coffee-rails (4.0.1)
6162
coffee-script (>= 2.2.0)
@@ -135,18 +136,15 @@ GEM
135136
mime-types (~> 1.16)
136137
treetop (~> 1.4.8)
137138
mime-types (1.25.1)
139+
mini_magick (3.3)
140+
subexec (~> 0.1.0)
138141
mini_portile (0.6.0)
139142
minitest (4.7.5)
140143
multi_json (1.10.1)
141144
multi_test (0.1.1)
142145
nokogiri (1.6.2.1)
143146
mini_portile (= 0.6.0)
144147
nprogress-rails (0.1.3.0)
145-
paperclip (4.1.1)
146-
activemodel (>= 3.0.0)
147-
activesupport (>= 3.0.0)
148-
cocaine (~> 0.5.3)
149-
mime-types
150148
polyglot (0.3.5)
151149
rack (1.5.2)
152150
rack-test (0.6.2)
@@ -210,6 +208,7 @@ GEM
210208
activesupport (>= 3.0)
211209
sprockets (~> 2.8)
212210
sqlite3 (1.3.7)
211+
subexec (0.1.0)
213212
thin (1.6.2)
214213
daemons (>= 1.0.9)
215214
eventmachine (>= 1.0.0)
@@ -241,6 +240,7 @@ DEPENDENCIES
241240
better_errors
242241
bootstrap_helper (= 4.2.3)
243242
capybara (= 2.1.0)
243+
carrierwave (~> 0.10.0)
244244
coffee-rails (= 4.0.1)
245245
country_select (= 1.3.1)
246246
cucumber-rails (= 1.3.0)
@@ -250,8 +250,8 @@ DEPENDENCIES
250250
jquery-rails (= 2.2.1)
251251
jquery-turbolinks (~> 2.0.2)
252252
kaminari-bootstrap (~> 0.1.3)!
253+
mini_magick (= 3.3)
253254
nprogress-rails (~> 0.1.3.0)
254-
paperclip (~> 4.1)
255255
rails (= 4.0.3)
256256
rspec-rails (= 2.13.1)
257257
ruby-ole (= 1.2.11.7)

README.md

+12-10
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,6 @@
22
管理系统基本功能框架,可在此基础上扩展业务功能。
33

44
##安装
5-
```shell
6-
git clone [email protected]:linuxnerd/ratab.git
7-
cd ratab
8-
bundle install
9-
cd config
10-
cp config.default.yml config.yml # 将参数修改成自己的参数(注意修改faye token)
11-
bundle exec rake db:schema:load
12-
bundle exec rake db:init # 创建一个管理员用户[email protected]
13-
```
145
头像上传功能必须安装[ImageMagick](http://www.imagemagick.org/),安装方法:
156

167
如果是ubuntu
@@ -21,6 +12,17 @@ sudo apt-get install imagemagick
2112
```
2213
brew install imagemagick
2314
```
15+
部署application
16+
```shell
17+
git clone [email protected]:linuxnerd/ratab.git
18+
cd ratab
19+
bundle install
20+
cd config
21+
cp config.default.yml config.yml # 将参数修改成自己的参数(注意修改faye token)
22+
bundle exec rake db:schema:load
23+
bundle exec rake db:init # 创建一个管理员用户[email protected]
24+
```
25+
2426
注意修改`config/environments`下各环境的邮箱配置
2527
```ruby
2628
config.action_mailer.delivery_method = :smtp
@@ -41,7 +43,7 @@ config.action_mailer.smtp_settings = {
4143
- 基于`faye-rails`的实时通知系统,直接操作Notification模型即可
4244
- 基本的国际化
4345
- 基于`spreadsheet`的导出到excel的功能
44-
- 基于`paperclip`的头像上传功能
46+
- 基于`carrierwave`的头像上传功能
4547

4648
##要注意的坑
4749
- `wice_grid``will_paginate`存在兼容性问题,paginate使用`kaminari-bootstrap` 0.1.3版本

app/models/user.rb

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class User < ActiveRecord::Base
2+
mount_uploader :avatar, AvatarUploader
23
has_many :notifications, dependent: :destroy
34
before_save { self.email = email.downcase }
45
before_create :create_remember_token
@@ -14,14 +15,6 @@ class User < ActiveRecord::Base
1415

1516
validates_length_of :password, :within => 6..16, message: '新密码长度不正确,应该在6到16位之间', on: :create
1617

17-
# paperclip
18-
has_attached_file :avatar,
19-
styles: { :medium => "300x300>", :thumb => "100x100>" },
20-
default_url: "avatars/profile-pic.jpg"
21-
validates_attachment :avatar,
22-
content_type: { content_type: /\Aimage\/.*\Z/ },
23-
size: { in: 0..4.megabytes }
24-
2518
# rails自带
2619
has_secure_password
2720

app/uploaders/avatar_uploader.rb

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# encoding: utf-8
2+
3+
class AvatarUploader < CarrierWave::Uploader::Base
4+
5+
# Include RMagick or MiniMagick support:
6+
# include CarrierWave::RMagick
7+
include CarrierWave::MiniMagick
8+
9+
# Choose what kind of storage to use for this uploader:
10+
storage :file
11+
# storage :fog
12+
13+
# Override the directory where uploaded files will be stored.
14+
# This is a sensible default for uploaders that are meant to be mounted:
15+
def store_dir
16+
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
17+
end
18+
19+
# Provide a default URL as a default if there hasn't been a file uploaded:
20+
def default_url
21+
ActionController::Base.helpers.asset_path("avatars/profile-pic.jpg")
22+
end
23+
24+
# Process files as they are uploaded:
25+
# process :scale => [200, 300]
26+
#
27+
# def scale(width, height)
28+
# # do something
29+
# end
30+
31+
# Create different versions of your uploaded files:
32+
version :thumb do
33+
process :resize_to_fit => [50, 50]
34+
end
35+
36+
version :medium do
37+
process :resize_to_fit => [300, 300]
38+
end
39+
40+
# Add a white list of extensions which are allowed to be uploaded.
41+
# For images you might use something like this:
42+
def extension_white_list
43+
%w(jpg jpeg gif png)
44+
end
45+
46+
# Override the filename of the uploaded files:
47+
# Avoid using model.id or version_name here, see uploader/store.rb for details.
48+
def filename
49+
"#{secure_token}.#{file.extension}" if original_filename
50+
end
51+
52+
protected
53+
def secure_token(length=16)
54+
var = :"@#{mounted_as}_secure_token"
55+
model.instance_variable_get(var) || model.instance_variable_set(var, SecureRandom.hex(length/2))
56+
end
57+
58+
end

app/views/layouts/_header.html.erb

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<% end %>
1111

1212
<ul class="nav ace-nav pull-right">
13-
13+
1414
<li>
1515
<a data-toggle="dropdown" class="dropdown-toggle" href="#">
1616
<i class="icon-bell-alt icon-animated-bell"></i>
@@ -95,4 +95,4 @@
9595
</div><!--/.container-fluid-->
9696
</div><!--/.navbar-inner-->
9797
</div>
98-
</header>
98+
</header>

app/views/users/edit.html.erb

+5-6
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@
2424
<i class="icon-plus-sign bigger-110"></i>
2525
加为好友
2626
</a>
27-
27+
2828
<a href="#" class="btn btn-small btn-block btn-primary">
2929
<i class="icon-envelope-alt"></i>
3030
发私信
3131
</a>
3232
</div>
33-
33+
3434
<div class="span9">
35-
<%= simple_form_for @user, :html => {class: 'form-horizontal'} do |f| %>
35+
<%= simple_form_for @user, :html => {class: 'form-horizontal', multipart: true} do |f| %>
3636
<fieldset>
3737
<legend>
3838
<h4 class="blue">
@@ -64,7 +64,7 @@
6464
</span>
6565
<% end %>
6666
<%= f.input :avatar, as: :file, input_html: { accept: "image/*" } %>
67-
67+
6868
<div class="form-actions" >
6969
<%= button_tag class: 'btn btn-primary' do %>
7070
<i class='icon-save'></i>
@@ -76,8 +76,7 @@
7676
</div><!--/span-->
7777
</div>
7878
</div>
79-
79+
8080
<!--PAGE CONTENT ENDS-->
8181
</div><!--/.span-->
8282
</div><!--/.row-fluid-->
83-

config/locales/carrierwave.zh-CN.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
errors:
2+
messages:
3+
carrierwave_processing_error: "无法重新调整大小"
4+
carrierwave_integrity_error: "并不是图片文件"
5+
carrierwave_download_error: "无法下载图片"
6+
extension_white_list_error: "不能上传 %{extension} 文件, 允许上传的文件扩展名有: %{allowed_types}"
7+
extension_black_list_error: "不能上传 %{extension} 文件, 不允许上传的文件扩展名有: %{prohibited_types}"

config/locales/paperclip.zh-CN.yml

-18
This file was deleted.

config/locales/zh-CN.yml

-6
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,3 @@
3838
phone: '联系电话'
3939
last_ip: '最后登录IP'
4040
current_password: '当前密码'
41-
errors:
42-
models:
43-
user:
44-
attributes:
45-
avatar_content_type:
46-
invalid: '图片文件格式非法'
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class AddAvatarToUsers < ActiveRecord::Migration
22
def change
3-
add_attachment :users, :avatar
3+
add_column :users, :avatar, :string
44
end
55
end

db/schema.rb

+2-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#
1212
# It's strongly recommended that you check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(version: 20140606064641) do
14+
ActiveRecord::Schema.define(version: 20140606135622) do
1515

1616
create_table "notifications", force: true do |t|
1717
t.string "title"
@@ -35,10 +35,7 @@
3535
t.string "remember_token"
3636
t.string "password_reset_token"
3737
t.datetime "password_expires_after"
38-
t.string "avatar_file_name"
39-
t.string "avatar_content_type"
40-
t.integer "avatar_file_size"
41-
t.datetime "avatar_updated_at"
38+
t.string "avatar"
4239
end
4340

4441
add_index "users", ["email"], name: "index_users_on_email", unique: true

0 commit comments

Comments
 (0)