watarase 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 19cc687771bed168df86daaa39b529f495639269
4
- data.tar.gz: 6b749b3459d7e36bebf6cc45d0697b491496f35c
3
+ metadata.gz: bb3e733879e4f9adcb7a2b98f5d1107bbcff7ab0
4
+ data.tar.gz: 72549fe8bd2bcd9c2330a2cde3e0d154132885b1
5
5
  SHA512:
6
- metadata.gz: c25b2bf0066a483130765bc1dd051a63a8a496fe8f8ce81248640298225a9c2e21c458137a51084fcb9a9e03f9a49b66c44d6c31b961ce119d235f14cad2aa54
7
- data.tar.gz: 2d52636c2edbd7393afb71f2dbe6e2b679429ff524b6621bd66141e1b836d32e5766e3de3b362612729567cdc42d59b927a73774bf4c6debebcb2aff6d3f3d4d
6
+ metadata.gz: 2ce6cb1186198b64b23ff4fb8c43e89a0115e372f9570fce72887d8bbbe6e4f949140a80defa43542bfacc91a4d6f1e8e2712fccdb34962b4f2f74a5be654b30
7
+ data.tar.gz: ee19faa4daa8f5a294fe89c98ddc5a59b099ba31c99af6d5ba265ef01139fb0bd60a36820e013bacda125763ae8b2dd9047a5d9b4b50a9c48bb36e58c69759d4
@@ -76,27 +76,25 @@ Add 'load_image' action in 'config/routes.rb'
76
76
 
77
77
  Define 'image_loadable <image_handler>' definition in controllers.
78
78
 
79
- class UsersController < ApplicationController
80
- image_loadable :user
81
- :
79
+ If you need image caching then, use options caches: and expire_actions:.
82
80
 
83
- Set image holder to image handler by method 'set_image_holder'.
81
+ class UsersController < ApplicationController
82
+ image_loadable :user, caches: true, expire_actions: [:update], save_actions: [:create, :update]
84
83
 
85
- def create
86
- @user = User.new(user_params)
87
- set_image_holder @user
88
- :
84
+ In this case, action caching enabled, and clearing the cache in update action.
85
+ The option save_actions is associate image_handler(ex. user) with image_holder(ex. user_image_holder).
89
86
 
90
- def update
91
- set_image_holder @user
92
- :
87
+ === Tips
93
88
 
94
- If you need image caching then, use options caches: and expire_actions:.
95
-
96
- image_loadable :user, caches: true, expire_actions: [:update]
89
+ def index
90
+ @users = User.all
91
+ end
97
92
 
98
- In this case, action caching enabled, and clearing the cache in update action.
93
+ It is a good idea to make the following changes:
99
94
 
95
+ def index
96
+ @users = User.all.merge(includes: [:user_image_holder])
97
+ end
100
98
 
101
99
  == View
102
100
 
@@ -1,6 +1,4 @@
1
1
  require 'rubygems'
2
- #require 'RMagick'
3
- #require 'rmagick'
4
2
  require 'rails/generators'
5
3
 
6
4
  module Watarase
@@ -23,6 +21,8 @@ module Watarase
23
21
  end
24
22
  end
25
23
  end
26
- require 'watarase/model'
24
+ require 'watarase/image_handler'
27
25
  require 'watarase/controller'
28
- require 'watarase/generators/uploader/uploader_generator'
26
+ require 'watarase/generators/uploader/uploader_generator'
27
+ require 'watarase/extension_whitelist'
28
+ require 'watarase/image_holder'
@@ -3,57 +3,60 @@ require 'action_controller/action_caching'
3
3
  module Watarase
4
4
  module Controller
5
5
  def self.included(controller)
6
- controller.extend Watarase::ImageLoader
6
+ controller.extend Watarase::Controller::ImageLoader
7
7
  end
8
- end
9
8
 
10
- module ImageLoader
11
- def image_loadable(_image_handler, options = {})
12
- ih = _image_handler.to_s.camelize.constantize
13
- self.class_variable_set(:@@image_handler, ih)
14
- self.send(:include, Watarase::InstanceMethods)
15
- self.send(:caches_action, :load_image) if options[:caches]
16
- self.send(:before_action, :expire_caches, only: options[:expire_actions]) if options[:expire_actions]
17
- self.send(:helper_method, :image_thumb_path)
18
- self.send(:helper_method, :image_data_path)
9
+ module ImageLoader
10
+ def image_loadable(_image_handler, options = {})
11
+ ih = _image_handler.to_s.camelize.constantize
12
+ self.class_variable_set(:@@image_handler, ih)
13
+ self.send(:include, Watarase::Controller::InstanceMethods)
14
+ self.send(:caches_action, :load_image) if options[:caches]
15
+ self.send(:before_action, :expire_caches, only: options[:expire_actions]) if options[:expire_actions]
16
+ self.send(:before_action, :image_params, only: options[:save_actions]) if options[:save_actions]
17
+ self.send(:helper_method, :image_thumb_path)
18
+ self.send(:helper_method, :image_data_path)
19
+ end
19
20
  end
20
- end
21
21
 
22
- module InstanceMethods
23
- def load_image
24
- image = image_holder.find(params[:id])
25
- column_name = :"#{(params[:image_column] || 'image_data')}"
26
- send_data image.send(column_name), type: image.content_type, disposition: 'inline'
27
- end
22
+ module InstanceMethods
23
+ def load_image
24
+ image = image_holder.find(params[:id])
25
+ column_name = :"#{(params[:image_column] || 'image_data')}"
26
+ send_data image.send(column_name), type: image.content_type, disposition: 'inline'
27
+ end
28
28
 
29
- def expire_caches
30
- expire_action action: :load_image
31
- end
29
+ def expire_caches
30
+ expire_action action: :load_image
31
+ end
32
32
 
33
- def image_params
34
- params.require(image_handler.name.underscore.to_sym).permit(:image_file, :remove_image)
35
- end
33
+ def image_params
34
+ ip = params.require(image_handler.name.underscore.to_sym).permit(:image_file, :remove_image)
35
+ puts "**** image_params **** #{ip} ****"
36
+ Thread.current[:image_params] = ip
37
+ end
36
38
 
37
- def image_handler
38
- self.class.class_variable_get(:@@image_handler)
39
- end
39
+ def image_handler
40
+ self.class.class_variable_get(:@@image_handler)
41
+ end
40
42
 
41
- def image_holder
42
- "#{image_handler.name.underscore}_#{Watarase.suffix}".camelize.constantize
43
- end
43
+ def image_holder
44
+ "#{image_handler.name.underscore}_#{Watarase.suffix}".camelize.constantize
45
+ end
44
46
 
45
- def image_thumb_path(model)
46
- url_for(controller: image_handler.name.downcase!.pluralize, action: 'load_image', id: model.send(:"#{image_holder.name.underscore}").id, image_column: :image_thumb)
47
- end
47
+ def image_thumb_path(model)
48
+ url_for(controller: image_handler.name.downcase!.pluralize, action: 'load_image', id: model.send(:"#{image_holder.name.underscore}").id, image_column: :image_thumb)
49
+ end
48
50
 
49
- def image_data_path(model)
50
- url_for(controller: image_handler.name.downcase!.pluralize, action: 'load_image', id: model.send(:"#{image_holder.name.underscore}").id, image_column: :image_data)
51
- end
51
+ def image_data_path(model)
52
+ url_for(controller: image_handler.name.downcase!.pluralize, action: 'load_image', id: model.send(:"#{image_holder.name.underscore}").id, image_column: :image_data)
53
+ end
52
54
 
53
- # Call before create, update
54
- def set_image_holder(_image_handler)
55
- _image_handler.send(:"#{image_holder.name.underscore}=", image_holder.new) unless _image_handler.send(:"#{image_holder.name.underscore}")
56
- _image_handler.send(:"#{image_holder.name.underscore}").uploaded_image = image_params
55
+ # Call before create, update
56
+ #def set_image_holder(_image_handler)
57
+ # _image_handler.send(:"#{image_holder.name.underscore}=", image_holder.new) unless _image_handler.send(:"#{image_holder.name.underscore}")
58
+ # _image_handler.send(:"#{image_holder.name.underscore}").uploaded_image = image_params
59
+ #end
57
60
  end
58
61
  end
59
62
  end
@@ -0,0 +1,21 @@
1
+ module Watarase
2
+ module ImageHolder
3
+ module ExtensionWhitelist
4
+
5
+ def self.included(model)
6
+ model.send(:before_save, :check_extension)
7
+ end
8
+
9
+ def check_extension
10
+ puts "**** check_extension ****"
11
+ if filename && extension_white_list && !extension_white_list.include?(File.extname(filename).sub(/\./, ''))
12
+ raise StandardError, "Unsupported file " + filename
13
+ end
14
+ end
15
+
16
+ def extension_white_list
17
+ %w(jpg jpeg gif png)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -9,26 +9,10 @@ module Watarase
9
9
 
10
10
  str_code = <<-"CODE"
11
11
 
12
- include Magick unless self.include? Magick
13
- belongs_to :#{file_name}, primary_key: :#{fk}, foreign_key: :#{file_name}_#{fk}
12
+ acts_as_image_holder
14
13
 
15
- def uploaded_image= (image_params)
16
- if image_params[:remove_image] && image_params[:remove_image] == "1"
17
- self.destroy
18
- elsif image_params[:image_file] && !image_params[:image_file].blank?
19
- self.filename = image_params[:image_file].original_filename
20
- self.content_type = image_params[:image_file].content_type
21
- data = image_params[:image_file].tempfile.read
22
- self.image_data = Image.from_blob(data).first.resize_to_fit(100, 100).to_blob
23
- self.image_thumb = Image.from_blob(data).first.thumbnail(35, 35).to_blob
24
- elsif self.new_record?
25
- self.destroy
26
- end
27
- end
14
+ belongs_to :#{file_name}, primary_key: :#{fk}, foreign_key: :#{file_name}_#{fk}
28
15
 
29
- def extension_white_list
30
- %w(jpg jpeg gif png)
31
- end
32
16
  CODE
33
17
 
34
18
  generate "model", "#{model_name} #{file_name}_#{fk}:#{fk_type} filename:string content_type:string image_data:binary image_thumb:binary"
@@ -0,0 +1,37 @@
1
+ require 'active_record/base'
2
+
3
+ module Watarase
4
+ module ImageHandler
5
+ def self.included(model)
6
+ model.extend Watarase::ImageHandler::Macro
7
+ end
8
+ end
9
+ end
10
+
11
+ module Watarase
12
+ module ImageHandler
13
+ module Macro
14
+ def acts_as_image_handler
15
+ image_holder = (self.name.underscore << '_' << Watarase.suffix).to_sym
16
+ self.send(:include, Watarase::ImageHandler::Associate)
17
+ self.send(:has_one, image_holder, primary_key: self.primary_key, foreign_key: "#{self.name.underscore}_#{self.primary_key}", autosave: true, dependent: :destroy)
18
+ self.send(:attr_accessor, :remove_image)
19
+ self.send(:before_save, :update_image_holder)
20
+ end
21
+ end
22
+
23
+ module Associate
24
+ def update_image_holder
25
+ puts "**** update image holder **** #{Thread.current[:image_params]} ****"
26
+ image_holder = (self.class.name.underscore << '_' << Watarase.suffix)
27
+ return if (!self.send(image_holder.to_sym) and (!Thread.current[:image_params] or !Thread.current[:image_params][:image_file]))
28
+
29
+ self.send((image_holder + '=').to_sym, image_holder.camelcase.constantize.new) unless self.send(image_holder.to_sym)
30
+ self.send(image_holder.to_sym).send(:uploaded_image=, (Thread.current[:image_params] || {}))
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ ActiveRecord::Base.send :include, Watarase::ImageHandler unless ActiveRecord::Base.include? Watarase::ImageHandler
37
+
@@ -0,0 +1,47 @@
1
+ #require 'RMagick'
2
+ require 'rmagick'
3
+
4
+ module Watarase
5
+ module ImageHolder
6
+ def self.included(model)
7
+ model.extend Watarase::ImageHolder::Macro
8
+ end
9
+ end
10
+ end
11
+
12
+ module Watarase
13
+ module ImageHolder
14
+ module Macro
15
+ def acts_as_image_holder
16
+ self.send(:include, Watarase::ImageHolder::ExtensionWhitelist)
17
+ self.send(:include, Watarase::ImageHolder::Store)
18
+ self.send(:before_save, :prepare_image)
19
+ end
20
+ end
21
+
22
+ module Store
23
+ def uploaded_image= (image_params)
24
+ puts "**** uploaded_image ****"
25
+ if image_params[:remove_image] && image_params[:remove_image] == "1"
26
+ self.destroy
27
+ elsif image_params[:image_file] && !image_params[:image_file].blank?
28
+ self.filename = image_params[:image_file].original_filename
29
+ @image_filename = self.filename
30
+ self.content_type = image_params[:image_file].content_type
31
+ @data = image_params[:image_file].tempfile.read
32
+ elsif self.new_record?
33
+ self.destroy
34
+ end
35
+ end
36
+
37
+ def prepare_image
38
+ puts "**** prepare_image ****"
39
+ return unless @data
40
+ self.image_data = Magick::Image.from_blob(@data).first.resize_to_fit(100, 100).to_blob
41
+ self.image_thumb = Magick::Image.from_blob(@data).first.thumbnail(35, 35).to_blob
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ ActiveRecord::Base.send :include, Watarase::ImageHolder unless ActiveRecord::Base.include? Watarase::ImageHolder
@@ -1,3 +1,3 @@
1
1
  module Watarase
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -1,12 +1,12 @@
1
1
  class UsersController < ApplicationController
2
- image_loadable :user, caches: true, expire_actions: [:update]
2
+ image_loadable :user, caches: true, expire_actions: [:update], save_actions: [:create, :update]
3
3
 
4
4
  before_action :set_user, only: [:show, :edit, :update, :destroy]
5
5
 
6
6
  # GET /users
7
7
  # GET /users.json
8
8
  def index
9
- @users = User.all
9
+ @users = User.all.merge(includes: [:user_image_holder])
10
10
  end
11
11
 
12
12
  # GET /users/1
@@ -27,7 +27,7 @@ class UsersController < ApplicationController
27
27
  # POST /users.json
28
28
  def create
29
29
  @user = User.new(user_params)
30
- set_image_holder @user
30
+ #set_image_holder @user
31
31
 
32
32
  respond_to do |format|
33
33
  if @user.save
@@ -44,7 +44,7 @@ class UsersController < ApplicationController
44
44
  # PATCH/PUT /users/1.json
45
45
  def update
46
46
 
47
- set_image_holder @user
47
+ #set_image_holder @user
48
48
 
49
49
  respond_to do |format|
50
50
  if @user.update(user_params)
@@ -1,4 +1,5 @@
1
1
  class User < ActiveRecord::Base
2
2
  self.primary_key = :username
3
3
  acts_as_image_handler
4
+ validates_presence_of :username
4
5
  end
@@ -1,18 +1,7 @@
1
1
  class UserImageHolder < ActiveRecord::Base
2
- include Magick unless self.include? Magick
2
+
3
+ image_holdable
4
+
3
5
  belongs_to :user, primary_key: :username, foreign_key: :user_username
4
6
 
5
- def uploaded_image= (image_params)
6
- if image_params[:remove_image] && image_params[:remove_image] == "1"
7
- self.destroy
8
- elsif image_params[:image_file] && !image_params[:image_file].blank?
9
- self.filename = image_params[:image_file].original_filename
10
- self.content_type = image_params[:image_file].content_type
11
- data = image_params[:image_file].tempfile.read
12
- self.image_data = Image.from_blob(data).first.resize_to_fit(100, 100).to_blob
13
- self.image_thumb = Image.from_blob(data).first.thumbnail(35, 35).to_blob
14
- elsif self.new_record?
15
- self.destroy
16
- end
17
- end
18
7
  end
@@ -1,22 +1,15 @@
1
1
  # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
2
- <%
3
- def image_data
4
- File.open(Rails.root.join('test/fixtures/images', 'default.png'), 'rb') do |f|
5
- "!!binary \"#{Base64.strict_encode64(f.read)}\""
6
- end
7
- end
8
- %>
9
2
 
10
3
  one:
11
4
  user_username: MyString1
12
- filename: file_one
13
- content_type: 'image/png'
14
- image_data: <%= image_data %>
15
- image_thumb: <%= image_data %>
5
+ filename: MyString.png
6
+ content_type: MyString
7
+ image_data:
8
+ image_thumb:
16
9
 
17
10
  two:
18
11
  user_username: MyString2
19
- filename: file_two
20
- content_type: 'image/png'
21
- image_data: <%= image_data %>
22
- image_thumb: <%= image_data %>
12
+ filename: MyString.jpg
13
+ content_type: MyString
14
+ image_data:
15
+ image_thumb:
@@ -4,11 +4,4 @@ class UserImageHolderTest < ActiveSupport::TestCase
4
4
  # test "the truth" do
5
5
  # assert true
6
6
  # end
7
- def setup
8
- @user_image_holder = UserImageHolder.first
9
- end
10
-
11
- test "image holder has image handler" do
12
- assert_respond_to(@user_image_holder, :user)
13
- end
14
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: watarase
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - kobayashi-tbn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-04-02 00:00:00.000000000 Z
11
+ date: 2013-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -75,8 +75,10 @@ extra_rdoc_files: []
75
75
  files:
76
76
  - lib/tasks/watarase_tasks.rake
77
77
  - lib/watarase/controller.rb
78
+ - lib/watarase/extension_whitelist.rb
78
79
  - lib/watarase/generators/uploader/uploader_generator.rb
79
- - lib/watarase/model.rb
80
+ - lib/watarase/image_handler.rb
81
+ - lib/watarase/image_holder.rb
80
82
  - lib/watarase/version.rb
81
83
  - lib/watarase.rb
82
84
  - MIT-LICENSE
@@ -124,8 +126,8 @@ files:
124
126
  - test/dummy/config/routes.rb
125
127
  - test/dummy/config.ru
126
128
  - test/dummy/db/migrate/20130316163018_create_users.rb
127
- - test/dummy/db/migrate/20130318134905_create_user_image_holders.rb
128
129
  - test/dummy/db/migrate/20130324161507_create_posts.rb
130
+ - test/dummy/db/migrate/20130412174428_create_user_image_holders.rb
129
131
  - test/dummy/db/schema.rb
130
132
  - test/dummy/db/seeds.rb
131
133
  - test/dummy/Gemfile
@@ -219,8 +221,8 @@ test_files:
219
221
  - test/dummy/config/routes.rb
220
222
  - test/dummy/config.ru
221
223
  - test/dummy/db/migrate/20130316163018_create_users.rb
222
- - test/dummy/db/migrate/20130318134905_create_user_image_holders.rb
223
224
  - test/dummy/db/migrate/20130324161507_create_posts.rb
225
+ - test/dummy/db/migrate/20130412174428_create_user_image_holders.rb
224
226
  - test/dummy/db/schema.rb
225
227
  - test/dummy/db/seeds.rb
226
228
  - test/dummy/Gemfile
@@ -1,20 +0,0 @@
1
- require 'active_record/base'
2
-
3
- module Watarase
4
- module Model
5
- def self.included(model)
6
- model.extend Watarase::ActsAsImageHandler
7
- end
8
- end
9
-
10
- module ActsAsImageHandler
11
- def acts_as_image_handler
12
- image_holder = (self.name.underscore << '_' << Watarase.suffix).to_sym
13
- self.send(:has_one, image_holder, primary_key: self.primary_key, foreign_key: "#{self.name.underscore}_#{self.primary_key}", autosave: true)
14
- self.send(:attr_accessor, :remove_image)
15
- end
16
- end
17
- end
18
-
19
- ActiveRecord::Base.send :include, Watarase::Model unless ActiveRecord::Base.include? Watarase::Model
20
-