lib/uploadbox/image_uploader.rb in uploadbox-0.0.3 vs lib/uploadbox/image_uploader.rb in uploadbox-0.0.4
- old
+ new
@@ -1,10 +1,11 @@
module Uploadbox
module ImageUploader
def uploads_one(upload_name, options={})
default_options = {
default: false,
+ removable: true,
retina: Uploadbox.retina,
quality: Uploadbox.retina ? (Uploadbox.retina_quality || 40) : (Uploadbox.image_quality || 80)
}
options = options.reverse_merge(default_options)
upload_versions = options.select{ |key| default_options.keys.exclude? key }
@@ -13,30 +14,48 @@
imageable_type = self.to_s
upload_class_name = imageable_type + upload_name.to_s.camelize
upload_class = Class.new(Image)
Uploadbox.const_set(upload_class_name, upload_class)
- # picture?
+ # @post.picture?
define_method("#{upload_name}?") { send(upload_name) and send(upload_name).file? }
- # attach_picture
+ # @post.attach_picture
define_method("attach_#{upload_name}") do |upload_id|
if upload_id.present?
self.send("attach_#{upload_name}!", upload_id)
end
end
- # attach_picture!
+ # @post.attach_picture!
define_method("attach_#{upload_name}!") do |upload_id|
self.send("#{upload_name}=", upload_class.find(upload_id))
end
+ # Post.update_picture_versions!
+ self.define_singleton_method "update_#{upload_name}_versions!" do
+ Uploadbox.const_get(upload_class_name).find_each{|upload| upload.file.recreate_versions!}
+ end
+
# Uploadbox::PostPicture < Image
- upload_class.class_eval <<-RUBY, __FILE__, __LINE__ + 1
- def self.versions
- #{upload_versions}
- end
- RUBY
+ upload_class.define_singleton_method :versions do
+ upload_versions
+ end
+
+ upload_class.define_singleton_method :removable? do
+ options[:removable]
+ end
+
+ # Uploadbox::PostPicture < Image
+ # upload_class.class_eval <<-RUBY, __FILE__, __LINE__ + 1
+ # def self.versions
+ # #{upload_versions}
+ # end
+
+ # def self.removable?
+ # #{options[:removable]}
+ # end
+ # RUBY
upload_class.instance_eval do
delegate *upload_versions.keys, to: :file
default_scope -> { where(imageable_type: imageable_type).where(upload_name: upload_name.to_s) }