avatars_for_rails 0.2.9 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/README.rdoc +20 -71
- data/app/assets/javascripts/avatars_for_rails.js +67 -11
- data/app/assets/stylesheets/avatars_for_rails.css +0 -117
- data/app/controllers/avatars_controller.rb +23 -82
- data/app/views/avatars/_crop.html.erb +10 -0
- data/app/views/avatars/_form.html.erb +8 -80
- data/avatars_for_rails.gemspec +10 -12
- data/config/locales/en.yml +1 -0
- data/config/locales/es.yml +1 -0
- data/config/routes.rb +1 -4
- data/lib/avatars_for_rails/active_record.rb +13 -0
- data/lib/avatars_for_rails/avatarable.rb +86 -0
- data/lib/avatars_for_rails/engine.rb +13 -0
- data/lib/avatars_for_rails/version.rb +3 -0
- data/lib/avatars_for_rails.rb +24 -23
- data/vendor/assets/javascripts/jquery.Jcrop.js +1694 -0
- data/vendor/assets/javascripts/jquery.fileupload-ui.js +747 -469
- data/vendor/assets/javascripts/jquery.fileupload.js +1060 -867
- data/vendor/assets/stylesheets/jquery.Jcrop.css +161 -31
- metadata +98 -63
- data/app/models/avatar.rb +0 -149
- data/app/views/avatars/_errors.html.erb +0 -5
- data/app/views/avatars/_list.html.erb +0 -30
- data/app/views/avatars/_new.html.erb +0 -7
- data/app/views/avatars/_precrop.html.erb +0 -38
- data/app/views/avatars/destroy.js.erb +0 -2
- data/app/views/avatars/edit.html.erb +0 -6
- data/app/views/avatars/index.html.erb +0 -2
- data/app/views/avatars/new.html.erb +0 -1
- data/app/views/avatars/show.html.erb +0 -5
- data/app/views/avatars/update.js.erb +0 -9
- data/lib/avatars_for_rails/avatars_controller_config.rb +0 -4
- data/vendor/assets/javascripts/jquery.Jcrop.min.js +0 -163
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4003a0bea75c7e7076c70515cd31c8787b340873
|
4
|
+
data.tar.gz: 8c9b84367fb971c6f2193afd14508b8bf02cc253
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2c09be333a4944a4964377b77da73796ece94fd9677a706a05e0563238885602be890a4a1626da9c23475bf05089ba8cf08ecbbaf357416c6efc16d4920735b0
|
7
|
+
data.tar.gz: 57a38b0525ddb5d88f5b17dabdec6258d61f210d4800c980857fc234056b68ab1ddaeeebaa7498febb720236862edcfcc7baf84f9b08501b2f57733e98b5b882
|
data/.gitignore
CHANGED
data/README.rdoc
CHANGED
@@ -1,7 +1,10 @@
|
|
1
|
-
=
|
1
|
+
= Avatars For Rails
|
2
2
|
|
3
|
-
|
3
|
+
Avatars For Rails is a integral solution to provide a model with avatar support.
|
4
4
|
|
5
|
+
It includes a database migration and views with jquery.fileupload and jquery.jcrop
|
6
|
+
|
7
|
+
It relies on paperclip and imagemagick
|
5
8
|
|
6
9
|
= Installation
|
7
10
|
|
@@ -15,9 +18,10 @@ and run:
|
|
15
18
|
|
16
19
|
Then, if you have a class named actor and you want it to have avatars, run:
|
17
20
|
|
18
|
-
rails generate avatars_for_rails:install
|
21
|
+
rails generate avatars_for_rails:install user
|
19
22
|
|
20
23
|
This will generate the following:
|
24
|
+
|
21
25
|
* A initializer file with configuration for avatars_for_rails.
|
22
26
|
* A migration providing the database schema for using avatars_for_rails with the actor class.
|
23
27
|
|
@@ -25,77 +29,22 @@ Do not forget to migrate your database
|
|
25
29
|
|
26
30
|
rake db:migrate
|
27
31
|
|
28
|
-
In your
|
29
|
-
|
30
|
-
<head>
|
31
|
-
<% stylesheet_link_tag :all %>
|
32
|
-
<% javascript_include_tag :defaults %>
|
33
|
-
<%= yield :headers %>
|
34
|
-
<%= csrf_meta_tag %>
|
35
|
-
<script type="text/javascript" charset="utf-8">
|
36
|
-
$(document).ready(function() {
|
37
|
-
<%= yield :javascript %>
|
38
|
-
})
|
39
|
-
</script>
|
40
|
-
</head>
|
41
|
-
|
42
|
-
In config/initializers/avatars_for_rails.rb you have to define:
|
43
|
-
|
44
|
-
AvatarsForRails.setup do |config|
|
45
|
-
config.avatarable_model = :actor
|
46
|
-
config.current_avatarable_object = :current_actor
|
47
|
-
config.avatarable_filters = [:authenticate_user!]
|
48
|
-
config.avatarable_styles = { :representation => "20x20>",
|
49
|
-
:tie => "30x30>",
|
50
|
-
:actor => '35x35>',
|
51
|
-
:profile => '94x94'}
|
52
|
-
end
|
53
|
-
|
54
|
-
* actor is the name of the object owner of the avatars
|
55
|
-
* current_actor will return the actor you wants to be the owner of the new avatars, etc For example, you can return the current logged in actor. This method can be defined on the application_controller of your app.
|
56
|
-
* authenticate_user! is the filter which will be applied as a before_filter for the avatars. You can use as many as you want.
|
57
|
-
* styles are the styles you want to have for your avatars. You can use your own sizes.
|
58
|
-
|
59
|
-
Finally, add to your actor model the relation with avatars_for_rails:
|
60
|
-
|
61
|
-
has_many :avatars
|
62
|
-
|
63
|
-
Now, if you go to /avatars you will see:
|
64
|
-
* A list of all the avatars for the current actor. You can change the default and delete avatars in this list.
|
65
|
-
* Forms for creating a new avatar with two options. A typical file filed or a drag and drop file field
|
32
|
+
In your model, you must include:
|
66
33
|
|
67
|
-
|
34
|
+
class User < ActiveRecord::Base
|
35
|
+
acts_as_avatarable
|
36
|
+
end
|
68
37
|
|
69
|
-
For rendering the avatars list:
|
70
|
-
<%= render :partial => 'list' %>
|
71
|
-
|
72
38
|
For rendering the new avatar form:
|
73
39
|
<%= render :partial => 'form' %>
|
74
40
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
<%= javascript_include_tag 'rails' %>
|
83
|
-
<%= javascript_include_tag 'avatars' %>
|
84
|
-
<%= javascript_include_tag 'jquery.form' %>
|
85
|
-
<%= javascript_include_tag 'jquery.fileupload' %>
|
86
|
-
<%= javascript_include_tag 'jquery.fileupload-ui' %>
|
87
|
-
<%= stylesheet_link_tag "jquery.Jcrop", :media => "screen, projection" %>
|
88
|
-
<%= stylesheet_link_tag "avatars", :media => "screen, projection" %><br/>
|
89
|
-
<%= stylesheet_link_tag "jquery.fileupload-ui", :media => "screen, projection" %><br/>
|
90
|
-
<%end%>
|
91
|
-
|
92
|
-
If you want to get an actor's active avatar you can do:
|
93
|
-
actor.avatars.active.first
|
94
|
-
|
95
|
-
For rendering an image_tag for an acctor's avatar:
|
96
|
-
<%= image_tag(avatar.logo.url(:style) ) %>
|
97
|
-
|
98
|
-
If you wan't to display an actor's active avatar:
|
99
|
-
<%= image_tag(actor.avatars.active.first, :id => "current_avatar_img") %>
|
41
|
+
You must include the javascript and css files
|
42
|
+
|
43
|
+
//= require avatars_for_rails
|
44
|
+
|
45
|
+
|
46
|
+
If you want to get an actor's avatar you can do:
|
47
|
+
user.logo
|
100
48
|
|
101
|
-
|
49
|
+
For rendering an image_tag for an user's avatar:
|
50
|
+
<%= image_tag(user.logo.url(:style) ) %>
|
@@ -1,15 +1,71 @@
|
|
1
1
|
//= require jquery
|
2
2
|
//= require jquery-ui
|
3
|
-
//= require jquery.Jcrop
|
3
|
+
//= require jquery.Jcrop
|
4
4
|
//= require jquery.form
|
5
5
|
//= require jquery.fileupload
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
}
|
15
|
-
|
6
|
+
// require jquery.fileupload-ui
|
7
|
+
//= require flashy
|
8
|
+
|
9
|
+
var AvatarForRails = AvatarForRails || (function($, undefined) {
|
10
|
+
var editCallbacks = [];
|
11
|
+
|
12
|
+
var addEditCallback = function(callback){
|
13
|
+
editCallbacks.push(callback);
|
14
|
+
};
|
15
|
+
|
16
|
+
var edit = function(){
|
17
|
+
$.each(editCallbacks, function(i, callback){ callback(); });
|
18
|
+
};
|
19
|
+
|
20
|
+
var initFileUpload = function() {
|
21
|
+
$('input[name*="logo"]').fileupload({
|
22
|
+
dataType: 'json',
|
23
|
+
done: uploadDone
|
24
|
+
});
|
25
|
+
};
|
26
|
+
|
27
|
+
var uploadDone = function(e, data) {
|
28
|
+
if (data.result.redirect_path) {
|
29
|
+
window.location = data.result.redirect_path;
|
30
|
+
} else if (data.result.crop) {
|
31
|
+
initCrop(data.result.crop);
|
32
|
+
} else {
|
33
|
+
Flashy.message('error', data.result.errors);
|
34
|
+
}
|
35
|
+
};
|
36
|
+
|
37
|
+
var initCrop = function(data) {
|
38
|
+
var div = $('section.avatar .update');
|
39
|
+
div.html(data);
|
40
|
+
div.find('img.crop').Jcrop({
|
41
|
+
bgColor: 'clear',
|
42
|
+
bgOpacity: 0.6,
|
43
|
+
setSelect: [ 0, 0, 200, 200 ],
|
44
|
+
aspectRatio: 1,
|
45
|
+
onSelect: updateCrop,
|
46
|
+
onChange: updateCrop
|
47
|
+
});
|
48
|
+
};
|
49
|
+
|
50
|
+
var updateCrop = function(coords) {
|
51
|
+
var img = $('section.avatar img.crop');
|
52
|
+
var iW = img.width();
|
53
|
+
var iH = img.height();
|
54
|
+
|
55
|
+
if ((coords.w === 0) || (coords.h === 0)){
|
56
|
+
coords.x = 0;
|
57
|
+
coords.y = 0;
|
58
|
+
}
|
59
|
+
|
60
|
+
$('input[name*="logo_crop_x"]').val(coords.x / iW);
|
61
|
+
$('input[name*="logo_crop_y"]').val(coords.y / iH);
|
62
|
+
$('input[name*="logo_crop_w"]').val(coords.w / iW);
|
63
|
+
$('input[name*="logo_crop_h"]').val(coords.h / iH);
|
64
|
+
};
|
65
|
+
|
66
|
+
addEditCallback(initFileUpload);
|
67
|
+
|
68
|
+
return {
|
69
|
+
edit: edit
|
70
|
+
};
|
71
|
+
})(jQuery);
|
@@ -1,120 +1,3 @@
|
|
1
1
|
/*
|
2
2
|
*= require jquery.Jcrop
|
3
|
-
*= require jquery.fileupload-ui
|
4
3
|
*/
|
5
|
-
|
6
|
-
/******************** Common ***********************/
|
7
|
-
|
8
|
-
#avatars_for_rails img { max-width: none !important; }
|
9
|
-
.clearfloat { clear: both; height: 0px;}
|
10
|
-
.space_center { padding-top: 9px; padding-right: 4px; text-align: center;}
|
11
|
-
h2{padding-left:10px;font-size: 14px;color: #2A3890;}
|
12
|
-
/******************** Avatars ***********************/
|
13
|
-
.avatars {
|
14
|
-
padding-left: 10px;
|
15
|
-
}
|
16
|
-
.avatar {
|
17
|
-
float:left;
|
18
|
-
position:relative;
|
19
|
-
}
|
20
|
-
.avatar .actions {
|
21
|
-
}
|
22
|
-
.avatar .delete_avatar {
|
23
|
-
position:absolute;
|
24
|
-
top:5px;
|
25
|
-
right:5px;
|
26
|
-
display:none;
|
27
|
-
}
|
28
|
-
.avatar .default {
|
29
|
-
border: 5px solid #2A3890;
|
30
|
-
}
|
31
|
-
.avatar .non_default {
|
32
|
-
border: 5px solid #E0EEF5;
|
33
|
-
}
|
34
|
-
#avatar_logo_drag{
|
35
|
-
margin-left: 25px;
|
36
|
-
}
|
37
|
-
/******************** New form ***********************/
|
38
|
-
.new_logo_form {
|
39
|
-
}
|
40
|
-
/******************** Slide Effect ***********************/
|
41
|
-
.boxgrid {
|
42
|
-
width: 104px;
|
43
|
-
height: 104px;
|
44
|
-
float:left;
|
45
|
-
overflow: hidden;
|
46
|
-
position: relative;
|
47
|
-
}
|
48
|
-
.boxgrid img {
|
49
|
-
position: absolute;
|
50
|
-
top: 0;
|
51
|
-
left: 0;
|
52
|
-
border: 0;
|
53
|
-
}
|
54
|
-
.boxcaption {
|
55
|
-
top: 104px;
|
56
|
-
left: 5px;
|
57
|
-
float: left;
|
58
|
-
position: absolute;
|
59
|
-
background: #E0EEF5;
|
60
|
-
opacity: .8;
|
61
|
-
width: 84px;
|
62
|
-
height: 28px;
|
63
|
-
/* For IE 5-7 */
|
64
|
-
filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=80);
|
65
|
-
/* For IE 8 */
|
66
|
-
-MS-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=80)";
|
67
|
-
}
|
68
|
-
.captionfull .boxcaption {
|
69
|
-
top: 104px;
|
70
|
-
left: 5px;
|
71
|
-
padding: 5px;
|
72
|
-
}
|
73
|
-
/**************** Crop **************/
|
74
|
-
#precropDiv {
|
75
|
-
width:499px;
|
76
|
-
display:inline-block;
|
77
|
-
padding: 0px 30px 0px 29px;
|
78
|
-
}
|
79
|
-
#precropPrev {
|
80
|
-
vertical-align:top;
|
81
|
-
display:inline-block;
|
82
|
-
/*padding: 5px 4px 5px 4px;*/
|
83
|
-
}
|
84
|
-
#precropPrevImg {
|
85
|
-
width:100px;
|
86
|
-
height:100px;
|
87
|
-
overflow:hidden;
|
88
|
-
margin-left:30px;
|
89
|
-
border: 1px solid #E0EEF5;
|
90
|
-
}
|
91
|
-
/************** BLOCK DIV SECTION *********/
|
92
|
-
.block .content{ padding: 5px 4px 5px 4px;}
|
93
|
-
.block .form_row { display: block; padding:5px; padding-left:20px; }
|
94
|
-
|
95
|
-
|
96
|
-
/*************REVIEW ********************/
|
97
|
-
* { margin: 0; padding: 0;}
|
98
|
-
html, body {height: 100%;}
|
99
|
-
.p { margin: 0px; padding: 0px; font-size: inherit; font-family: inherit; font-weight: inherit;
|
100
|
-
text-align: inherit; color: inherit; line-height: inherit; vertical-align: top;}
|
101
|
-
p { padding-top: 0px; margin-top: 0px;}
|
102
|
-
img { border: 0px;}
|
103
|
-
div { margin: 0px; padding: 0px; }
|
104
|
-
.AbsWrap { width: 100%; position: relative;}
|
105
|
-
.rowWrap { width: 100%;}
|
106
|
-
/**a:link,a:visited { color: inherit; text-decoration: inherit;}**/
|
107
|
-
a:hover { text-decoration: underline;}
|
108
|
-
.clearfloat { clear: both; height: 0px;}
|
109
|
-
h2{padding-left:10px;}
|
110
|
-
body{font-size:1.0em; color:#2A3890; background-color:#ffffff; }
|
111
|
-
#wrapper { background-color: #f5f5f5; width: 100%; margin: 0px auto; }
|
112
|
-
#wrapper_body{min-height: 552px; width: 960px; margin:0px auto; margin-left: auto; margin-right: auto;
|
113
|
-
margin-top: auto; margin-bottom:auto;}
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
.button{margin: 10px 0 10px 10px;padding: 3px 3px 3px 3px;color:#FFFFFF;background-color: #1F4A75;}
|
119
|
-
|
120
|
-
|
@@ -1,98 +1,39 @@
|
|
1
1
|
class AvatarsController < ApplicationController
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
def index
|
7
|
-
@avatars = current_avatarable_object.avatars
|
8
|
-
|
9
|
-
respond_to do |format|
|
10
|
-
format.html # index.html.erb
|
11
|
-
end
|
2
|
+
# Apply the filters configured in avatars_for_rails initializer
|
3
|
+
AvatarsForRails.controller_filters.each do |filter|
|
4
|
+
before_filter filter
|
12
5
|
end
|
13
6
|
|
14
|
-
def
|
15
|
-
|
16
|
-
@avatar = Avatar.create(params[:avatar])
|
17
|
-
|
18
|
-
if params[:drag_name].blank?
|
7
|
+
def update
|
8
|
+
if current_avatarable.update_attributes avatarable_params
|
19
9
|
respond_to do |format|
|
20
|
-
format.html
|
10
|
+
format.html { redirect_to request.referrer || root_path }
|
11
|
+
format.json { render json: { redirect_path: request.referrer || root_path } }
|
21
12
|
end
|
13
|
+
elsif current_avatarable.errors[:logo_crop]
|
14
|
+
render json: { crop: render_to_string(partial: 'crop', object: current_avatarable, as: :avatarable) }.to_json
|
22
15
|
else
|
23
|
-
|
24
|
-
render :partial => 'precrop'
|
25
|
-
else
|
26
|
-
@avatar.logo.errors['invalid_type'] = I18n.t('avatar.error.no_image_file')
|
27
|
-
render :partial => 'errors'
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
32
|
-
|
33
|
-
def update
|
34
|
-
|
35
|
-
if !current_avatarable_object.avatars.blank?
|
36
|
-
|
37
|
-
new_logo = Avatar.find(params[:id])
|
38
|
-
|
39
|
-
if (new_logo.avatarable == current_avatarable_object)
|
40
|
-
actual_logo = current_avatarable_object.avatars.active.first
|
41
|
-
unless actual_logo.blank?
|
42
|
-
return if new_logo == actual_logo
|
43
|
-
old_logo_id = actual_logo.id
|
44
|
-
current_avatarable_object.avatars.each do |old_logo|
|
45
|
-
old_logo.active = false
|
46
|
-
old_logo.save
|
47
|
-
end
|
48
|
-
else
|
49
|
-
old_logo_id = params[:id].to_s
|
50
|
-
end
|
51
|
-
|
52
|
-
new_logo.active = true
|
53
|
-
new_logo.save
|
54
|
-
|
55
|
-
respond_to do |format|
|
56
|
-
format.js { render :layout=>false , :locals=>{:new_id => params[:id],:old_id => old_logo_id}}
|
57
|
-
end
|
58
|
-
|
59
|
-
end
|
16
|
+
render json: current_avatarable.errors.to_json
|
60
17
|
end
|
61
18
|
end
|
62
19
|
|
63
|
-
def
|
64
|
-
@avatar = current_avatarable_object.avatars.create(params[:avatar])
|
65
|
-
|
66
|
-
if @avatar.new_record?
|
67
|
-
if (params[:avatar].blank? || params[:avatar][:drag].nil?)
|
68
|
-
render :new, :layout => false
|
69
|
-
else
|
70
|
-
render :json => {:name => File.basename(@avatar.logo.queued_for_write[:original].path) }
|
71
|
-
end
|
72
|
-
else
|
73
|
-
redirect_to avatars_path
|
74
|
-
end
|
20
|
+
def destroy
|
75
21
|
end
|
76
22
|
|
77
|
-
|
78
|
-
@avatar = Avatar.find(params[:id])
|
23
|
+
private
|
79
24
|
|
80
|
-
|
81
|
-
|
82
|
-
respond_to do |format|
|
83
|
-
format.js { render :layout=>false , :locals=>{:deleted_id => params[:id]}}
|
84
|
-
end
|
85
|
-
end
|
25
|
+
def current_avatarable
|
26
|
+
send AvatarsForRails.controller_avatarable
|
86
27
|
end
|
87
28
|
|
88
|
-
def
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
29
|
+
def avatarable_params
|
30
|
+
params.
|
31
|
+
require(current_avatarable.class.to_s.underscore).
|
32
|
+
permit(:logo,
|
33
|
+
:logo_crop_x,
|
34
|
+
:logo_crop_y,
|
35
|
+
:logo_crop_w,
|
36
|
+
:logo_crop_h,
|
37
|
+
:avatar_tmp_basename)
|
96
38
|
end
|
97
|
-
|
98
39
|
end
|