Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't require rails and check for rails defined #319

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

TSMMark
Copy link

@TSMMark TSMMark commented Aug 1, 2022

Hi, this gem is awesome! So awesome that I wanted to use it in vanilla Ruby projects as well, and not only Rails projects.

This PR resolves all of the issues I faced when running this outside of a Rails-app environment.

Thank you!

@TSMMark
Copy link
Author

TSMMark commented Aug 1, 2022

Damn, so some other gems required by this gem also define a Rails module... E.g. https://github.com/rails/rails-html-sanitizer/blob/55818711cce9e7d7185a116abe7b0cada7414785/lib/rails-html-sanitizer.rb#L6-L7

This is really unfortunate because many gems check to see if we're in a "Rails app" by checking defined?(::Rails). So defining a module Rails just to nest modules & classes under it makes this check meaningless and it always seems like the codebase is a "Rails app". I'm going to try something else like monkeypatching defined?(::Rails) to always return false or something.

EDIT: defined? is an operator and not a method — so monkeypatching it won't work. Any suggestions?

@TSMMark TSMMark closed this Aug 1, 2022
TSMMark added a commit to Vydia/redshift_simple_migrator that referenced this pull request Aug 1, 2022
@TSMMark
Copy link
Author

TSMMark commented Aug 1, 2022

Alright so I was able to get this working properly by forking 3 other gems and fixing their defined?(Rails) checks just like this one.

E.g. defined?(::Rails::Engine), defined?(::Rails::Railtie) etc.

@TSMMark TSMMark reopened this Aug 1, 2022
@@ -6,7 +6,7 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }

gemspec

gem "rails", github: "rails/rails"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gem needs to continue being tested with Rails, so this can't be removed.

@@ -50,7 +50,7 @@ def guess_the_first_application_binding
@bindings.find do |binding|
source_location = SourceLocation.new(binding)
source_location.path.to_s.start_with?(Rails.root.to_s)
end
end if defined?(Rails.root) && Rails.root
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this change should be made like this. We should be configuring the application path, and using the Railtie we can set it to Rails.root

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rails.root is not a method in my case. Would you prefer just checking for the method

Suggested change
end if defined?(Rails.root) && Rails.root
end if defined?(Rails.root)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. I'd prefer if this class didn't call Rails.root at all, and instead had a application_root configuration that could be set by this gem Railtie to ExceptionMapper.application_root = Rails.root

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants