Search code examples
refinerycms

How to extend refinerycms controller with decorators?


I have generated an extension called Doctors.

I'm trying to extend the default functionality with a decorators using the following the following guide http://refinerycms.com/guides/extending-controllers-and-models-with-decorators.

decorator:

Refinery::PagesController.class_eval do

    before_filter :find_doctors, only: [:doctors]

    protected

    def find_doctors
      @find_doctors = Refinery::Doctors::Doctor.all
    end      

end                                                

if I replace [:doctors] for [:home] I can see the objects in the homepage but I want to show the items in the Doctor index view.

What am I missing?


Solution

  • With your code, you are trying to run the 'find_doctors' method before the action 'doctors' in Refinery::PagesController but the action 'doctors' does not exist in Refinery::PagesController.

    Replacing [:home] instead of [:doctors] works for homepage because the action 'home' does exist in Refinery::PagesController.

    So, what you need to do is decorate Refinery::DoctorsController instead of Refinery::PagesController and run find_doctors method before index for index view.

    Refinery::DoctorsController.class_eval do
    
       before_filter :find_doctors, only: [:index]
    
       protected
    
       def find_doctors
         @find_doctors = Refinery::Doctors::Doctor.all
       end
    end