I have read previous threads on this topic, but the solutions I've found there have not solved my problem. I have a rails 3.2.9 application that does not do database access - it uses an HTTP Rest protocol to another application to access persistent data. The app runs OK in a local test environment with "rails server" using WEbrick, but fails to run with Nginx/Passenger with the error "* Exception LoadError in application (Please install the sqlite3 adapter: gem install activerecord-sqlite3-adapter (sqlite3 is not part of the bundle. Add it to Gemfile.))". From the stack trace it would appear that ActiveRecord wants to eagerly establish a database connection in code that executes prior to the request being processed. I have tried to follow the instructions to remove ActiveRecord from my dependencies with no luck. I generated with --skip-activerecord, which produced an application.rb like this as expected:
require File.expand_path('../boot', __FILE__)
# Pick the frameworks you want:
# require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"
There are no references to activerecord or active_record anywhere in my application except in the gemlock file created by bundler, in comments and in readme. Bundler reports ActiveRecord as a dependency, and 'bundle viz' reports rails itself as being the gem requiring ActiveRecord. Any suggestions or advice would be most welcome.
In response to eric's question, here is my Gemfile
source 'https://rubygems.org'
gem 'rails', '3.2.9'
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
As you have discovered Rails itself has ActiveRecord listed as a dependency in it's Gemspec. The setup you have archived so far is the standard way of removeing ActiveRecord from Rails. If you really want to go further and also remove the gem you will, most likely, have to fork the Rails gem and remove the dependency in it's Gemspec.
It might be that simple, but you might also find that there is additional glue code in Rails to tie ActiveRecord in and you will have to remove that as well. All in all I'm wondering if its worth it.
If you don't need ActiveRecord you have already prevented it from effectively loading. Some parts might still load, but most of it wont. The win in memory footprint/performance against the time you will spend removing a core Rails feature makes me wonder if you are not looking at the wrong framework for your needs?
If you have requirements that are so tight that Rails is still to heavy you should probably look at Sinatra or similar instead. You could also stick with Rails and do a custom Rack middleware stack to keep just the parts of the call stack that you need.
I hope this gave you some guidance to, if not a viably solution, some alternatives to fullfil the higher concerns, since there is no reason in it self to remove the ActiveRecord gem.
Related
I am developing a gem at the moment. Here's how the .gemspec looks like:
gem.add_dependency 'activerecord', '~> 4.2'
...
gem.add_development_dependency 'rails', '4.2.5'
...
and here's my Gemfile:
source 'https://rubygems.org'
gemspec
I am setting up my main file, lib/my_gem.rb like so:
module MyGem
module Lib
end
end
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
Bundler.require
However, if I start bundle console from my gem's folder, the dependencies are not required:
$ bundle console
Resolving dependencies...
irb(main):001:0> Rails
NameError: uninitialized constant Rails
...
irb(main):002:0> ActiveRecord
NameError: uninitialized constant ActiveRecord
...
What am I doing wrong?
I believe dependencies included via the gemspec command in the Gemfile are not automatically required by Bundler.require. Only gems listed directly in the Gemfile itself are.
Additionally, gems included in only certain Bundler groups, like 'development', may not be required by a Bundle.require even if they were included directly in the Gemfile, you need to tell bundler to require groups other than default.
You can always require gems manually though, like require 'rails'. Bundle.require doesn't do anything but require gem for each gem in your Gemfile (or at least default group in your Gemfile), it's doing any magic other than looking up all the gems in your Gemfile and requiring them. Bundle.require is considered by some to be a bad practice anyway, you should just require the dependencies you need in the files you need them, some say. Although Rails doesn't agree, and Rails apps have their own somewhat complicated way of auto-loading things.
But if you are in a Rails app, as your example dependencies suggest... why are you doing any require 'bundler/setup' or Bundle.require yourself at all though, instead of letting Rails boot process take care of it? Rails boot process will take care of requiring the Bundler groups you probably expect too (like the "development" group when in Rails.env == 'development').
You can use the bundler api yourself directly like you're doing, it's not too hard. But Rails ordinarily takes care of this for you, and if you are using Rails, rails probably already has done a Bundler.setup and Bundler.require as part of Rails boot process.
I want to use Coveralls.io for my gem Headhunter that I'm developing at the moment. The doc says, I should simply add
gem 'coveralls', require: false
to the project, but as far as I know, this isn't the right way to load gems within another gem. Instead, stuff like that should happen in the .gemspec file. So I tried to add it like this:
s.add_development_dependency('coveralls', '>= 2.0')
But this doesn't work - it breaks my gem's whole functionality:
$ rake
/Users/josh/.rvm/rubies/ruby-2.0.0-p353/bin/ruby -S rspec ./spec/headhunter/css_hunter_spec.rb ./spec/headhunter/css_validator_spec.rb ./spec/headhunter/html_validator_spec.rb
/Users/josh/Documents/Work/MuheimWebdesign/headhunter/lib/headhunter/css_validator.rb:6:in `<class:CssValidator>': undefined method `full_gem_path' for nil:NilClass (NoMethodError)
This is the file that breaks:
require 'net/http'
require 'nokogiri/xml'
module Headhunter
class CssValidator
VALIDATOR_PATH = Gem.loaded_specs['headhunter'].full_gem_path + '/lib/css-validator/'
So Gem.loaded_specs['headhunter'] isn't available anymore, no idea what's going on here.
What's wrong here?
I was wondering the same and I just got it working.
You need to add:
spec.add_development_dependency "coveralls", "0.7.0"
to your .gemspec (0.7.0 is the coveralls gem latest version as the time of writing this)
make sure to run bundle installsuccessfully
and add:
require 'coveralls'
Coveralls.wear!
to the beginning of your spec_helper.rb or test_helper.rb, before requiring anything else.
Hope this helps.
I have a ruby gem and I want to use the Hash.from_xml method in the gem that is included in rails active_support module. I have the below code in my gemspec:
gem.add_dependency 'active_support', '~> 3.0.0'
However, when I build and install the gem locally, run irb, require the gem, I am not seeing the methods from active support included?
Any suggestions on what I am doing wrong or how to debug? Thanks!
You need to require the methods you need from ActiveSupport; they aren't added by default.
As Yevgeniy mentioned in a comment, the way to do this is require 'active_support/all' if you need everything - or if, for example, you want only the Hash extensions then use require 'active_support/core_ext/hash'. Note that this usually doesn't go in the gemspec, but rather in whatever file your gem uses to set itself up.
Perhaps even better would be to require the required ActiveSupport files in the actual files needing them, but that's a matter of taste.
I'm developing a Rails3 engine application, and I want to use Haml for the views.
First, what I have done was to add this to the engine Gemfile:
gem "haml"
While I was testing my engine, it was working OK (I have used https://github.com/josevalim/enginex to generate the gem and test it with the dummy application).
My problems started when I tried to use the engine on a real Rails application. The application does not have gem "haml" on it's own Gemfile, and so it was not initializing Haml, so I was receiving template not found errors as it was not looking for the .haml views. I was thinking that by requiring Haml on the Engine it would be enought for it to be also required by the Rails application.
What I have done for now was to add a config/initializers/haml.rb on the engine with this code:
require 'haml'
Haml.init_rails(binding)
It's working now, but I'm wondering if this is really a good way to do it.
Why Rails is not calling Haml "init.rb" file and so initializing Haml correctly by just adding gem "haml" to the engine Gemfile?
Two things are necessary. First, in the .gemspec:
s.add_dependency 'haml', ['>= 3.0.0']
And in your lib/gem_name.rb:
require 'haml'
And then run bundle both inside the gem and app directories.
I think you will have to put haml in the engine gemspec as a dependency in order for bundler to install haml in the target application (and show up in its Gemfile.lock). Something like this:
Gem::Specification.new do |s|
s.add_dependency(%q<haml>, [">= 0"])
end
I just tested this out on one of my engines. Without the dependency in the .gemspec it did not install haml in the target app (did not appear in Gemfile.lock). After I added haml to the gemspec as a dependency, it does show up:
PATH
remote: /rails_plugins/mine/my_engine
specs:
my_engine (0.0.0)
formtastic
haml
inherited_resources
settingslogic
sqlite3-ruby
GEM
remote: http://rubygems.org/
specs:
#................
haml (3.0.25)
#................
If you are using jeweler, it will add the dependencies to the gemspec automatically based on what is in your Gemfile.. it even adds a developement_dependency if you have the group defined in your Gemfile. I have only looked at enginex briefly, so I don't know if it has a similar rake task to build the gemspec.
This might help clarify some things:
http://yehudakatz.com/2010/12/16/clarifying-the-roles-of-the-gemspec-and-gemfile/
I just created a new gem (using bundler) and want to add Active Record support. So I added s.add_dependency "activerecord", "~> 3.0" to my gemspec. Then I use Bundler.setup and Bundler.require and thought that I have access to Active Record now, but I haven't. I have to explicitly use require "active_record". Any idea why Bundler.require does not work for me in that case?
Firstly, if you're packaging a gem, do not use Bundler.require. Bundler.require is for apps not gems.
In .gemspec, specify the dependencies of your deployed gem.
In your Gemfile, include the line gemspec to automatically include the dependencies listed in your .gemspec in your Gemfile.
You may also optionally create gem groups for dev and test.
In your code, explicitly require any libraries you need.
I lost a couple of hours on this today so I hope this helps.
(Sources 1, 2)
Secondly, though the ActiveRecord gem is called "activerecord", the lib is called "active_record". This is what you would need in Gemfile.
gem 'activerecord', :require => "active_record"
Unless you include the :require option, ActiveRecord won't be loaded correctly and you won't know about it until you try to use it.
If you want use Bundler you need define your Gemfile with Activerecord
gem 'activerecord', "~> 3.0.0"
Or you need define bundler to use your gemspec with adding gemspec in your Gemfile
gemspec
See http://gembundler.com/rubygems.html
I had this problem, and the issue in my case was that I was naming a directory in my gem active record, as in:
lib ->
active_record ->
base.rb <- containing some monkey patches to base
This was causing mass confusion including sweet error messages like:
Gem Load Error is: uninitialized constant ActiveRecord::Base
Did you mean? ActiveRecord::Base
Simply moving changing the file from lib/active_record/base.rb to lib/active_record_base.rb fixed it for me.