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
activeadmin-0.6.6 spec/support/templates/post_decorator.rb
activeadmin-0.6.5 spec/support/templates/post_decorator.rb
activeadmin-0.6.4 spec/support/templates/post_decorator.rb
yousty-activeadmin-1.0.4.pre spec/support/templates/post_decorator.rb
yousty-activeadmin-1.0.3.pre spec/support/templates/post_decorator.rb
yousty-activeadmin-1.0.2.pre spec/support/templates/post_decorator.rb
activeadmin-0.6.3 spec/support/templates/post_decorator.rb
yousty-activeadmin-1.0.1.pre spec/support/templates/post_decorator.rb
yousty-activeadmin-1.0.0.pre spec/support/templates/post_decorator.rb
activeadmin-0.6.2 spec/support/templates/post_decorator.rb
activeadmin-0.6.1 spec/support/templates/post_decorator.rb
aa-rails4-0.6.0 spec/support/templates/post_decorator.rb
lalala-4.0.0.dev.141 vendor/deps/active_admin/spec/support/templates/post_decorator.rb
lalala-4.0.0.dev.136 vendor/deps/active_admin/spec/support/templates/post_decorator.rb
lalala-4.0.0.dev.135 vendor/deps/active_admin/spec/support/templates/post_decorator.rb
lalala-4.0.0.dev.134 vendor/deps/active_admin/spec/support/templates/post_decorator.rb
lalala-4.0.0.dev.133 vendor/deps/active_admin/spec/support/templates/post_decorator.rb
lalala-4.0.0.dev.132 vendor/deps/active_admin/spec/support/templates/post_decorator.rb
lalala-4.0.0.dev.131 vendor/deps/active_admin/spec/support/templates/post_decorator.rb
lalala-4.0.0.dev.129 vendor/deps/active_admin/spec/support/templates/post_decorator.rb