Is it possible to avoid loading action_controller with Rails 3? - ruby-on-rails

Basically I want to load this:
require "active_record/railtie"
require "active_resource/railtie"
require "action_mailer/railtie"
and not load action_controller because in this particular situation I have no use for it.
Looking at the code in Rails' master, I can see:
# For now, action_controller must always be present with
# rails, so let's make sure that it gets required before
# here. This is needed for correctly setting up the middleware.
# In the future, this might become an optional require.
require "action_controller/railtie"
... so I was wondering: Is it possible to load Rails with only active_record, active_resource and action_mailer, or do I always have to load action_controller?

Rails is a MVC framework for the web, while Resque "is a Redis-backed Ruby library for creating background jobs".
If you need models, then use ActiveRecord.
If you need views, Ruby has ERB in its standard library.
If you don't need routes and controllers, then using a web framework doesn't make any sense.

Related

explanation of application.rb file in Rails App

I'm trying to get a deeper understanding of how a rails app initializes. I'm looking over the config/application.rb file and I'm confused by these three lines:
require_relative 'boot'
require 'rails/all'
Bundler.require(*Rails.groups)
From what I can tell all three of these lines are loading the gems used by the Rails Application. boot.rb appears to load all the gems as does Bundler.require(*Rails.groups). Why is it necessary to have all three lines of code?
Mostly correct, and you can verify what is "needed" by disabling one line at a time in a working Rails app.
require_relative 'boot': Application still runs.
require 'rails/all': Method not found error for a gem not listed in my Gemfile (one of Rails' built-ins (require 'rails/all')
Bundler.require(*Rails.groups): Method not found for gem from Gemfile.
So, the second and third are independent and essential. boot.rb's call to bundler/setup cannot stand in for either of the other two, because its function is actually to clean the load path by making sure that only Gemfile gems are included, and everything else is removed. See the last line of the Bundler setup source. So, while the app runs, it could be running with access to other gems that you did not intend to include, and give you a false sense that the app is working when it could fail for another user who only installed the Gemfile dependencies.
So you may get away with only the second and third in the short term, but would definitely want all three on anything that someone else may someday have to execute. The overhead is minimal so I would not remove any of these.
require_relative 'boot'
Sets up Bundler and load paths for gems
require 'rails/all'
This loads the rails gems. It can be replaced in order to explicitly require only the rails gems that you need (i.e. require "action_mailer/railtie"
Bundler.require(*Rails.groups)
This requires gems listed in your Gemfile by default. If you remove this line, you would have to require each gem by hand.

Require a gem in rails

I'd like to include unitwise in my project, so I added it in the Gemfile and I want to use the core extensions of this gem in a model, so I have to require 'unitwise/ext' which isn't by default. Should I require this file in every models I use it, or is there a way to require it one time for the whole project?
You can require it once either by creating an initializer for it, or adding this line to application.rb.
require 'unitwise/ext'
You could create separate ruby file in config/initializer/ to require unitwise/ext which will be available in all place in the project or you could require in application.rb, here also it will be available everywhere in application.
It is better add one line in application.rb, instead creating separate file in initializer to require that file.

ruby gem, requiring a rails module bad practice?

I am working on a ruby gem that parses xml. I want to utilize the rails Hash.from_xml method and am wondering if requiring active_support or any large library in a gem is bad practice. Is this adding too much just so I can use its one method, or is this considered standard/ok when building a ruby gem? I would be adding require 'active_support/all' to my gem.
I prefer to require as little as possible and to only cherry-pick the specific definition that I want to use.
In your example that would look like this:
require 'active_support'
require 'active_support/core_ext/hash/conversions'
Read about how to require only specific definitions from ActiveSupport in the official Rails Guides.

Best way to test a rails view helper without rails

I am making a gem that defines some framework agnostic helpers for generating html.
Testing under Sinatra is trivial. But what do I need to test heplers by rails erb and haml rendering without having to require the full stack?
I guess at some point I have to require 'action_view' and 'action_view/renderer/renderer'

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

Resources