# ActionSet [![Build Status](https://travis-ci.com/fractaledmind/actionset.svg?branch=master)](https://travis-ci.com/fractaledmind/actionset) [![codecov](https://codecov.io/gh/fractaledmind/actionset/branch/master/graph/badge.svg)](https://codecov.io/gh/fractaledmind/actionset) ## Installation Add this line to your application's Gemfile: ```ruby gem 'actionset', require: 'action_set' ``` And then execute: $ bundle Or install it yourself as: $ gem install actionset ## Usage In order to make the **`ActionSet`** helper methods available to your application, you need to `include` the module into your `ApplicationController`. ```ruby class ApplicationController < ActionController::Base include ActionSet end ``` Or, if you only want or need **`ActionSet`** in certain controllers, you can `include` the module directly into those controllers. The simplest setup is to use the `process_set` helper method in your `index` action. Typically, `index` actions look something like the following: ```ruby def index @things = Thing.all end ``` In order to wire up the filtering, sorting, and paginating behaviors, we simply need to update our `index` action to: ```ruby def index @things = process_set(Thing.all) end ``` Now, `@things` will be properly filtered, sorted, and paginated according to the request parameters. > **Note:** `process_set` applies pagination and will paginate your collection regardless of the request parameters. Unless there are request parameters overriding the defaults, your collection will be paginated to 25 items per page showing the first 25 items (page 1). If you want to only filter and sort _without_ paginating, simply use the `filter_set` and `sort_set` helper methods directly, e.g. `sort_set(filter_set(Thing.all))` > **Note:** If you use some authorization library, like [`Pundit`](https://github.com/varvet/pundit), which applies authorization scoping to your `index` action, you can compose that behavior with **`ActionSet`** easily: > ```ruby > def index > @things = process_set(policy_scope(Thing.all)) > end > ``` In addition to filtering, sorting, and paginating, **`ActionSet`** provides exporting functionality via the `export_set` helper method. One common use case is to have an `index` action that renders a filtered, sorted, and paginated collection, but allows for a CSV export as well. In such cases, you typically want the HTML collection to be paginated, but the CSV not to be. This behavior is also relatively simple to achieve: ```ruby def index things = sort_set(filter_set(Thing.all)) respond_to do |f| f.html { @things = paginate_set(things) } f.csv { export_set(things) } end end ``` With our controller properly wired up, we now simply need to have our views submitting request parameters in the shape that **`ActionSet`** expects. **`ActionSet`** provides view helpers to simplify such work. Sorting is perhaps the simplest to setup. To create an (ARIA accessible) anchor link to sort by some particular attribute, use the `sort_link_for` view helper. You pass the attribute name (or dot-separated path), and then can add the text for the link (defaults to title-casing your attribute) and/or any HTML attributes you'd like added to the anchor link. A notable feature of the `sort_link_for` helper is that it intelligently infers the sort direction from whatever the current request state is. That is, if no sorting has been applied for that attribute, the link will apply sorting to that attribute in the _ascending_ direction. If sorting is currently being applied for that attribute in the _ascending_ direction, the link will apply sorting to that attribute in the _descending_ direction, and vice versa. Filtering is somewhat more involved. **`ActionSet`** expects filters to be placed under the `filter` request parameter, aside from that one expectation, we leave all other view layer implementation details up to you. You can build your filtering interface however best fits your application. However, if you need a simple default, we suggest the following pattern: a simple form on your `index` action view that simply reloads that action with whatever filter params the user has submitted. In Rails, building such a form is relatively simple: ```erb <%= form_for(form_for_object_from_param(:filter), method: :get, url: things_path) do |form| %>