How to Share the Factories That I Have in a Gem and Use It in Other Project

How can I share the factories that I have in a GEM and use it in other project?

An alternative to require-ing each factory file as suggested in the other answer, is to update the FactoryBot.definition_file_paths configuration.

In your gem defining the factories:

Create a file which will resolve the factory path:

# lib/my_gem/test_support.rb

module MyGem
module TestSupport
FACTORY_PATH = File.expand_path("../../spec/factories", __dir__)
end
end

In you app / gem using the factories from the other gem:

# spec/spec_helper.rb or similar

require "my_gem/test_support"

FactoryBot.definition_file_paths = [
MyGem::TestSupport::FACTORY_PATH,
# Any other paths you want to add e.g.
# Rails.root.join("spec", "factories")
]

FactoryBot.find_definitions

The advantage of the definition_file_paths solution is that other functionality like FactoryBot.reload will work as intended.

How do I build my own gem and use it in another project?

What about trying this:

require "rubygems"
require "bundler/setup"

instead of just rubygems?

Tar a bundler project and send to another system

Received some help from a coworker, the gist is I was doing it all wrong.

If you want to transport gems and see consistent performance with bundler do this.

  1. bundle install <gem>
  2. bundle package

    • This creates a cache of the downloaded gems for transport.
  3. tar -czf ./transport.tar.gz ./Gemfile ./Gemfile.lock ./vendor

On the next machine:

  1. tar -xzf ./transport.tar.gz
  2. bundle exec <command>

How to deal with different gem dependencies within Bundler for scripts within a single Rails project?

I've been contemplating a similar problem, and although I don't have a solution in use anywhere yet, your question did make me think it out some more. I think you should be able to use a group to accomplish this. You can add something like this to your Gemfile:

group :workers do
gem "extra_gem_1"
gem "extra_gem_2"
end

Then, you can call

Bundler.require(:default, :workers)

and that should load your gems. How this works will depend on your setup, you might be able to add logic to config/application.rb, or you might need to do this elsewhere. This might be hackish, but it works in the console anyway.

When installing your gems, you can call:

bundle install --without workers

to exclude those gems from production.

Alternatively, you can use two Gemfiles, but that seems like a mess as well since presumably there's some crossover.

Making a Ruby project that is not a gem (or some other kind)

You have to actually try to build a gem so this is easy!

to use bundler without Rails, a gem, whatever just create a directory

  mkdir my-non-gem-project
cd my-non-gem-project

install bundler

  gem install bundler

and initialize your Gemfile

  bundle init

that will create a Gemfile for you and you can add to it and run bundle to install the dependencies from it

The simplest way to use bundler in your project would then be to open your main app file and add

require 'bundler/setup'
Bundler.require

This will require all of the gems you have in your Gemfile in the file this is added to. I am pretty sure that this file must be in the same directory as your Gemfile. More information here

Have fun with your Ruby project!



Related Topics



Leave a reply



Submit