Merged
Conversation
vinistock
reviewed
Feb 20, 2024
| def resolve_database_info_from_model(model_name) | ||
| const = ActiveSupport::Inflector.safe_constantize(model_name) | ||
| unless const && const < ActiveRecord::Base && !const.abstract_class? | ||
| unless const && defined?(ActiveRecord) && const < ActiveRecord::Base && !const.abstract_class? |
Member
There was a problem hiding this comment.
We might be able to support other backends (like Mongoid) if we shift this to check for features rather than inheritance. Would something like this work?
Suggested change
| unless const && defined?(ActiveRecord) && const < ActiveRecord::Base && !const.abstract_class? | |
| unless const && const.respond_to?(:columns) && !const.abstract_class? |
Contributor
Author
There was a problem hiding this comment.
It looks like we can't assume the existence of those methods:
irb(main):003* class Post
irb(main):004* include Mongoid::Document
irb(main):005* field :title, type: String
irb(main):006* field :body, type: String
irb(main):007* has_many :comments
irb(main):008> end
=>
#<Mongoid::Association::Referenced::HasMany:0x0000000116e39c18
@extension=nil,
@module_path="",
@name=:comments,
@options={},
@owner_class=Post,
@polymorphic=false,
@validate=true>
irb(main):009> Post.columns
(irb):9:in `<main>': undefined method `columns' for Post:Class (NoMethodError)
Post.columns
^^^^^^^^
irb(main):010> Post.abstract_class?
(irb):10:in `<main>': undefined method `abstract_class?' for Post:Class (NoMethodError)
Post.abstract_class?
^^^^^^^^^^^^^^^^
Contributor
Author
There was a problem hiding this comment.
The closest match would be fields:
irb(main):011> Post.fields
=>
{"_id"=>
#<Mongoid::Fields::Standard:0x000000011783f820
@default_name="___id_default__",
@default_val=#<Proc:0x0000000116fbe160 /Users/andyw8/.gem/ruby/3.2.1/gems/mongoid-8.1.4/lib/mongoid/fields.rb:137 (lambda)>,
@label=nil,
@name="_id",
@options=
{:default=>#<Proc:0x0000000116fbe160 /Users/andyw8/.gem/ruby/3.2.1/gems/mongoid-8.1.4/lib/mongoid/fields.rb:137 (lambda)>,
:pre_processed=>true,
:type=>BSON::ObjectId,
:klass=>Post},
@pre_processed=true>,
"title"=>#<Mongoid::Fields::Standard:0x0000000117838fc0 @default_val=nil, @label=nil, @name="title", @options={:type=>String, :klass=>Post}>,
"body"=>#<Mongoid::Fields::Standard:0x00000001178387a0 @default_val=nil, @label=nil, @name="body", @options={:type=>String, :klass=>Post}>}
Member
There was a problem hiding this comment.
Ah, that's too bad. I thought there'd be a common interface for these. Thanks for looking into it.
vinistock
approved these changes
Feb 20, 2024
Member
vinistock
left a comment
There was a problem hiding this comment.
Let's move forward with the fix and consider support for other ORMs later
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #262
I tried to add a test which undefines
ActiveRecord:but it fails because of the ActiveSupport internals: