wice_grid icon indicating copy to clipboard operation
wice_grid copied to clipboard

Decorators support

Open aldesantis opened this issue 11 years ago • 5 comments

I've looked in the documentation, but I haven't found any information about decorators.

Right now I'm doing it like this:

g.column 'Foo' do |record|
  @record = record.decorate
  @record.foo
end

g.column 'Bar' do
  @record.bar
end

I've also tried experimenting with callbacks, but with little success.

Would it be possible to add a decorate option to the grid helper?

aldesantis avatar Nov 23 '13 16:11 aldesantis

That's a reasonable request, I will add support for decorators

leikind avatar Feb 25 '14 08:02 leikind

Thanks!

aldesantis avatar Feb 25 '14 17:02 aldesantis

I've managed to get draper (in Rails 4) to play nicely with wice_grid by making the collection presenter delegate certain methods to the model and by tricking the inheritance check for ActiveRecord::Relation in the WiceGrid#initialize method.

The following concern contains the logic to perform the trick:

# module for making a decorator compatible with WiceGrid
module WiceGridSupport
  extend ActiveSupport::Concern

  included do

    # delegate to context, which is the underlying model class
    active_record_methods = [
      :connection,
      :model_name,
      :table_name,
      :columns,
      :where,
      :merge_conditions,
      :unscoped,
      :page
    ]
    delegate *active_record_methods, :to => :context

    # pretend to be an ActiveRelation
    def is_a_with_active_relation?(klass)
      klass == ActiveRecord::Relation || is_a_without_active_relation?(klass)
    end
    alias_method_chain :is_a?, :active_relation

    def klass
      context
    end

  end

end

Include this concern in your draper collection decorator:

class ModelCollectionPresenter < Draper::CollectionDecorator
  include WiceGridSupport
end

And finally, in your controller, when instantiating the collection decorator, pass the model class as the context.

E.g.

class MyController < ActionController
  def index
    query = Model.where(...)
    decorator = ModelCollectionPresenter.new(query, :context => Model)
    @grid = initialize_grid(decorator)
  end

  ...
end

virtualstaticvoid avatar Apr 05 '14 23:04 virtualstaticvoid

Thank you very much! I had tried some fixes myself but they didn't work. I'll use your solution right away and let you know.

aldesantis avatar Apr 06 '14 08:04 aldesantis

This approach does not seem to work here. If you pass the model through context, then wice_grid will bypass your query.

Even if you pass query as context and also delegate the klass method, even though this will make the query work again, the result is still undecorated - which makes sense, the decorated relation is bypassed through delegation and wice builds a new relation on top of the underlying one.

In order to make it work, one would have to decorate the relation after wice is done manipulating it, but before rendering the grid, possibly with an option like the one @alessandro1997 suggested

agios avatar Dec 04 '14 12:12 agios