ruby-on-railsrubyinheritancesuperclass

Ruby on Rails Admin- Is there a way to consolidate reuse an application controller class inside its paired admin controller class?


I have two controllers with the same class name. One inherits from Application Controller and the other inherits from the AdminController (for nondisclosure reasons lets call it Sample Controller):

class SampleController < ApplicationController
  def sample_method
    //do stuff
  end
end
module Admin
  class SampleController < AdminController
    def sample_method
      //do the same stuff as the above controller's method
    end
  end
end

Is there a way to make the regular controller (the first non admin) a super to the Admin Sample Controller?

I can't find any "double inheritance" examples on the internet so far. I also don't want to create duplicate code for the functions that will perform the same tasks for the Admin version of SampleController.

Hope this makes sense. Please excuse any obvious ignorance here - I'm a self taught ruby programmer :-)


Solution

  • So for a start, no, you don't have two classes with the same name. One's named SampleController and the other's named Admin::SampleController.

    Then no, ruby doesn't support multiple inheritance, HOWEVER you can move the common functions to a "mixin" module and include that in both controllers. See more here