Including Rails time helpers in a non-Rails Ruby application - ruby-on-rails

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.

Related

What is therubyracer or libv8 for in a rails application?

I was working on a project and was having issues with therubyracer and libv8 often, so i decided to remove them. It seemed to be the only thing that used them as a dependency was less-rails which I wanted to remove anyways.
My main question is what are they for and do I need them in the average application and if so why?
There are a few things for which a Rails app might use a Javascript Runtime:
1) The Rails Asset Pipeline requires a Javascript Runtime in order to perform Javascript compression.
2) Certain Rails ActionView Helpers like javascript_include_tag require a Javascript Runtime in order to execute Javascript functions.
Not every Rails application use those features, hence the reason that therubyracer gem is initially commented out in your Gemfile; Furthermore note that you could alternatively use NodeJS as your Javascript runtime.
For details, see:
http://guides.rubyonrails.org/asset_pipeline.html#javascript-compression
http://www.rubydoc.info/docs/rails/ActionView/Helpers/JavaScriptHelper
http://www.rubydoc.info/docs/rails/ActionView%2FHelpers%2FAssetTagHelper%3Ajavascript_include_tag
Proper "Rails" way to perform javascript on specific pages
rubyracer provides following features:
1. We can write your ruby codes inside js codes.
2. We can invoke the js functions from ruby codes.
3. Manipulation of javscript objects & the passing them to javascript functions.
Those are the few basic benefits of using rubyracer gem.
Please read complete details here

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'

Undefined method 'devise' when including User model outside Rails

I am developing an application that consists in two parts: A ruby command line application and a Rails application for the front-end.
I've been using ActiveRecord and the Rails models in the Ruby application by including them individually the following way:
Dir[File.dirname(__FILE__) + '/../../RailsApp/app/models/*.rb'].each do |file|
filename = File.basename(file, File.extname(file))
require_relative "../../RailsApp/app/models/" + filename
end
And by manually mantaining two nearly identical Gemfile files for dependencies (an approach that I think is not the best)
Yesterday I added Devise to the Rails application, and now when the ruby app tries to include the user model, the message undefined method devise for class <xxxxx> appears.
I added the devise gem in my ruby app's Gemfile, but the error continues. If I understand correctly, Devise is a Rails Engine, which is why it's not being loaded just by requiring the gem.
I read that a better approach to include Rails models inside another application is by requiring the environment.rb file, but when I tried that, I got an error with ActionMailer:
undefined method `action_mailer' for #<Rails::Application::Configuration:0xbbb54bc>
How can I solve the issue with devise? Is there a better way to include Rails models inside another application than the one I'm currently using to avoid mantaining two gemfiles ? Thanks
Even though this architecture must change in the near future, we managed to work around this error by including devise and its initializer in the ruby application.
# FIXME: Dependency needed in Rails but not in the engine.
require 'devise'
require_relative "../../RailsApp/config/initializers/devise.rb"
#Load all the models
You could try putting require 'devise' before you call the models, but I'm not sure if that would work?
I agree that this general architecture feels wrong.
I would extract the code that is common to both applications to a separate library and then include that library in both applications. Your easiest bet to do this is to make your own library into a Gem and then include it both Gemfiles. The common gems will be specified in the shared library. Making Gems is really easy. Good instructions can be found at http://asciicasts.com/episodes/245-new-gem-with-bundler (or the associated Railscast).
Your command-line app probably shouldn't need to care about Devise. If you need a User model in both, you can just include it in the shared library and then add any extra functionality (such as Devise) in the Rails app.
Does that help?
You could move the time of loading your models into the parent app initialization step, rather than at the time your gem is loading itself.
Build a shared_code gem, as your other answer suggests.
Move your shared_code loading loop into an initializer file (such as require 'user')
List the gem dependencies (like devise) of your models in the shared_code gemspec.
In your shared_code gem, include a generator to install the initializer file into apps that use the shared_code gem.
Then to use your shared code in the parent app,
Gemfile:
gem 'shared_code'
console:
rails generate shared_code:install
This is the same pattern devise and other gems use to configure themselves at the correct time in the initialization sequence of the parent application.
If you use require_relative on the devise initializer in the user.rb file itself, within the shared_code gem, you are removing the possibility that two different parent applications that are using your shared_code might need to initialize devise (or another gem that both the app and your gem depend on) differently.
Details on the generator:
You can create the shared_code:install command by creating the generator like this:
lib/generators/shared_code/install_generator.rb
module SharedCode
class InstallGenerator < Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)
def copy_initializer_file
copy_file "shared_code.rb", "config/initializers/shared_code.rb"
end
end
end
Put your model load code into lib/generators/shared_code/templates/shared_code.rb

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