No commit activity in last 3 years
No release in over 3 years
Extends Draper by adding pagination and scoping methods
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 1.3.0
 Project Readme

Draper::Extensions Build Status Code Climate Coverage Status Dependency Status

Extends Draper by adding pagination and scoping methods

Installation

Add this line to your application's Gemfile:

gem 'draper-extensions'

And then execute:

$ bundle

Or install it yourself as:

$ gem install draper-extensions

Usage

This extension extends these things on the draper gem:

  • Add #decorate to ActiveRecord::Relation.
> listing = Listing.first
> listing.events.decorate
=> #<Draper::CollectionDecorator:0x000001059500f0
 @context={},
 @decorator_class=EventDecorator,
 @object=
  [#<Event id: 1, name: "BugSmash", ar_listing_id: 1, created_at: "2014-05-10 12:43:16", updated_at: "2014-05-10 12:43:16">]>
  • Add #decorate to Kaminari::PaginatableArray.
class Listing
  include Mongoid::Document

  embeds_many   :product_categories

  def products
    @products ||= Kaminari.paginate_array(product_categories.map(&:products))
  end
end

> listing = Listing.first
> listing.products.page(1).decorate
=> #<Draper::CollectionDecorator:0x000001059500f0
 @context={},
 @decorator_class=ProductDecorator,
 @object=
  [#<Product id: 1, name: "Laptop", created_at: "2014-05-10 12:43:16", updated_at: "2014-05-10 12:43:16">]>
  • Add #decorates_scope to Draper::CollectionDecorator.
# app/models/listing.rb
class Listing
  include Mongoid::Document

  field       :name,   type: String

  embeds_many :events
end

# app/models/event.rb
class Event
  include Mongoid::Document

  field       :name,      type: String
  field       :starts_at, type: Time

  scope       :upcomings, -> { where(:starts_at.gt => Time.now) }

  embedded_in :listing
end

# app/decorators/listing_decorator.rb
class ListingDecorator < Draper::Decorator
  decorates_association :events, with: EventsDecorator
end

# app/decorators/events_decorator.rb
class EventsDecorator < Draper::CollectionDecorator
  decorates_scope :upcomings
end

> listing = Listing.first
> listing.decorate.events.upcomings
=> #<EventsDecorator:0x000001016e1ed0
 @context={},
 @decorator_class=EventDecorator,
 @object=
  #<Mongoid::Criteria
  selector: {"starts_at"=>{"$gt"=>2014-05-10 13:03:29 UTC}}
  options:  {}
  class:    Event
  embedded: true>
>

Authors