public_activity gem decorated with Draper

1k views Asked by At

I'm trying to decorate a collection of activities created by the public_activity gem using Draper.

However, it appears that PublicActivity::Activity doesn't inherit from ActiveRecord (am I right in assuming that?) so I need to add include Draper::Decoratable to the model, but where is that PublicActivity model exactly?

Btw, this is the error I'm getting:

Could not infer a decorator for PublicActivity::Activity.

2

There are 2 answers

0
niftygrifty On BEST ANSWER

Well, it turns out that this won't work:

class StudentActivitiesController < ApplicationController
  def index
    @activities = PublicActivity::Activity.order("created_at desc").decorate
  end
end

I'm guessing this is because the namespaced query PublicActivity::Activty doesn't inherit from ActiveRecord and therefore doesn't include Draper::Decoratable, but I'm not sure. Please correct me if I'm wrong.

However, with Draper you can decorate objects as well as queries, so this does work...

class StudentActivitiesController < ApplicationController
  def index
    @activities = PublicActivity::Activity.order("created_at desc")
    @activities = StudentActivitiesDecorator.decorate_collection(@activities)
  end
end

The decorator must also include decorates PublicActivity::Activity

0
firedev On

Just put activity_decorator.rb in decorators/public_activity:

# app/decorators/public_activity/activity_decorator.rb

class PublicActivity::ActivityDecorator < Draper::Decorator
end