photofy 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/photofy.rb +132 -111
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9d91dc5fee2f8769167a2bd1652afe389190d87e
|
4
|
+
data.tar.gz: 27a6f56405e6b5df8ad47d303eb01a3c03cf567d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f8d67b670200d45149d1d6e5d6ff13f1b0d5da5d2f1f20589914c53990ab9b78e976f871bbed7c182065910267ba9e0255565dfc325f67e7932d35d046473fbc
|
7
|
+
data.tar.gz: 61b15cac3e9c70458472f32c6a5001566f6d9d0ac7e76f294806f5354a97fa4840b742e1874fa7643e44f004c398520ec3422a7f804e161e63c928f3c7e935a1
|
data/lib/photofy.rb
CHANGED
@@ -1,154 +1,175 @@
|
|
1
|
-
begin
|
2
|
-
require "RMagick"
|
3
|
-
rescue Exception => e
|
4
|
-
puts "Unable to load 'RMagick' for after_photofy methods"
|
5
|
-
end
|
6
|
-
|
7
1
|
module Photofy
|
8
2
|
def self.included(base)
|
9
3
|
base.extend(ClassMethods)
|
10
4
|
end
|
11
5
|
|
12
6
|
module ClassMethods
|
13
|
-
attr_accessor :
|
14
|
-
attr_accessor :
|
15
|
-
attr_accessor :
|
16
|
-
|
17
|
-
|
18
|
-
#
|
19
|
-
|
20
|
-
|
7
|
+
attr_accessor :photofied_flag
|
8
|
+
attr_accessor :photo_fields
|
9
|
+
attr_accessor :photos_repository
|
10
|
+
attr_accessor :photo_formats
|
11
|
+
|
12
|
+
#Getter to check if model is enabled with file_folder
|
13
|
+
def is_photofied?
|
14
|
+
@photofied_flag.nil? ? false : @photofied_flag
|
15
|
+
end
|
16
|
+
|
17
|
+
#Generates photo field which can be used to store a post processed image(using rmagick) of parent photo field.
|
18
|
+
#Takes three arguments:
|
19
|
+
#1. parent photo field name
|
20
|
+
#2. field name
|
21
|
+
#3. Proc with rmagick img object
|
21
22
|
#return value should be an object of rmagick's image object
|
22
23
|
#
|
23
24
|
#Example usage..
|
24
|
-
#after_photofy :stamp, Proc.new{|img| img.scale(25, 25)}
|
25
|
-
#will give a 'stamp' attribute which on save of
|
26
|
-
#after_photofy :portfolio, Proc.new{|img| img.scale(150, 250)}
|
27
|
-
#will give a 'portfolio' attribute which on save of
|
28
|
-
|
29
|
-
|
30
|
-
#define_method "#{photo_field}" do
|
31
|
-
# File.exist?(send("#{photo_field}_path")) ? File.read(send("#{photo_field}_path")) : nil
|
32
|
-
#end
|
33
|
-
|
34
|
-
#define_method "#{photo_field}?" do
|
35
|
-
# send("#{photo_field}").nil? ? false : true
|
36
|
-
#end
|
37
|
-
|
38
|
-
#define_method "#{photo_field}_cover_path" do
|
39
|
-
# directoy_path = FileUtils.mkdir_p File.join(self.class.photo_repository, photo_field.to_s)
|
40
|
-
# File.join(directoy_path, "#{photo_field}_#{self.send(self.class.primary_key)}.jpg")
|
41
|
-
#end
|
42
|
-
|
43
|
-
define_method "re_photofy_#{photo_field}!" do |proc|
|
44
|
-
send("process_n_save_#{photo_field}", proc)
|
45
|
-
end
|
46
|
-
|
47
|
-
define_method "process_and_save_#{photo_field}!" do
|
48
|
-
send("process_n_save_#{photo_field}", p)
|
49
|
-
end
|
50
|
-
|
51
|
-
define_method "process_n_save_#{photo_field}" do |proc|
|
52
|
-
begin
|
53
|
-
if File.exist?(send("#{photo_field}_path"))
|
54
|
-
img = Magick::Image.read(send("#{photo_field}_path")).first # path of Orignal image that has to be worked upon
|
55
|
-
img = proc.call(img)
|
56
|
-
img.write(send("#{photo_field}_path"))
|
57
|
-
end
|
58
|
-
rescue Exception => e
|
59
|
-
puts "Unable to process_n_save_#{photo_field} due to #{e.message}"
|
60
|
-
e.backtrace.each { |trace| puts trace }
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
define_method "destroy_#{photo_field}" do
|
65
|
-
File.delete(send("#{photo_field}_path")) if File.exist?(send("#{photo_field}_path"))
|
66
|
-
end
|
67
|
-
|
68
|
-
send(:after_save, "process_and_save_#{photo_field}!")
|
69
|
-
send(:after_destroy, "destroy_#{photo_field}")
|
70
|
-
end
|
71
|
-
|
72
|
-
#Getter to check if model is enabled with photofied
|
73
|
-
def is_photofield?
|
74
|
-
@photofield_flag.nil? ? false : @photofield_flag
|
75
|
-
end
|
76
|
-
|
77
|
-
def register_callbacks(photo_filed)
|
78
|
-
send(:after_save, "#{photo_filed}_store!")
|
79
|
-
send(:after_destroy, "#{photo_filed}_destroy!")
|
25
|
+
#after_photofy :profile, :stamp, Proc.new{|img| img.scale(25, 25)}
|
26
|
+
#will give a 'stamp' attribute which on save of 'profile' photo field will scale it to 25 x 25 dimensions
|
27
|
+
#after_photofy :profile, :portfolio, Proc.new{|img| img.scale(150, 250)}
|
28
|
+
#will give a 'portfolio' attribute which on save of 'profile' photo field will scale it to 150 x 250 dimensions
|
29
|
+
def after_photofy(parent_photo_field, photo_field, proc = Proc.new { |img| puts "Rmagick image: #{img.inspect}" })
|
30
|
+
photofy(photo_field, image_processor: proc, parent_photo_field: parent_photo_field)
|
80
31
|
end
|
81
32
|
|
33
|
+
#Example
|
34
|
+
#photofy(:door)
|
35
|
+
#photofy(:small_door, {parent_photo_field: :door})
|
36
|
+
#photofy(:window, {image_processor: Proc.new { |img| img.scale(25, 25) }})
|
37
|
+
#after_photofy :door, :ventilator, Proc.new { |img| img.scale(25, 25) }
|
38
|
+
#
|
82
39
|
#Generates photo filed from photo_field arguments and provides methods like
|
83
40
|
#if photo_filed is "collage" then it provides methods on top of it as
|
84
41
|
#collage >> Getter,
|
85
|
-
#collage? >> Returns true if
|
86
|
-
#collage = >> Setter.
|
87
|
-
#collage_path >>
|
42
|
+
#collage? >> Returns true if assignment is having value other than nil else false,
|
43
|
+
#collage = >> Setter. Acceptable inputs are file upload(ActionDispatch::Http::UploadedFile), filer handle and String(format validation is ignored),
|
44
|
+
#collage_path >> File path of assignment,
|
45
|
+
#collage_path_to_write >> File path of assignment to write (specific to writing. Used internally),
|
88
46
|
#collage_persisted? >> true if provided file/data is stored on disk,
|
89
47
|
#collage_store! >> to store provided file/data on disk,
|
90
48
|
#collage_destroy! >> to store destroy stored file/data from disk
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
49
|
+
#
|
50
|
+
#Options:
|
51
|
+
#image_processor: a proc for image processing like Proc.new { |img| img.scale(25, 25) }
|
52
|
+
#parent_photo_field: a parent photo field name to be used as source
|
53
|
+
def photofy(photo_field, options = {})
|
54
|
+
collect_photo_formats(photo_field, options)
|
55
|
+
|
56
|
+
@photo_fields ||=[]
|
57
|
+
@photo_fields << photo_field
|
58
|
+
|
59
|
+
define_method 'initialize_photo_buffers' do
|
60
|
+
@photo_file_buffer ||= {}
|
61
|
+
@photo_file_ofn ||= {}
|
98
62
|
end
|
99
|
-
register_callbacks(photo_filed)
|
100
63
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
64
|
+
#Defining getter
|
65
|
+
define_method "#{photo_field}" do
|
66
|
+
send('initialize_photo_buffers')
|
67
|
+
|
68
|
+
if @photo_file_buffer[photo_field].nil?
|
69
|
+
send("#{photo_field}_persisted?") ? File.read(send("#{photo_field}_path")) : nil
|
70
|
+
else
|
71
|
+
@photo_file_buffer[photo_field]
|
72
|
+
end
|
105
73
|
end
|
106
74
|
|
107
|
-
define_method "#{
|
108
|
-
send("#{
|
75
|
+
define_method "#{photo_field}?" do
|
76
|
+
send("#{photo_field}").nil? ? false : true
|
109
77
|
end
|
110
78
|
|
111
|
-
|
79
|
+
#Defining setter
|
80
|
+
define_method "#{photo_field}=" do |file_upload|
|
81
|
+
send('initialize_photo_buffers')
|
82
|
+
|
112
83
|
if file_upload.class == ActionDispatch::Http::UploadedFile
|
113
|
-
return false unless self.class.
|
114
|
-
|
84
|
+
return false unless self.class.photo_formats[photo_field].include?(File.extname(file_upload.original_filename).downcase)
|
85
|
+
@photo_file_buffer[photo_field] = File.read(file_upload.path)
|
86
|
+
@photo_file_ofn[photo_field] = File.basename(file_upload.original_filename)
|
87
|
+
|
115
88
|
elsif file_upload.class == File
|
116
|
-
return false unless self.class.
|
117
|
-
|
89
|
+
return false unless self.class.photo_formats[photo_field].include?(File.extname(file_upload.path).downcase)
|
90
|
+
@photo_file_buffer[photo_field] = file_upload.read
|
91
|
+
@photo_file_ofn[photo_field] = File.basename(file_upload.path)
|
92
|
+
|
93
|
+
file_upload.rewind
|
118
94
|
elsif file_upload.class == String
|
119
|
-
#return false unless self.class.photo_formats.include?(File.extname(file_upload).downcase)
|
120
|
-
|
95
|
+
#return false unless self.class.photo_formats[photo_field].include?(File.extname(file_upload).downcase)
|
96
|
+
@photo_file_buffer[photo_field] = file_upload
|
97
|
+
#@photo_file_ofn[photo_field] = File.basename(file_upload.path) #As there is nothing like original_file_name for a string :)
|
121
98
|
end
|
99
|
+
|
100
|
+
file_upload
|
101
|
+
end
|
102
|
+
|
103
|
+
define_method "#{photo_field}_path" do
|
104
|
+
directory_path = FileUtils.mkdir_p File.join(self.class.photos_repository, photo_field.to_s)
|
105
|
+
(guessed_file = Dir[File.join(directory_path, "#{self.send(self.class.primary_key).to_s}_*")].first).nil? ?
|
106
|
+
File.join(directory_path, self.send(self.class.primary_key).to_s) : guessed_file
|
122
107
|
end
|
123
108
|
|
124
|
-
define_method "#{
|
125
|
-
directoy_path = FileUtils.mkdir_p File.join(self.class.
|
126
|
-
File.join(directoy_path, self.send(self.class.primary_key).to_s)
|
109
|
+
define_method "#{photo_field}_path_to_write" do |overload_dir = nil|
|
110
|
+
directoy_path = FileUtils.mkdir_p File.join(self.class.photos_repository, photo_field.to_s, overload_dir.to_s)
|
111
|
+
File.join(directoy_path, "#{self.send(self.class.primary_key).to_s}#{(@photo_file_ofn[photo_field].nil?) ? "_" : "_#{@photo_file_ofn[photo_field]}" }")
|
127
112
|
end
|
128
113
|
|
129
|
-
define_method "#{
|
130
|
-
|
114
|
+
define_method "#{photo_field}_persisted?" do
|
115
|
+
send('initialize_photo_buffers')
|
116
|
+
(@photo_file_buffer[photo_field].nil? and File.file?(send("#{photo_field}_path")))
|
131
117
|
end
|
132
118
|
|
133
|
-
define_method "#{
|
134
|
-
return unless self.class.
|
135
|
-
|
136
|
-
|
137
|
-
|
119
|
+
define_method "#{photo_field}_store" do |proc, parent_photo_field|
|
120
|
+
return unless self.class.is_photofied?
|
121
|
+
|
122
|
+
send('initialize_photo_buffers')
|
123
|
+
|
124
|
+
#Loading content from parent_photo_field if specified
|
125
|
+
send("#{photo_field}=", File.open(send("#{parent_photo_field}_path"))) unless parent_photo_field.nil?
|
126
|
+
|
127
|
+
unless @photo_file_buffer[photo_field].nil?
|
128
|
+
File.delete(send("#{photo_field}_path")) if File.exist?(send("#{photo_field}_path")) #Clearing any existing existence
|
129
|
+
File.open(send("#{photo_field}_path_to_write"), "wb+") { |f| f.puts(@photo_file_buffer[photo_field]) }
|
130
|
+
|
131
|
+
unless proc.nil?
|
132
|
+
FileUtils.copy(send("#{photo_field}_path_to_write"), send("#{photo_field}_path_to_write", "original_source"))
|
133
|
+
img = Magick::Image.read(send("#{photo_field}_path")).first
|
134
|
+
img = proc.call(img)
|
135
|
+
img.write(send("#{photo_field}_path_to_write"))
|
136
|
+
end
|
137
|
+
|
138
|
+
@photo_file_buffer[photo_field] = nil
|
139
|
+
@photo_file_ofn[photo_field] = nil
|
138
140
|
end
|
139
141
|
end
|
140
142
|
|
141
|
-
define_method "#{
|
142
|
-
|
143
|
-
|
144
|
-
|
143
|
+
define_method "#{photo_field}_store!" do
|
144
|
+
send("#{photo_field}_store", options[:image_processor], options[:parent_photo_field])
|
145
|
+
end
|
146
|
+
|
147
|
+
define_method "#{photo_field}_destroy!" do
|
148
|
+
return unless self.class.is_photofied?
|
149
|
+
|
150
|
+
send('initialize_photo_buffers')
|
151
|
+
@photo_file_buffer[photo_field] = nil
|
152
|
+
File.delete(send("#{photo_field}_path")) if File.exist?(send("#{photo_field}_path"))
|
145
153
|
end
|
146
154
|
|
147
|
-
|
155
|
+
send(:after_save, "#{photo_field}_store!")
|
156
|
+
send(:after_destroy, "#{photo_field}_destroy!")
|
157
|
+
|
158
|
+
@photofied_flag = true
|
159
|
+
end
|
160
|
+
|
161
|
+
def photos_repository
|
162
|
+
@photos_repository ||= FileUtils.mkdir_p File.join(Rails.root, "photofy", self.name)
|
148
163
|
end
|
149
164
|
|
150
|
-
|
151
|
-
|
165
|
+
#Collects valid photo formats specific to photo fields
|
166
|
+
def collect_photo_formats(photo_field, options)
|
167
|
+
if options.is_a?(Hash)
|
168
|
+
@photo_formats ||= {}
|
169
|
+
@photo_formats[photo_field] = options[:formats].is_a?(Array) ? options[:formats].collect { |x| x.starts_with?(".") ? x : ".#{x}" } : [".jpeg", ".jpg", ".gif", ".png", ".bmp"]
|
170
|
+
else
|
171
|
+
raise 'InvalidArguments'
|
172
|
+
end
|
152
173
|
end
|
153
174
|
|
154
175
|
end
|
metadata
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: photofy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Praveen Kumar Sinha
|
8
8
|
- Annu Yadav
|
9
|
-
-
|
9
|
+
- Sachin Choudhary
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
@@ -17,8 +17,10 @@ description: |-
|
|
17
17
|
#Generates photo filed from photo_field arguments and provides methods like
|
18
18
|
#if photo_filed is "collage" then it provides methods on top of it as
|
19
19
|
#collage >> Getter,
|
20
|
-
#collage
|
21
|
-
#
|
20
|
+
#collage? >> Returns true if assignment is having value other than nil else false,
|
21
|
+
#collage = >> Setter. Acceptable inputs are file upload(ActionDispatch::Http::UploadedFile), filer handle and String(format validation is ignored),
|
22
|
+
#collage_path >> File path of assignment,
|
23
|
+
#collage_path_to_write >> File path of assignment to write (specific to writing. Used internally),
|
22
24
|
#collage_persisted? >> true if provided file/data is stored on disk,
|
23
25
|
#collage_store! >> to store provided file/data on disk,
|
24
26
|
#collage_destroy! >> to store destroy stored file/data from disk
|