image_holder 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +3 -0
  4. data/Rakefile +32 -0
  5. data/lib/image_holder.rb +5 -0
  6. data/lib/image_holder/image_handling.rb +36 -0
  7. data/lib/image_holder/image_scaling.rb +16 -0
  8. data/lib/image_holder/version.rb +3 -0
  9. data/lib/tasks/image_holder_tasks.rake +4 -0
  10. data/test/dummy/README.rdoc +28 -0
  11. data/test/dummy/Rakefile +6 -0
  12. data/test/dummy/app/assets/images/users/1/logo1.jpg +0 -0
  13. data/test/dummy/app/assets/javascripts/application.js +13 -0
  14. data/test/dummy/app/assets/javascripts/users.js +2 -0
  15. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  16. data/test/dummy/app/assets/stylesheets/scaffold.css +56 -0
  17. data/test/dummy/app/assets/stylesheets/users.css +4 -0
  18. data/test/dummy/app/controllers/application_controller.rb +5 -0
  19. data/test/dummy/app/controllers/users_controller.rb +58 -0
  20. data/test/dummy/app/helpers/application_helper.rb +2 -0
  21. data/test/dummy/app/helpers/users_helper.rb +2 -0
  22. data/test/dummy/app/models/user.rb +3 -0
  23. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  24. data/test/dummy/app/views/users/_form.html.erb +25 -0
  25. data/test/dummy/app/views/users/edit.html.erb +6 -0
  26. data/test/dummy/app/views/users/index.html.erb +29 -0
  27. data/test/dummy/app/views/users/new.html.erb +5 -0
  28. data/test/dummy/app/views/users/show.html.erb +14 -0
  29. data/test/dummy/bin/bundle +3 -0
  30. data/test/dummy/bin/rails +4 -0
  31. data/test/dummy/bin/rake +4 -0
  32. data/test/dummy/config.ru +4 -0
  33. data/test/dummy/config/application.rb +23 -0
  34. data/test/dummy/config/boot.rb +5 -0
  35. data/test/dummy/config/database.yml +25 -0
  36. data/test/dummy/config/environment.rb +5 -0
  37. data/test/dummy/config/environments/development.rb +29 -0
  38. data/test/dummy/config/environments/production.rb +80 -0
  39. data/test/dummy/config/environments/test.rb +36 -0
  40. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  41. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  42. data/test/dummy/config/initializers/inflections.rb +16 -0
  43. data/test/dummy/config/initializers/mime_types.rb +5 -0
  44. data/test/dummy/config/initializers/secret_token.rb +12 -0
  45. data/test/dummy/config/initializers/session_store.rb +3 -0
  46. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  47. data/test/dummy/config/locales/en.yml +23 -0
  48. data/test/dummy/config/routes.rb +58 -0
  49. data/test/dummy/db/development.sqlite3 +0 -0
  50. data/test/dummy/db/migrate/20130904081554_create_users.rb +10 -0
  51. data/test/dummy/db/schema.rb +23 -0
  52. data/test/dummy/log/development.log +190 -0
  53. data/test/dummy/public/404.html +58 -0
  54. data/test/dummy/public/422.html +58 -0
  55. data/test/dummy/public/500.html +57 -0
  56. data/test/dummy/public/favicon.ico +0 -0
  57. data/test/dummy/test/controllers/users_controller_test.rb +49 -0
  58. data/test/dummy/test/fixtures/users.yml +9 -0
  59. data/test/dummy/test/helpers/users_helper_test.rb +4 -0
  60. data/test/dummy/test/models/user_test.rb +7 -0
  61. data/test/dummy/tmp/cache/assets/development/sprockets/0800f54ee19cf3cee10b956fa9786799 +0 -0
  62. data/test/dummy/tmp/cache/assets/development/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
  63. data/test/dummy/tmp/cache/assets/development/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
  64. data/test/dummy/tmp/cache/assets/development/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
  65. data/test/dummy/tmp/cache/assets/development/sprockets/371bf96e99717688ed7313a0c53f4212 +0 -0
  66. data/test/dummy/tmp/cache/assets/development/sprockets/4050a4e5062ab95c9f32e9b6940821ea +0 -0
  67. data/test/dummy/tmp/cache/assets/development/sprockets/44ed932298a15e744f836458162deebf +0 -0
  68. data/test/dummy/tmp/cache/assets/development/sprockets/510da110ae528e2d22533be39ff696c5 +0 -0
  69. data/test/dummy/tmp/cache/assets/development/sprockets/5f1a0d05e77ca8b9a1fc2a47e17a8174 +0 -0
  70. data/test/dummy/tmp/cache/assets/development/sprockets/6b271a015985ac298f3ee8d49c8dcc9f +0 -0
  71. data/test/dummy/tmp/cache/assets/development/sprockets/6fc757c2c8329244ca95d6909865bbc2 +0 -0
  72. data/test/dummy/tmp/cache/assets/development/sprockets/87b209c0c9da28094a8d5581a21262c6 +0 -0
  73. data/test/dummy/tmp/cache/assets/development/sprockets/cebc6db0bbb8120f430da3970b173d2f +0 -0
  74. data/test/dummy/tmp/cache/assets/development/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
  75. data/test/dummy/tmp/cache/assets/development/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
  76. data/test/dummy/tmp/cache/assets/development/sprockets/f56253b5f374fff1a33fbbc9881c9124 +0 -0
  77. data/test/dummy/tmp/cache/assets/development/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
  78. data/test/image_holder_test.rb +7 -0
  79. data/test/test_helper.rb +15 -0
  80. metadata +233 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c784b450a5ac386b80bfbb0f81062d5f273ce18d
4
+ data.tar.gz: 7b5c1ae402c8a04fe9e3b47c3961d48c32dfafd4
5
+ SHA512:
6
+ metadata.gz: cceb09855f713fddd0ca3ba649aa48702c5e94ba74617a7adc6e0765c32b32e003a4e004ffeb3e3f133623849b52292947ab509e38cd73869ae6bf3cd882ff8b
7
+ data.tar.gz: 8fbf7098914bee00fcc2685a854e5979b430bfffc03d3b8d9b3d3de60a32fb737b9fdb3ee8ecdd8224b9cc73cc8d2d824458c1e3cbd416e34a94a0672c929751
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 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,3 @@
1
+ = ImageHolder
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'ImageHolder'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
22
+ require 'rake/testtask'
23
+
24
+ Rake::TestTask.new(:test) do |t|
25
+ t.libs << 'lib'
26
+ t.libs << 'test'
27
+ t.pattern = 'test/**/*_test.rb'
28
+ t.verbose = false
29
+ end
30
+
31
+
32
+ task default: :test
@@ -0,0 +1,5 @@
1
+ require 'image_holder/image_handling'
2
+ require 'image_holder/image_scaling'
3
+
4
+ module ImageHolder
5
+ end
@@ -0,0 +1,36 @@
1
+ module ImageHolder
2
+ module ClassMethods
3
+ def has_attached_image(*args)
4
+ column = args[0].to_sym
5
+ class_name = self.name.downcase.pluralize
6
+ self.send(:define_method, "save_#{column}_image".to_sym) {
7
+ #if the value is not a file then return
8
+ if self.send(column).nil? or self.send(column).class != ActionDispatch::Http::UploadedFile
9
+ self.send("#{column.to_s}=", args[-1][:default])
10
+ return
11
+ end
12
+ #uploaded tempfile
13
+ image = self.send(column).tempfile
14
+ #id of instance being crated
15
+ index = self.class.last.nil? ? 1 : self.class.last.id + 1
16
+ #extension of file
17
+ ext = self.send(column).original_filename.split('.')[-1]
18
+ #filename to be saved as
19
+ filename = column.to_s + index.to_s + '.' + ext
20
+ #making directory
21
+ `mkdir -p #{Rails.root}/app/assets/images/#{class_name}/#{index}`
22
+ #moving image from temp storage to assets
23
+ `mv #{image.path} #{Rails.root}/app/assets/images/#{class_name}/#{index}/#{filename}`
24
+ #changing data in column to the path of image
25
+ self.send("#{column.to_s}=", "#{class_name}/#{index}/#{filename}")
26
+ #scaling the image
27
+ #scaling should be in new thread, if possible
28
+ #maybe a *to do* for new thread
29
+ ImageScaling.scale_image("#{Rails.root}/app/assets/images/#{class_name}/#{index}/#{filename}", args[-1][:scale]) if args[-1].is_a?(Hash) and args[-1][:scale]
30
+ }
31
+ #this method is called before save of any instance
32
+ self.send(:before_save, "save_#{column}_image".to_sym)
33
+ end
34
+ end
35
+ ActiveRecord::Base.send(:extend, ClassMethods)
36
+ end
@@ -0,0 +1,16 @@
1
+ module ImageHolder
2
+ module ImageScaling
3
+
4
+ require 'RMagick'
5
+
6
+ def self.scale_image(path, scale)
7
+ img = Magick::Image.read(path).first
8
+ target = Magick::Image.new(scale, scale) do
9
+ self.background_color = 'white'
10
+ end
11
+ img.resize_to_fit!(scale, scale)
12
+ target.composite(img, Magick::CenterGravity, Magick::CopyCompositeOp).write(path)
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module ImageHolder
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :image_holder do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Dummy::Application.load_tasks
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,2 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,56 @@
1
+ body { background-color: #fff; color: #333; }
2
+
3
+ body, p, ol, ul, td {
4
+ font-family: verdana, arial, helvetica, sans-serif;
5
+ font-size: 13px;
6
+ line-height: 18px;
7
+ }
8
+
9
+ pre {
10
+ background-color: #eee;
11
+ padding: 10px;
12
+ font-size: 11px;
13
+ }
14
+
15
+ a { color: #000; }
16
+ a:visited { color: #666; }
17
+ a:hover { color: #fff; background-color:#000; }
18
+
19
+ div.field, div.actions {
20
+ margin-bottom: 10px;
21
+ }
22
+
23
+ #notice {
24
+ color: green;
25
+ }
26
+
27
+ .field_with_errors {
28
+ padding: 2px;
29
+ background-color: red;
30
+ display: table;
31
+ }
32
+
33
+ #error_explanation {
34
+ width: 450px;
35
+ border: 2px solid red;
36
+ padding: 7px;
37
+ padding-bottom: 0;
38
+ margin-bottom: 20px;
39
+ background-color: #f0f0f0;
40
+ }
41
+
42
+ #error_explanation h2 {
43
+ text-align: left;
44
+ font-weight: bold;
45
+ padding: 5px 5px 5px 15px;
46
+ font-size: 12px;
47
+ margin: -7px;
48
+ margin-bottom: 0px;
49
+ background-color: #c00;
50
+ color: #fff;
51
+ }
52
+
53
+ #error_explanation ul li {
54
+ font-size: 12px;
55
+ list-style: square;
56
+ }
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,58 @@
1
+ class UsersController < ApplicationController
2
+ before_action :set_user, only: [:show, :edit, :update, :destroy]
3
+
4
+ # GET /users
5
+ def index
6
+ @users = User.all
7
+ end
8
+
9
+ # GET /users/1
10
+ def show
11
+ end
12
+
13
+ # GET /users/new
14
+ def new
15
+ @user = User.new
16
+ end
17
+
18
+ # GET /users/1/edit
19
+ def edit
20
+ end
21
+
22
+ # POST /users
23
+ def create
24
+ @user = User.new(user_params)
25
+
26
+ if @user.save
27
+ redirect_to @user, notice: 'User was successfully created.'
28
+ else
29
+ render action: 'new'
30
+ end
31
+ end
32
+
33
+ # PATCH/PUT /users/1
34
+ def update
35
+ if @user.update(user_params)
36
+ redirect_to @user, notice: 'User was successfully updated.'
37
+ else
38
+ render action: 'edit'
39
+ end
40
+ end
41
+
42
+ # DELETE /users/1
43
+ def destroy
44
+ @user.destroy
45
+ redirect_to users_url, notice: 'User was successfully destroyed.'
46
+ end
47
+
48
+ private
49
+ # Use callbacks to share common setup or constraints between actions.
50
+ def set_user
51
+ @user = User.find(params[:id])
52
+ end
53
+
54
+ # Only allow a trusted parameter "white list" through.
55
+ def user_params
56
+ params.require(:user).permit(:name, :logo)
57
+ end
58
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module UsersHelper
2
+ end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ has_attached_image :logo, default: 'default_user_log.png', scale: 80
3
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
6
+ <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,25 @@
1
+ <%= form_for(@user) do |f| %>
2
+ <% if @user.errors.any? %>
3
+ <div id="error_explanation">
4
+ <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
5
+
6
+ <ul>
7
+ <% @user.errors.full_messages.each do |msg| %>
8
+ <li><%= msg %></li>
9
+ <% end %>
10
+ </ul>
11
+ </div>
12
+ <% end %>
13
+
14
+ <div class="field">
15
+ <%= f.label :name %><br>
16
+ <%= f.text_field :name %>
17
+ </div>
18
+ <div class="field">
19
+ <%= f.label :logo %><br>
20
+ <%= f.file_field :logo %>
21
+ </div>
22
+ <div class="actions">
23
+ <%= f.submit %>
24
+ </div>
25
+ <% end %>
@@ -0,0 +1,6 @@
1
+ <h1>Editing user</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Show', @user %> |
6
+ <%= link_to 'Back', users_path %>
@@ -0,0 +1,29 @@
1
+ <h1>Listing users</h1>
2
+
3
+ <table>
4
+ <thead>
5
+ <tr>
6
+ <th>Name</th>
7
+ <th>Logo</th>
8
+ <th></th>
9
+ <th></th>
10
+ <th></th>
11
+ </tr>
12
+ </thead>
13
+
14
+ <tbody>
15
+ <% @users.each do |user| %>
16
+ <tr>
17
+ <td><%= user.name %></td>
18
+ <td><%= user.logo %></td>
19
+ <td><%= link_to 'Show', user %></td>
20
+ <td><%= link_to 'Edit', edit_user_path(user) %></td>
21
+ <td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
22
+ </tr>
23
+ <% end %>
24
+ </tbody>
25
+ </table>
26
+
27
+ <br>
28
+
29
+ <%= link_to 'New User', new_user_path %>