Skip to content

Commit

Permalink
Fixes issue mrrooijen#1 regarding the broken functionality of adding …
Browse files Browse the repository at this point in the history
…multiple attachments. "has_attached_file" should now be called as "has_mongoid_attached_file". It seems it would only allow an invocation to occur once per model.
  • Loading branch information
Michael van Rooijen committed Feb 8, 2011
1 parent 3ae36d5 commit 0ecbcc8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,42 @@ Simply define the `mongoid-paperclip` gem inside your `Gemfile`. Additionally, y

gem "mongoid-paperclip", :require => "mongoid_paperclip"
gem "aws-s3", :require => "aws/s3"

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_attached_file :avatar

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 the `has_attached_file` method will automatically define the necessary `:avatar` fields for you in the background.
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 the `has_mongoid_attached_file` method will automatically define the necessary `:avatar` fields for you in the background.


A more complex example
----------------------

Just like Paperclip, Mongoid::Paperclip takes a second argument (hash of options) for the `has_attached_file` method, so you can do more complex things such as in the following 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_attached_file :attachment,

has_mongoid_attached_file :attachment,
:path => ':attachment/:id/:style.:extension',
:storage => :s3,
:url => ':s3_alias_url',
Expand All @@ -65,7 +65,7 @@ Just like Paperclip, Mongoid::Paperclip takes a second argument (hash of options
},
:convert_options => { :all => '-background white -flatten +matte' }
end

@user.pictures.each do |picture|
<%= picture.attachment.url %>
end
Expand Down
23 changes: 16 additions & 7 deletions lib/mongoid_paperclip.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# encoding: utf-8

begin
require "paperclip"
rescue LoadError
Expand Down Expand Up @@ -39,15 +41,15 @@
# include Mongoid::Document
# include Mongoid::Paperclip
#
# has_attached_file :avatar
# has_mongoid_attached_file :avatar
# end
#
# The above example is all you need to do. This will load the Paperclip library into the User model
# and add the "has_attached_file" class method. Provide this method with the same values as you would
# and add the "has_mongoid_attached_file" class method. Provide this method with the same values as you would
# when using "vanilla Paperclip". The first parameter is a symbol [:field] and the second parameter is a hash of options [options = {}].
#
# Unlike Paperclip for ActiveRecord, since MongoDB does not use "schema" or "migrations", Mongoid::Paperclip automatically adds the neccesary "fields"
# to your Model (MongoDB collection) when you invoke the "#has_attached_file" method. When you invoke "has_attached_file :avatar" it will
# to your Model (MongoDB collection) when you invoke the "#has_mongoid_attached_file" method. When you invoke "has_mongoid_attached_file :avatar" it will
# automatially add the following fields:
#
# field :avatar_file_name, :type => String
Expand All @@ -67,11 +69,11 @@ def self.included(base)
module ClassMethods

##
# Adds Mongoid::Paperclip's "#has_attached_file" class method to the model
# Adds Mongoid::Paperclip's "#has_mongoid_attached_file" class method to the model
# which includes Paperclip and Paperclip::Glue in to the model. Additionally
# it'll also add the required fields for Paperclip since MongoDB is schemaless and doesn't
# have migrations.
def has_attached_file(field, options = {})
def has_mongoid_attached_file(field, options = {})

##
# Include Paperclip and Paperclip::Glue for compatibility
Expand All @@ -80,7 +82,7 @@ def has_attached_file(field, options = {})

##
# Invoke Paperclip's #has_attached_file method and passes in the
# arguments specified by the user that invoked Mongoid::Paperclip#has_attached_file
# arguments specified by the user that invoked Mongoid::Paperclip#has_mongoid_attached_file
has_attached_file(field, options)

##
Expand All @@ -90,7 +92,14 @@ def has_attached_file(field, options = {})
field(:"#{field}_file_size", :type => Integer)
field(:"#{field}_updated_at", :type => DateTime)
end

##
# This method is deprecated
def has_attached_file(field, options = {})
raise "Mongoid::Paperclip#has_attached_file is deprecated, " +
"Use 'has_mongoid_attached_file' instead"
end
end

end
end
end
6 changes: 3 additions & 3 deletions mongoid-paperclip.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Gem::Specification.new do |gem|

gem.name = 'mongoid-paperclip'
gem.version = '0.0.2'
gem.version = '0.0.3'
gem.platform = Gem::Platform::RUBY
gem.authors = 'Michael van Rooijen'
gem.email = 'meskyanichi@gmail.com'
Expand All @@ -14,7 +14,7 @@ Gem::Specification.new do |gem|
gem.files = %x[git ls-files].split("\n")
gem.test_files = %x[git ls-files -- {spec}/*].split("\n")
gem.require_path = 'lib'

gem.add_dependency 'paperclip', ['~> 2.3.6']

end
end

0 comments on commit 0ecbcc8

Please sign in to comment.