lib/joint.rb in joint-0.4 vs lib/joint.rb in joint-0.5

- old
+ new

@@ -6,47 +6,52 @@ autoload :Version, 'joint/version' def self.configure(model) model.class_inheritable_accessor :attachment_names model.attachment_names = Set.new + model.send(:include, model.attachment_accessor_module) end module ClassMethods + def attachment_accessor_module + @attachment_accessor_module ||= Module.new + end + def attachment(name, options = {}) options.symbolize_keys! name = name.to_sym self.attachment_names << name after_save :save_attachments after_save :destroy_nil_attachments before_destroy :destroy_all_attachments - key "#{name}_id".to_sym, ObjectId - key "#{name}_name".to_sym, String - key "#{name}_size".to_sym, Integer - key "#{name}_type".to_sym, String + key :"#{name}_id", ObjectId + key :"#{name}_name", String + key :"#{name}_size", Integer + key :"#{name}_type", String validates_presence_of(name) if options[:required] - class_eval <<-EOC + attachment_accessor_module.module_eval <<-EOC def #{name} @#{name} ||= AttachmentProxy.new(self, :#{name}) end def #{name}? - !nil_attachments.include?(:#{name}) && self.send(:#{name}_id?) + !nil_attachments.include?(:#{name}) && send(:#{name}_id?) end def #{name}=(file) if file.nil? nil_attachments << :#{name} assigned_attachments.delete(:#{name}) else - self["#{name}_id"] = BSON::ObjectId.new if self["#{name}_id"].nil? - self["#{name}_size"] = File.size(file) - self["#{name}_type"] = Wand.wave(file.path) - self["#{name}_name"] = Joint.file_name(file) + send("#{name}_id=", BSON::ObjectId.new) if send("#{name}_id").nil? + send("#{name}_size=", File.size(file)) + send("#{name}_type=", Wand.wave(file.path)) + send("#{name}_name=", Joint.file_name(file)) assigned_attachments[:#{name}] = file nil_attachments.delete(:#{name}) end end EOC