Rails 3, Paperclip - Custom Interpolations - ruby-on-rails-3

I've been having some troubles making custom Interpolation, gone through every example I could find on web, but no matter what I did, had no success.
At the moment I have this:
Model
has_attached_file :photo,
:path => ":rails_root/public/images/:img_name-:style.:extension",
:styles => {
:original => '100x100',
:thumb => '30x30'
}
initializers/paperclip.rb
Paperclip.interpolates :img_name do |attachment, style|
attachment.instance.img_name
end
img_name is field populated in form on upload with the image.
The error I get on upload is:
Invalid argument - (C:/Users/.../stream20110410-384-stl2lk20110230-213-1fm2bab, C:/.../photo_upload/public/images/:img_name-original.jpg)

Seems to work if it's directly in the model:
class Model < ActiveRecord::Base
Paperclip.interpolates :img_name do |attachment, style|
attachment.instance.img_name
end
has_attached_file :photo,
:path => ":rails_root/public/images/:img_name-:style.:extension",
:styles => {
:original => '100x100',
:thumb => '30x30'
}
end

Related

Image resize issue with PaperClip and Rails 3

Having terrible trouble with my application image rendering since an upgrade to Ruby 1.9.3, Rails 3.1.0.rc4 and Paperclip 3.4.0.
No matter what variation of settings I give Paperclip the infile linked to below comes out blurred as is shown by the linked outfile.
The outfile must fit into a box of 620x412 as shown here.
Link to input file
Link to output file this code generates
The full code for the model is below ...
class Propertyimage < ActiveRecord::Base
belongs_to :property
validates_presence_of :description
validates_presence_of :sortorder
has_attached_file :image, :styles => { :export => {:geometry => "620x412#", :quality => 100, :format => 'JPG'} },
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename"
end
I had a similar problem and was able to fix after a lot of trial and error using three criteria: image size specification, convert_options, and scaling the image. For example, in your Propertyimage class try:
has_attached_file :image,
:styles => { :original => ["640x480", :jpg], :export => {:geometry => "620x412#", :quality => 100, :format => 'JPG'} },
:convert_options => { :all => "-quality 100" },
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename"
Then you can play with the sizes of your image tag, or as in my case using a PDF, Id used the scale option:
pdf.image the_file_name, :at => [0, 720], :scale => 0.75

Rails 3.0 associated model with download 'save to' image

My Rails 3.0.3 application has a scaffold 'month' which has a link where the user can download an image using 'save to'.
Now I need to make an association where the month model belongs_to the wallpaper model.
Routes:
root :to => 'inicio#index'
resources :wallpapers do
resources :months
end
# the route that works with no association
# match 'download/:id' => 'months#download', :as => :download
# the route I tried
match 'wallpapers/:id/months/:id' => 'months#download', :as => :download
Month model:
class Month < ActiveRecord::Base
belongs_to :wallpaper
has_attached_file :wallpaper_picture, :styles => {
:default => { :geometry => '530x330', :quality => 80, :format => 'jpg'}
}
end
Wallpaper model with friendlyid:
class Wallpaper < ActiveRecord::Base
has_many :months, :dependent => :destroy
extend FriendlyId
friendly_id :title, :use => :slugged
end
In months_controller I made the download method, this method works with no association:
class MonthsController < InheritedResources::Base
belongs_to :wallpaper, :finder => :find_by_slug!
def download
#wallpaper = Wallpaper.find(params[:wallpaper_id])
#month = #wallpaper.month.find(params[:id])
send_file #month.wallpaper_picture.path,
:filename => #month.wallpaper_picture_file_name,
:type => #month.wallpaper_picture_content_type,
:disposition => 'attachment'
end
end
View months/index
- #months.each do |month|
= link_to image_tag(month.wallpaper_picture(:default)), wallpaper_month_path(month.wallpaper, month)
I've tried changing in the months_controller the download method, but it is wrong:
#months = Wallpaper.month.conditions({:person_id => some_id})
Here is how I got it
Routes
resources :wallpapers do
resources :months
end
match 'wallpaper/:wallpaper_id/download/:id' => 'months#download', :as => :download
In the routes I must pass the :wallpaper_id (has_many :months),
the :id is the id of the current controller (belongs_to :wallpaper)
'download' will be the name of the path used in the view 'download_path'
In this path I must pass the foreign key and the id
View months/index
- #months.each do |month|
= link_to 'Download Picture', download_path(month.wallpaper_id, month.id)
In months_controller the download method will receive those parameters and pass the associated image to the send_file method.
def download
#wallpaper = Wallpaper.find(params[:wallpaper_id])
#month = #wallpaper.months.find(params[:id])
send_file #month.wallpaper_picture.path,
:filename => #month.wallpaper_picture_file_name,
:type => #month.wallpaper_picture_content_type,
:disposition => 'attachment'
end
PD: if send_file fails in Production, change it to send_data or
comment out this line in config/production.rb
config.action_dispatch.x_sendfile_header = "X-Sendfile"
send_file just sends an empty file

Dynamic generate bucket name for S3

Im trying to create an dynamic bucket name depending on my polymorphic association type.
My first approach was trying something like this:
class PostImage < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
has_attached_file :image, :styles => { :small => "200x200>", :thumb => "50x50>" },
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => "/:style/:id/:filename",
:bucket => self.imageable_type.to_s
end
If i try to create a new object i got the next error:
NoMethodError: undefined method `imageable_type' for #< Class:0x007fd3fe0b15d8
I find out on the S3 documentation this:
bucket: This is the name of the S3 bucket that will store your files. Remember that the bucket must be unique across all of Amazon S3. If the bucket does not exist Paperclip will attempt to create it. The bucket name will not be interpolated. You can define the bucket as a Proc if you want to determine it's name at runtime. Paperclip will call that Proc with attachment as the only argument.
The problem is that i don't get how i can get this working to set the name of my polymorphic association as the name of the bucket.
Any help will be appreciated.
Hope it help somebody,
The final solution was based on this post: rails paperclip S3 with dynamic bucket name
Read the post for get a better explanation of how use the Proc.
The final code:
class PostImage < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
has_attached_file :image, :styles => {
:square=> "170x170#",
:rectangle=> "220x170#",
:large => "600x400#" }, :storage => :s3, :s3_credentials => "#{Rails.root}/config/s3.yml",
:path => "/:style/:id/:filename",
:bucket => lambda { |attachment| "#{attachment.instance.imageable_type.to_s.downcase}-gallery" }
end

inconsistant photo url using paperclip when query in two apps

I am using paperclip to upload photos in my two rails app which share a single database. Now the problem is, if I upload a photo in app-a, for instance, paperclip gives me a url as:
http://s3.amazonaws.com/testing-item-photos-akiajbfjoiwfjzd6aypa/Users/yujunwu/app-a/public/item_19/created_at_2012-09-29%2021:52:02%20UTC/large.jpg?1348955522
Here is what I set up in my item model:
class Item < ActiveRecord::Base
attr_accessible :photo, :photo_url, :poll_id, :brand, :number_of_votes
has_attached_file :photo,
:styles => { :thumbnail => "100x100#",
:small => "150x150>",
:medium => "250x250>",
:large => "400x400>" },
:storage => :s3,
:s3_credentials => S3_CREDENTIALS,
:url=>"/item_:id/created_at_:created_at/:style.jpg"
Paperclip.interpolates :created_at do |attachment, style|
attachment.instance.created_at
end
end
In the other app app-b, when I query the url with item.photo.url(:large), it gave me:
http://s3.amazonaws.com/testing-item-photos-akiajbfjoiwfjzd6aypa/Users/yujunwu/app-b/public/item_19/created_at_2012-09-29%2021:52:02%20UTC/large.jpg?1348955522
Therefore I got a wrong url.
Are there any ways I can do that by configuring paperclip? Thanks!
As described here : https://github.com/thoughtbot/paperclip#defaults , you can change the default config, in your case I guess you want to change the :local_root so it doesn't include the app name.

Paperclip custom path with id belongs_to

I'm using paperclip inside a project.
The concerned model look like this :
class Asset < ActiveRecord::Base
has_paper_trail # Track model with paper_trail
has_attached_file :image, :styles => { :medium => "300x300>"},
:path => ":rails_root/public/attachments/project_#{:project_id}/ressources/:basename_:style.:extension"
belongs_to :project
end
As you see I want to have the id of concerned project in my image path, how can I make this ?
Try this
has_attached_file :image, :styles => { :medium => "300x300>"},
path => ":rails_root/public/attachments/:project_id/ressources/:basename_:style.:extension"
and then write
Paperclip.interpolates :project_id do |attachment, style|
"project_#{attachment.instance.project_id}"
end