No commit activity in last 3 years
No release in over 3 years
ActsAsIndexable is a Rails engine that provides a configurable DSL for rendering tables in index views of your application. It works with any collection of enumerable objects, ActiveRecord or otherwise.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 2.5.0, ~> 2.5
>= 1.7.1, ~> 1.7
>= 1.5.1, ~> 1.4
>= 4.5.0, ~> 4.5
>= 4.6.4, ~> 4.6
>= 2.4.3, ~> 2.4
>= 0.10.3, ~> 0.10
>= 3.4.0, ~> 3.4
>= 3.0.1, ~> 3.0
>= 1.3.11, ~> 1.3

Runtime

>= 4.2.3, ~> 4.2
 Project Readme

Build Status

acts_as_indexable

This gem is a Rails engine that provides a configurable DSL for rendering tables in index views of your application. It works with any collection of enumerable objects, ActiveRecord or otherwise.

Usage

You first need to install the gem via Bundler by adding the following to your application’s Gemfile…

gem ‘acts_as_indexable’, ‘~> 0.0.1’

In your controller, you need to include the ActsAsIndexable::View concern and define the attributes you’d like to render within your table via the current_attrs method…

class WidgetsController < ApplicationController
  include ActsAsIndexable::View

  def current_attrs
    {
      id: {},
      title: {},
      created_at: {}
    }
  end
end

Lastly, drop the following helper method into your template, passing your enumerable collection of objects as the first argument…

<%= render_indexable Widget.all %>

This should render something similar to the following…

Add custom class selectors to your table by passing them along to render_indexable, like so...

<%= render_indexable Widget.all, class: 'table table-striped' %>

Customizations

For more control over what attributes are exposed, you can define @current_attrs directly within an action or before_filter. For example…

def index
  @current_attrs = {
    id: {},
    username: {}
  }
end

There are number of other customizations you can invoke on each attribute to get the output of your table exactly the way you want it. To customize any attribute, you simply pass the following keys/values within each attribute definition, like so...

{
  title: {
    link_to: :self,
    class: 'btn',
    label: 'Name',
    ...
  }
}

Descriptions for the available customization options are as follows...

Key Description
label Customize the th element for each attribute
link_to Link the attribute's value. Acceptable values are :self and /path/to/:id
label Customize the th element for each attribute
format Format the value according to current locale. Useful for customizing date/time objects
partial Path to custom partial for the contents of td
class Apply CSS selectors to td elements
sort_by Creates parameterized links to head elements if present

Sorting

To sort, apply sort_by key/value to the desired parameter, with value being a string which your target controller will sort by. This will create linkable header with parameters for ?direction and ?sort. Implementing the ordering functionality will need to be done on the target controller as well as overriding the sort_order and/or sort_column functions if needed

Actions

Because most index views need a column for relevant CTAs, you can define a special attribute called actions to handle this. The declaration for this column deviates from the above conventions slightly, such that you can define multiple links to be rendered...

{
  actions: {
    edit: { class: 'btn btn-primary' },
    delete: { class: 'btn btn-secondary' }
  }
}

Testing

bundle exec rspec

License

This project rocks and uses MIT-LICENSE.