Update Gem to Work on Rails 5 and 6

February 1, 2021

I've had to update some gems to run on both Rails 5 and 6. Some apps are still on Rails 5 and new apps are being built on Rails 6 that need the same functionality. These are the steps I took. No guarantee this will work for all gems, but it worked for the 3 I've updated.

In the gemfile, add an option to specify the version of Rails to use. Also, make sure Rails is part of the gemfile.

                    rails_version = ENV["RAILS_VERSION"] || "default"

                    rails = case rails_version
                    when "master"
                    {github: "rails/rails"}
                    when "default"
                    "~> #{rails_version}"

                    gem "rails", rails

In the gemspec file, modify the version of rails allowed

                    s.add_dependency "rails", ">= 5.0", '< 7.0'

If the gem has a config/application.rb file, modify the Rails version

                    config.load_defaults Rails::VERSION::STRING.to_f

If Travis CI is being used, add the 2 versions under env in .travis.yml so tests are run on both versions

                        - "RAILS_VERSION="
                        - "RAILS_VERSION=6.0.3"

Run the gem's tests with a specific Rails version

                        RAILS_VERSION=6.1.1 bundle exec rake