MightyGrid is very simple and flexible grid solution for Ruby On Rails.
Features
- Ruby 2.x support
- Rails 4.x support
- Easy building grids
- Flexible filtering
- Simple sorting
Installation
Add this line to your application's Gemfile:
gem 'mighty_grid'
Then run the following generator command in order to generate files for gem customization:
$ rails g mighty_grid:install
After generation you will see the following files:
config/initializers/mighty_grid.rb
config/locales/mighty_grid.en.yml
Quick Start
-
Creating grid
Any grid can be created in a folder
app/grids
for example as follows:class ProductsGrid < MightyGrid::Base scope { Product } end
-
Initialize the grid in a controller
def index @products_grid = ProductsGrid.new(params) end
-
Show created grid
<%= grid @products_grid do |g| %> <% - g.column :id %> <% - g.column :name %> <% - g.column :description %> <% end %>
Usage
Filters
A simple example of the use of filters:
class ProductsGrid < MightyGrid::Base
scope { Product }
filter :name
filter :status, :enum, collection: [['active', 'Active'], ['inactive', 'Inactive']]
filter :author, :string, attribute: :name, model: User
end
General configuration options
You can configure the following default values by overriding these values using MightyGrid.setup method.
per_page # 15 by default
order_direction # 'asc' by default
order_type # 'single' by default
order_asc # '↑' by default
order_desc # '↓' by default
order_asc_link_class # '' by default
order_desc_link_class # '' by default
order_active_link_class # 'mg-order-active' by default
order_wrapper_class # '' by default
grid_name # 'grid' by default
table_class # '' by default
header_tr_class # '' by default
pagination_theme # 'mighty_grid' by default
There's a handy generator that generates the default configuration file into config/initializers directory.
Thinking Sphinx Support
Example
class ProductsGrid < MightyGrid::Base
scope { Product }
use_thinking_sphinx true
sphinx_options indices: ['product_public_core']
search :query
end
Scope should contain only the model class (not relation). You can have only one search field.
Note: Currently, filtering is not supported for the search.
Running tests
To run the tests you need specify database and Rails version.
- List of available Rails versions: 4.0, 4.1, 4.2.
- List of DB: sqlite, postgresql, mysql.
Example run:
$ DB=postgresql appraisal rails_41 rake spec cucumber
Contributing
- Fork it ( http://github.com/jurrick/mighty_grid/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request