-
Notifications
You must be signed in to change notification settings - Fork 30
Handle apps without ActiveRecord #263
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
Conversation
@@ -29,7 +29,7 @@ class Server | |||
sig { params(model_name: String).returns(T.nilable(T::Hash[Symbol, T.untyped])) } | |||
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? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?
unless const && defined?(ActiveRecord) && const < ActiveRecord::Base && !const.abstract_class? | |
unless const && const.respond_to?(:columns) && !const.abstract_class? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?
^^^^^^^^^^^^^^^^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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}>}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, that's too bad. I thought there'd be a common interface for these. Thanks for looking into it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's move forward with the fix and consider support for other ORMs later
Fixes #262
I tried to add a test which undefines
ActiveRecord
:but it fails because of the ActiveSupport internals: