Class: Espresso::Collection

Inherits:
Array
  • Object
show all
Defined in:
lib/espresso/collection.rb,
lib/espresso/collection/searchlogic.rb,
lib/espresso/collection/will_paginate.rb

Overview

Represents collection of resources. Used in Espresso::Controller InheritedResources extension

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, options = {}) ⇒ Collection

Initiates a collection of resources

Parameters:

  • base (AtiveRecord::Base)

    Base class having Model included

  • options (Hash) (defaults to: {})

    options for building the collection

Options Hash (options):

  • :page (Number)

    current page number

  • :per_page (Number)

    per-page limit

  • :search (Hash)

    conditions for searching



20
21
22
23
# File 'lib/espresso/collection.rb', line 20

def initialize(base, options = {})
  @base, @options = base, options
  replace(collection)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object (protected)

Proxy to #collection methods



32
33
34
# File 'lib/espresso/collection.rb', line 32

def method_missing(method_name, *args)
  collection.send(method_name, *args)
end

Instance Attribute Details

#baseActiveRecord::Base

Use ActiveRecord::Base#searchlogic method to generate base for finding the collection

Returns:



9
10
11
# File 'lib/espresso/collection/searchlogic.rb', line 9

def base
  @base
end

#collectionWillPaginate::Collection

Finds collection by ActiveRecord::Base.paginate method and options provided in #initialize method

Returns:

  • (WillPaginate::Collection)

    single page of resources



27
28
29
# File 'lib/espresso/collection.rb', line 27

def collection
  @collection
end

#options=(value) ⇒ Object

Sets the attribute options

Parameters:

  • value

    the value to set the attribute options to.



11
12
13
# File 'lib/espresso/collection.rb', line 11

def options=(value)
  @options = value
end

#searchObject

Returns the value of attribute search.



11
12
13
# File 'lib/espresso/collection.rb', line 11

def search
  @search
end