avatars_for_rails 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. data/.gitignore +10 -0
  2. data/Gemfile +3 -0
  3. data/Gemfile.lock +138 -0
  4. data/MIT-LICENSE +20 -0
  5. data/README.rdoc +91 -0
  6. data/Rakefile +29 -0
  7. data/app/controllers/avatars_controller.rb +109 -0
  8. data/app/models/avatar.rb +138 -0
  9. data/app/views/avatars/_errors.html.erb +6 -0
  10. data/app/views/avatars/_form.html.erb +128 -0
  11. data/app/views/avatars/_includes.html.erb +14 -0
  12. data/app/views/avatars/_list.html.erb +39 -0
  13. data/app/views/avatars/_new.html.erb +15 -0
  14. data/app/views/avatars/_precrop.html.erb +38 -0
  15. data/app/views/avatars/destroy.js.erb +2 -0
  16. data/app/views/avatars/edit.html.erb +8 -0
  17. data/app/views/avatars/index.html.erb +4 -0
  18. data/app/views/avatars/new.html.erb +1 -0
  19. data/app/views/avatars/show.html.erb +8 -0
  20. data/app/views/avatars/update.js.erb +6 -0
  21. data/avatars_for_rails.gemspec +40 -0
  22. data/config/locales/en.yml +28 -0
  23. data/config/routes.rb +6 -0
  24. data/lib/avatars_for_rails/avatars_controller_config.rb +4 -0
  25. data/lib/avatars_for_rails.rb +27 -0
  26. data/lib/generators/avatars_for_rails/install_generator.rb +28 -0
  27. data/lib/generators/avatars_for_rails/templates/initializer.rb +11 -0
  28. data/lib/generators/avatars_for_rails/templates/migration.rb +20 -0
  29. data/lib/generators/avatars_for_rails/templates/public/images/Jcrop.gif +0 -0
  30. data/lib/generators/avatars_for_rails/templates/public/images/cancel.png +0 -0
  31. data/lib/generators/avatars_for_rails/templates/public/images/pbar-ani.gif +0 -0
  32. data/lib/generators/avatars_for_rails/templates/public/images/rails.png +0 -0
  33. data/lib/generators/avatars_for_rails/templates/public/javascripts/application.js +2 -0
  34. data/lib/generators/avatars_for_rails/templates/public/javascripts/avatars.js +8 -0
  35. data/lib/generators/avatars_for_rails/templates/public/javascripts/controls.js +965 -0
  36. data/lib/generators/avatars_for_rails/templates/public/javascripts/dragdrop.js +974 -0
  37. data/lib/generators/avatars_for_rails/templates/public/javascripts/effects.js +1123 -0
  38. data/lib/generators/avatars_for_rails/templates/public/javascripts/jquery-ui.min.js +401 -0
  39. data/lib/generators/avatars_for_rails/templates/public/javascripts/jquery.Jcrop.min.js +163 -0
  40. data/lib/generators/avatars_for_rails/templates/public/javascripts/jquery.fileupload-ui.js +259 -0
  41. data/lib/generators/avatars_for_rails/templates/public/javascripts/jquery.fileupload.js +475 -0
  42. data/lib/generators/avatars_for_rails/templates/public/javascripts/jquery.form.js +815 -0
  43. data/lib/generators/avatars_for_rails/templates/public/javascripts/jquery.js +7179 -0
  44. data/lib/generators/avatars_for_rails/templates/public/javascripts/prototype.js +6001 -0
  45. data/lib/generators/avatars_for_rails/templates/public/javascripts/rails.js +158 -0
  46. data/lib/generators/avatars_for_rails/templates/public/stylesheets/.gitkeep +0 -0
  47. data/lib/generators/avatars_for_rails/templates/public/stylesheets/avatars.css +115 -0
  48. data/lib/generators/avatars_for_rails/templates/public/stylesheets/jquery.Jcrop.css +35 -0
  49. data/lib/generators/avatars_for_rails/templates/public/stylesheets/jquery.fileupload-ui.css +70 -0
  50. data/spec/avatars_for_rails_spec.rb +7 -0
  51. data/spec/dummy/Rakefile +7 -0
  52. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  53. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  54. data/spec/dummy/app/models/actor.rb +7 -0
  55. data/spec/dummy/app/views/layouts/application.html.erb +20 -0
  56. data/spec/dummy/config/application.rb +46 -0
  57. data/spec/dummy/config/boot.rb +10 -0
  58. data/spec/dummy/config/database.yml +22 -0
  59. data/spec/dummy/config/environment.rb +5 -0
  60. data/spec/dummy/config/environments/development.rb +26 -0
  61. data/spec/dummy/config/environments/production.rb +49 -0
  62. data/spec/dummy/config/environments/test.rb +35 -0
  63. data/spec/dummy/config/initializers/avatars_for_rails.rb +6 -0
  64. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  65. data/spec/dummy/config/initializers/inflections.rb +10 -0
  66. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  67. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  68. data/spec/dummy/config/initializers/session_store.rb +8 -0
  69. data/spec/dummy/config/locales/en.yml +5 -0
  70. data/spec/dummy/config/routes.rb +60 -0
  71. data/spec/dummy/config.ru +4 -0
  72. data/spec/dummy/db/migrate/20110411073237_create_actors.rb +13 -0
  73. data/spec/dummy/db/migrate/20110411073238_create_avatars.rb +21 -0
  74. data/spec/dummy/db/schema.rb +32 -0
  75. data/spec/dummy/public/404.html +26 -0
  76. data/spec/dummy/public/422.html +26 -0
  77. data/spec/dummy/public/500.html +26 -0
  78. data/spec/dummy/public/favicon.ico +0 -0
  79. data/spec/dummy/public/images/Jcrop.gif +0 -0
  80. data/spec/dummy/public/images/cancel.png +0 -0
  81. data/spec/dummy/public/images/pbar-ani.gif +0 -0
  82. data/spec/dummy/public/images/rails.png +0 -0
  83. data/spec/dummy/public/javascripts/application.js +2 -0
  84. data/spec/dummy/public/javascripts/avatars.js +8 -0
  85. data/spec/dummy/public/javascripts/controls.js +965 -0
  86. data/spec/dummy/public/javascripts/dragdrop.js +974 -0
  87. data/spec/dummy/public/javascripts/effects.js +1123 -0
  88. data/spec/dummy/public/javascripts/jquery-ui.min.js +401 -0
  89. data/spec/dummy/public/javascripts/jquery.Jcrop.min.js +163 -0
  90. data/spec/dummy/public/javascripts/jquery.fileupload-ui.js +259 -0
  91. data/spec/dummy/public/javascripts/jquery.fileupload.js +475 -0
  92. data/spec/dummy/public/javascripts/jquery.form.js +815 -0
  93. data/spec/dummy/public/javascripts/jquery.js +7179 -0
  94. data/spec/dummy/public/javascripts/prototype.js +6001 -0
  95. data/spec/dummy/public/javascripts/rails.js +158 -0
  96. data/spec/dummy/public/stylesheets/.gitkeep +0 -0
  97. data/spec/dummy/public/stylesheets/avatars.css +115 -0
  98. data/spec/dummy/public/stylesheets/jquery.Jcrop.css +35 -0
  99. data/spec/dummy/public/stylesheets/jquery.fileupload-ui.css +70 -0
  100. data/spec/dummy/public/system/logos/1/actor/stream20110411-3539-1e9l1hn-0.jpg +0 -0
  101. data/spec/dummy/public/system/logos/1/original/stream20110411-3539-1e9l1hn-0.jpg +0 -0
  102. data/spec/dummy/public/system/logos/1/profile/stream20110411-3539-1e9l1hn-0.jpg +0 -0
  103. data/spec/dummy/public/system/logos/1/representation/stream20110411-3539-1e9l1hn-0.jpg +0 -0
  104. data/spec/dummy/public/system/logos/1/tie/stream20110411-3539-1e9l1hn-0.jpg +0 -0
  105. data/spec/dummy/public/system/logos/2/actor/stream20110411-3539-1hlzauh-0.jpeg +0 -0
  106. data/spec/dummy/public/system/logos/2/original/stream20110411-3539-1hlzauh-0.jpeg +0 -0
  107. data/spec/dummy/public/system/logos/2/profile/stream20110411-3539-1hlzauh-0.jpeg +0 -0
  108. data/spec/dummy/public/system/logos/2/representation/stream20110411-3539-1hlzauh-0.jpeg +0 -0
  109. data/spec/dummy/public/system/logos/2/tie/stream20110411-3539-1hlzauh-0.jpeg +0 -0
  110. data/spec/dummy/script/rails +6 -0
  111. data/spec/integration/navigation_spec.rb +9 -0
  112. data/spec/spec_helper.rb +33 -0
  113. metadata +333 -0
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ .bundle/
2
+ log/*.log
3
+ pkg/
4
+ spec/dummy/db/*.sqlite3
5
+ spec/dummy/log/*.log
6
+ spec/dummy/tmp/
7
+ spec/dummy/public/images/tmp/
8
+ spec/dummy/public/system/
9
+ *.DS_Store
10
+ .project
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,138 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ avatars_for_rails (0.0.1)
5
+ foreigner (~> 0.9.1)
6
+ paperclip (~> 2.3.4)
7
+ rmagick (~> 2.13.1)
8
+
9
+ GEM
10
+ remote: http://rubygems.org/
11
+ specs:
12
+ abstract (1.0.0)
13
+ actionmailer (3.0.5)
14
+ actionpack (= 3.0.5)
15
+ mail (~> 2.2.15)
16
+ actionpack (3.0.5)
17
+ activemodel (= 3.0.5)
18
+ activesupport (= 3.0.5)
19
+ builder (~> 2.1.2)
20
+ erubis (~> 2.6.6)
21
+ i18n (~> 0.4)
22
+ rack (~> 1.2.1)
23
+ rack-mount (~> 0.6.13)
24
+ rack-test (~> 0.5.7)
25
+ tzinfo (~> 0.3.23)
26
+ activemodel (3.0.5)
27
+ activesupport (= 3.0.5)
28
+ builder (~> 2.1.2)
29
+ i18n (~> 0.4)
30
+ activerecord (3.0.5)
31
+ activemodel (= 3.0.5)
32
+ activesupport (= 3.0.5)
33
+ arel (~> 2.0.2)
34
+ tzinfo (~> 0.3.23)
35
+ activeresource (3.0.5)
36
+ activemodel (= 3.0.5)
37
+ activesupport (= 3.0.5)
38
+ activesupport (3.0.5)
39
+ arel (2.0.9)
40
+ builder (2.1.2)
41
+ capybara (0.3.9)
42
+ culerity (>= 0.2.4)
43
+ mime-types (>= 1.16)
44
+ nokogiri (>= 1.3.3)
45
+ rack (>= 1.0.0)
46
+ rack-test (>= 0.5.4)
47
+ selenium-webdriver (>= 0.0.3)
48
+ childprocess (0.1.8)
49
+ ffi (~> 1.0.6)
50
+ columnize (0.3.2)
51
+ culerity (0.2.15)
52
+ diff-lcs (1.1.2)
53
+ erubis (2.6.6)
54
+ abstract (>= 1.0.0)
55
+ factory_girl (1.3.3)
56
+ ffi (1.0.7)
57
+ rake (>= 0.8.7)
58
+ foreigner (0.9.1)
59
+ forgery (0.3.7)
60
+ nokogiri (~> 1.4)
61
+ i18n (0.5.0)
62
+ json_pure (1.5.1)
63
+ linecache (0.43)
64
+ mail (2.2.15)
65
+ activesupport (>= 2.3.6)
66
+ i18n (>= 0.4.0)
67
+ mime-types (~> 1.16)
68
+ treetop (~> 1.4.8)
69
+ mime-types (1.16)
70
+ nokogiri (1.4.4)
71
+ paperclip (2.3.10)
72
+ activerecord (>= 2.3.0)
73
+ activesupport (>= 2.3.2)
74
+ polyglot (0.3.1)
75
+ rack (1.2.2)
76
+ rack-mount (0.6.14)
77
+ rack (>= 1.0.0)
78
+ rack-test (0.5.7)
79
+ rack (>= 1.0)
80
+ rails (3.0.5)
81
+ actionmailer (= 3.0.5)
82
+ actionpack (= 3.0.5)
83
+ activerecord (= 3.0.5)
84
+ activeresource (= 3.0.5)
85
+ activesupport (= 3.0.5)
86
+ bundler (~> 1.0)
87
+ railties (= 3.0.5)
88
+ railties (3.0.5)
89
+ actionpack (= 3.0.5)
90
+ activesupport (= 3.0.5)
91
+ rake (>= 0.8.7)
92
+ thor (~> 0.14.4)
93
+ rake (0.8.7)
94
+ rmagick (2.13.1)
95
+ rspec (2.5.0)
96
+ rspec-core (~> 2.5.0)
97
+ rspec-expectations (~> 2.5.0)
98
+ rspec-mocks (~> 2.5.0)
99
+ rspec-core (2.5.1)
100
+ rspec-expectations (2.5.0)
101
+ diff-lcs (~> 1.1.2)
102
+ rspec-mocks (2.5.0)
103
+ rspec-rails (2.5.0)
104
+ actionpack (~> 3.0)
105
+ activesupport (~> 3.0)
106
+ railties (~> 3.0)
107
+ rspec (~> 2.5.0)
108
+ ruby-debug (0.10.4)
109
+ columnize (>= 0.1)
110
+ ruby-debug-base (~> 0.10.4.0)
111
+ ruby-debug-base (0.10.4)
112
+ linecache (>= 0.3)
113
+ rubyzip (0.9.4)
114
+ selenium-webdriver (0.1.4)
115
+ childprocess (>= 0.1.7)
116
+ ffi (>= 1.0.7)
117
+ json_pure
118
+ rubyzip
119
+ sqlite3 (1.3.3)
120
+ sqlite3-ruby (1.3.3)
121
+ sqlite3 (>= 1.3.3)
122
+ thor (0.14.6)
123
+ treetop (1.4.9)
124
+ polyglot (>= 0.3.1)
125
+ tzinfo (0.3.26)
126
+
127
+ PLATFORMS
128
+ ruby
129
+
130
+ DEPENDENCIES
131
+ avatars_for_rails!
132
+ capybara (~> 0.3.9)
133
+ factory_girl (~> 1.3.2)
134
+ forgery (~> 0.3.6)
135
+ rails (~> 3.0.5)
136
+ rspec-rails (~> 2.5.0)
137
+ ruby-debug (~> 0.10.3)
138
+ sqlite3-ruby
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2011 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,91 @@
1
+ = AvatarsForRails
2
+
3
+ This project rocks and uses MIT-LICENSE.
4
+
5
+
6
+ = Installation
7
+
8
+ Add to your Gemfile:
9
+
10
+ gem 'avatars_for_rails'
11
+
12
+ and run:
13
+
14
+ bundle update
15
+
16
+ Then, if you have a class named actor and you want it to have avatars, run:
17
+
18
+ rails generate avatars_for_rails:install actor
19
+
20
+ This will generate the following:
21
+ * A public/ file with the js and css files required.
22
+ * A initializer file with configuration for avatars_for_rails.
23
+ * A migration providing the database schema for using avatars_for_rails with the actor class.
24
+
25
+ Do not forget to migrate your database
26
+
27
+ rake db:migrate
28
+
29
+ In your application layout you have to add this on the <head> label:
30
+
31
+ <head>
32
+ <% stylesheet_link_tag :all %>
33
+ <% javascript_include_tag :defaults %>
34
+ <%= yield :headers %>
35
+ <%= csrf_meta_tag %>
36
+ <script type="text/javascript" charset="utf-8">
37
+ $(document).ready(function() {
38
+ <%= yield :javascript %>
39
+ })
40
+ </script>
41
+ </head>
42
+
43
+ In config/initializers/avatars_for_rails.rb you have to change:
44
+
45
+ def current_avatarable_object
46
+ return Actor.find(:all).first
47
+ end
48
+
49
+ to return the actor you wants to be the owner of the new avatars, etc For example, you can return the current logged in actor.
50
+
51
+ Finally, add to your actor model the relation with avatars_for_rails:
52
+
53
+ has_many :avatars
54
+
55
+ Now, if you go to /avatars you will see:
56
+ * A list of all the avatars for the current actor. You can change the default and delete avatars in this list.
57
+ * Forms for creating a new avatar with two options. A typical file filed or a drag and drop file field
58
+
59
+ For rendering the avatars list:
60
+ <%= render :partial => 'list' %>
61
+
62
+ For rendering the new avatar form:
63
+ <%= render :partial => 'form' %>
64
+
65
+ For include the javacript and css files:
66
+ <%= render :partial => 'includes' %>
67
+ or
68
+ <% content_for :headers do %>
69
+ <%= javascript_include_tag 'jquery' %>
70
+ <%= javascript_include_tag 'jquery.Jcrop.min' %>
71
+ <%= javascript_include_tag 'jquery-ui.min' %>
72
+ <%= javascript_include_tag 'rails' %>
73
+ <%= javascript_include_tag 'avatars' %>
74
+ <%= javascript_include_tag 'jquery.form' %>
75
+ <%= javascript_include_tag 'jquery.fileupload' %>
76
+ <%= javascript_include_tag 'jquery.fileupload-ui' %>
77
+ <%= stylesheet_link_tag "jquery.Jcrop", :media => "screen, projection" %>
78
+ <%= stylesheet_link_tag "avatars", :media => "screen, projection" %><br/>
79
+ <%= stylesheet_link_tag "jquery.fileupload-ui", :media => "screen, projection" %><br/>
80
+ <%end%>
81
+
82
+ For rendering an image_tag for an acctor's avatar:
83
+ <%= image_tag(avatar.logo.url(:style) ) %>
84
+
85
+ The avaible styles for now are:
86
+
87
+ :representation => 20x20 px
88
+ :tie => 30x30 px
89
+ :actor => 35x35 px
90
+ :profile => 94x94 px
91
+
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ # encoding: UTF-8
2
+ require 'rubygems'
3
+
4
+ begin
5
+ require 'bundler/setup'
6
+ require 'bundler'
7
+ rescue LoadError
8
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
9
+ end
10
+
11
+ require 'rake'
12
+ require 'rake/rdoctask'
13
+
14
+ require 'rspec/core'
15
+ require 'rspec/core/rake_task'
16
+
17
+ RSpec::Core::RakeTask.new(:spec)
18
+
19
+ task :default => :spec
20
+
21
+ Rake::RDocTask.new(:rdoc) do |rdoc|
22
+ rdoc.rdoc_dir = 'rdoc'
23
+ rdoc.title = 'AvatarsForRails'
24
+ rdoc.options << '--line-numbers' << '--inline-source'
25
+ rdoc.rdoc_files.include('README.rdoc')
26
+ rdoc.rdoc_files.include('lib/**/*.rb', 'app/**/*.rb')
27
+ end
28
+
29
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,109 @@
1
+ #class AvatarsController < InheritedResources::Base
2
+ class AvatarsController < ApplicationController
3
+ def index
4
+ @avatars = Avatar.all
5
+
6
+ respond_to do |format|
7
+ format.html # index.html.erb
8
+ #format.xml { render :xml => @gastos }
9
+ end
10
+ end
11
+
12
+ def new
13
+ @avatars = Avatar.all
14
+ @avatar = Avatar.create(params[:avatar])
15
+
16
+ if params[:drag_name].blank?
17
+ respond_to do |format|
18
+ format.html # new.html.erb
19
+ end
20
+ else
21
+ #debugger
22
+ if Avatar.check_image_tmp_valid(params[:drag_name].to_s)
23
+ render :partial => 'precrop'
24
+ else
25
+ @avatar.logo.errors['invalid_type'] = I18n.t('avatar.error.no_image_file')
26
+ render :partial => 'errors'
27
+ end
28
+ end
29
+
30
+ end
31
+
32
+ def update
33
+ #debugger
34
+ if !current_avatarable_object.avatars.blank?
35
+
36
+ new_logo = Avatar.find(params[:id])
37
+
38
+ if (new_logo.avatarable == current_avatarable_object)
39
+ actual_logo = current_avatarable_object.avatars.active.first
40
+ unless actual_logo.blank?
41
+ #actual_logo.active = false
42
+ #actual_logo.save
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
60
+ end
61
+
62
+ # redirect_to avatars_path
63
+ end
64
+
65
+ def create
66
+ @avatar = current_avatarable_object.avatars.create(params[:avatar])
67
+
68
+ if @avatar.new_record?
69
+ if (params[:avatar].blank? || params[:avatar][:drag].nil?)
70
+ render :new
71
+ else
72
+ render :json => {:name => File.basename(@avatar.logo.queued_for_write[:original].path) }
73
+ end
74
+ else
75
+ #debugger
76
+ #@avatar.updating_logo = true
77
+ #@avatar.avatarable.id = current_avatarable_object.id
78
+ # unless current_avatarable_object.avatars.blank?
79
+ # current_avatarable_object.avatars.each do |old_logo|
80
+ # old_logo.active = false
81
+ # old_logo.save
82
+ # end
83
+ # end
84
+ #debugger
85
+ #@avatar.active = true
86
+ #@avatar.save
87
+ redirect_to avatars_path
88
+
89
+ end
90
+ end
91
+
92
+ def destroy
93
+ @avatar = Avatar.find(params[:id])
94
+
95
+ if (@avatar.avatarable == current_avatarable_object)
96
+ @avatar.destroy
97
+ respond_to do |format|
98
+ format.js { render :layout=>false , :locals=>{:deleted_id => params[:id]}}
99
+ end
100
+ end
101
+ #redirect_to avatars_path
102
+
103
+ end
104
+
105
+ #def current_avatarable_object
106
+ #return Actor.find(:all).first
107
+ #end
108
+
109
+ end
@@ -0,0 +1,138 @@
1
+ require 'RMagick'
2
+
3
+ class Avatar < ActiveRecord::Base
4
+ has_attached_file :logo,
5
+ :styles => { :representation => "20x20>",
6
+ :tie => "30x30>",
7
+ :actor => '35x35>',
8
+ :profile => '94x94' },
9
+ :default_url => "/images/logos/:style/:subtype_class.png"
10
+
11
+ before_post_process :process_precrop
12
+ attr_accessor :crop_x, :crop_y, :crop_w, :crop_h, :name,:updating_logo,:drag,:drag_name
13
+ validates_attachment_presence :logo, :if => :uploading_file?, :message => I18n.t('avatar.error.no_file')
14
+
15
+ after_validation :precrop_done
16
+
17
+ #belongs_to :actor
18
+ belongs_to AvatarsForRails.avatarable_model
19
+
20
+ scope :active, where(:active => true)
21
+
22
+ before_create :make_active
23
+ after_create :disable_old
24
+
25
+ def make_active
26
+ self.updating_logo = true
27
+ self.active = true
28
+ end
29
+
30
+ def disable_old
31
+ #unless self.avatarable.avatars.blank?
32
+ self.avatarable.avatars.where("id != ?", self.id).each do |old_logo|
33
+ old_logo.update_attribute :active, false
34
+ end
35
+ #end
36
+ end
37
+
38
+
39
+
40
+ def avatarable
41
+ __send__ AvatarsForRails.avatarable_model # __send__ :actor
42
+ end
43
+
44
+ def uploading_file?
45
+ return @name.blank?
46
+ end
47
+
48
+ def precrop_done
49
+ return if @name.blank? || !@updating_logo.blank?
50
+
51
+ precrop_path = File.join(Avatar.images_tmp_path,@name)
52
+
53
+ make_precrop(precrop_path,@crop_x.to_i,@crop_y.to_i,@crop_w.to_i,@crop_h.to_i)
54
+ @avatar = Avatar.new :logo => File.open(precrop_path), :name => @name
55
+
56
+ self.logo = @avatar.logo
57
+
58
+ FileUtils.remove_file(precrop_path)
59
+ end
60
+
61
+ def self.images_tmp_path
62
+ images_path = File.join(RAILS_ROOT, "public", "images")
63
+ tmp_path = FileUtils.mkdir_p(File.join(images_path, "tmp"))
64
+ end
65
+
66
+ def self.copy_to_temp_file(path)
67
+ FileUtils.cp(path,Avatar.images_tmp_path)
68
+ end
69
+
70
+ def self.get_image_dimensions(name)
71
+ img_orig = Magick::Image.read(name).first
72
+ dimensions = {}
73
+ dimensions[:width] = img_orig.columns
74
+ dimensions[:height] = img_orig.rows
75
+ dimensions
76
+ end
77
+
78
+ def self.check_image_tmp_valid(path)
79
+
80
+ begin
81
+ path = File.join(Avatar.images_tmp_path,path)
82
+ img_orig = Magick::Image.read(path).first
83
+ return true
84
+ rescue
85
+ return false
86
+ end
87
+ end
88
+
89
+ def process_precrop
90
+
91
+ if @name.blank? && ( logo.content_type.present? && !logo.content_type.start_with?("image/"))
92
+ logo.errors['invalid_type'] = I18n.t('avatar.error.no_image_file')
93
+ return false
94
+ end
95
+
96
+ return if !@name.blank?
97
+
98
+ if resize_image(logo.queued_for_write[:original].path,500,500)
99
+ logo.errors['precrop'] = "You have to make precrop"
100
+ Avatar.copy_to_temp_file(logo.queued_for_write[:original].path)
101
+ else
102
+
103
+ end
104
+ end
105
+
106
+ def resize_image(path,width,height)
107
+ begin
108
+ img_orig = Magick::Image.read(path).first
109
+ img_orig = img_orig.resize_to_fit(width, height)
110
+ img_orig.write(path)
111
+ return true
112
+ rescue
113
+ logo.errors['invalid_type'] = I18n.t('avatar.error.no_image_file')
114
+ return false
115
+ end
116
+ end
117
+
118
+ def make_precrop(path,x,y,width,height)
119
+ begin
120
+ img_orig = Magick::Image.read(path).first
121
+ rescue
122
+ logo.errors['invalid_type'] = I18n.t('avatar.error.no_image_file')
123
+ return false
124
+ end
125
+
126
+ dimensions = Avatar.get_image_dimensions(path)
127
+
128
+ unless (width == 0) || (height == 0)
129
+ crop_args = [x,y,width,height]
130
+ img_orig = img_orig.crop(*crop_args)
131
+ end
132
+
133
+ img_orig = img_orig.resize_to_fill(500,500)
134
+
135
+ img_orig.write(path)
136
+ end
137
+ end
138
+
@@ -0,0 +1,6 @@
1
+ <br class="clearfloat" />
2
+ <div class="space_center"></div>
3
+
4
+ <% unless @avatar.logo.errors["invalid_type"].blank?%>
5
+ <%= @avatar.logo.errors["invalid_type"]%>
6
+ <% end %>
@@ -0,0 +1,128 @@
1
+ <div id ="avatars_for_rails">
2
+ <div class= "block">
3
+ <div class = "content">
4
+ <%= form_for Avatar.new,:html => { :multipart => true,:id =>"uploadForm"} do |f| %>
5
+ <div class="form_row">
6
+ <%= f.file_field :logo %>
7
+
8
+ <%= f.submit t('avatar.upload'), :class => "button" %>
9
+ </div>
10
+ <div id= "prueba">
11
+ </div>
12
+ <% end %>
13
+ </div>
14
+ </div>
15
+
16
+ <div class="files">
17
+ <%= form_for Avatar.new, :html => { :class => "upload", :multipart => true } do |f| %>
18
+ <%= f.file_field :logo %>
19
+ <div>Upload files</div>
20
+ <%= f.hidden_field :drag %>
21
+ <% end %>
22
+
23
+ <table class="upload_files"></table>
24
+ <table class="download_files"></table>
25
+ <div id="show_pic"></div>
26
+ <div id="show_crop"></div>
27
+ </div>
28
+
29
+ <div id= "precrop_drag_response"></div>
30
+ </div>
31
+
32
+
33
+ <% content_for :javascript do%>
34
+ $(document).ready(function() {
35
+ var options = {
36
+ target: '#precrop_drag_response',
37
+ success: showResponse
38
+ };
39
+
40
+ $('#uploadForm').submit(function() {
41
+ $(this).ajaxSubmit(options);
42
+ return false;
43
+ });
44
+ });
45
+
46
+ function update_crop(coords) {
47
+ var rx = 100/coords.w;
48
+ var ry = 100/coords.h;
49
+
50
+ if((coords.w == 0) || (coords.h == 0)){
51
+ rx = 100/$("#cropImage").width();
52
+ ry = 100/$("#cropImage").height();
53
+ coords.x = 0;
54
+ coords.y = 0;
55
+ }
56
+
57
+ $('#preview').css({
58
+ width: Math.round(rx * $("#cropImage").width()) + 'px',
59
+ height: Math.round(ry * $("#cropImage").height()) + 'px',
60
+ marginLeft: '-' + Math.round(rx * coords.x) + 'px',
61
+ marginTop: '-' + Math.round(ry * coords.y) + 'px'
62
+ });
63
+ var ratio = 1;
64
+ $("#crop_x").val(Math.round(coords.x * ratio));
65
+ $("#crop_y").val(Math.round(coords.y * ratio));
66
+ $("#crop_w").val(Math.round(coords.w * ratio));
67
+ $("#crop_h").val(Math.round(coords.h * ratio));
68
+ }
69
+
70
+ function showResponse(responseText, statusText, xhr, $form) {
71
+ jQuery('#cropImage').Jcrop({
72
+ bgColor: 'clear',
73
+ bgOpacity: .6,
74
+ setSelect: [ 0, 0, 200, 200 ],
75
+ aspectRatio: 1 ,
76
+ onChange: update_crop,
77
+ onSelect: update_crop
78
+ });
79
+ }
80
+
81
+
82
+
83
+
84
+
85
+ $(function () {
86
+ $('.upload').fileUploadUI({
87
+ uploadTable: $('.upload_files'),
88
+ downloadTable: $('.download_files'),
89
+ buildUploadRow: function (files, index) {
90
+ var file = files[index];
91
+ return $('<tr><td>' + file.name + '<\/td>' +
92
+ '<td class="file_upload_progress"><div><\/div><\/td>' +
93
+ '<td class="file_upload_cancel">' +
94
+ '<div class="ui-state-default ui-corner-all ui-state-hover" title="Cancel">' +
95
+ '<span class="ui-icon ui-icon-cancel">Cancel<\/span>' +
96
+ '<\/div><\/td><\/tr>');
97
+ },
98
+ buildDownloadRow: function (file) {
99
+
100
+ },
101
+ onComplete: function (event, files, index, xhr, handler) {
102
+ $.ajax({
103
+ type: "GET",
104
+ url: "/avatars/new/?drag_name=" + handler.response.name,
105
+ success: function(data){
106
+ $("#precrop_drag_response").html(data);
107
+ jQuery('#cropImage').Jcrop({
108
+ bgColor: 'clear',
109
+ bgOpacity: .6,
110
+ setSelect: [ 0, 0, 200, 200 ],
111
+ aspectRatio: 1 ,
112
+ onChange: update_crop,
113
+ onSelect: update_crop
114
+ });
115
+ }
116
+ });
117
+ }
118
+ });
119
+ });
120
+
121
+
122
+
123
+
124
+
125
+
126
+
127
+ <% end %>
128
+
@@ -0,0 +1,14 @@
1
+ <% content_for :headers do %>
2
+ <%= javascript_include_tag 'jquery' %>
3
+ <%= javascript_include_tag 'jquery.Jcrop.min' %>
4
+ <%= javascript_include_tag 'jquery-ui.min' %>
5
+ <%= javascript_include_tag 'rails' %>
6
+ <%= javascript_include_tag 'avatars' %>
7
+ <%= javascript_include_tag 'jquery.form' %>
8
+ <%= javascript_include_tag 'jquery.fileupload' %>
9
+ <%= javascript_include_tag 'jquery.fileupload-ui' %>
10
+
11
+ <%= stylesheet_link_tag "jquery.Jcrop", :media => "screen, projection" %>
12
+ <%= stylesheet_link_tag "avatars", :media => "screen, projection" %><br/>
13
+ <%= stylesheet_link_tag "jquery.fileupload-ui", :media => "screen, projection" %><br/>
14
+ <%end%>