Getting correct path/url from Paperclip Gem during after_post_process callback? - ruby-on-rails

I have a fairly standard Paperclip setup, it's almost straight out of the readme. I have a simple method triggered via callback to get the primary colors out of an uploaded image, and save them to the corresponding instance.
class Image < ActiveRecord::Base
has_attached_file :file, :styles => { large: "800x>", :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :file, :content_type => /\Aimage\/.*\Z/
after_post_process :get_colors
def get_colors
colors = Miro::DominantColors.new(self.file.url)
colors = colors.to_hex.join(',')
self.colors = colors
self.save
end
end
As you can see, I have an after_post_process callback, and it does get triggered. The trouble is that when I call self.file.url I get a path that looks like this:
"/system/images/files//original/Peterson-Products-Wireframe-v01.jpg?1398443345".
It's missing the :id_partion portion. It's real path should look more like:
"/system/images/files/000/000/033/original/Peterson-Products-Wireframe-v01.jpg?1398443345"
Should I be using some other callback? I only want this triggered once per upload... Never again if the image is updated. Is this a bug in paperclip that I should be filing on Github?
Rails Version 4.1
Paperclip Version 4.1
Ruby 2.1.0
Thanks so much!

try queued_for_write
colors = Miro::DominantColors.new(file.queued_for_write[:original].path)

Related

Adding Paperclip Attachment to Spree Orders Table,

I am working on an ecommerce website using Solidus, Rails. The site allows you to order photo frames & prints from a variety of options.
To print a photo a user must upload the jpg file of the photo. So, to allow that I modified the orders table and added a paperclip attachment called 'attachment'
I ran the following command
rails generate paperclip SpreeOrder attachment
Which generated the migrations, then I ran rake db:migrate
Then I created a spree/order_decorator.rb file, and added has_attached_file
module Spree::OrderDecorator
has_attached_file :attachment, styles: {
:medium => {
:geometry => "640x480",
:format => 'jpeg'
},
:thumb => { :geometry => "160x120", :format => 'jpeg', :time => 10}
}, :processors => [:transcoder]
validates_attachment_content_type :attachment, content_type: /\Aimage\/.*\z/
Spree::Order.prepend self
end
After this I ran the server, and ended up getting this error
undefined method `has_attached_file' for Spree::OrderDecorator:Module (NoMethodError)
I have configured solidus for use with paperclip only, so I am really confused as to why I am getting this error, even later I manually went and generated a paperclip.rb file in the config/initializers directory, but still I get the same error.
Please help with this!!
Thank You!!
You should add those paperclip method at class level in the prepended module:
def self.prepended(base)
base.has_attached_file
end

Paperclip gem with Ruby IO File Object

I'm trying to attach images to an ActiveRecord object with the Paperclip gem. However, I'm trying to attach images, not from a form, but from a script that looks for images in a folder.
Here is my code
file = File.open("some_image.jpg", "r")
my_object.image = file
my_object.save
file.close
The script runs successfully (no errors). However, when I check the website, the images are not showing up. The images were apparently never attached.
I've looked through the documentation, but wasn't able to find anything that solved the problem.
Also, here is MyObject class
class MyObject < ActiveRecord::Base
# Attachments
has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
end
Any ideas?

Getting URL for public assets in Rails model

I have a Rails application that uses Paperclip and saves images to S3. When the user uploads an asset without an image, it gets the default image set in the Paperclip setup.
My API serves those assets and has the links to the images in the JSON response (using jbuilder), however I can't seem to return the default image URL, it only returns "missing.png" and I wanted it to return the entire URL to the server with the missing image path attached to it.
I'm setting the default url in the model, and I've tried using ActionView::Helpers::AssetUrlHelper to get the image_url but it never works even though it is working inside the rails console. Any idea on what can I do to solve it?
The JBuilder file:
json.profile_picture_smallest asset.profile_picture.url(:smallest)
json.profile_picture_small asset.profile_picture.url(:small)
json.profile_picture_medium asset.profile_picture.url(:medium)
json.profile_picture_large asset.profile_picture.url(:large)
json.profile_picture_original asset.profile_picture.url(:original)
The part of paperclip that is included in the Model
module Picturable
extend ActiveSupport::Concern
included do
has_attached_file :profile_picture, path: '/images/' + name.downcase.pluralize + '/:style/:basename', default_url: "missing.png",
styles: {
smallest: '50x50>',
small: '100x100>',
medium: '200x200>',
large: '400x400>',
png: ['400x400>',:png]
}, :convert_options => {
smallest: '-trim',
small: '-trim',
medium: '-trim',
large: '-trim',
png: '-trim'
}
# Validate the attached image is image/jpg, image/png, etc
validates_attachment_content_type :profile_picture, :content_type => /\Aimage\/.*\Z/
end
def set_uuid_name
begin
self.profile_picture_file_name = SecureRandom.uuid
end while self.class.find_by(:profile_picture_file_name => self.profile_picture_file_name)
end
end
Paperclip config:
Paperclip::Attachment.default_options[:s3_host_name] = 's3hostname'
Development config:
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => 'paperclipdev',
:access_key_id => 'accesskey',
:secret_access_key => 'secretaccesskey'
}
}
I think the way to do this is use the asset helpers in your jbuilder file:
json.profile_picture_smallest asset_url(asset.profile_picture.url(:smallest))
It's worth a mention here that you can also pass a symbol method name to paperclip for the default_url parameter if you want the default url to be dynamic based on the model.

How does lambda function works in rails with an example from paperclip gem

I had a problem with the Paperclip gem where the default_url doesn't load after it fingerprinted in production environment, my code was like this:
class User
# Attachments to Paperclip - Profile pic
has_attached_file :profilepic_attachment,
:styles => {
thumb: '100x100#',
square: '500x500#'
},
:default_url => ActionController::Base.helpers.asset_path("missing/default_user.png"),
:preserve_files => true
validates_attachment_content_type :profilepic_attachment, content_type: /\Aimage\/.*\Z/
end
Note that when I do rails c in production and print out ActionController::Base.helpers.asset_path("missing/default_user.png"). The fingerprinted version (correct version) is printed out.
default_user-fb34158daae99f297ad672c43bb1a4d3917d8e272b5f2254aa055392aa2faa94.png.
However, when I inspect it from browser, the original /assets/missing/default_user.png appeared.
I struggled for long time until I came across this post, which tells me to change
:default_url => ActionController::Base.helpers.asset_path("missing/default_user.png"),
to
:default_url => lambda { |image| ActionController::Base.helpers.asset_path("missing/default_user.png") },
I wasn't sure what happened, but then it works. I then wonder what lambda function is for and when is it used in rails? also, it passed in a variable |image| but seems it wasn't use in the code. Why is that?
Thanks!

Why doesn't work Paperclip interpolations?

I am trying to make work Paperclip interpolations the whole afternoon, but still not success.
Here is how I have set up the Image model:
has_attached_file :image,
:styles => { :thumb => '300x300#',
:medium => "300x300>",
:original => "900x900>" },
:path => ":rails_root/public/images/:user_id/:style/:basename.:extension",
:url => "/images/:user_id/:style/:basename.:extension"
In /config/initializers/paperclip.rb is following:
Paperclip.options[:command_path] = "/usr/local/bin"
module Paperclip
module Interpolations
def user_id attachment, style_name
attachment.instance.user_id.to_s
end
end
end
But every time I save a file, the files is saved as
/images//original/file-name.jpg
The user's ID is missing.
What is wrong in this sample? I still cannot find the right config of Paperclip setup. I would be very grateful for every help.
Thank you
Try to add this in your Image model
Paperclip.interpolates :user_id do |attachment, style|
attachment.instance.user_id.to_s
end

Resources