Skip to content

Instantly share code, notes, and snippets.

@henrik
Last active October 5, 2020 13:34
Show Gist options
  • Save henrik/0d805593bd79246a7922 to your computer and use it in GitHub Desktop.
Save henrik/0d805593bd79246a7922 to your computer and use it in GitHub Desktop.
Make your Rails code notify you about those things you wanted to fix when you upgrade Rails.
# This middleware is probably no longer needed in Rails 4.
RailsVersions.revisit_this_on_rails_4
module RailsVersions
def self.revisit_this_on_rails_4
# Our unit tests may fake out Rails, so we guard against that.
return unless defined?(Rails::VERSION)
return if Rails::VERSION::MAJOR < 4
return if Rails.env.production?
raise "This code should be revisited on Rails 4!"
end
end
require "spec_helper"
require "rails_versions"
describe RailsVersions, ".revisit_this_on_rails_4" do
it "raises a helpful message on Rails >= 4" do
stub_const "Rails::VERSION::MAJOR", 4
expect {
RailsVersions.revisit_this_on_rails_4
}.to raise_error "This code should be revisited on Rails 4!"
end
it "does not raise on Rails < 4" do
stub_const "Rails::VERSION::MAJOR", 3
expect {
RailsVersions.revisit_this_on_rails_4
}.not_to raise_error
end
it "does not raise in production" do
stub_const "Rails::VERSION::MAJOR", 4
allow(Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new("production"))
expect {
RailsVersions.revisit_this_on_rails_4
}.not_to raise_error
end
it "does not explode if the Rails::VERSION constant is not defined" do
hide_const "Rails::VERSION"
expect {
RailsVersions.revisit_this_on_rails_4
}.not_to raise_error
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment