Best way to test a rails view helper without rails - ruby-on-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'

Related

When to load Gem code when depending on rails

I'm developing a Gem that is to be used exclusively in Rails projects. It has been developed inside of a Rails application's lib directory and is now to be extracted in a separate Gem.
Some of the classes depend on the Rails framework to be loaded though. Two examples:
class OurGem::Hookup
CONFIG_PATH = 'config/hookup.rb'.freeze
[...]
end
class OurGem::RoutingContainer
include Rails.application.routes.url_helpers
[...]
end
Normally, I load the Gem code in the Gem's main module file using require. But as this is loaded by bundler, Rails is not ready and things like Rails.application and Rails.root cannot be used. The first example could be worked around by not evaluating the path at load time, but the second one seems kind of tricky to me.
What is the proper way of doing this? Register an initializer using a railtie and require the "delicate" files there?
This is a strange setup, because your gem depends on your rails app and your rails app depends on your gem.
This looks far too coupled to me. Have you considered creating a self-contained rails engine that your main app mounts, instead?
You might be able to get away with doing this, though:
# Gemfile
gem 'our_gem', require: false
# config/initializers/our_gem.rb
require 'our_gem'
OurGem::Hookup.config_path = '...'
This ensures that your gem is only being loaded after the rails application initialises - so things like Rails.application.routes.url_helpers will be defined.

Is there a Rails equivalent of Sinatra's 'register'?

I'm in the process of creating a rubygem which will be used with both Sinatra and Rails applications. Ideally, I'd like to have a single gem which can work with both frameworks. It's very simple - it provides some helpers, styles, scripts and view partials.
For Sinatra, I use the register method to register the module, which in turn adds the helpers, adds some entries to the load paths and optionally creates some actions/routes. So far so good.
My question is: What is the rails equivalent of this? Engines?
Since you need to define routes, I think a rails engine would work best.
You can load helpers with railties too, but I don't think it's possible to define routes with railties.
Rails Engines:
http://edgeguides.rubyonrails.org/engines.html
Railties:
http://edgeapi.rubyonrails.org/classes/Rails/Railtie.html

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.

Is it possible to avoid loading action_controller with Rails 3?

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.

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