Paaperclip + couchrest

22 views
Skip to first unread message

Cyrus

unread,
Nov 20, 2009, 10:42:10 AM11/20/09
to CouchRest
how do I user ?

example:

class Author < CouchRest::ExtendedDocument
use_database COUCHDB_SERVER.default_database

has_attached_file :avatar,
:styles => {
:original => "90x120"
}

end

viatropos

unread,
Nov 23, 2009, 4:37:40 PM11/23/09
to CouchRest
class Image < CouchRest::ExtendedDocument
use_database SERVER.default_database
include Paperclip

property :size, :type => "Integer", :default => 1
property :title, :type => "String", :default => ""
property :tags, :type => "Array", :default => []
property :content, :type => "String", :default => ""
property :path, :type => "String"
property :content_type, :type => "String"

# for paperclip
property :attachment_file_name, :type => "String"
property :attachment_content_type, :type => "String"
property :attachment_file_size, :type => "Integer"
property :attachment_updated_at, :type => "Time"

view_by :path
view_by :title
view_by :tags

def initialize(passed_keys={})
super(passed_keys)
self[:title] = self[:attachment_file_name]
self[:content_type] = self[:attachment_content_type]
self[:path] = self.attachment.url
end

# Haven't handled these, or any validations
# set_callback :before_post_process, :before, :check_before
#
set_callback :before_attachment_post_process, :before, :check_before
end

http://github.com/viatropos/paperclip

I just made it work with paperclip, haven't tried any of the paperclip
validations so they probably wont work. Tests mostly pass, I still
need to solve a problem with ruby versions (I'm on 1.8.7) and libxml
before everything's going well. But the basics are there.

Goodluck
Message has been deleted

Cyrus

unread,
Nov 23, 2009, 8:10:48 PM11/23/09
to CouchRest
tanks,

I will try to do this.

Cyrus

unread,
Nov 24, 2009, 11:14:39 AM11/24/09
to CouchRest

I tried but an error happened !

----
NoMethodError in Cajus/authorsController#index

undefined method `after_save' for Author:Class

RAILS_ROOT: /Users/cyrus/jus/jus-revista
Application Trace | Framework Trace | Full Trace

/usr/local/lib/ruby/gems/1.8/gems/couchrest-0.33/lib/couchrest/more/
extended_document.rb:117:in `method_missing'
/Users/cyrus/jus/jus-revista/vendor/plugins/paperclip/lib/paperclip.rb:
221:in `has_attached_file'
/Users/cyrus/jus/jus-revista/app/models/author.rb:5
/Users/cyrus/jus/jus-revista/app/controllers/cajus/
authors_controller.rb:6:in `index'
----


see my model please.
------

class Author < CouchRest::ExtendedDocument
use_database COUCHDB_SERVER.default_database
include Paperclip

has_attached_file :photo,
:styles => {
:normal => "90x120"
},
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":attachment/:id/:style.:extension",
:bucket => 'development'


property :name
property :surname
property :slug
property :picture_url
property :email
property :gender, :default => 'M'
property :resume
property :articles, :cast_as => ["Article"]
property :site

timestamps!

def initialize(passed_keys={})
super(passed_keys)
self[:title] = self[:attachment_file_name]
self[:content_type] = self[:attachment_content_type]
self[:path] = self.attachment.url
end

# for paperclip
property :attachment_file_name, :type => "String"
property :attachment_content_type, :type => "String"
property :attachment_file_size, :type => "Integer"
property :attachment_updated_at, :type => "Time"


set_callback :before_photo_author_process, :before, :check_before

# save_callback :before, :create_slug
# save_callback :before, :append_articles
# create_callback :before, :separate_name
# save_callback :after, :create_or_save_in_cache
#
view_by :slug
view_by :name



alias full_name name

def to_s
self.id
end

def begin_names
self.name.gsub(/\s*#{self.surname}$/, "")
end

protected
def create_or_save_in_cache
self.add_in_cache(to_s)
end

def separate_name
n = self.name.nil? ? [""] : self.name.strip.split(" ")
self.surname = n.pop if n.size > 1
end

def append_articles
self.articles = articles.map{|a| a.to_s} if self.articles
end

def create_slug
self.slug = name.parameterize.to_s
end
end

Alex

unread,
Nov 25, 2009, 9:21:26 AM11/25/09
to CouchRest
"Paperclip is intended as an easy file attachment library for
ActiveRecord" is a key.

Cyrus

unread,
Nov 25, 2009, 2:01:19 PM11/25/09
to CouchRest
I got,

I installed the gem http://github.com/viatropos/paperclip as plugin
and modify two lines in vedor/plugins/paperclip/paperclip.rb

behind line 223

#after_save :save_attached_files
#before_destroy :destroy_attached_files

create_callback :after, :save_attached_files
save_callback :after, :save_attached_files
destroy_callback :after, :destroy_attached_files
---
is working very well !!!
see The model
--
class Author < CouchRest::ExtendedDocument
use_database COUCHDB_SERVER.default_database
include Paperclip


has_attached_file :photo, :styles => {:normal => "90x120"},
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":attachment/:id_:style.:extension",
:bucket => 'youbocket

property :photo_file_name, :type => "String"
property :photo_content_type, :type => "String"
property :photo_file_size, :type => "Integer"
property :photo_updated_at, :type => "Time"

def initialize(passed_keys={})
super(passed_keys)
self[:title] = self[:attachment_file_name]
self[:content_type] = self[:attachment_content_type]
self[:path] = self.photo.url
end


end


thanks viatropos !! Its a good extension of papperclip for couchrest !
Reply all
Reply to author
Forward
0 new messages