how can we include installed gem in controller in RoR? - ruby-on-rails

I have installed gem successfully.But how can we use use in controller?

Look inside of config/environment.rb. Inside of the Rails::Initializer.run block you should see a commented-out note that describes using config.gem.
You want to add the gem that you need with that method, like this:
config.gem "foo"
There are other options that you might need, depending on what gem you are trying to use. Mention what it is, and I can be more specific.
Also be sure to read the docs for the gem method.

Add require 'gem' to top of controller or specific method.
Sample use of rubyzip gem for ex
def zip(data, filename)
require 'zip/zip'
require 'zip/zipfilesystem'
zipfile = "/tmp/rubyzip-#{rand 32768}"
Zip::ZipOutputStream::open(zipfile) do |io|
io.put_next_entry(filename)
io.write data
end
zippy = File.open(zipfile).read
File.delete(zipfile)
zippy
end

Related

Rails: How to use initializer code inside gem

I'm using the okcomputer gem for a Rails app (All you need to do to use this gem is to place some code in an initializer).
I'd like to wrap this gem inside a custom gem that can be used without an initializer. I hope to be able to use this gem in several dockerized microservices just by installing it.
I read that it's possible to put initializer code in an init.rb file at the root of the gem. In my case, that doesn't seem to be working (the routes generated by okcomputer are not found), but I'm not sure where the issue lies.
In general, can I expect code in init.rb to behave like code in an initializer?
Your best approach is probably to create a Railties and run your code in an after_initialize hook.
module Gemname
class MyCoolRailtie < ::Rails::Railtie
config.after_initialize do
OKComputer::Registry.register "resque_critical",
OKComputer::ResqueBackedUpCheck.new("critical", 10)
end
end
end
https://api.rubyonrails.org/classes/Rails/Railtie.html

Rails bootstrap gem monkeypatching method not working

I'm using the excellent twitter-bootstrap-rails gem. There is a helper within that gem (NavbarHelper) which is used to generate Bootstrap navbars with a Ruby helper. I want to monkey patch the gem such that the dropdown lists won't have carets.
So, I looked into the source and found the relevant method here. All I have to do is override it. I created a new file in config/initializers called navbar.rb with the following content:
NavbarHelper.module_eval do
def name_and_caret(name)
"HELLO WORLD"
end
end
Presumably, all of the dropdown titles then should be rendered as "HELLO WORLD" in my app (as referenced by the gem source). However, this is not occurring, and the gem does not appear to be monkeypatched at all.
I tried placing puts NavbarHelper.methods - Object.methods in the initializers file, and there were no results, which makes me think that Rails is not loading the gem correctly before the initializers. I have also checked and verified that the gem is not using autoload for its helpers.
Edit
What may be complicating this is the fact that my Gemfile includes the gem in the following manner:
gem 'twitter-bootstrap-rails', git: 'git://github.com/seyhunak/twitter-bootstrap-rails.git', branch: 'bootstrap3'
I'm not sure if this specific versioning means the monkeypatching doesn't work.
Edit #2
It seems there is only one version of the gem on my system, so I don't think that's the issue. Also, I have tried placing require 'twitter-bootstrap-rails at the top of the initializers file, with no results.
The problem is that you patch the method on this module but the module already got included at this point. Try to define this in your application_helper.rb
def name_and_caret(name)
super("blub #{name}")
end

Using Datamapper with existing rails application

I have an existing Rails 3 application using ActiveRecord, and I want to switch to Datamapper. The instructions given in the dm-rails page only talk about creating a new application. Does anyone know how to throw away all activerecord dependancies and migrate to datamapper?
Thanks!
It's realtively straightforward, but there are a couple of things you need to do.
In your Gemfile, remove "rails" and instead require the following.
gem 'activesupport', RAILS_VERSION, :require => 'active_support'
gem 'actionpack', RAILS_VERSION, :require => 'action_pack'
gem 'actionmailer', RAILS_VERSION, :require => 'action_mailer'
gem 'railties', RAILS_VERSION, :require => 'rails'
Where RAILS_VERSION is the version of Rails you want to use (e.g. ~> 3.1). This is basically all of rails except ActiveRecord.
At the top of config/application.rb, remove the require for rails (I forget what the original require looks like) and replace it with specific requires for the railties you need.
require "action_controller/railtie"
require "action_mailer/railtie"
I think the only other one is a Test::Unit railtie, but we're not using Test::Unit, so we don't include it.
Finally, if you want to use the identity map (I suggest you do, but it's not needed), place in your ApplicationController's class body:
use Rails::DataMapper::Middleware::IdentityMap
That should be everything; the rest is just configuring your database.yml according to the README (it's pretty much cross-compatible with a standard rails one anyway).
For reference, take a look at what the generators does:
-zsh$ curl http://datamapper.org/templates/rails.rb
apply 'http://datamapper.org/templates/rails/gemfile.rb'
apply 'http://datamapper.org/templates/rails/application.rb'
If you look at the contents of those two files you'll see the extra stuff you'd get if you had used the generator.

Base/Home directory of the gem

As part of the RSpec, i need to reference a file contained in a gem I am depending on (call it gem foo). I control gem foo, so I can make changes to it as needed.
Gem 'foo' contains a file that I need to reference with in the a rspec spec. Is there a reasonably stable RubyGem or Bundler API to figure out 'foo' base directory?
Assuming 'foo' is already required in my Gemfile:
in Gemfile:
gem 'foo'
I want to do something like this (in something_spec.rb):
filename = File.expand_path('examples/xml/result.xml', Gem.gem_base_path('foo'))
What is gem_base_path API call?
I would recommend creating a function in your gem to do this:
module Foo
class Configuration
def self.result_xml_path
File.realpath("../examples/xml/result.xml")
end
end
end
You can then do the following in your spec:
filename = Foo::Configuration.result_xml_path
This is much safer since you are getting all the information from the gem. It also looks cleaner.
This may do what you need without ant need to touch the 'foo' gem:
matches = Gem::Specification.find_all_by_name 'foo'
spec = matches.first
filename = File.expand_path('examples/xml/result.xml', spec.full_gem_path)
I have used this code to make something similar to what you need (namely loading in my specs some factories defined in a gem used by my project)

What's the best way to use yajl-ruby with my Rails project?

Rails 2.3.6 started using the fast new json library, yajl-ruby, "if available".
In the "JSON gem Compatibility API" section of the yajl-ruby readme it outlines a method to just drop in yajl-ruby inclusion and have the rest of the app seamlessly pick it up.
So, ideally, I'd like
Rails to use it
My gems to use it
My application code to use it
What's the easiest way to achieve this? My guess:
config.gem 'yajl-ruby', :lib => 'yajl/json_gem'
As the very first gem in environment.rb. Doing this doesn't result in any errors, but I'm not sure how to know if rails is picking it up for its own use.
Thanks!
John
I'd recommend using yajl-ruby's API directly instead of the JSON gem compatibility API mainly for the reason that the JSON gem's to_json method conflict with ActiveSupport and has had long-standing issues making them work together.
If you just do config.gem 'yajl-ruby', :lib => 'yajl' instead, you'll need to use Yajl::Parser and Yajl::Encoder directly to parse/encode objects. The advantage of this is you'll be certain there won't be any conflicts with method overrides and as such, be guaranteed your JSON encoding/parsing code will work as expected.
The disadvantage is if you're using any gems that use the JSON gem, they'll continue to do so but you're own code will use yajl-ruby.
If you wanted to, you could use your config.gem line, then in an initializer require 'yajl' so you'd have both API's loaded. The yajl/json_gem include will override anything that's using the JSON gem with yajl - to ensure this overrides those methods try to make sure require 'yajl/json_gem' happens last.
If you're using Rails 3, you can add this to an initializer:
ActionController::Renderers.add :json do |json, options|
json = Yajl.dump(json) unless json.respond_to?(:to_str)
json = "#{options[:callback]}(#{json})" unless options[:callback].blank?
self.content_type ||= Mime::JSON
self.response_body = json
end
To make sure render :json => ... calls use yajl-ruby as well.
Sorry if this isn't really answering your question but I wanted to at least give the suggestion of using yajl-ruby's API directly :)

Resources