Created
July 11, 2014 10:30
-
-
Save pataiji/76a103daa8ed2c8e9fc7 to your computer and use it in GitHub Desktop.
monkeypatch of wice_grid for draper support ( Inspired from https://github.com/leikind/wice_grid/issues/120 )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
== grid(@posts_grid) do |g| | |
- g.column attribute: 'title' | |
- g.column name: 'body' do |u| | |
- u.truncated_body |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PostDecorator < Draper::Decorator | |
delegate_all | |
def truncated_body | |
object.body.truncate(15) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PostsController < ApplicationController | |
def index | |
@posts_grid = initialize_grid Post, decorate: true | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if defined?(Wice::WiceGrid) && defined?(Draper::CollectionDecorator) && defined?(Draper::Decorator) | |
module WiceGridDraperSupport | |
def initialize(klass_or_relation, controller, opts = {}) | |
@decorate = opts.delete(:decorate) | |
super | |
end | |
def read | |
super | |
@resultset = @resultset.decorate(klass: @klass) if @decorate == true | |
end | |
end | |
module DraperCollectionWiceGridSupport | |
active_record_methods = [ | |
:columns, | |
:connection, | |
:merge_conditions, | |
:page, | |
:table_name, | |
:unscoped, | |
:where | |
] | |
delegate *active_record_methods, to: :klass | |
active_record_relation_methods = [ | |
:current_page, | |
:last_page?, | |
:limit_value, | |
:num_pages, | |
:offset_value, | |
:total_count, | |
:total_pages | |
] | |
delegate *active_record_relation_methods, to: :object | |
attr_reader :klass | |
def initialize(object, options = {}) | |
@klass = options.delete(:klass) | |
super | |
end | |
def is_a?(klass) | |
klass == ActiveRecord::Relation || super | |
end | |
end | |
class Wice::WiceGrid | |
prepend WiceGridDraperSupport | |
end | |
class Draper::CollectionDecorator | |
prepend DraperCollectionWiceGridSupport | |
end | |
class Draper::Decorator | |
def self.decorate_collection(object, options = {}) | |
options.assert_valid_keys(:with, :context, :klass) | |
collection_decorator_class.new(object, options.reverse_merge(with: self)) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I made a little tweak to allow the controller to specify which decorator to use.