Module: Attach::Attachable::ClassMethods
- Defined in:
- lib/attach/attachable.rb
Constant Summary collapse
- DEFAULT_SIZES =
[[:full, 400, 300, '#eee'], [:thumb, 96, 72, '#eee']]
Instance Method Summary collapse
-
#attachment(fld, klass, sizes = nil) ⇒ Object
For models with single photo.
- #attachments(fld, klass, sizes = nil) ⇒ Object
Instance Method Details
#attachment(fld, klass, sizes = nil) ⇒ Object
For models with single photo
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/attach/attachable.rb', line 33 def (fld, klass, sizes = nil) self.__send__(:instance_variable_set, "@#{fld}_sizes".to_sym, sizes) # def photo define_method(fld) do return nil unless self.id klass.first(:attachable_type => self.class.name, :attachable_id => self.id, :field_name => fld, :order => [:position.asc]) end # def photos define_method(fld.to_s.pluralize) do if self.id klass.all(:attachable_type => self.class, :attachable_id => self.id, :field_name => fld, :order => [:position.asc]) else [] end end # def klass.photo_sizes singleton_class = class << self; self; end singleton_class.send(:define_method, "#{fld}_sizes") do instance_variable_get("@#{fld}_sizes".to_sym) || DEFAULT_SIZES end # def new_photo= define_method("new_#{fld}=") do |upload| photo = klass.create( :attachable_type => self.class, :attachable_id => self.id, :filename => upload.original_filename.split(/[\\\/]/)[-1], :content_type => upload.content_type, :size => upload.size, :field_name => fld ) if photo.filename[-4..-1].downcase == ".pdf" photo.update(:filename => photo.filename[0..-4] + "png") end @attachments_needing_id ||= Hash.new{ |hash, key| hash[key] = Array.new } @attachments_needing_id[fld.to_s] << photo unless self.id self.class.__send__("#{fld}_sizes").each do |style| dir_path = "public/assets/#{photo.id}/#{style[0]}" File.makedirs(dir_path) img = Magick::Image.read(upload.path) img[0]. resize_matte(style[1], style[2], style[3]). write(photo.path(style[0])) end end # def replace_photo= # Deletes any current photos and adds a new photo define_method("replace_#{fld}=") do |upload| return false if upload.blank? self.__send__(fld.to_s.pluralize).each {|del_photo| del_photo.destroy} self.__send__("new_#{fld}=", upload) end # def destroy_photo_attachments= # # Delete all attachments of attachment class. Called by before :destroy define_method("destroy_#{fld}_attachments") do self.__send__(fld.to_s.pluralize).destroy! end self.before(:destroy, "destroy_#{fld}_attachments".to_sym) end |
#attachments(fld, klass, sizes = nil) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/attach/attachable.rb', line 102 def (fld, klass, sizes = nil) (fld.to_s.singularize, klass, sizes) define_method("new_#{fld}=") do |uploads| uploads.each { |upload| self.__send__("new_#{fld.to_s.singularize}=", upload) } end define_method("delete_#{fld}=") do |del_photos| del_photos.each { |id| self.__send__(fld).get(id).destroy } end end |