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.
Related
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.
I'm writing a tool to parse lua plugins created by other users. The only guarentee about the plugin is that it has a data.lua file in a known directory. Inside there users are free to do anything they wish. This particular plugin using require to load a file and that file loads another file. Both are relative paths but the second is relative to the location of the first file.
data.lua
foo/bar.lua
foo/baz.lua
data.lua:
require("foo.bar")
foo/bar.lua:
require("baz")
When I try to execute data.lua I get an error when foo/bar.lua tries to require "baz". None of the paths it tries are ./foo/.
Any idea how I can fix this? I could find any documentation specifically about this case, it seemed like I need to hard code /foo/ into the path but I don't know it ahead of time. This seems like something that should be automatic is there a setting I'm missing or am I running the wrong version of lua? I'm using NLua 4.0
Thanks
I tested this script using node-lua and it fixes the issue for me!
https://gist.github.com/hoelzro/1299679
Relavent code:
local oldrequire = require
function require(modname)
local regular_loader = package.loaders[2]
local loader = function(inner)
if string.match(modname, '(.*)%.') then
return regular_loader(string.match(modname, '(.*)%.') .. '.' .. inner)
end
end
table.insert(package.loaders, 1, loader)
local retval = oldrequire(modname)
table.remove(package.loaders, 1)
return retval
end
To get this to work with Lua 5.2 change all uses of package.loaders to package.searchers.
Also if you want to override the global require function you need this snippet as well:
_G.require = require
You can alter the search behavior of require by changing the package.path variable.
Resources on package.path:
https://www.lua.org/manual/5.3/manual.html#pdf-package.path
http://lua-users.org/wiki/PackagePath
Example adding foo folder to search locations:
package.path = package.path .. ';./foo/?.lua'
the ? character will be where the string passed to require is placed.
Alternatively you can add a default file to load, to the package.path:
package.path = package.path .. ';./nested_require.lua'
Then define the behavior you would like within this file. You can use the global variable _REQUIREDNAME to reference the value passed to the require function.
The documentation for this method can be found here at the bottom of the page: https://www.lua.org/pil/8.1.html
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.
Hey i'm trying to localize a plugin called Donate Plus ( which locallized technicly).
the plugin came with en_CA and de_DE files, i've tried creating a he_IL file without success.
So i've tried with the de files came with the plugin but didn't work.
I've set the WPLANG in wp-config.php to de_DE yet that dosen't change the code.
this is the setting code :
load_plugin_textdomain( 'dplus', '/wp-content/plugins/donate-plus' );
And i did check that all the string are set to be localized.
Anyone has a clue?
I just was with a similar isue, did you try to rename your files from de_DE.po and de_DE.mo to name-of-plugin-de_DE.mo and name-of-plugin-de_DE.po (changing name-of-plugin with yours, of course)?
dplus-de_DE.mo and dplus-de_DE.po It must work ;)
load_plugin_textdomain takes three parameters.
In your case it would be something like this (assuming the .po and .mo files are located in a subdir called 'languages')
load_plugin_textdomain( 'dplus', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
I checked the source of DonatePlus Plugin and I found that the Plugin is doing localization wrongly.
The load_plugin_textdomain() call is made inside the DonatePlus classes constructor. But it should be present inside the 'init' hook. Trying adding the following code (which is at the of the file) inside the init function.
if( class_exists('DonatePlus') )
$donateplus = new DonatePlus();
Where are all the .po and .mo files stored? Are they inside the /wp-content/plugins/donate-plus folder itself? If not then change the path or move the files.
I had a similar issue where I was loading the translation files with the load_plugin_textdomain function from within a service class using PSR-4. This meant that the dirname( plugin_basename( __FILE__ ) ) string returned the wrong path.
The correct path is the relative path your-plugin/languages (assuming you are loading the translation files from the /languages directory).
Absolute paths such as /var/www/html/wp-content/plugins/my-plugin/languages won't work.
My plugins file structure looks something like this:
- my-plugin
- assets
- languages
- services
- Api
- Base
Translation.php
- ...
Plugin.php
- vendor
- views
composer.json
composer.lock
index.php
my-plugin.php
uninstall.php
Since my Translation service is placed in the /services/Base/ directory, this worked for me:
$root = plugin_basename(dirname(__FILE__, 3));
load_plugin_textdomain( 'my-plugin', false, "$root/languages/");
Also, I used no action hook at all instead of init or plugins_loaded and fired the load_plugin_textdomain function at the beginning of the plugin, since the hooks don't fire early enough for the admin menu and action links to get translated.
Use:
load_textdomain( TEXT_DOMAIN , WP_PLUGIN_DIR .'/'.dirname( plugin_basename( FILE ) ) . '/languages/'. get_locale() .'.mo' );
I have a file "settings.ini" which needs to reside next to the Qt executable.
I can add a custom build step for this in Qt Creator which calls something like this:
copy %{sourceDir}/settings.ini %{buildDir}/settings.ini
This works great so far, but I'd like to include this in the *.pro file so I can put this up in our SVN too.
How can I do this using qmake/.pro-files only?
To copy %{sourceDir}/settings.ini to the build directory without requiring to call make install use:
copydata.commands = $(COPY_DIR) $$PWD/settings.ini $$OUT_PWD
first.depends = $(first) copydata
export(first.depends)
export(copydata.commands)
QMAKE_EXTRA_TARGETS += first copydata
$$PWD is the path of current .pro file. If your settings.ini file is not located in the same directory than the project file, then use something like $$PWD/more_dirs_here/settings.ini
Note: I found this solution here. I recommend to read the whole article as it explains how it works.
You probably want to use the INSTALLS keyword in QMake. It will require you to run make install after your build, but it does work cross-platform.
install_it.path = %{buildDir}
install_it.files += %{sourceDir}/settings.ini
INSTALLS += install_it
for osx bundles you can handle it this way
see Resource files in OS X bundle
add this to you project file:
APP_QML_FILES.files = path/to/file1.qml path/to/file2.qml
APP_QML_FILES.path = Contents/Resources
QMAKE_BUNDLE_DATA += APP_QML_FILES
this example copies the files to Contents/Resources
Compatible with Windows and Mac OSX Dev environments:
Change {AppName} to respective application name
# Define mac/windows specific target dirs
TARGETDIR = ''
macx {
TARGETDIR += $$OUT_PWD/{AppName}.app/Contents/MacOS/
}
else {
TARGETDIR += $$OUT_PWD
}
# Directories do not exist for the first build
# Without mkdata, build is successful after 5 tries. To avoid, use mkdata
mkdata.commands = $(MKDIR) $${TARGETDIR}
copydata.commands = $(COPY_FILE) $$PWD/settings.ini $${TARGETDIR}
first.depends = $(first) mkdata copydata
export(first.depends)
export(mkdata.commands)
export(copydata.commands)
QMAKE_EXTRA_TARGETS += first mkdata copydata
Happy to add Unix support if someone posts Unix solution in the comments.