Get full path to file from within migration - activerecord

I need to insert the contents of a file into the database during a migration (Rails 3.2.13). What's the proper way to reference a file that is elsewhere in the project?
db/migrate/the_migration.rb
class ...
content = File.read("../../app/views/layours/application.html.erb")
end
The relative path doesn't seem to work - I get:
No such file or directory - ../../app/views/layouts/application.html.erb
How can I map this path to an absolute path?

You can try the code below:
class ...
path = File.expand_path('../../app/views/layouts/application.html.erb', __FILE__)
content = File.read(path)
end

assuming you are using rake to apply an active record migration. The file path will be relative to where you started rake which I'm sure will be the projects root.
The file path would be:
content = File.read("app/views/layouts/application.html.erb")

Related

Laravel location of created files

I'm attempting to modify a text file stored on the server using Larvel's File::put(filelocation,filecontents)
but I can't figure out what location this is relative to on the server. If I have a file "somestuff.json" within my LaravelApp/public folder, what location string do I use for a parameter to File::put() ?
File name for File::methods() is not relative you have to give the full file path:
File::put('/var/www/LaravelApp/public/somestuff.json', $filecontents);
But Laravel has some helpers to help you with this:
File::put(public_path().'/somestuff.json', $filecontents);
Also:
base_path(); // the base of your application LaravelApp/
app_path(); // Your LaravelApp/app
They are all in the file vendor/laravel/framework/src/Illuminate/Support/helpers.php.

no such mongoid configuration file issue

I have a minitest_helper.rb and mongoid.yml files in a directory. I putted the below code in minitest_helper;
require 'mongoid'
Mongoid.load!("mongoid.yml", :test)
Although these files in a same directory, Mongoid couldn't load yml file and I got 'no such file' as below:
/home/developer/.rvm/gems/ruby-1.9.3-p374/gems/mongoid-3.1.2/lib/mongoid/config
/environment.rb:40:in `initialize': No such file or directory - mongoid.yml
(Errno::ENOENT)
Also I don't use any framework like Rails, Sinatra etc.
What is wrong?
As you can see in the documentation, the #load! needs the full path to the file. Try changing the code into something like this:
Mongoid.load!(File.join('path_to_the_yml','starting_at_root_of_the_project', 'mongoid.yml') , :test)
The way you construct the File.join will depend on your directory structure. If I had an structure such as this:
project_root
--lib
--spec
----fixtures
------test.xml # the path for this file is project_root/spec/fixtures/test.xml
Then the File.join will look like this:
File.join('spec','fixtures', 'test.xml')

RSpec locating resource path

I have a little project (non Rails) and I'm using RSpec for testing. In order to load models I'm using:
require_relative "../lib/checkout"
However I'm encountering the problem with loading config files, for instance, the test no longer locates my "items.csv" when the following:
CSV.foreach("items.csv") do |row|
Note that the problem occurs only when spec is run from the spec directory, i.e:
rspec checkout_spec.rb
Running it from the project root is fine:
rspec spec/checkout_spec.rb
Any help would be appreciated.
CSV.foreach uses the current directory to find the file. You should probably use File.expand_path to get to an absolute path to items.csv to avoid this problem.
Edited to add an example
Assuming that the file is at root/items.csv, and the ruby file with this code is at root/lib/file.rb, you could write
path = File.expand_path File.join(File.dirname(__FILE__), '..', 'items.csv')
CSV.foreach path do |row|
# rest of code...

Change the default folder where temporary profile is created by firefox

I am getting Errno::ENOSPC error while trying to run watir tests headlessly on firefox. Actually the cause of this error is that I am logged it from a non-root user and when I run my tests, it tries to create directory for temporary firefox profile in 'tmp' folder. Because it doesn't use 'sudo', it is giving this error.
If I do 'mkdir xyz' in 'tmp', it gives 'No space in device' error, which is same as above.
How can I change the default profile folder (which is '/tmp') where webdriver is trying to create temporary profile? I want webdriver to create the temporary profile by itself but in the folder which I can set.
I am using Linux, ruby 1.9.2p320, selenium-webdriver 2.26.0 and watir-webdriver 0.6.1.
Appreciate your help!
I believe you have to monkey patch selenium-webdriver, as there does not appear to be a built in way to specify the directory that includes the temporary profiles.
Background
Seleium-webdriver (and therefore watir-webdriver) creates the temporary Firefox profile directory in \selenium-webdriver-2.26.0\lib\selenium\webdriver\firefox\profile.rb using the method:
def layout_on_disk
profile_dir = #model ? create_tmp_copy(#model) : Dir.mktmpdir("webdriver-profile")
FileReaper << profile_dir
install_extensions(profile_dir)
delete_lock_files(profile_dir)
delete_extensions_cache(profile_dir)
update_user_prefs_in(profile_dir)
profile_dir
end
The temporary folder is created by the:
Dir.mktmpdir("webdriver-profile")
Which is defined in the tmpdir library. For the Dir.mktmpdir method, the second optional parameter defines the parent folder (ie where to create the temporary profile). If no value is specified, as in this case, the temporary folder is created in Dir.tmpdir, which in your situation is the 'tmp' folder.
Solution
To change where the temporary folder is created, you can monkey patch the layout_on_disk method to specify your desired directory in the call to Dir.mktmpdir. It would look like:
require 'watir-webdriver'
module Selenium
module WebDriver
module Firefox
class Profile
def layout_on_disk
#In the below line, replace 'your/desired/path' with
# the location of where you want the temporary profiles
profile_dir = #model ?
create_tmp_copy(#model) :
Dir.mktmpdir("webdriver-profile", 'your/desired/path')
FileReaper << profile_dir
install_extensions(profile_dir)
delete_lock_files(profile_dir)
delete_extensions_cache(profile_dir)
update_user_prefs_in(profile_dir)
profile_dir
end
end
end
end
end
browser = Watir::Browser.new :ff
#=> The temporary directory will be created in 'your/desired/path'.
Setting the environment variable TMPDIR is enough to make ruby, and selenium, write their temporary files where you want.
From http://ruby-doc.org/stdlib-1.9.3/libdoc/tempfile/rdoc/Tempfile.html :
Dir.tmpdir‘s return value might come from environment variables (e.g. $TMPDIR).
So, if you're in your shell:
export TMPDIR="/whatever/you/want/"

unshift + file.join in ruby

$:.unshift File.join(File.dirname(__FILE__),\
'vendor','addressable-2.1.0','lib','addressable','uri')
Does the code above access a file that has this path:
'vendor/addressable-2.1.0/lib/addressable/uri'
I'm trying to vendor the addressable gem into a Sinatra app to deploy it to my hosting provider but I keep receiving:
"no such file to load -- addressable/uri"
after putting the 'unshift' line in config.ru.
The above code adds the path "vendor/addressable-2.1.0/lib/addressable/uri" to the global variable used for looking up external files. The path will be relative to the directory that houses the file this code is placed in. So were {dir} is the directory config.ru is placed, it will add {dir}/vendor/addressable-2.1.0/lib/addressable/uri to the lookup path for includes.
What the line does is it puts the path 'vendor/addressable-2.1.0/lib/addressable/uri' (relative to the directory your ruby script is in) into the load path, which is the list of directories ruby looks through when looking for files you require.
By itself the line won't try to access any files.

Resources