I am using the package and initialise it in my rails application in config/initializers/prawn_rails.rb with include PrawnRailsForms
I am trying to override a method to increase the font size of field.upcase with the code below
include PrawnRailsForms
DocumentExtensions.module_eval do
def make_field_box(field)
stroke_bounds
bounds.add_left_padding 2
move_down 2
text field.upcase, size: 18
end
end
But the font doesn't get affected in the pdf views even I restarted the server.
You need to do the monkey pathing here.
Create a prawn-rails-forms directory in lib directory.
Create a document_extensions.rb file inside prawn-rails-forms directory.
Add below code to the document_extensions.rb
module PrawnRailsForms
module DocumentExtensions
private
def make_field_box(field)
stroke_bounds
bounds.add_left_padding 2
move_down 2
text field.upcase, size: 8
end
end
end
Restart the server.
Extended
If you find still not working after restarting the server then
Add config.autoload_paths += %W(#{config.root}/lib) to the config/application.rb OR
Move prawn-rails-forms directory to the config/initializers
Please let me know if it works for you.
Related
I'm new to ruby. Actually I'm trying to create an empty file "myfile.txt" in each of the following directories:
../../../../../TESTS/Test_A/myTest_A/
../../../../../TESTS/Test_B/myTest_B/
../../../../../TESTS/Test_C/myTest_C/
../../../../../TESTS/Test_D/myTest_D/
As you can see, the name of the Top directory is "TEST" and than after this, every directory have a different name but starts with "Test_", and than each "Test_*" directory contains only one directory and there my file should go in. I'm trying something like this:
require 'pathname'
def create_myFile
pn = Pathname.new('../../../../../TESTS/Test_*/**')
myFile = File.new("#{pn}/myFile.txt", "w+")
end
create_myFile
It doesn't work. Any suggestions?
Pathname does not accept wildcards. Dir#[] does:
Dir['../../../../../TESTS/Test_*/**'].each do |dir|
File.new File.join(dir, 'myFile.txt'), 'w+'
end
I'm trying to load a library from a Controller and it is throwing me this error
cannot load such file -- /xsd
It is, in fact, being loaded in the first line of the library file
require 'xsd/qname'
# {http://www.f2b.com.br/soap/wsbillingaction.xsd}F2bAcaoCobranca
# mensagem - F2bAcaoCobranca::Mensagem
# cliente - F2bAcaoCobranca::Cliente
# acao_cobranca - F2bAcaoCobranca::Acao_cobranca
class F2bAcaoCobranca
#...
end
The library's file is in the following directory:
lib
--f2b
--acao_cobranca
--wsbillingaction.rb
And my controller's action has the following lines of code
def index
require 'f2b/acao_cobranca/wsbillingaction.rb'
end
What is giving me this error? Is it some old stuff from previous versions of Ruby/Rails? How can I fix it?
wsbillingaction depend on another lib (xsd/qname), you should include it in your project or install it as a gem
I am uploading txt files using carrierwave. The files are not small (80 MB - 500 MB) and I want to remove some of the lines to reduce this size (about 80% of the file size is going to be reduced).
I have created a model method in order to clear these lines:
require 'fileutils'
def clear_unnecessary_lines
old_file_path = Rails.root.join('public').to_s + log_file.to_s
new_file_path = old_file_path.sub! '.txt', '_temp.txt'
File.open(old_file_path, 'r') do |old_file|
File.open(new_file_path, 'w') do |new_file|
old_file.each_line do |line|
new_file.write(line) unless line.grep(/test/).size > 0
end
end
end
FileUtils.mv new_file_path, old_file_path
end
but I am getting error when I am trying to open the new file saying there is no such file. As I have read opening a file with the w option should create an empty file for writing. Then why I am getting such error?
Also, since log_file column is holding the path to the original file, and I am changing it, could you tell how to rename the new file with the old name? As I have checked I should specify only old and new names, not paths.
Errno::ENOENT: No such file or directory - /home/gotqn/Rails/LogFilesAnalyser/LogFilesAnalyser/public/uploads/log_file/log_file/3/log_debug_temp.txt
It is strange that If I execute the following command in rails console, it is not throwing an error and the file is created.
File.open('/home/gotqn/Rails/LogFilesAnalyser/LogFilesAnalyser/public/uploads/log_file/log_file/3/log_debug_temp.txt','w')
Ah, i see your problem now. When you do this
new_file_path = old_file_path.sub! '.txt', '_temp.txt'
you call the "self-altering" version of sub, ie sub!. This will actually change the value of old_file_path as a side effect. Then, in the next line, you try to open this file, which hasn't been created yet. Take out the exclamation mark and you should be fine.
Something I'm not getting about the version process...
I have a zip file with a file inside, and I want to upload the file as a "version" of the zip:
Uploader:
version :specificFile do
process :extract_file
end
def extract_file
file = nil
Zip::ZipFile.open(current_path) do |zip_file|
file = zip_file.select{|f| f.name.match(/specificFile/)}.first
zip_file.extract(file, "tmp/" + file.name.gsub("/", "-")){ true }
end
File.open("tmp/" + file.name.gsub("/", "-"))
end
Usage:
=link_to "Specific File", instance.uploader.specificFile.url
Only this just nets me two copies of the zip. Clearly, there's something I'm missing about how version / process works, and I haven't been able to find documentation that actually explains the magic.
So how do I do this, and what am I missing?
This provided the "why", although it took a bit to understand:
How do you create a new file in a CarrierWave process?
To rephrase, when you go to create a version, carrierwave makes a copy of the file and then passes the process the file path. When the process exits, carrierwave will upload the contents of that path - not the file the process returns, which is what I thought was going on.
Working code:
version :specificFile do
process :extract_file
def full_filename (for_file = model.logo.file)
"SpecificFile.ext"
end
end
def extract_plist
file = nil
Zip::ZipFile.open(current_path) do |zip_file|
file = zip_file.select{|f| f.name.match(/specificFile/)}.first
zip_file.extract(file, "tmp/" + file.name.gsub("/", "-")){ true }
end
File.delete(current_path)
FileUtils.cp("tmp/" + file.name.gsub("/", "-"), current_path)
end
So, to make what I want to happen, happen, I:
Tell carrierwave to use a particular filename. I'm using a hardcoded value but you should be able to use whatever you want.
Overwrite the contents of current_path with the contents you want under the version name. In my case, I can't just overwrite the zip while I'm "in it" (I think), so I make a copy of the file I care about and overwrite the zip via File and FileUtils.
PS - It would be nice to avoid the duplication of the zip, but it doesn't look like you can tell carrierwave to skip the duplication.
i wrote some helper methods for my application like this
module Magick
class Draw
def qrcode(qrcode, left_corner, top_corner, right_corner, bottom_corner)
size = qrcode.modules.length
width = right_corner - left_corner
height = bottom_corner - top_corner
wset = width.to_f / size
hset = height.to_f / size
...............
Where to place such code in rails ?
A common and easy way to load those patches is to create a file inside your ./config/initializers directory (ie: rmagick.rb) and put your code in this file.
All files in this directory are loaded and executed on environment startup.
You could also create a new file with your code in the lib/ directory (ie: rmagick_draw.rb) and add this line in ./config/application.rb:
config.autoload_paths += %W(#{config.root}/lib)
And then require the file anywhere you need it.
You can put such files into app\classes directory. All classes from this directory are available by default in Rails3.