wice_grid
wice_grid copied to clipboard
Decorators support
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?
That's a reasonable request, I will add support for decorators
Thanks!
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
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.
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