We are writing a gem that includes multiple common gems used in a couple of our apps. We want to be able to have some kind of config to require or not require a certain gems. But the application.rb or enviroment.rb/*rb is loaded after gems is loaded. So we can not set a config there. The following way works but its not a good idea and I was wondering if there is a cleaner way to do this
bin/rails . (In consuming app)
#!/usr/bin/env ruby
DISABLE_TRANSPORT = true
APP_PATH = File.expand_path('../config/application', __dir__)
require_relative '../config/boot'
require 'rails/commands'
Gemfile (In consuming app)
gem 'fruit_chain'
fruit_chain/lib/fruit_chain.rb (Our gem)
require analytic
require transport unless defined?(DISABLE_TRANSPORT) && DISABLE_TRANSPORT
require marketing
...
module FruitChain
end
I'm not sure to understand what you want exactly... however, use require: false in the Gemfile may help you ?
# Gemfile
gem 'my_super_gem', require: false
And in an initializer you require them:
# config/initializer/fruit_chain.rb
require 'my_super_gem'
I've encountered the same problem and have only thought to include the gems explicitly as a responsibility of the app, and then injecting that gem into the parent gem through a configuration, and ensuring they conform to the same interface.
Related
My project was created using PostgresSQL but we really intend to use MongoDB, what should I change in order for that to happen?
A lot of scaffolding already took place, so we are trying to savage any of the work already done...
Remove database adapter gems from your Gemfile (mysql2, sqlite3, etc.)
Change your config/application.rb
Remove require 'rails/all' line and require frameworks you want to use, for example:
require "action_controller/railtie"
require "action_view/railtie"
require "action_mailer/railtie"
require "active_job/railtie"
require "action_cable/engine"
require "sprockets/railtie"
require "rails/test_unit/railtie"
NOTE: You should be using the require snippet from the rails/all.rb file that is current with your version of Rails without the active_record railtie. Here is a link to rails/all.rb on the Rails master branch.
Delete your config/database.yml file, db/schema.rb and migrations (if any)
Delete migration check in test/test_helper.rb
Delete any ActiveRecord configuration from your config/environments files
Better late than never! This should come in handy to somebody, someday!!!!
The following python-based migration framework does the job.
https://github.com/datawrangl3r/pg2mongo
Coming to the performance, the migration of each JSON object is dynamic and there shouldn't be any memory lock issues when you use the above framework.
The same situation I also I have to face . I am adding some additional points to above answer .
1) Create a file in initializers and put this code
Mongoid.load!(Rails.root.join("config/mongoid.yml"))
2) You have to remove from every model which are inheriting from ApplicationRecord and delete the application_record file.
3) If you have installed devise so you have to change
**From **
require 'devise/orm/active_record'
to
require 'devise/orm/mongoid'
4) If you are using carrierwave so in gem file you have to replace
From
gem 'carrierwave', github: 'carrierwaveuploader/carrierwave'
To
gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'
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'm in the process of upgrading an older Rails 3.2 application to 4.1 and am having an issue with prereqs in that I am having to add a "require" line to my application.rb file for each of my gems in order to get them to load.
This can't be right.. Previously devise, paperclip, pusher, rescue, rolify, stripe..etc would load and work perfectly without doing this. Now I have to add the following to the application rb file at top for them to work.
require 'devise'
require 'paperclip'
require 'pusher'
require 'resque'
require 'rolify'
require 'stripe'
Obviously I'm doing something wrong in my conversion. Thoughts? Why am I forced to add requires?
I have written other Rails 4.1 apps from scratch and never had to do this so I'm not sure why its making me do it here..
I'm also using RVM if that's possibly the issue?
Any help or thoughts you might have would be very much appreciated.
Yep that was it.
In Rails 4.0 they added the following to the application.rb file that wasn't in my 3.2 file.
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)
I want to add some functionality to the ActiveMerchant gem in order to test the PayPal Express gateway, a pull request has been attempted for this but was turned down on Github.
I want to add a single class to the ActiveMerchant Billing module:
module ActiveMerchant #:nodoc:
module Billing #:nodoc:
class PaypalBogusGateway < BogusGateway
# some codes here
end
end
end
I have done this successfully by downloading and pulling the gem into my project locally and trhowing my new file in there:
#Gemfile
gem 'activemerchant', '1.34.1', path: "vendor/gems/activemerchant-1.34.1", require: 'active_merchant'
But of course, that's not the best idea because I'll have to manually pull any updates if I want them.
Is there any way I can add the class to their module using their gem that's been pulled from the RubyGems source?
Thanks
EDIT
Putting it in the lib folder should work but my code requires some classes from the gem to inherit from, like:
require File.dirname(__FILE__) + '/paypal/paypal_common_api'
require File.dirname(__FILE__) + '/paypal/paypal_express_response'
require File.dirname(__FILE__) + '/paypal_express_common'
replacing File.dirname(FILE) with wherever the gem is installed... This will be different across server environments right?
Add activemerchant to the Gemfile, bundle install
In config/application.rb make sure lib is included in the autoload paths
# need to uncomment or add this to the configuration
config.autoload_paths += %W(#{config.root}/lib)
place your class in a file using nested directories to match the modules
# lib/active_merchant/billing/paypal_bogus_gateway.rb
do NOT include any require statements in your bogus gateway, rails (via bundler should require everything from the Gemfile)
restart rails
You might want to just fork the project on GitHub and add your changes to it. Even if it is just a single class. And then, in your Gemfile, do this:
gem "active_merchant", :git => "git://github.com/<your-user-name-here>/active_merchant.git"
What do you do when you want to use a gem for development/testing that you don't want to force other devs to use? Right now I have
begin
require 'redgreen'
rescue LoadError
end
in test_helper.rb and no gem config, but that seems like a clumsy approach, albeit a functional one. I'd like to do something like the following:
config.gem "redgreen", :optional => true
Any other suggestions? Or should I just vendor those pretty superficial gems...?
EDIT
To be clear, I am only talking about those specific gems, like redgreen, which aren't actually used in the functional code, but only in the coding process. There is no need to vendor these at all, except to avoid the conditional require.
Gems that are specific to your development environment should be installed in your gemset or local gems, but not in the Gemfile.
A classic example is the ruby-debug-base19x which Rubymine needs for debugging. This is installed in your local gemset, but not in the Gemfile because not all coders use Rubymine.
[EDIT]
Indeed, everything is run in the context of the bundle, and outside gems are not reachable. There do exist some workarounds indeed. Most of them are dirty :)
I found a lot of good solutions in this bundler issue.
The nicest solution was to add this to your .irbrc :
# Add all gems in the global gemset to the $LOAD_PATH so they can be used even
# in places like 'rails console'.
if defined?(::Bundler)
global_gemset = ENV['GEM_PATH'].split(':').grep(/ruby.*#global/).first
if global_gemset
all_global_gem_paths = Dir.glob("#{global_gemset}/gems/*")
all_global_gem_paths.each do |p|
gem_path = "#{p}/lib"
$LOAD_PATH << gem_path
end
end
end
require 'irb/completion'
require 'rubygems'
require 'wirble'
Wirble.init
Wirble.colorize
If you then install wirble to the global gemset, it can then be found.
Original source: https://gist.github.com/794915
Hope this helps.
I answered a similar question of my own here
User-level bundler Gemfile
One way to do this is to create different environments:
group :scott do
end
Then
bundle --with-env=scott
Ok, I think I've come up with something. Basically, the idea is to only execute a secondary Gemfile when a Rails app is executing. To do this we add two things:
First, we alter the rails script a little:
# in ./script/rails
Kernel::IN_RAILS_APP = true
APP_PATH = File.expand_path('../../config/application', __FILE__)
require File.expand_path('../../config/boot', __FILE__)
require 'rails/commands'
Second, we tell bundler to pull in the secondary Gemfile if we're in a rails app and a secondary file exists:
# Gemfile
if Kernel.const_defined?(:IN_RAILS_APP)
local_gemfile = File.dirname(__FILE__) + "/Gemfile.local"
if File.exists?(local_gemfile)
puts 'using local gemfile'
self.instance_eval(Bundler.read_file(local_gemfile))
end
end
Now you can add a Gemfile.local to your project and run specific gems on a per-machine basis. bundle install works normally since the IN_RAILS_APP constant doesn't exist.
** Make sure to add Gemfile.local to your .gitignore.
In my opinions this is what environments are for. Fortunately there is also a way provided to do it with what is in your Gemfile, this is also how rails use it: groups
Pretty much use the environments the same way rails use it. Here is what you could find in your Gemfile:
group :test do
# Pretty printed test output
gem 'turn', :require => false
end
And here is what you can find in your config/application.rb
Bundler.require(:default, Rails.env) if defined?(Bundler)
All you would need to do is to change your local environment settings and the others working with you won't be affected unless they decide to. Everything gets committed and nothing gets lost.
Here some links :
http://yehudakatz.com/2010/05/09/the-how-and-why-of-bundler-groups/
http://gembundler.com/groups.html
If you want it to be optional, it's better to freeze the gem as a plugin. However, it's not a good idea to use different gems than the rest of a development team, as it creates some inconsistencies in the codebase that can be hard to track down later. I would say add it to config.gem, and just tell the other developers to do:
rake gems:install
And you're done.
This is how I tackled the same problem under Rails 3.1. In my Gemfile:
if File.exists? './tmp/eric_dev_gems'
gem 'redgreen'
gem 'awesome_print'
gem 'wirble'
gem 'wirb'
gem 'hirb'
end
Create a file in ./tmp/ (or in some folder which is in your .gitignore) of your choosing. I used eric_dev_gems. This should be ignored by git, and will only exist on your system unless one of your teammates decides he wants to create that file too.
I solved it by putting this in my gem file:
$gem_names ||= ENV['GEM_PATH'].split(':').map{|g| Dir.glob("#{g}/gems/*").map{|p|p.split('/gems/').last}}.flatten
gem 'redgreen' if $gem_names.any?{|n| n=~/redgreen/ }
That way the gem will only be used if you manually installed it on your system.
This works well but has the downside that it puts the gem name in the Gemfile.lock. This is of little consequence because the gem does not get installed with bundle install but it does make your lock file a bit messy and can cause the lock file to change a bit from one developer to the next.
If that is an issue for you another option is to keep the gemfile clean and require the gem by its full path, or you can add the path for just that gem. Like this:
$gem_paths ||= ENV['GEM_PATH'].split(':').map{|g| Dir.glob("#{g}/gems/*")}.flatten
$gem_paths.grep(/redgreen/).each {|p|$LOAD_PATH << p+'/lib'}
require 'redgreen' rescue nil