Skip to content

Instantly share code, notes, and snippets.

@tiagogodinho
Created March 21, 2012 23:10
Show Gist options
  • Select an option

  • Save tiagogodinho/2154022 to your computer and use it in GitHub Desktop.

Select an option

Save tiagogodinho/2154022 to your computer and use it in GitHub Desktop.
MediaMagick

MediaMagick

TODO: Write a gem description

Installation

Add this line to your application's Gemfile:

gem 'media_magick'

And then execute:

$ bundle

Or install it yourself as:

$ gem install media_magick

Usage

Model

class Album
  include Mongoid::Document
  include MediaMagick
  
  attach_many :photos
end

album = Album.new
album.photos.new(photo: params[:file])
album.save!
album.photos.first.url

album.photos.first.filename
album.photos.first.content_type
album.photos.first.width  # only images
album.photos.first.height # only images

Custom classes

class Album
  include Mongoid::Document
  include MediaMagick
  
  attach_many :photos do
    field :tags, type: Array
  end
end

album = Album.new
album.photos.new(photo: params[:file], tags: ['ruby', 'guru'])
album.save!
album.photos.first.tags

Custom uploader

class Album
  include Mongoid::Document
  include MediaMagick
  
  attach_many :soundtrack, custom_uploader: true do
    # Carrierwave
    mount_uploader :music, AmazonS3Uploader
  end

  attach_many :photos, custom_uploader: true do
    # Dragonfly
    field :image_uid
    image_accessor :image
  end
end

View

<%= attachment_container @album, :photos do %>
      <a class="pickAttachments btn" href="javascript://">select files</a>
      <a class="uploadAttachments btn" href="javascript://">upload files</a>
    
    <ul class="loadedAttachments">
      <%= render :partial => 'photos', :collection => @album.photos, :as => 'photo' %>
    </ul>
  <% end %>

Javascript

$(document).ready(function () {
  $(".attachmentUploader").pluploadIt();
});

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment