Sha256: c3c61a586166b68f1e741607b9979572f5ea05ece25ea91d14dab2144af016a7

Contents?: true

Size: 1.28 KB

Versions: 58

Compression:

Stored size: 1.28 KB

Contents

# Minimal example of a decorator
require_dependency 'post'

# DelegateClass(Post) does not delegate attribute methods
# in some environments:
# http://travis-ci.org/#!/gregbell/active_admin/jobs/2021466
#
class PostDecorator < SimpleDelegator
  delegate :id, :to => :__getobj__

  def self.decorate(object)
    new(object)
  end

  def self.decorate_collection(collection)
    DecoratedEnumerableProxy.new(collection)
  end

  def self.model_name
    ActiveModel::Name.new Post
  end

  def decorator_method
    'A method only available on the decorator'
  end

  # Minimal example of decorating a collection.
  # A full example can be found in the draper project:
  # https://github.com/jcasimir/draper/blob/master/lib/draper/decorated_enumerable_proxy.rb
  class DecoratedEnumerableProxy < DelegateClass(ActiveRecord::Relation)
    include Enumerable

    delegate :as_json, :collect, :map, :each, :[], :all?, :include?, :first, :last, :shift, :to => :decorated_collection

    def klass
      PostDecorator
    end

    def wrapped_collection
      __getobj__
    end

    def decorated_collection
      @decorated_collection ||= wrapped_collection.collect { |member| klass.decorate(member) }
    end
    alias_method :to_ary, :decorated_collection

    def each(&blk)
      to_ary.each(&blk)
    end
  end
end

Version data entries

58 entries across 58 versions & 4 rubygems

Version Path
lalala-4.0.0.dev.74 vendor/deps/active_admin/spec/support/templates/post_decorator.rb
lalala-4.0.0.dev.73 vendor/deps/active_admin/spec/support/templates/post_decorator.rb
lalala-4.0.0.dev.72 vendor/deps/active_admin/spec/support/templates/post_decorator.rb
lalala-4.0.0.dev.71 vendor/deps/active_admin/spec/support/templates/post_decorator.rb
lalala-4.0.0.dev.66 vendor/deps/active_admin/spec/support/templates/post_decorator.rb
lalala-4.0.0.dev.65 vendor/deps/active_admin/spec/support/templates/post_decorator.rb
activeadmin-0.6.0 spec/support/templates/post_decorator.rb
lalala-4.0.0.dev.64 vendor/deps/active_admin/spec/support/templates/post_decorator.rb
lalala-4.0.0.dev.63 vendor/deps/active_admin/spec/support/templates/post_decorator.rb
lalala-4.0.0.dev.62 vendor/deps/active_admin/spec/support/templates/post_decorator.rb
lalala-4.0.0.dev.61 vendor/deps/active_admin/spec/support/templates/post_decorator.rb
lalala-4.0.0.dev.60 vendor/deps/active_admin/spec/support/templates/post_decorator.rb
lalala-4.0.0.dev.59 vendor/deps/active_admin/spec/support/templates/post_decorator.rb
lalala-4.0.0.dev.58 vendor/deps/active_admin/spec/support/templates/post_decorator.rb
lalala-4.0.0.dev.57 vendor/deps/active_admin/spec/support/templates/post_decorator.rb
lalala-4.0.0.dev.56 vendor/deps/active_admin/spec/support/templates/post_decorator.rb
lalala-4.0.0.dev.50 vendor/deps/active_admin/spec/support/templates/post_decorator.rb
lalala-4.0.0.dev.46 vendor/deps/active_admin/spec/support/templates/post_decorator.rb