71
loading...
This website collects cookies to deliver better user experience
activeadmin
sits very well with the relational DB using Rails activerecord
and for the projects that uses NoSQL DB like Mongo, there is another gem under the same umbrella - activeadmin-mongoid
(https://github.com/activeadmin/activeadmin-mongoid)Mongoid
based Rails projects mostly won't require activerecord
in their model methods and also wouldn't need any migration because we aren't dealing with the usual ideas of the SQL world.activeadmin-mongoid
as a gem also avoids to require Rails's activerecord
related elements. This is something to be considered when introducing any new additions to the gem. (atleast at the time of writing)activeadmin-mongoid
depends on the activeadmin
and holds the generator calls like rake activeadmin:install
to generate the initializer files. By default, the activeadmin generates configuration files and a db migration file as well.activerecord
related files. So, now my task is to do the exact thing what Nic did, but without letting the activerecord
getting required through any dependent calls or so.require 'active_admin/mongoid'
require 'generators/active_admin/install/install_generator.rb'
ActiveAdmin::Generators::InstallGenerator.class_eval do
# Make this a no-op so the AR migrations are not installed when ActiveAdmin-Mongoid is used.
def create_migrations;end
end
class_eval
.ActiveAdmin::Generators::InstallGenerator
from here helped me to understand that the current class is the one which requires activerecord
related files, but the parent class of this class doesn't requires any of those.ActiveAdmin::Generators::InstallGenerator
to do the following:When a class subclasses from it, I need to change the way the subclass is described.
Then either override the method definition or disallow it from overriding.
inherited
which will be called when a class is used by another class to derive itself. Using that inherited
you can control specific way of the derived class. Cool, isn't it? I even tweeted about it here https://twitter.com/nav_devl/status/1405329547701096452create_migrations
method. But turns out, when the self.inherited was called, there are no user defined methods present. Only after the self.inherited is finished the the class implements all other user definied methods. 😔method_added
and this is called every time a new method method definition is added to a class. Now can you see where we are heading towards?self.inherited
and self.method_added
🤯create_migrations
in the parent class.self.method_added
's definition. Classy, right? 😎ActiveAdmin::Generators::InstallGenerator
we are redefining the self.method_added
of that subclass to remove the method definition of create_migration
remove_method
will remove only the method definition scoped to that class, so our derived class will now lookup to parent class for definition when create_migration
is called.Rails::Generators::NamedBase.class_eval do
def create_migrations
end
def self.inherited(klass)
super
if klass.name == "ActiveAdmin::Generators::InstallGenerator"
klass.class_eval do
def self.method_added(method_name)
super
remove_method method_name if method_name == :create_migrations
end
end
end
end
end