Module: Mongomatic::Plugins::Grid::ClassMethods
- Defined in:
- lib/matic_grid.rb
Instance Method Summary collapse
-
#attachment(name, prefix = 'grid') ⇒ Object
Declare an attachment for the object.
-
#attachment_types ⇒ Object
All the attachments types for this class.
-
#grid ⇒ Object
Accessor to Grid.
Instance Method Details
#attachment(name, prefix = 'grid') ⇒ Object
Declare an attachment for the object
eg: attachment :image
31 32 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 101 102 103 |
# File 'lib/matic_grid.rb', line 31 def (name,prefix='grid') ## # Add this name to the attachment_types .push(name).uniq! ## # Return the Grid object. # eg: image.filename, image.read define_method(name) do grid.get(self["#{name}_id"]) if self["#{name}_id"] end ## # Create a method to set the attachment # eg: object.image = File.open('/tmp/somefile.jpg') define_method("#{name}=") do |file| case when file.is_a?(Hash) && file[:tempfile] send(:create_attachment, name, file) when file.respond_to?(:read) send(:create_attachment, name, file) else send(:delete_attachment, name, self["#{name}_id"]) end end ## # Create a method to set the attachment for binary string. # eg: object.set_image(binary_string, "generated_filename.png") define_method("set_#{name}") do |binary, filename| if !binary.nil? send(:create_attachment_raw, name, binary, filename) else send(:delete_attachment, name, self["#{name}_id"]) end end ## # Unset the attachment, queue for removal define_method("unset_#{name}") do send(:delete_attachment, name, self["#{name}_id"]) end ## # Return the relative URL to the attachment for use with Rack::Grid # eg: /grid/4ba69fde8c8f369a6e000003/somefile.png define_method("#{name}_url") do _id = self["#{name}_id"] _name = self["#{name}_name"] ["/#{prefix}", _id, _name].join('/') if _id && _name end ## # Return the relative URL to the thumbnail for use with Rack::GridThumb # eg: /grid/4ba69fde8c8f369a6e000003/somefile.png define_method("#{name}_thumb") do |thumb| _id = self["#{name}_id"] _name = self["#{name}_name"] base = File.basename(_name) ext = File.extname(_name) _name = "#{_name}_#{thumb}#{ext}" ["/#{prefix}", _id, _name].join('/') if _id && _name end ## # Helper methods for details about the file # eg: image_name, image_size, image_type, image_id %W{ #{name}_name #{name}_size #{name}_type #{name}_id }.each do |attr| define_method(attr) do self[attr] end end end |
#attachment_types ⇒ Object
All the attachments types for this class
113 114 115 |
# File 'lib/matic_grid.rb', line 113 def @attachment_types ||= [] end |
#grid ⇒ Object
Accessor to Grid
107 108 109 |
# File 'lib/matic_grid.rb', line 107 def grid @grid ||= Mongo::Grid.new(Mongomatic.db) end |