ruby gem, requiring a rails module bad practice? - ruby-on-rails

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.

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 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'

Including Rails time helpers in a non-Rails Ruby application

I primarily use Rails to develop in and I love the 1.hour.from_now, 34.minutes, and 24.megabytes helpers that Rails has built into it. However right now I am building just a Ruby application having these helpers would be nice. Is it possible to just get these helpers in a Ruby application without having to bring in the whole Rails framework?
These methods come from ActiveSupport's core extensions (specifically ones on Integer & Numeric), so you can just require them:
require 'active_support/core_ext/integer/time'
require 'active_support/core_ext/numeric/time'
Or if you want all core extensions ActiveSupport provides:
require 'active_support/core_ext'
Or if you want all of ActiveSupport (core extensions included):
require 'active_support/all'
Of course you have to ensure the activesupport gem is installed.

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.

In Ruby on Rails, why do some gems require config/initializers/foo.rb and some gems don't?

For example, I think the gems such as haml doesn't need a file in config/initializers/, while devise needs a config/initializers/devise.rb.
Why do some gems not need an initializer file and some do? Can they all be made to be without one or all require one? What's the rule?
Some gems need more configuration than others, usually when the functionality is related to an account or needs to be manually configured to work with your specific rails apps (like hoptoad_notifier and devise), other gems provide or add in more general functionality in your rails apps, often these will work without any configuration but do have some flexibility and can be customized with an initialiser (like haml and will_paginate).

Resources