0.0
No commit activity in last 3 years
No release in over 3 years
Add methods to ActiveRecord relations and collections
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

> 3.2.0
 Project Readme

Collectively

A simple gem for adding methods to ActiveRecord relations and collections.

Great for quickly adding statistical methods to subsets of results from ActiveRecord.

Support for Rails 3.2+ and 4.0+

Usage

Generate a collection:

rails g collectively:collection posts

Add some code to app/collections/posts_collection.rb:

class PostsCollection < Collectively::Base
  def page_views_by_day
    collection.inject({}){|memo, post|
      post.page_views.each do |page_view|
        date = page_view.created_at.to_date.to_s
        memo[date] ||= 0
        memo[date] += 1
      end
      
      memo
    }
  end
end

Add 'acts_collectively' to app/models/post.rb:

class Post < ActiveRecord::Base
  acts_collectively
  # optionally override the collection name:
  # acts_collectively as: :blogs #=> BlogsCollection
  has_many :page_views
end

Access your collection methods:

  # Page views by day this month
  Post.where(created_at: (Time.beginning_of_month..Time.now)).collection.page_views_by_day

  # Page views since first post
  Post.all.collection.page_views_by_day

Authors

  • Cory ODaniel