Simple and flexible grid solution for Ruby On Rails
MightyGrid is very simple and flexible grid solution for Ruby On Rails.
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
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 %>
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
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.
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.
To run the tests you need specify database and Rails version.
Example run:
$ DB=postgresql appraisal rails_41 rake spec cucumber
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)