-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Mongoid support #2714
Comments
Firstly, let's distinguish between A) "Gracefully-degraded support" (most features work, those that don't are omitted, AR is not forced to load) and B) "Full support" (all features work identically on Mongoid and AR). I imagine most in Mongo community including are quite happy with having only (A), and if anyone needs a given feature they can submit a PR. Kaminari - Kaminari gem works as-is for Mongoid. Have used it in production w/ Mongoid for 6 months. Pagination works equally well in both Mongo and SQL. Count Queries - No issue with using count on mongo. (Full disclosure: there was an issue on mongoDB 2.3 where count was slow, it has now been patched). Ransack - I propose to omit Ransack search for mongo (just show a placeholder in the UI "Search not yet supported on Mongo"). I myself don't need search in ActiveAdmin, so I won't be working on this. As I have said (many times before 😄) my only hard requirement here is that AA and it's dependencies should not force AR to load. Currently this PR is still outstanding: activerecord-hackery/polyamorous#3 |
Yeah I imagine that all features centered around associations wouldn't have a nice analog in the Mongoid world. Is there anything you can think of that would need to be disabled? Related: how would we determine the model's attributes for the default index, show, and edit views?
If that's the course we take, it'd certainly be good to show something like that in development, but in production the filter sidebar should probably be hidden so as not to confuse users.
Why don't we make Ransack a conditional dependency? #2513 would be useful here. While I have you here, is there a Mongoid equivalent to this code? User.reorder('email asc').uniq.pluck 'email' |
Actually -- Mongoid supports the same associations which AR does, although mongoDB itself doesn't support table joins. Suppose
Agreed.
Ransack is not the problem--we've fixed it already here: activerecord-hackery/ransack#296. The problem is Polyamorous, namely whenever it appears in your Gemfile, its gem initializer routine calls
|
Cool, I'll have to look more into using associations with Mongoid.
I guess that's been the Rails community's standard approach to work around the fact that Bundler doesn't support conditional dependencies. I would love to see that type of feature in either Bundler or Active Admin, but the community already has a standard and it seems that polyamorous should follow it. I have no special power over @ernie, so all we can do is pester him 🐱 (note that he can't dedicate much time anymore) |
Haha, thanks @ernie :-) |
I've been working on using Ransack for advanced sorting: #2608 What will we do for Mongoid? |
Thanks @ernie! |
Glad I could inspire something :) One thing to note is mongoid has two different types of relations: Embedded, which embeds the child model/document into the parent document, so when called it only needs to do one query and all children are available. Referenced, which is similar to AR joins, except does two queries like @johnnyshields mentioned. |
I do not know the particulars of Ransack, however, one way to do it would be:
In general you should assume that Mongoid behaves like ActiveRecord. There will be small differences in syntax, however, for every AR method there is a 1:1 functional equivalent in Mongoid, and like AR the query methods of Mongoid are chainable. |
One nice thing about this approach that, since we are dealing purely in chainable query objects, you can partially omit functionality for Mongoid but still forward the query object downstream. class Ransack::ActiveRecord::Sorter
# returns the ordered relation
def apply_sort(scope, order)
scope.reorder("#{order.field} #{order.direction}")
end
end
class Ransack::Mongoid::Sorter
# can't be bothered to implement this function on Mongoid...
def apply_sort(scope, order)
scope
end
end |
Here's a spec that I assume Greg Bell wrote up a long time ago, that was in the wiki. I'm going to remove it from the wiki since it's not helping anyone over there. Resource RegistrationWhen a resource is registered, it should instantiate the correct adapter for the given backend. So if an The type could also be explicitly passed in. For example: ActiveAdmin.register User, :type => :mongoid Abstract AdapterAdapters will have to support the following:
Libraries can register a new AbstractAdapter subclass and should also be able to specify some logic for when it get's used. For example: class MyCustomAdapter < ActiveAdmin::ResourceAdpater::Abstract; end
ActiveAdmin::ResourceAdpater.register :my_custom, MyCustomAdapater do |resource_class|
# This block get's run if the user does not explicitly pass
# in the :type option. Return true from it if your adapter should be used
resource_class.ancestors.include?(ActiveRecord::Base)
end Abstract Adapter Feature SupportActive Admin supports many features which may not be supported by all ORMs. Initially, the sidebar filtering comes to mind. ORMs should have a way of letting the system know if certain features are enabled on them. For example: class SimpleAdapter < ActiveAdmin::ResourceAdapater::Abstract
def supports_filters?
false
end
end
ActiveAdmin.register SomeObj, :type => :simple_adapter do
filter :name #=> raises an exception since this feature is not support
end |
This is great news to have official mongoid support in AA @johnnyshields, why not this instead? class Ransack::ActiveRecord::Sorter
def apply_sort(scope, order)
scope.reorder("#{order.field} #{order.direction}")
end
end
class Ransack::Mongoid::Sorter
def apply_sort(scope, order)
scope.order("#{order.field} #{order.direction}")
# or:
# scope.order(order.field.to_sym => order.direction.to_sym)
end
end |
@fred agreed, that would be fine. I was just giving an example of the "lazy approach", i.e. implement the logic in AR but have a dummy implementation in Mongoid that would not cause an error. |
class SimpleAdapter < ActiveAdmin::ResourceAdapater::Abstract
def supports_filters?
false
end
end That should be more granular, not every Adapter support all filters but the most support the basics! class SimpleAdapter < ActiveAdmin::ResourceAdapater::Abstract
def supported_filters
{
relational: [],
simple: [:contains, :lteq, :gteq, :equal]
}
end
end Or the Adapter has a class SimpleAdapter < ActiveAdmin::ResourceAdapater::Abstract
def filter_supported? type
....
end
end |
@johnnyshields, a minor note:
AR actually has the same behavior as described for Mongoid. In AR 3.2, Either way, |
@jordansexton thanks for correcting me, it's been ages since I've used AR. Switched to Mongoid and never looked back! |
It's great to have official support. Where can I follow on this progress? Is there a branch where I can test it from? |
how about this feature now? I can't find any doc for this. |
I am still using elia/activeadmin-mongoid with a few patches. |
ransack has now a open (working) PR for mongoid support activerecord-hackery/ransack#407 |
Ransack PR for mongoid support has been merged, please give a shot everyone 👍 |
So how do I try it? |
@pencilcheck You can try this gemspec: gem 'mongoid', '~> 4.0.0'
gem 'ransack', github: 'activerecord-hackery/ransack'
gem 'activeadmin', github: 'activeadmin'
gem 'activeadmin-mongoid', github: 'Zhomart/activeadmin-mongoid', branch: 'ransack' |
@Zhomart can you help us to improve the mongoid support in activeadmin himself? |
@timoschilling Sure. Is there any active implementation of mongoid support in activeadmin going on? |
@Zhomart we have no active work on this. We have lib/active_admin/orm and some |
The branch Try gem 'activeadmin', github: 'Zhomart/active_admin', branch: 'mongoid-old' |
@Zhomart This works. Thank you so much for your help and your work on Mongoid support! Thanks also to @timoschilling for incorporating this into Activeadmin. I'm very glad I can finally use it with Mongoid! |
Thank you all for the awesome work on this! I just updated to:
and everything seems to work except for sorting. I'm noticing something funny about the query:
It looks like it's including the model name and some extra quotes. I believe it should just be Edit: |
@Zhomart we're using your mongoid-old branch and sorting doesnt appear to be working at all? Is this a known issue or should I spend some time investigating? |
@awsmsrc thanks for the report. I've temp. fixed it on the mongoid-old branch. |
Thanks @Zhomart what is the difference between mongoid and mongoid-old? which should i be using? |
@awsmsrc mongoid-old is the my first attempt to integrate mongoid to activeadmin using "defined?" everywhere; and comments doesn't work. |
Should I use Zhomart mongoid fork or is activeadmin supporting mongoid now? |
I'm currently using this temprorary solution: # Try the one after EDIT:. This one is too old.
gem 'activeadmin', github: 'Zhomart/active_admin', branch: 'formtastic2'
gem 'activeadmin-mongoid', github: 'Zhomart/activeadmin-mongoid', branch: 'ransack-mongoid' I've stuck on design problem while adding mongoid to the activeadmin, as it was initially designed to use only one ORM. EDIT: Try this one: gem 'ransack', github: 'Zhomart/ransack', branch: 'mongoid'
gem 'activeadmin', git: 'git@github.com:Zhomart/active_admin.git', branch: 'mongoid-old'
gem 'formtastic'
gem 'formtastic-bootstrap', '~> 3.0.0' |
Cool, thanks. It's a shame that's as far as we can do for now. :\ EDIT It has some issues when I setup with a brand new rails project. I will try a different repo then. Thanks. |
@pencilcheck did you ever get it working with your Rails project? What version of Mongoid were you using? |
Nice! |
Any update on this? Is ActiveAdmin supporting mongoid with rails 4? |
How about rails 5? |
@varyonic Neat! |
Anyone know of a workaround for Rails 4 + Mongoid 5? I'm getting a "undefined method 'connection'" error which I assume is because of ActiveAdmin current incompatibility with mongo. The solutions provided here (specifically for Rails 5 and Mongoid 6) gives me gem dependencies problems. |
Try this branch and open any issue with Elia and Nic here |
Any further issues or suggestions should be directed to activeadmin-mongoid. We may reopen this or other issues related to better activeadmin-mongoid integration when ready. |
Inspired from a conversation in IRC with @machonemedia, I actually read the source code of @elia's activeadmin-mongoid gem and was surprised by how little code was actually there. I've never used Mongoid, but it's about time that Active Admin supported a different ORM.
Now in this ticket I'm not looking for +1s; #26 has already made the desire for this clear. Instead I want to identify the hurdles in the way for Active Admin to natively support Mongoid, and what we can do to get from here to there.
There are obviously changes (like #1713) to be made to Active Admin, but I'm more concerned about our dependencies:
Kaminari
Do things like #2638 apply to huge MongoDB data sets?
Ransack
Ransack doesn't currently support Mongoid, and it may never happen. There's an in-progress attempt at adding that functionality, but I don't know how far that's going to go.
I've set the 1.0.0 milestone to February, so the question is can we get a compatibility layer for Ransack 90% done in that time? If that's a dead end, what other options do we have?
CC @johnnyshields, @kristianmandrup
The text was updated successfully, but these errors were encountered: