Rails, Rake, moving a folder to a new location - ruby-on-rails

I need to move a folder from a plugin to the main app/views. I guess using rake to do this with the following command is the easiest way:
require 'fileutils'
FileUtils.mv('/vendor/plugins/easy_addresses/lib/app/views', '/app/views/')
I'm just not sure where to tell script where to look and where to place the folder.
The file I want to move is in the following location: `vender/plugins/easy_addresses/lib/app/views/easy_addresses
easy_ addresses is the name of the folder in views that I want to move to my_app/app/views/

FileUtils.mv('/source/', '/destination/')

There is a constant which has the rails root, just prepend it to your pathes:
File.join(RAILS_ROOT, "app", "views")
Here RAILS_ROOT holds the location "where to look", and using File.join on the path components takes care of concatenating the components using the right path separator suitable for the used system.
In the result the above method call gives you the complete absolute path to "app/views" in your application.
Edit:
In Rails >= 3 you can use Rails.root.join('app', 'views').

Related

Change pseudo-absolute assets path in Rails 3

I tried every way I found, but I don't know how to do it.
Rails always writes /assets/... and would like to have ./assets/ or assets/ because my application is in a subfolder, not in the root folder.
I tried with config.action_controller.asset_host = "." but Rails writes http://./assets/
I tried with config.assets.prefix = "." but Rails writes /./assets/
Suppose that my application lives in: http://domain.com/example/, with the default behaviour of Rails every path points to http://domain.com/ and not to http://domain.com/example/. I don't want to specify an absolute path, because that is not a portable solution.
Check this as an example: http://apidock.com/rails/ActionView/Helpers/AssetTagHelper/image_tag
You should be able to edit your environment file (config/environments/development.rb) under Rails 3, adding the line:
config.action_controller.relative_url_root = '/example'
This line allows you to run in a subdirectory called "example." Note that you'll want to duplicate this change in production.rb and test.rb as well.

How add new folder with class in rails app?

I have 2 questions in rails app context:
I have some classes which aren't "modele", but require in my sytem, so I want separe them
1) How can I add "class' folder in app/? (if I create it and put classes, their are no included)
2) how can I put folder "model" in "app/class" folder (same thing here, the model are not included if I move it)
thx.
It´s kind of unclear what you are asking.
But if you want to autoload additional directories you can do it by placing something like this in config/application.rb
config.autoload_paths << Rails.root.join('app/class')
But please don´t call your directory class, use something descriptive instead.
By convention code that does not fit inside models, controllers, views, helpers or concerns and placed in the lib directory at the project root.
Edit:
You can load subdirectories by using a glob:
config.autoload_paths << Rails.root.join('app/classes/**/')
For quite some time Rails has autoloaded all paths under /app, as mentioned here
You may have run into a problem when using a "app/class" directory since "class" is a reserved word and "Class" is a class in Ruby.
There is a problem with your example:
exemple: "app/classes/effects/attribute.rb" with "class Effect::Attribute"
Notice that in the file path "effects" has an "s" at the end, whereas your module name does not "Effect::Atttribute". Those should match. Either both end with "s" or not, and when they do match Rails autoloading should work.
You should remove any of the other suggestions about appending to config.autoload_paths.

Importing a file into my app from a hosted account (godaddy)

I have already built several Rake tasks to import a series of CSV files into my PostgreSQL database, but I've always had those tasks directed to a path on my local computer.
I need to have the tasks set up to access a secure on directory so my client can simply copy files into this directory, and when the Rake task is executed it automatically pulls from the online source.
This seems like it would be a simple adjustment in my code but I'm not sure how to make it work. If anyone has any suggested reading or can quickly help edit my code it would be appreciated.
Below is a copy of one of my Rake tasks:
require 'csv'
namespace :import_command_vehicles_csv do
task :create_command_vehicles => :environment do
puts "Import Command Vehicles"
csv_text = File.read('/Users/Ben/Sites/ror/LFD/command_vehicles.csv', :encoding => 'windows-1251:utf-8')
csv = CSV.parse(csv_text, :headers => true)
csv.each_with_index do |row,index|
row = row.to_hash.with_indifferent_access
CommandVehicle.create!(row.to_hash.symbolize_keys)
command_vehicle = CommandVehicle.last
if #report_timesheet_hash.key?(command_vehicle.report_nr)
command_vehicle.timesheet_id = "#{#report_timesheet_hash[command_vehicle.report_nr]}"
end
#command_vehicle.timesheet_id = #timesheet_id_array[index]
command_vehicle.save
end
end
end
You can create rake tast that can accept parameters and then you can pass whatever path you like.
Check out this link Set Multiple Environmental Variables On Invocation of Rake Task to see how to pass parameters via enviroment variables and this link http://viget.com/extend/protip-passing-parameters-to-your-rake-tasks to see bracket notation.
'/Users/Ben/Sites/ror/LFD/command_vehicles.csv' will only work on your local machine, unless you have an identical path to your resources on the remote host, which is hardly likely. As a result, you need to change the absolute path, to one that is relative to your file.
I have a folder hierarchy on my Desktop, stuck several folders down, like:
- test
| lib
| - test.rb
| config
| - config.yaml
And test.rb contains:
File.realpath('../config/config.yaml', File.dirname(__FILE__))
__FILE__ is a special variable that returns the absolute path to the script currently being interpreted. That makes it easy to find where something else is if you know where it exists relative to the current __FILE__.
The value returned by realpath will be the absolute path to the "config.yaml" file, based on its relative path from the current file. In other words, that would return something like:
/Users/ttm/Desktop/tests/junk/test/config/config.yaml
The nice thing about Rake is it's really using Ruby scripts, so you can take advantage of things like File.realpath. Also, remember that Rake's idea of the current-working directory is based on the location of the script being run, so everything it wants to access has to be relative to the script's location, or an absolute path, or you have to change Rake's idea of the current-working directory by using a chdir or cd.
Doing the later makes my head hurt eventually so I prefer to define some constants pointing to the required resources, and figuring out the real absolute path becomes easy doing it this way.
See "How can I get the name of the command called for usage prompts in Ruby?"
for more information.

Is it possible to get the database.yml file from within an rails engine configuration?

I am in the process of creating a rails mountable engine and I need to retrieve the database.yml file from the application that is using the engine from within the lib/MyEngine/engine.rb file. First of all is this even possible? And if so how would you achieve this?
I have tried using "Rails.root" from within the engine.rb file but it returns nil.
Any help on this would be great and thank you in advance.
Should be enough this:
path = Dir.pwd + '/config/database.yml'
I use:
path = "#{ENV["APP_ROOT"]}/config/database.yml"
If you want to load it already parsed:
YAML.load_file("#{ENV["APP_ROOT"]}/config/database.yml")
Also, to be sure you get the APP_ROOT variable set up I use to add before those lines:
ENV["APP_ROOT"] ||= File.expand_path("#{File.dirname(__FILE__)}/..")
(obv. fix that path depending on how many level you should go back to reach the root folder)
File.join(Rails.root, 'config', 'database.yml')
This will also work on OS other than MacOSX, linux, unix where '/' is not standard delimiter for path.

How to gemify a Rails (engine) plugin?

I have a engine style Rails plugin from which I can create a gem using Jeweler. But when I require it in my Rails environment (or erb) the models within the plugin are not loaded. I have followed a number of tutorials and read just about everything on the subject.
# environment.rb
config.gem 'myengine'
# in irb
require 'myengine'
I have unpacked the gem and verified that all files are present. My init.rb has been moved to a new folder called 'rails' as per. All files in 'lib' are automatically added to the $LOAD_PATH, so require 'myengine' runs lib/myengine.rb. I verified this by putting a puts 'hello' within.
Is it because of the physical presence of plugins in a known place that Rails can add all the models, controller etc. to the relevant load_paths? Do I need to replicate this manually when using a gem?
Would gemspec require_paths be a way of adding additional paths other than lib? I assume however that Rails does not just require every single file, but loads them on demand hence the need for the filename and class name to match?
%w{ models controllers helpers }.each do |dir|
path = File.join(File.dirname(__FILE__), 'app', dir) + '/'
$LOAD_PATH << path
puts 'requiring'
Dir.new(path).entries.each do |file|
if file =~ /\.rb/
puts file
require file
end
end
end
By adding the above to lib/myengine.rb all the models/controllers are required. But like I said in my question this is unlikely to be a good way forward.
Offhand I'd say the part about adding those directories to the search path is right on. What you shouldn't need to do is require each file manually (as you allude to in your last sentence). What Rails does when you reference a non-existent constant is to search for a file with the same name (underscored of course) in the load path.
If for some reason you can not abide by the constraint (think about it long and hard) then you are going to need to dig deeper into Rails and see how the reloading mechanism works so you can tie into it properly in development mode.
The problem was the files (in app) where not being added to the gem because when using Jeweler it only automatically adds files to required_paths which are committed to git.

Resources