How to disable gem temporarily in Rails 3 using Bundler? - ruby-on-rails-3

I have discovered that the redgreen gem is incompatible with ISO-8859-1 encoding of test files. What is the best way to disable one gem temporarily to run certain test files?

My first advice would be to save those files in UTF-8.
Alternatively, I'd look into the grouping feature of Bundler.
In your Gemfile
group :utf8 do
gem 'redgreen'
end
And then require that group only when you want to use the redgreen gem.
Bundler.require(:utf8, :default, Rails.env)

Related

How to set sinatra-authentication to use erb instead of haml?

I'm trying to set up the sinatra-authentication gem in a simple sinatra app, and running into an issue where sinatra can't find the correct views. I understand that sinatra-authentication uses haml by default, but I'm using erb in this app.
This in mind, I found in the sinatra-authenticaiton docs that there is a setting which allows you to change the template engine, by adding the following to your app file:
configure do
set :template_engine, :erb # for example
end
I've added this to my app.rb file, and sinatra is still looking for the signup.haml when I try to hit the /signup route in my app.
A couple of notes:
I've included the gem in my Gemfile, and successfuly run a bundle install on my app.
source 'https://rubygems.org'
gem 'sinatra'
gem 'data_mapper'
gem 'pg'
gem 'dm-postgres-adapter'
gem 'sinatra-authentication'
I saw something in the documentation that suggested that I may need to specify the location of my view files, so I added the following to my configuration block.
set :sinatra_authentication_view_path, Pathname(__FILE__).dirname.expand_path + "views/"
**I think I've required the gem accurately in my app file by adding
require "sinatra-authentication"
use Rack::Session::Cookie, :secret => 'mys3cr3tk3y'
This gist is a current representation of my app.rb file in the root of my sinatra app. https://gist.github.com/rriggin/5378641#file-gistfile1-txt
Here is a screenshot of the error sinatra throws: http://cl.ly/image/0y041t0K3u3O
When I run the app locally, a 'dm-users' table is created in my local db as expected.
Is there another configuration setting that I'm missing in order to get sinatra-authentication to properly look for the erb templates rather than haml files. Any help would be greatly appreciated.
Thanks
The specs don't test that the template_engine setting works, and looking at the way the setting is called, I believe it's not correct, i.e.
send settings.template_engine, get_view_as_string("index.#{settings.template_engine}"), :layout => use_layout?
might better work as:
send app.settings.template_engine, get_view_as_string("index.#{app.settings.template_engine}"), :layout => use_layout?
that's what I reckon. If you fork the project, change the line and add it to your Gemfile and it works then consider writing a quick spec for it and you'll have improved the mainline of that project as well as fixed your problem.

rails 3 - LoadError (cannot load such file -- zip/zip)

I'm using rubyzip to zip a csv file so uses can download it. This works perfectly in development mode. But when I tried zipping the file on the production server (rackspace) I received the error: LoadError (cannot load such file -- zip/zip). Is it a path issue? Anyone know a fix?
The error is being called in my code on this line: require 'zip/zip'
I've tried the solution from here, but it didn't help.
I fixed this problem by specifying gem version 0.9.9 in Gemfile:
gem 'rubyzip', "~> 0.9.9"
Using rubyzip (1.0.0) caused an error.
When upgrading rubyzip to 1.0.0 change require 'zip/zip' to require 'zip'.
I had this problem after adding roo to a Rails project.
Roo needed the new interface, something else (some other gem) was using the old interface - so most of these answers didn't work (couldn't lower the version of rubyzip, rubyzip2 is deprecated, didn't have require zip/zip in my project).
What worked for me was cassio-s-cabral's answer referring to the rubyzip github page.
gem 'rubyzip', '>= 1.0.0' # will load new rubyzip version
gem 'zip-zip' # will load compatibility for old rubyzip API.
I had the same problem: error thrown on "require 'zip/zip'" code, and the solution from this post also did not help.
After a long research I found that the problem was that my "require 'zip/zip'" statement was done in a separate
lib/exporters/package_exporter.rb
file, and for some reason "require" statements are not handled in "lib" folder in production by default.
When I moved "require 'zip/zip'" to the beginning of my
app/controllers/packages_controller.rb
the problem was solved!
I had a similar issue with active_support, just added the 'zip' gem to my Gemfile and it worked fine
I'm use rubyzip2 gem to fix this problem
gem 'rubyzip2'
what work for me was to install 2 gems:
gem install rubyzip
gem install zip
and in the script put
require 'rubygems'
require 'zip/zip'
In their github page explains what to do.
Rubyzip interface changed!!! No need to do require "zip/zip" and Zip
prefix in class names removed.
If you have issues with any third-party gems what required old
version of rubyzip you can use next workaround:
gem 'rubyzip', '>= 1.0.0' # will load new rubyzip version
gem 'zip-zip' # will load compatibility for old rubyzip API.

no such file to load -- google_chart using gem gchartrb

I am trying to use the gem gchartrb to create some graphs/charts in my RoR application.
I have looked into several tutorial and all say the same thing, that I have to add
require 'google_chart'
But I am getting the message:
no such file to load -- google_chart
I have the require inside my controller, I have confirmed that the gem is installed.
I am using Rails 3.
Also, I have tried adding config.gem 'gchartrb', :lib => 'google_chart' in my environment.rb as suggested here but nothing changed
Thanks for your help
EDIT:
I have also tried with the gem googlecharts, what I have in my Gemfile is:
gem "googlecharts", :require => "gchart"
but I get no such file to load -- gchart when I try to load the view.
I am not sure, it is required now or not. But it worked for me in Rails 3 as well. I am using Rails 3.0.10. I added below 2 lines and it worked for me.
1) gem 'gchartrb' in Gemfile
2) require 'google_chart' in config/boot.rb
Hope it helps!
config.gem is for rails 2.3.X.
For rails 3, you will need to add the gem to your Gemfile and run gem bundle
You may also need to check that the google_charts gem actually supports Rails 3...
Given that the latest code update seems to have been in 2008 - that might not actually be likely. :(
You can try it anyway and see...

How do I make gemspec dependencies autoload in a Rails 3 app, using a Gemfile

I have a Rails 3 app that I am turning into a Rails engine / gem.
This engine has some gem dependencies that I have put inside it's .gemspec file.
I have created a new 'parent' Rails 3 app, and I would like to add my engine gem to the Gemfile and have the gem's dependencies automatically 'loaded', but this does not work for me! bundle install installs the gem dependencies fine, but when I start the server, the app crashes because they are not loaded.
For example, my engine's gemspec contains these lines:
s.add_runtime_dependency(%q<rails>, ["= 3.0.7"])
s.add_runtime_dependency(%q<acts_as_commentable>, [">= 3.0.1"])
s.add_runtime_dependency(%q<haml>, [">= 3.1.1"])
.. and the parent Rails 3 application has these lines in its Gemfile:
source 'http://rubygems.org'
gem 'my_engine', :path => "~/src/gems/my_engine"
But I get the following error:
undefined local variable or method `acts_as_commentable'
from /home/user/src/gems/my_engine/app/models/account.rb:66:in `<class:Account>'
But if I add gem 'acts_as_commentable', '>= 3.0.1' to the Gemfile of the parent Rails 3 app, then the gem is loaded and the error disappears.
I am using Rails 3.0.8.
Does anyone have any suggestions? Do I need to change something about the way my engine is loading?
During main Rails app boot, Bundler will only require dependencies directly listed in the Gemfile but not any sub-dependencies. It's your library's/Engine's responsibility to require its dependencies when it itself gets required. You can do so using initializers in your Railtie.
class MyRailtie < Rails::Railtie
initializer "require stuff" do
require "stuff"
end
end
In our Rails Engine we used a small trick to require dependencies automatically. Unfortunately you can't specify whether or not they should load in the .gemspec, which would allow for greater control.
Gem.loaded_specs["our_rails_engine"].dependencies.each do |d|
begin
require d.name
rescue LoadError => le
# Put exceptions here.
raise le if d.name !~ /factory_girl_rails/
end
end
I'm looking at Spree (the superhero of Rails Engines!), and they do this in spree_core-0.60.1/lib/spree_core.rb:
require "rails/all"
require 'state_machine'
require 'paperclip'
require 'stringex'
require 'will_paginate'
require 'nested_set'
require 'acts_as_list'
require 'resource_controller'
require 'active_merchant'
require "meta_search"
require "find_by_param"
So the answer is that within your gem, you have to require all of it's gem dependencies one by one. Well, that's how I will do it for now. But please comment if this ever changes in the future.
Seems it don't work, i create a host project and a sub-project with rails 3 engine.
Added the gem to engine's gemspec
s.add_dependency 'simple_form'
then added the require to engine_name.rb like below
require 'simple_form'
But if delete the line [gem 'simple_form'] in host project's Gemfile, it will show undefined immediatly

Rails 3 Initializers: No such file to load

I am trying to use the Sunlight API gem with a Rails project. I have installed the gem and can successfully use it from irb.
However, when I put the require statement (require 'sunlight') in sunlight.rb in config/initializers, I get the following error:
/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta3/lib/active_support/dependencies.rb:209:in `require': no such file to load -- sunlight (LoadError)
I checked the permissions on the gems directory, and it is world readable/executable.
Here is the code from sunlight.rb:
require 'rubygems'
require 'sunlight'
Sunlight::Base.api_key = 'bb7b775755054c54aa9715d202f6785c'
Can anyone tell me how to fix this? TIA!
Is sunlight listed in your Gemfile? Rails3 uses Bundler to manage Gem installations.