https://github.com/coryodaniel/collectively
Add methods to ActiveRecord relations and collections.
https://github.com/coryodaniel/collectively
Last synced: about 1 year ago
JSON representation
Add methods to ActiveRecord relations and collections.
- Host: GitHub
- URL: https://github.com/coryodaniel/collectively
- Owner: coryodaniel
- License: mit
- Created: 2013-07-30T17:51:49.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2013-07-30T22:31:59.000Z (almost 13 years ago)
- Last Synced: 2025-03-11T18:56:25.374Z (over 1 year ago)
- Language: Ruby
- Size: 145 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.markdown
- License: MIT-LICENSE
Awesome Lists containing this 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:
```bash
rails g collectively:collection posts
```
Add some code to app/collections/posts_collection.rb:
```ruby
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:
```ruby
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:
```ruby
# 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