How can I do something inside the model depending on the whether it's creating or updating a record? I'm sure it's really simple, but I can't seem to figure it out.
These are different styles for updating or creating attachments with paperclip.
class Photo < ActiveRecord::Base
belongs_to :product
#after_upate :flag_somthin
Paperclip.interpolates :product_id do |attachment, style|
attachment.instance.product_id
end
has_attached_file :data,
:storage => 's3',
:s3_credentials => "#{RAILS_ROOT}/config/s3_credentials.yml",
:bucket => 'leatherarts.com',
:s3_host_alias => 'leatherarts.com.s3.amazonaws.com',
:url => ':s3_alias_url',
:path => "images/products/:product_id/:style/:basename.:extension",
:styles => lambda { |style| style.instance.choose_styles },
:default_style => :medium,
:default_url => 'http://leatherarts.com.s3.amazonaws.com/images/records/m1.png',
:s3_headers => { 'Expires' => 2.months.from_now.httpdate }
validates_attachment_presence :data
validates_attachment_size :data, :less_than => 10.megabytes
validates_attachment_content_type :data, :content_type => ['image/jpeg','image/gif','image/png']
def choose_styles
{ :thumb => "60x60#", :small => "200x200>", :medium => "400x400>", :large => "1000x1000>", :backup => "2000x2000>" }, :on => :create
{ :thumb => "60x60#", :small => "200x200>", :medium => "400x400>", :large => "1000x1000>" }, :on => :update
end
end
Use the new_record? method to return different hashes:
def choose_styles
defaults = { :thumb => "60x60#", :small => "200x200>", :medium => "400x400>", :large => "1000x1000>" }
defaults.merge! :backup => "2000x2000>" if new_record?
defaults
end
Related
I used paperclip background process with delay jobs with ffmpeg.
In paperclip processors folder i have ffmpeg.rb file also.
my model
class product < ActiveRecord::Base
has_attached_file :video, :styles => {
:mp4video => { :geometry => "640x480", :format => 'mp4', :convert_options => {:output => {:ar => 44100}} },
:webmvideo =>{ :geometry => "1024x576", :format => 'webm', :convert_options => {:output => {:ar => 44100}} },
:oggvideo => { :geometry => "1024x576", :format => 'ogg', :convert_options => {:output => {:ar => 44100}} },
:thumb => { :geometry => "100x100#", :format => 'jpg', :time => 10 }
},
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/aws.yml",
:processors => [:transcoder]
process_in_background :video
end
I want to update my styles in paperclip, but when i run the command, a error is returned. Here is the code of my styles:
image.rb
class Image < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
validates_presence_of :image
has_attached_file :image, :processors => [:ffmpeg],
:styles => { :video => { :geometry => "640x480", :format => 'mp4' },
:thumb => { :geometry => "100x100>", :format => 'jpg' },
:large => { :geometry => "400x300>", :format => 'jpg' },
:medium => { :geometry => "400x200>", :format => 'png' },
:icon => { :geometry => "36x36>", :format => 'jpg' },
:profile => { :geometry => "28x28>", :format => 'jpg' } },
:default_url => "/images/:style/missing.png"
end
Returned Error:
Regenerating Image -> image -> [:icon, :large, :medium, :profile, :thumb, :video]
rake aborted!
Cocaine::CommandNotFoundError
How i solve this error?
It seems to be that you have not installed the "paperclip-ffmpeg" gem.
See this link: https://github.com/owahab/paperclip-ffmpeg#requirements
When I try to upload a video using Paperclip, I get an error message can't dump File.
Model Video :
class Video < ActiveRecord::Base
has_attached_file :avatar,
:storage => :s3,
:styles => {
:mp4 => { :geometry => "640x480", :format => 'mp4' },
:thumb => { :geometry => "300x300>", :format => 'jpg', :time => 5 }
}, :processors => [:ffmpeg]
validates_attachment_presence :avatar
validates_attachment_content_type :avatar,
:content_type => /video/,
:message => "Video not supported"
end
I was doing some image optimization on my site. Things are working awesome on development machine but in production mode I am getting an error ie:
ActionView::Template::Error (can't dup Symbol):
This is happening only in case of aws, If I remove this then things working perfectly fine:
A code I was using and was working perfectly:
has_attached_file :attachment, {
:styles => {
:medium=>"654x346>",
:small => "260x400>",
:thumb => "75x75#",
:facebook_meta_tag => "200x200#"
}, :include_updated_timestamp => false
}.merge(PAPERCLIP_STORAGE_OPTIONS)
But now I modified this and Its working perfectly fine in development environment but not working on production environment.
has_attached_file :attachment,
:styles => {
:medium => {
:geometry => "654x346>",
:quality => 60,
:format => 'JPG'
},
:small => {
:geometry => "260x400>",
:quality => 60,
:format => 'JPG'
},
:thumb => {
:geometry =>"75x75#",
:quality => 60,
:format => 'JPG'
},
:facebook_meta_tag => {
:geometry =>"200x200#",
:quality => 50,
:format => 'JPG'
}
}.merge(PAPERCLIP_STORAGE_OPTIONS)
Other relevant settings:
PAPERCLIP_STORAGE_OPTIONS = {
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/amazons3.yml",
}
amazons3.yml
production:
access_key_id: XXXXxxxXXXXxxx
secret_access_key: XXXXxxxXXXXxxxXXXXxxxXXXXxxxXXXXxxxXXXXxxx
bucket: images.XXXXxxx.com
Here is the working code:
has_attached_file :attachment, {
:styles => {
:medium => ["654x346>", :jpg],
:small => ["260x400>", :jpg],
:thumb => ["75x75#", :jpg],
:facebook_meta_tag =>["200x200#", :jpg]
},
:convert_options => {
:medium => "-quality 60",
:small => "-quality 60",
:thumb => "-quality 60",
:facebook_meta_tag => "-quality 60"
}
}.merge(PAPERCLIP_STORAGE_OPTIONS)
ActionView::Template::Error tends to be an error raised inside of a view (Rails re-wraps errors inside views inside ActionView::Template::Error exceptions).
To help you more I'd need to see the backtrace and the view involved.
This is not really an answer, but if this helps you find the cause you may as well accept it.
I have the following paperclip setup. What happens is that I'm using a proc to set the sizes for various styles. However, the proc gets called on new and during the super call. I walked through the debugger and it seems like it processes the :photo parameter first so it initializes the attachment and calls the styles proc at which point the actual object (Photo) has not been initialized by the passed in params(particularly the photo.gallery_id so it doesn't set the styles correctly. I even tried reprocessing and it didn't help. I've spent a couple of days on this and still no luck. any help is appreciated!
class Photo < ActiveRecord::Base
has_and_belongs_to_many :staffs
has_attached_file :photo,
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => "/assets/:id/:class/:style/:image_name.:extension",
:url => "/assets/:id/:class/:style/:image_name.:extension",
:styles => Proc.new { |clip| clip.instance.attachment_styles}
def attachment_styles
if self.gallery.nil?
{ :original => {
:processors => [:watermark],
:geometry =>"600x800!",
:watermark_path => ':rails_root/public/images/watermark.png',
:position => 'SouthEast'},
:thumbnail => {
:processors => [:watermark],
:geometry => "200x300!",
:watermark_path => ':rails_root/public/images/watermark.png',
:position => 'SouthEast'}
}
elsif self.photo.styles.empty?
gallery_type = GalleryType.find_by_id(self.gallery_id)
{ :original => {
:processors => [:watermark],
:geometry =>"#{gallery_type.width_max}x#{gallery_type.height_max}!",
:watermark_path => ':rails_root/public/images/watermark.png',
:position => 'SouthEast'},
:thumbnail => {
:processors => [:watermark],
:geometry => "#{gallery_type.width_min}x#{gallery_type.height_min}!",
:watermark_path => ':rails_root/public/images/watermark.png',
:position => 'SouthEast'}
}
else
self.photo.styles
end
end
def reprocess_att
self.photo.reprocess!
end
def initialize(galleryid, params = {})
begin
param.merge!({"gallery_id" => galleryid.to_s})
super(params)
rescue => e
puts e.message()
end
end
From what I can see, the order of the parameters is important. I had:
attachments.build(:upload => File.new(File.dirname(__FILE__) + '/../fixtures/test-image.jpg'),
:styles => {:small => ['100x100#', :jpg], :medium => ['250x250', :jpg]})
And this wasn't setting up the styles correctly. They they were nil. I changed it to:
attachments.build(:styles => {:small => ['100x100#', :jpg], :medium => ['250x250', :jpg]},
:upload => File.new(File.dirname(__FILE__) + '/../fixtures/test-image.jpg'))
And then the code:
:styles => lambda { |a| a.instance.styles || {} }
worked perfectly. Hope this helps.
Thanks for the answer!
I've been fighting with this for a few weeks now. I'm using Paperclip with FFMPEG to make thumbnails of uploaded videos. I have an option to set what frame to use as the thumbnail.
I'm also using a nested form (awesome nested forms) for my asset upload. So what I did was put the frame time param before the file browse button. This solved the problem for me since I'm not using builder.