Coder Social home page Coder Social logo

iq-scm / mongoid-paperclip Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mrrooijen/mongoid-paperclip

0.0 0.0 0.0 46 KB

Mongoid::Paperclip enables you to use Paperclip with the Mongoid ODM for MongoDB.

License: MIT License

Ruby 100.00%

mongoid-paperclip's Introduction

Mongoid::Paperclip

Integrate kt-paperclip into Mongoid. (Kt-paperclip is a maintained fork of the original Paperclip that is now deprecated)

This is actually easier and faster to set up than when using Paperclip and the ActiveRecord ORM.

Setting it up

Gemfile

gem "mongoid-paperclip"

Next let's assume we have a User model and we want to allow our users to upload an avatar.

Rails.root/app/models/user.rb - include the Mongoid::Paperclip module and invoke the provided class method

class User
  include Mongoid::Document
  include Mongoid::Paperclip

  has_mongoid_attached_file :avatar
end

That's it

That's all you have to do. Users can now upload avatars. Unlike ActiveRecord, Mongoid doesn't use migrations, so we don't need to define the Paperclip columns in a separate file. Invoking has_mongoid_attached_file will automatically define the necessary :avatar fields for you.

A more complex example

Just like Paperclip, Mongoid::Paperclip takes a second argument (hash of options) for the has_mongoid_attached_file method, so you can do more complex things such as in the following example.

class User
  include Mongoid::Document
  embeds_many :pictures
end

class Picture
  include Mongoid::Document
  include Mongoid::Paperclip

  embedded_in :user, :inverse_of => :pictures

  has_mongoid_attached_file :attachment,
    :path           => ':attachment/:id/:style.:extension',
    :storage        => :s3,
    :url            => ':s3_alias_url',
    :s3_host_alias  => 'something.cloudfront.net',
    :s3_credentials => File.join(Rails.root, 'config', 's3.yml'),
    :styles => {
      :original => ['1920x1680>', :jpg],
      :small    => ['100x100#',   :jpg],
      :medium   => ['250x250',    :jpg],
      :large    => ['500x500>',   :jpg]
    },
    :convert_options => { :all => '-background white -flatten +matte' }
end

@user.pictures.each do |picture|
  <%= picture.attachment.url %>
end

Note on embedded documents: if you plan to save or update the parent document, you MUST add cascade_callbacks: true to your embeds_XXX statement. Otherwise, your data will be updated but the paperclip functions will not run to copy/update your file.

In the above example:

class User
  embeds_many :pictures, :cascade_callbacks => true
  accepts_nested_attributes_for :pictures, ...
  attr_accepted :pictures_attributes, ...
end

@user.update_attributes({ ... :pictures => [...] })

Optional fingerprinting

Paperclip will skip calculating the fingerprint of a file when the {file}_fingerprint field is missing from the model. This can be desirable if attaching large files to a model. To disable adding the fingerprint field pass the disable_fingerprint option as in this example:

class User
  include Mongoid::Document
  include Mongoid::Paperclip

  has_mongoid_attached_file :usage_report, disable_fingerprint: true
end

Testing

If you want to help develop this plugin, clone the repo and bundle to get all dependencies.

Then to run the tests:

rspec

There you go

Quite a lot of people have been looking for a solution to use Paperclip with Mongoid so I hope this helps!

If you need more information on either Mongoid or kt-paperclip I suggest checking our their official documentation and website.

License

Mongoid::Paperclip is released under the MIT license. See LICENSE for more information.

mongoid-paperclip's People

Contributors

2pelaezenrique avatar alexandrebini avatar florentmorin avatar flyerhzm avatar imajes avatar jspaper avatar kpopper avatar mbell697 avatar megatux avatar mrrooijen avatar mschulkind avatar mshibuya avatar sdhull avatar tilsammans avatar tzar avatar warlley avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.