Require multiple rails sites from script - ruby-on-rails

I want to access the databases used by two completely separate Rails sites from an external Ruby script. I know that I can require the environment of a Rails app, like this:
#!/usr/bin/env ruby
require "/path/to/rails/app/config/environment"
I can then use all the models defined in the Rails app to read and write data and it will use the database settings defined for the site.
But now I want to do this for two sites at the same time, e.g.:
#!/usr/bin/env ruby
require "/path/to/first/rails/app/config/environment"
require "/path/to/second/rails/app/config/environment"
Clearly, that will not work as the application can not be initialised twice. Is there any way to avoid the collision?

Related

Rails requiring standard Ruby libraries?

I ran into an issue testing a new app in production environment.that Net::HTTP was not defined, but in development it was.
Naturally a require 'net/http' somewhere solves this (e.g. I put it in config/application.rb after Bundler.require(*Rails.groups),as I understand, there is no gem name I can add to Gemfile).
But can I find a list of which Ruby standard modules/classes need to be required, or should I just start adding everything I need to application.rb to be clear (date, json, net/http, etc.)?

Best practices when including Rails models in another application

I'm developing a ruby application that uses the models and data from another Ruby on Rails web application as its main data source.
The Rails models were included in this application by including the environment.rb file in the main file like this:
# Require Rails
require_relative "../../RailsApp/config/environment.rb"
This works but there are uninitialized dependencies when loading models that use gems that are defined in the Rails Gemfile. (For example, acts_as_taggable_on, rack-pjax, devise, etc)
This ruby application dependencies are also managed through Bundler, so at the moment the only way to get the application working is to copy and paste the contents from the Rails' Gemfile into the ruby app's Gemfile.
Obviously this approach is not optimal as the gem requirements are duplicated.
Is there a better way to include Rails and the dependencies that its models require in another application? Is there a way to include a Gemfile into another?
Here are some options, in order of simplicity
Just keep everything in one app, a lot of stuff is easier this way
Use plugins to share common code
Use web services to share data
You could extract the models and code out from RailsAppA into a Gem. RailsAppA then includes that Gem and uses it.
The gem can remain in a private repository and does not need published.
Both apps would then do something like:
gem "yourapp-modelage", git: "http://github.com/you/yourapp-modelage.git"
Then, App2 would also use that Gem... How much goes into the Gem will depends on how much you need to re-use.

How do I access routes of Rails application without loading the environment?

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

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

Invoking ruby classes outside of rails app structure

I have a question regarding how to call ruby classes that reside outside of rails app structure.
I have few Ruby classes that are used/invoked from cron job periodically.
I would like to use the same ruby classes to be invoked from a rails controller
after user creates a model object.
Is it possible to invoke code that resides outside of rails app directory without copying the ruby classes
over. I am mostly concerned that both the classes will be out of sync soon if I have to copy them to rails app folder.
Ruby app reside in, /usr/local/railsapp1 . The ruby classes reside in /usr/local/other_task/
Any suggestions would be of great help.
thanks
You can. Just use in your config/environment.rb
config.load_paths << "/usr/local/other_task/"
This will load whatever classes in that folder into your rails environment.
But the path is hard-coded in this case, so you have to be careful when deploying.

Resources