buddies_avatar 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 (76) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.md +53 -0
  3. data/Rakefile +39 -0
  4. data/app/assets/images/avatar_missing.png +0 -0
  5. data/app/controllers/avatars_controller.rb +28 -0
  6. data/app/uploaders/avatar_uploader.rb +47 -0
  7. data/app/views/avatars/crop.html.erb +51 -0
  8. data/app/views/avatars/show.html.erb +8 -0
  9. data/config/routes.rb +5 -0
  10. data/db/migrate/20110916040104_add_avatar_to_users.rb +5 -0
  11. data/lib/buddies_avatar.rb +8 -0
  12. data/lib/buddies_avatar/engine.rb +4 -0
  13. data/lib/buddies_avatar/orm/active_record.rb +24 -0
  14. data/lib/buddies_avatar/orm/base.rb +37 -0
  15. data/lib/buddies_avatar/version.rb +3 -0
  16. data/lib/tasks/buddies-imager_tasks.rake +4 -0
  17. data/test/buddies_avatar_test.rb +4 -0
  18. data/test/dummy/Rakefile +7 -0
  19. data/test/dummy/app/assets/javascripts/application.js +9 -0
  20. data/test/dummy/app/assets/stylesheets/application.css +7 -0
  21. data/test/dummy/app/controllers/application_controller.rb +7 -0
  22. data/test/dummy/app/helpers/application_helper.rb +2 -0
  23. data/test/dummy/app/models/user.rb +3 -0
  24. data/test/dummy/app/views/layouts/application.html.erb +21 -0
  25. data/test/dummy/config.ru +4 -0
  26. data/test/dummy/config/application.rb +45 -0
  27. data/test/dummy/config/boot.rb +10 -0
  28. data/test/dummy/config/database.yml +25 -0
  29. data/test/dummy/config/environment.rb +5 -0
  30. data/test/dummy/config/environments/development.rb +30 -0
  31. data/test/dummy/config/environments/production.rb +60 -0
  32. data/test/dummy/config/environments/test.rb +42 -0
  33. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  34. data/test/dummy/config/initializers/inflections.rb +10 -0
  35. data/test/dummy/config/initializers/mime_types.rb +5 -0
  36. data/test/dummy/config/initializers/secret_token.rb +7 -0
  37. data/test/dummy/config/initializers/session_store.rb +8 -0
  38. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  39. data/test/dummy/config/locales/en.yml +5 -0
  40. data/test/dummy/config/routes.rb +58 -0
  41. data/test/dummy/db/development.sqlite3 +0 -0
  42. data/test/dummy/db/migrate/20110916023339_create_users.rb +8 -0
  43. data/test/dummy/db/schema.rb +22 -0
  44. data/test/dummy/db/test.sqlite3 +0 -0
  45. data/test/dummy/log/test.log +348 -0
  46. data/test/dummy/public/404.html +26 -0
  47. data/test/dummy/public/422.html +26 -0
  48. data/test/dummy/public/500.html +26 -0
  49. data/test/dummy/public/favicon.ico +0 -0
  50. data/test/dummy/public/uploads/user/avatar/980190963/panda.jpg +0 -0
  51. data/test/dummy/public/uploads/user/avatar/980190963/thumb_panda.jpg +0 -0
  52. data/test/dummy/script/rails +6 -0
  53. data/test/dummy/test/fixtures/users.yml +11 -0
  54. data/test/dummy/test/unit/user_test.rb +7 -0
  55. data/test/dummy/tmp/cache/assets/CE9/370/sprockets%2Fe47df1d575bc7016996ff3f56107c214 +0 -0
  56. data/test/dummy/tmp/cache/assets/D11/320/sprockets%2F47bbed07487b763434fb5fd8a503a715 +0 -0
  57. data/test/dummy/tmp/cache/assets/D32/A10/sprockets%2F13fe41fee1fe35b49d145bcc06610705 +0 -0
  58. data/test/dummy/tmp/cache/assets/D3E/AB0/sprockets%2F562af01abc225db5e83d8549b9a759b2 +0 -0
  59. data/test/dummy/tmp/cache/assets/D44/120/sprockets%2Fcf7c841ae2c76843f593fb04d1246bb9 +0 -0
  60. data/test/dummy/tmp/cache/assets/D45/1B0/sprockets%2Ff79ee6554cade6f1426532e76320ba9b +0 -0
  61. data/test/dummy/tmp/cache/assets/D45/FE0/sprockets%2Fe4d788461df33de2f68b1e4b4a49307a +0 -0
  62. data/test/dummy/tmp/cache/assets/D4C/5B0/sprockets%2F93616d41b28b4de77ad6b50945f7dc8e +0 -0
  63. data/test/dummy/tmp/cache/assets/D54/ED0/sprockets%2F71c9fa01091d432b131da3bb73faf3d4 +0 -0
  64. data/test/dummy/tmp/cache/assets/D84/210/sprockets%2Fabd0103ccec2b428ac62c94e4c40b384 +0 -0
  65. data/test/dummy/tmp/cache/assets/D94/EC0/sprockets%2F7d1c40e48a2633c3fab4f4adea96e630 +0 -0
  66. data/test/dummy/tmp/cache/assets/DA8/CF0/sprockets%2F05451cfdd8b7a96ef18a6da9d5f0448c +0 -0
  67. data/test/dummy/tmp/cache/assets/DD7/370/sprockets%2F7a362d81a180d98caedc3ee6b56f95ce +0 -0
  68. data/test/dummy/tmp/cache/assets/DDD/D70/sprockets%2Ffd1e460aec97cee6c89170cbcc7872f6 +0 -0
  69. data/test/dummy/tmp/cache/assets/E04/890/sprockets%2F2f5173deea6c795b8fdde723bb4b63af +0 -0
  70. data/test/dummy/tmp/capybara/capybara-20110916132329600183993.html +23 -0
  71. data/test/dummy/tmp/capybara/capybara-201109161324214823412123.html +23 -0
  72. data/test/fixtures/invalid.txt +1 -0
  73. data/test/fixtures/panda.jpg +0 -0
  74. data/test/integration/navigation_test.rb +51 -0
  75. data/test/test_helper.rb +15 -0
  76. metadata +254 -0
@@ -0,0 +1,20 @@
1
+ Copyright 2011 Lin He
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.
@@ -0,0 +1,53 @@
1
+ ## UNDER DEVELOPMENT
2
+
3
+ ## What does it do?
4
+
5
+ Part of the 'buddies' gem that aims to bring some common social features to a rails site. Buddies avatar gem provides user avatar and cropping feature. Image cropping technique is credited to railscasts.com
6
+
7
+ ## Setup to use rackspace
8
+
9
+ * Add `fog` gem
10
+ * Overwrite user
11
+
12
+ user.rb
13
+
14
+ class User ...
15
+
16
+ # For crop only, avatar_geometry opens it
17
+ def avatar_rackspace_path
18
+ "http://#{RACKSPACE_CONFIG[:uploads_url]}/" + avatar.current_path
19
+ end
20
+
21
+ def avatar_upload_path
22
+ if Rails.env.test? or Rails.env.cucumber?
23
+ super
24
+ else
25
+ avatar_rackspace_path
26
+ end
27
+ end
28
+
29
+ end
30
+
31
+ * Setup carrierwave initializer
32
+
33
+ Load rackspace.yml
34
+
35
+ RACKSPACE_CONFIG = YAML.load(File.read("#{Rails.root}/config/rackspace.yml"))[Rails.env].symbolize_keys
36
+
37
+ config/initializers/carrierwave.rb
38
+
39
+ CarrierWave.configure do |config|
40
+ config.fog_credentials = {
41
+ :provider => 'Rackspace',
42
+ :rackspace_username => RACKSPACE_CONFIG[:username],
43
+ :rackspace_api_key => RACKSPACE_CONFIG[:api_key]
44
+ }
45
+ config.fog_directory = RACKSPACE_CONFIG[:uploads_dir]
46
+ config.fog_host = "http://#{RACKSPACE_CONFIG[:uploads_url]}"
47
+ end
48
+
49
+ avatar_uploader
50
+
51
+ storage :fog unless Rails.env.test? or Rails.env.cucumber?
52
+
53
+ This project rocks and uses MIT-LICENSE.
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'BuddiesAvatar'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
24
+ load 'rails/tasks/engine.rake'
25
+
26
+
27
+ Bundler::GemHelper.install_tasks
28
+
29
+ require 'rake/testtask'
30
+
31
+ Rake::TestTask.new(:test) do |t|
32
+ t.libs << 'lib'
33
+ t.libs << 'test'
34
+ t.pattern = 'test/**/*_test.rb'
35
+ t.verbose = false
36
+ end
37
+
38
+
39
+ task :default => :test
@@ -0,0 +1,28 @@
1
+ class AvatarsController < ApplicationController
2
+ before_filter :assign_user
3
+
4
+ def show
5
+ end
6
+
7
+ def update
8
+ if @user.update_attributes(params[:user])
9
+ if params[:user][:avatar].blank?
10
+ redirect_to avatar_url
11
+ else
12
+ redirect_to crop_avatar_url
13
+ end
14
+ else
15
+ flash.now.alert = "Invalid image format"
16
+ render :show
17
+ end
18
+ end
19
+
20
+ def crop
21
+ end
22
+
23
+ private
24
+
25
+ def assign_user
26
+ @user = current_user
27
+ end
28
+ end
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+
3
+ class AvatarUploader < CarrierWave::Uploader::Base
4
+ include CarrierWave::MiniMagick
5
+
6
+ storage :file
7
+ # storage :fog unless Rails.env.test? or Rails.env.cucumber?
8
+
9
+ def store_dir
10
+ "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
11
+ end
12
+
13
+ def default_url
14
+ "/images/avatar_missing.png"
15
+ end
16
+
17
+ process :resize_to_limit => [500, 500]
18
+
19
+ version :thumb do
20
+ process :crop_and_resize => [100, 100]
21
+ end
22
+
23
+ def cache_dir
24
+ "#{Rails.root}/tmp/uploads"
25
+ end
26
+
27
+ def extension_white_list
28
+ %w(jpg jpeg gif png)
29
+ end
30
+
31
+ def crop_and_resize(width, height)
32
+ return if !model.cropping?
33
+ manipulate! do |img|
34
+ img.crop model.crop_string
35
+ img.resize "#{width}x#{height}"
36
+ img = yield(img) if block_given?
37
+ img
38
+ end
39
+ [:crop_x, :crop_y, :crop_w, :crop_h].each { |field| model.update_attribute field, nil }
40
+ end
41
+
42
+ # Override the filename of the uploaded files:
43
+ # Avoid using model.id or version_name here, see uploader/store.rb for details.
44
+ # def filename
45
+ # "something.jpg" if original_filename
46
+ # end
47
+ end
@@ -0,0 +1,51 @@
1
+ <%= content_for :head do %>
2
+ <%= stylesheet_link_tag "jquery.Jcrop" %>
3
+ <%= javascript_include_tag "jquery.Jcrop.min" %>
4
+ <script type="text/javascript" charset="utf-8">
5
+ $(function() {
6
+ $('#cropbox').Jcrop({
7
+ onChange: update_crop,
8
+ onSelect: update_crop,
9
+ setSelect: [0, 0, 500, 500],
10
+ aspectRatio: 1
11
+ });
12
+ });
13
+
14
+ function update_crop(coords) {
15
+ var rx = 100/coords.w;
16
+ var ry = 100/coords.h;
17
+ var lw = $('#cropbox').width();
18
+ var lh = $('#cropbox').height();
19
+
20
+ var ratio = <%= @user.avatar_geometry[:width] %> / lw;
21
+
22
+ $('#preview').css({
23
+ width: Math.round(rx * lw) + 'px',
24
+ height: Math.round(ry * lh) + 'px',
25
+ marginLeft: '-' + Math.round(rx * coords.x) + 'px',
26
+ marginTop: '-' + Math.round(ry * coords.y) + 'px'
27
+ });
28
+
29
+ $("#crop_x").val(Math.round(coords.x * ratio));
30
+ $("#crop_y").val(Math.round(coords.y * ratio));
31
+ $("#crop_w").val(Math.round(coords.w * ratio));
32
+ $("#crop_h").val(Math.round(coords.h * ratio));
33
+ }
34
+ </script>
35
+ <% end %>
36
+
37
+ <div id="avatar_box">
38
+ <%= image_tag @user.avatar.url, id: "cropbox" %>
39
+ </div>
40
+
41
+ <div id="avatar_preview" style="width:100px; height:100px; overflow:hidden">
42
+ <%= image_tag @user.avatar.url, id: "preview" %>
43
+ </div>
44
+
45
+ <%= form_for @user, url: avatar_path do |f| %>
46
+ <% for attribute in [:crop_x, :crop_y, :crop_w, :crop_h] %>
47
+ <%= f.hidden_field attribute, :id => attribute %>
48
+ <% end %>
49
+ <p><%= f.submit "Crop" %></p>
50
+ <% end %>
51
+
@@ -0,0 +1,8 @@
1
+ <div id='user_avatar_box'>
2
+ <%= image_tag @user.avatar.thumb.url %>
3
+ </div>
4
+ <%= form_for @user, url: avatar_path, html: { multipart: true } do |f| %>
5
+ <%= f.file_field :avatar, id: 'avatar_field' %>
6
+ <%= f.hidden_field :avatar_cache %>
7
+ <%= f.submit "Upload" %>
8
+ <% end %>
@@ -0,0 +1,5 @@
1
+ Rails.application.routes.draw do
2
+ resource :avatar do
3
+ member { get :crop }
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class AddAvatarToUsers < ActiveRecord::Migration
2
+ def change
3
+ add_column :users, :avatar, :string
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ require "buddies_avatar/engine"
2
+
3
+ module BuddiesAvatar
4
+ module ORM
5
+ autoload :Base, 'buddies_avatar/orm/base'
6
+ autoload :ActiveRecord, 'buddies_avatar/orm/active_record'
7
+ end
8
+ end
@@ -0,0 +1,4 @@
1
+ module BuddiesAvatar
2
+ class Engine < Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,24 @@
1
+ module BuddiesAvatar
2
+ module ORM
3
+ module ActiveRecord
4
+ extend ActiveSupport::Concern
5
+ include Base
6
+
7
+ included do
8
+ attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
9
+ end
10
+
11
+ def cropping?
12
+ !crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
13
+ end
14
+
15
+ def crop_string
16
+ "#{crop_w}x#{crop_h}+#{crop_x}+#{crop_y}"
17
+ end
18
+
19
+ def clean_crop_fields
20
+ [:crop_x, :crop_y, :crop_w, :crop_h].each { |field| update_attribute field, nil }
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,37 @@
1
+ module BuddiesAvatar
2
+ module ORM
3
+ module Base
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ after_update :reprocess_avatar, if: :cropping?
8
+ mount_uploader :avatar, AvatarUploader
9
+ end
10
+
11
+ def cropping?
12
+ raise NotImplementedError
13
+ end
14
+
15
+ def crop_string
16
+ raise NotImplementedError
17
+ end
18
+
19
+ def avatar_geometry
20
+ img = MiniMagick::Image.open avatar_upload_path
21
+ @geometry = { width: img[:width], height: img[:height] }
22
+ end
23
+
24
+ protected
25
+
26
+ def avatar_upload_path
27
+ avatar.current_path
28
+ end
29
+
30
+ private
31
+
32
+ def reprocess_avatar
33
+ avatar.recreate_versions!
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module BuddiesAvatar
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :buddies-imager do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,4 @@
1
+ require 'test_helper'
2
+
3
+ class BuddiesAvatarTest < ActiveSupport::TestCase
4
+ end
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
3
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
4
+
5
+ require File.expand_path('../config/application', __FILE__)
6
+
7
+ Dummy::Application.load_tasks
@@ -0,0 +1,9 @@
1
+ // This is a manifest file that'll be compiled into including all the files listed below.
2
+ // Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
3
+ // be included in the compiled file accessible from http://example.com/assets/application.js
4
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
5
+ // the compiled file.
6
+ //
7
+ //= require jquery
8
+ //= require jquery_ujs
9
+ //= require_tree .
@@ -0,0 +1,7 @@
1
+ /*
2
+ * This is a manifest file that'll automatically include all the stylesheets available in this directory
3
+ * and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
4
+ * the top of the compiled file, but it's generally better to create a new file per style scope.
5
+ *= require_self
6
+ *= require_tree .
7
+ */
@@ -0,0 +1,7 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery
3
+
4
+ def current_user
5
+ User.last
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ include BuddiesAvatar::ORM::ActiveRecord
3
+ end
@@ -0,0 +1,21 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag "application" %>
6
+ <%= javascript_include_tag "application" %>
7
+ <%= yield :head %>
8
+ <%= csrf_meta_tags %>
9
+ </head>
10
+ <body>
11
+
12
+ <div id="flash_messages">
13
+ <% flash.each do |name, msg| %>
14
+ <%= msg %>
15
+ <% end %>
16
+ </div>
17
+
18
+ <%= yield %>
19
+
20
+ </body>
21
+ </html>
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Dummy::Application
@@ -0,0 +1,45 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require 'rails/all'
4
+
5
+ Bundler.require
6
+ require "buddies_avatar"
7
+
8
+ module Dummy
9
+ class Application < Rails::Application
10
+ # Settings in config/environments/* take precedence over those specified here.
11
+ # Application configuration should go into files in config/initializers
12
+ # -- all .rb files in that directory are automatically loaded.
13
+
14
+ # Custom directories with classes and modules you want to be autoloadable.
15
+ # config.autoload_paths += %W(#{config.root}/extras)
16
+
17
+ # Only load the plugins named here, in the order given (default is alphabetical).
18
+ # :all can be used as a placeholder for all plugins not explicitly named.
19
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
20
+
21
+ # Activate observers that should always be running.
22
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
23
+
24
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
25
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
26
+ # config.time_zone = 'Central Time (US & Canada)'
27
+
28
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
29
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
30
+ # config.i18n.default_locale = :de
31
+
32
+ # Configure the default encoding used in templates for Ruby 1.9.
33
+ config.encoding = "utf-8"
34
+
35
+ # Configure sensitive parameters which will be filtered from the log file.
36
+ config.filter_parameters += [:password]
37
+
38
+ # Enable the asset pipeline
39
+ config.assets.enabled = true
40
+
41
+ # Version of your assets, change this if you want to expire all your assets
42
+ config.assets.version = '1.0'
43
+ end
44
+ end
45
+