Module: MongoMapperExt::Storage::ClassMethods
- Defined in:
- lib/mongomapper_ext/storage.rb
Instance Method Summary collapse
- #file_key(name, opts = {}) ⇒ Object
- #file_list(name) ⇒ Object
- #gridfs ⇒ Object
-
#upgrade_file_keys(*keys) ⇒ Object
NOTE: this method will be removed on next release.
Instance Method Details
#file_key(name, opts = {}) ⇒ Object
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 |
# File 'lib/mongomapper_ext/storage.rb', line 62 def file_key(name, opts = {}) opts[:in] ||= :file_list define_method("#{name}=") do |file| if opts[:max_length] && file.respond_to?(:size) && file.size > opts[:max_length] errors.add(name, I18n.t("mongomapper_ext.storage.errors.max_length", :default => "file is too long. max length is #{opts[:max_length]} bytes")) end if cb = opts[:validate] if cb.kind_of?(Symbol) send(opts[:validate], file) elsif cb.kind_of?(Proc) cb.call(file) end end if self.errors.on(name).blank? send(opts[:in]).get(name.to_s).put(name.to_s, file) else # we store the errors here because we want to validate before storing the file mm_storage_errors.merge!(errors.errors) end end define_method(name) do send(opts[:in]).get(name.to_s) end define_method("has_#{name}?") do send(opts[:in]).has_key?(name.to_s) end end |
#file_list(name) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/mongomapper_ext/storage.rb', line 44 def file_list(name) key name, MongoMapperExt::FileList define_method(name) do list = self[name] list.parent_document = self list end after_create do |doc| doc.send(name).sync_files doc.save(:validate => false) end before_destroy do |doc| doc.send(name).destroy_files end end |
#gridfs ⇒ Object
40 41 42 |
# File 'lib/mongomapper_ext/storage.rb', line 40 def gridfs @gridfs ||= Mongo::Grid.new(self.database) end |
#upgrade_file_keys(*keys) ⇒ Object
NOTE: this method will be removed on next release
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/mongomapper_ext/storage.rb', line 96 def upgrade_file_keys(*keys) cname = self.collection_name self.find_each do |object| keys.each do |key| object.upgrade_file_key(key, false) end object.save(:validate => false) end self.database.drop_collection(cname+".files") self.database.drop_collection(cname+".chunks") end |