Sha256: 8a8c0fe08441b2a5f3d40acf0a76be063ffa303a4f0a3a995f93f8bb6c37af91

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 KB

Contents

# CollectionExtensions

Sometimes an operation just doesn't fit well into a scope, but you don't want to lose your declarative code style
by operating on all the objects individually. This gem adds a few lines of code to make it easier to add methods to collections of objects.

<img src="https://secure.travis-ci.org/arches/collection_extensions.png" />

## Installation

Add this line to your application's Gemfile:

    gem 'collection_extensions'

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install collection_extensions

## Usage

Include the module in your model and specify what collections you want to extend:

    class User < ActiveRecord::Base
        include CollectionExtensions
        has_many :orders, :preferences
        extend_collections :orders, :preferences
        
        # the original unextended collection is still available, aliased as orig_* (eg, orig_orders or orig_preferences)
    end

This will use the modules OrdersCollectionExtensions and PreferencesCollectionExtensions to extend those associations
whenever you use them. For example:

    module OrdersCollectionExtensions
      def for_product_id(product_id)
        # 'self' is the array of orders
        select {|o| o.line_items.collect(&:product_id).include? product_id}
      end
    end
    
    > User.find(1).orders.for_product_id(2)

If you want a different naming convention, set the config variable using `%s` string substitution:

    CollectionExtensions::Config.naming_convention = "MethodsForCollectionsOf%s"


## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Added some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
collection_extensions-0.0.1 README.md