How to save a paperclip attachement from the params hash? - ruby-on-rails

I'm trying to create an instance of my CompanyLogo model if the hash params[:vacancy]['companies']['company_logo'] is pressent. I'm attempting to do this:
l = CompanyLogo.new( :company_logo => params[:vacancy]['companies']['company_logo'].tempfile.path )
but that gives me
CompanyLogo(#70304040244500) expected, got ActionDispatch::Http::UploadedFile(#70304015073440)
Is there a way to do this?

If your CompanyLogo has_attached :logo,and you are saving file manually then simply company_logo_object.logo = File.new(actual_path) and
company_logo_object.save solve your purpose

Try saving the logo first (tested in Rails 3.2.2 using Paperclip)
(logo = CompanyLogo.new(:image => params[:image])).save
company_object.update_attributes(logo: logo)

Related

Fill Pdf Form in Ruby using pdf-form gem

I want to programmatically fill a pdf using pdf-form gem
I was able to read the fields using the following code:
require 'pdf_forms'
pdftk = PdfForms.new('/usr/local/bin/pdftk')
fields = pdftk.get_fields('*/Desktop/sample_pdf.pdf')
for f in fields
print f.to_s
end
But when I tried to fill the Pdf with the provide synthax, I created only an empty, not openable pdf.
pdftk.fill_form '/path/to/form.pdf', 'target.pdf', ['Value Name'] => 'Value to be inserted'
Does anybody know a working solution?
Thanks in advance!!!
Additional Information:
the fields of the pdf are the following: Name, Address, Dropdown1, Dropdown2, Dropdown3, Check Box4, Check Box1, Check Box3, Check Box2, Text5, Button7, Text6, Group6, %
I tried it with filling just one field:
pdftk.fill_form 'sample_pdf.pdf', 'sample_pdf_filled.pdf', {["Name"]=>"Value to be inserted"}
The result: I have an pdf "sample_pdf_filled.pdf", which I cannot open, because it's empty. I thought my problem is the form-filling, but it seems, that the pdf is not created correctly.
Note: I am working on a mac (unix)
Has anybody a solution?
Method #fill_form is defined like this:
def fill_form(template, destination, data = {}, fill_options = {})
You example would produce something like this:
irb(main):001:0> { ['Value Name'] => 'Value to be inserted' }
=> {["Value Name"]=>"Value to be inserted"}
The data is supposed to be a Hash. If you want to specify a key with a space you can use it like this: :'Value Name'.
irb(main):002:0> { :'Value Name' => 'Value to be inserted' }
=> {:"Value Name"=>"Value to be inserted"}
I found a solution - for all MAC OS X 10.11 El Capitan User:
PDFtk Server on OS X 10.11

How do I access a hash value that only exists in 1 attribute out of many?

So this one is a bit tricky.
I have an attribute that looks like this:
[22] pry(main)> n.media.meta_info[:response][:outputs]
=> [{"id"=>486,
"url"=>"http://some-video.com/by-fire.mp4",
"label"=>"webmp4",
"state"=>"finished",
"format"=>"mpeg4",
"type"=>"standard",
"frame_rate"=>30.06,
{"id"=>488848287,
"url"=>"http://some-video.com/by-fire.webm",
"label"=>"webwebm",
"state"=>"finished",
"format"=>"webm",
"type"=>"standard",
"frame_rate"=>30.06,
{"id"=>488848288,
"url"=>"http://some-video.com/by-fire.ogv",
"label"=>"webogv",
"state"=>"finished",
"format"=>"ogg",
"type"=>"standard",
"frame_rate"=>30.059,
{"id"=>488848289,
"url"=>
"https://zencoder-temp-storage-us-east-1.s3.amazonaws.com/",
"label"=>nil,
"state"=>"finished",
"format"=>"mpeg4",
"type"=>"standard",
"frame_rate"=>30.06,
"thumbnails"=>
[{"label"=>nil,
"images"=>
[{"dimensions"=>"56x100",
"file_size_bytes"=>15142,
"format"=>"PNG",
"url"=>"https://some-video.s3.amazonaws.com/uploads/video/video_file/id/by-fire.png"}]}],
"md5_checksum"=>nil}]
I am trying to access the thumbnails info, specifically the URL for the thumbnails.
I can't figure out how to get there though.
When I try to go the nested hash key of thumbnails it doesn't work:
[23] pry(main)> n.media.meta_info[:response][:outputs][:thumbnails]
TypeError: no implicit conversion of Symbol into Integer
from (pry):22:in `[]'
Thoughts?
The [{ at the beginning of the output indicates that an array is returned. You first need to find a element in the array that contains a thumbnails key:
outputs = n.media.meta_info[:response][:outputs]
output_with_thumbnail = outputs.find { |elem| elem.keys.include?('thumbnails') }
Then continue like this:
output_with_thumbnail['thumbnails']
If you're just trying to find the thumbnail, and don't care about the rest of the outputs, you can use #find like so:
thumbnails = n.media.meta_info[:response][:outputs].find {|it| it[:thumbnails] }[:thumbnails]
You have an array of hashes, thumbnails are in the 3rd:
n.media.meta_info[:response][:outputs][3][:thumbnails]
It looks like
outputs = n.media.meta_info[:response][:outputs]
is an Array of hashes. So, you need to iterate over them first:
outputs.each do |output|
# deal with each output here
end
You can check for :thumbnails like so:
if (thumbnails = output[:thumbnails])
# we've got thumbnails, deal with it here
end

rmagick does not overwrite EXIF property

I'm trying to reset the Orientation EXIF field in my image using rmagick 2.13.1 and ruby 1.9.3p194, but it doesn't seem to save.
img = ::Magick::Image::read(local_source_path).first
img.get_exif_by_entry()
=> [["ColorSpace", "1"],
["ExifImageLength", "2448"],
["ExifImageWidth", "3264"],
["ExifOffset", "38"],
["Orientation", "6"]]
Orientation can also be acquired thru the properties
img.properties
outputs
{"date:create"=>"2014-01-08T17:26:10-08:00",
"date:modify"=>"2014-01-08T17:26:05-08:00",
"exif:ColorSpace"=>"1",
"exif:ExifImageLength"=>"2448",
"exif:ExifImageWidth"=>"3264",
"exif:ExifOffset"=>"38",
"exif:Orientation"=>"6",
"jpeg:colorspace"=>"2",
"jpeg:sampling-factor"=>"2x2,1x1,1x1"}
I try to set Orientation property to nil & save:
img['exif:Orientation'] = nil
Now,
img.properties
=> {"date:create"=>"2014-01-08T17:26:10-08:00",
"date:modify"=>"2014-01-08T17:26:05-08:00",
"exif:ColorSpace"=>"1",
"exif:ExifImageLength"=>"2448",
"exif:ExifImageWidth"=>"3264",
"exif:ExifOffset"=>"38",
"jpeg:colorspace"=>"2",
"jpeg:sampling-factor"=>"2x2,1x1,1x1"}
which looks correct.
img.write(local_dest_path)
but when I use get_exif_by_entry
img.get_exif_by_entry('Orientation')[0][1]
I still get 6 & not nil.
When I read the written file that was modified local_dest_path, I still get "6".
Is there a set_exif_by_entry function that I'm missing? Any ideas? Thanks
Couldn't set to nil, but I was able to set it to 1 during the write
img.write('local_dest_path') { self.orientation = ::Magick::TopLeftOrientation }
The constant UndefinedOrientation didn't work either. Other values available are listed in OrientationType.

Call a method from constructed string in Ruby / Rails

I've got a problem in doing some metaprogramming in Ruby / Rails which must be minor, but I can't get the clue.
I wan't to assign values to an active record relation, with my model having attributes:
MyModelClass.p1_id,
.p2_id,
...
.p8_id
SecondModel.position #Integer in (1..8)
I now want to do the following
sms = SecondModel.where(:xyz => 'bla')
sms.each do |sm|
mmc = MyModellClass.first
mmc.#somehow construct method here = sm.id
end
So that somehow this is accomplished
mmc.p1_id = sm.id
mmc.p2_id = sm.id
..
mmc.p8_id = sm.id
To sum up: I want to create that p*n*_id stuff dynamically, but I can't find out, how to tell Ruby, that this should be a method. I tried so far:
mmc.send('p#{sm.position.to_s}_id'.to_sym) = sm.id
But this doesn't work. Any clues?
You were close. Try this:
mmc.send("p#{sm.position.to_s}_id=", sm.id)
Here we call the method with = and pass the value of attribute as the second argument of send

Saving images with Carrierwave inside a ruby worker

I'm trying to write a method to store an image from a given url, inside a ruby worker. It comes along my Rails app in which I display the object image.
Here is what I've come up with :
def store(url)
#object = Object.find(1)
#object[:image] = CarrierWave::Uploader.store!(image_url)
end
It doesn't seem to work at all.
Any clues?
Is there another way around?
[EDIT]
Here is the current situation :
def store
#object = Object.find(1)
my_uploader = ImageUploader.new
image = open("http://twitpic.com/show/iphone/xxxx.jpg")
# or for a local file:
image = File.open(Rails.root.join('xxxx.png'))
#object[:image] = my_uploader.store!(image)
#object.save!
end
The filename in the [:image] attibute is still wrong. It gives "[:store_versions!]". How do I get the filename right?
[EDIT2]
Got the filename right by adding #artwork[:image] = my_uploader.filename before save.
But #object = Object.find(1) won't work. How do I access the Object class, which is inside my rails app, from the worker?
#object.image.store!(image) finally did the job!
You'll want to create a new uploader object and point it to your file
image = File.open(Rails.root.join('path', 'to', 'file.png'))
#object[:image] = YourUploader.new(image)

Resources