How do I access routes of Rails application without loading the environment? - ruby-on-rails

I am trying to access Rails.application.routes in a Capistrano receipt.
It's working when requiring Rails and the whole environment require 'config/environment'
Can I require more specific files for accessing the application's routes without loading the environment? It takes so much time to load.
Background information:
The directory for storing the picture's cache of the isolated and mounted engine is named like the mount-path:
/public/myMountedEngine/pictures/...
I want to read the mount-path of the mounted isolated engine for symlinking the nested pictures folder.

You will have to load the config/application.rb at least to get the routes, so you'll get something like this:
require "./config/application"
MyApp::Application.routes

Related

Load multiple configuration files in Rails engine

I'm building a Rails Engine and right now I set all my configuration variables in config/environments/development.rb (within the engine itself, i can also overwrite it from the application later) and can access it from the application with ::Rails.application.config.my_item
This file is getting big and some variables such as config.title = 'Lambda Website' could be placed somewhere else, I was thinking to make a config/settings/my_file.rb and just include it to be able to call it the same way as the development.rb variables but it's more complicated than I expected ... I tried a couple of gems that didn't work at all, maybe because it's an engine. I also tried to require files but it blows up too.
How can I simply split this configuration file easily ? Is there an easy way to include configuration files within an engine ? Both YAML/ERB solution are welcome ...
Thank you guys ;)
Inside your
app/config/initializerz/custom_setting.rb
#custom_setting.rb
YOUR_CONSTANT = WHATEVER
Then feel free to use this constant this anywhere in your app.

Rails/Gollum: Gollum does not use path prefix to load assets

I mount gollum inside my Rails app like provided this answer.
It works, however the CSS and Javascript assets do not load properly.
routes.rb:
authenticate :user do
mount Precious::App, at: 'wiki'
end
So I can access the wiki at /wiki. This works, but gollum tries to load the css files from eg. http://localhost:3000/css/gollum.css which does not work, instead of http://localhost:3000/wiki/css/gollum.css. How do I tell gollum to use the correct prefix?
This is almost certainly a bug in either Rails or Gollum. I know that Gollum does have the ability to map its assets to a subpath, because I do it in my apps (via Rack, though, not Rails routes), like this:
map "/wiki" do
run Precious::App
end
Reporting a bug would probably be your best bet.

Loading parts of a Rails 3 application

I am developing a gem for Rails 3 that consists of two main components. The first is a rails generator that adds some new files/folders to a rails project. The second is a runtime environment that loads all the aforementioned files (some ruby classes that use my DSL) as well as a portion of the default Rails stack. Essentially it's everything you'd expect to be able to access in rails c, sans routing, controllers, helpers and views. What is the proper way to load a Rails environment, except for specific portions?
Sidenote: I'd love to see any good articles regarding requiring Rails applications.
I am not entirely clear what you mean, or if this will help, but it sounds similar to something I do in a utility I wrote.
My utility loads the environment like so:
#!/usr/bin/env ruby
require File.expand_path('../../config/environment', __FILE__)
The require of the ../../config/boot will cause the gems defined in your Gemfile to load. So if you needed only part of the Rails stack then you would only require that part of the stack in your Gemfile.
This gives me my rails context, access to models and other resources.
(UPDATE)
To skip parts of the rails stack - take a look at how its been done to swap out ActiveRecord:
http://www.mongodb.org/display/DOCS/Rails+3+-+Getting+Started
Hope that helps.
Maybe you need Rails::Initializable?
You can do like that:
initializer "active_support.initialize_whiny_nils" do |app|
require 'active_support/whiny_nil' if app.config.whiny_nils
end

where should I store files in rails?

1) I am downloading datafeeds (xml) files from a URL to unzip/import them into the database. Where should I store them in the rails file structure?
2) How does the rails file structure work, can rails access the entire hosting environment? I basically mean, if I store my XML feed in /lib/files would I use that path in my models, or the longer full linux path?
Appreciate any advice!
You should probably use the tmp/ folder to store those temporary files
Its a good practice to always use the full path. You can get the rails root dir via Rails.root
Rails can access any thing that the user account under which the rails process is running, can access. ie: if you run the rails server process under root (which is not a good idea BTW), the app could access any path that root can access. This might of course be limited by whatever access control mechanisms in place by the OS(ex: SELinux).

Rails - Creating temp files in a portable way

My rails application runs on a Ubuntu server machine.
I need to create temporary files in order to "feed" them to a second, independent app (I'll be using rake tasks for this, in case this information is needed)
My question is: what is the best way of creating temporary fields on a rails application?
Since I'm in ubuntu, I could create them on /tmp/whatever, but what would work only in linux.
I'd like my application to be as portable as possible - so it can be installed on Windows machines & mac, if needed.
Any ideas?
Thanks a lot.
tmp/ is definitively the right place to put the files.
The best way I've found of creating files on that folder is using ruby's tempfile library.
The code looks like this:
require 'tempfile'
def foo()
# creates a temporary file in tmp/
Tempfile.open('prefix', Rails.root.join('tmp') ) do |f|
f.print('a temp message')
f.flush
#... do more stuff with f
end
end
I like this solution because:
It generates random file names automatically (you can provide a prefix)
It automatically deletes the files when they are no longer used. For example, if invoked on a rake task, the files are removed when the rake task ends.
Rails apps also have their own tmp/ directory. I guess that one is always available and thus a good candidate to use and keep your application portable.

Resources