README.md in active_hash_relation-0.0.2 vs README.md in active_hash_relation-0.0.3
- old
+ new
@@ -21,10 +21,12 @@
_\*Actually nothing is exposed, but a user could retrieve resources based
on unknown attributes (attributes not returned from the API) by brute forcing
which might or might not be a security issue. If you don't like that check
[whitelisting](https://github.com/kollegorna/active_hash_relation#whitelisting)._
+*New*! You can now do [__aggregation queries__](https://github.com/kollegorna/active_hash_relation#aggregation_queries).
+
## Installation
Add this line to your application's Gemfile:
gem 'active_hash_relation'
@@ -61,11 +63,11 @@
For each param, `apply_filters` method will search in the model's (derived from the first param, or explicitly defined as the last param) all the record's column names and associations. (filtering based on scopes are not working at the moment but will be supported soon). For each column, if there is such a param, it will apply the filter based on the column type. The following column types are supported:
#### Primary
You can apply a filter a column which is a primary key by value or using an array like:
* `{primary_key_column: 5}`
-* `{primary_key)column: [1,3,4,5,6,7]}`
+* `{primary_key_column: [1,3,4,5,6,7]}`
#### Integer, Float, Decimal, Date, Time or Datetime/Timestamp
You can apply an equality filter:
* `{example_column: 500}`
or using a hash as a value you get more options:
@@ -140,9 +142,31 @@
end
```
With the above settings, when the association name is `resource`,
`Api::V1::ResourceFilter.new(resource, params[resource]).apply_filters` will be
called to apply the filters in resource association.
+
+## Aggregation Queries
+Sometimes we need to ask the database queries that act on the collection but don't want back an array of elements but a value instead! Now you can do that on an ActiveRecord::Relation by simply calling the aggregations method inside the controller:
+
+```ruby
+apply_filters(resource, {
+ aggregate: {
+ integer_column: { avg: true, max: true, min: true, sum: true },
+ float_column: {avg: true, max: true, min: true },
+ datetime_column: { max: true, min: true }
+ }
+})
+```
+
+and you will get a hash (HashWithIndifferentAccess) back that holds all your aggregations like:
+```ruby
+{"float_column"=>{"avg"=>25.5, "max"=>50, "min"=>1},
+ "integer_column"=>{"avg"=>4.38, "sum"=>219, "max"=>9, "min"=>0},
+ "datetime_at"=>{"max"=>2015-06-11 20:59:14 UTC, "min"=>2015-06-11 20:59:12 UTC}}
+```
+
+These attributes usually go to the "meta" section of your serializer. In that way it's easy to parse them in the front-end (for ember check [here](http://guides.emberjs.com/v1.10.0/models/handling-metadata/). Please note that you should apply the aggregations __after__ you apply the filters, if there any.
## Contributing