0.0
No commit activity in last 3 years
No release in over 3 years
SampleFilter is a Rails Engine plugin that makes to filter and sort ActiveRecord lists
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

 Project Readme

SampleFilter

SampleFilter is a Rails Engine plugin that makes to filter and sort ActiveRecord lists.

Installation

Add this line to your application's Gemfile:

gem 'sample_filter'

And then execute:

$ bundle

Or install it yourself as:

$ gem install sample_filter

Install javascripts air-datepicker dependency:

$ yarn add air-datepicker

Require SampleFilter javascripts with air-datepicker dependency in your app/assets/javascripts/application.js:

//= require jquery
//= require air-datepicker/dist/js/datepicker.min
//= require sample_filter/script

And then require styles in your app/assets/stylesheets/application.css:

/*
 *= require air-datepicker/dist/css/datepicker.min
 *= require sample_filter/style
 */

Usage

Configuring Models

The SampleFilter method in your models accepts options to configure its modules. For example:

class Entity < ApplicationRecord
  sample_filter(
    {
      title: { type: :string },
      amount: { type: :number },
      created_at: { type: :date, default_value: { from: '09.10.2016', to: '17.12.2023' } },
      kind: {
        type: :list,
        values: { red: 1, green: 2 },
        default_value: 2
      },
      status: {
        type: :list,
        values: [:active, :inactive],
        default_value: :inactive
      },
      confirmed: { type: :boolean, default_value: true },
      sort: {
        type: :sorting,
        values: [:id, :amount, :created_at],
        default_value: 'amount_desc',
        hidden_input: true # this option transform select to hidden input in form
      }
    }
  )
end

for update values on demand in list or sorting fields, call method

Entity.filter_update_value(:kind, [:black, :white])
# or hash
Entity.filter_update_value(:kind, { black: 3, white: 4 })

Form helpers

<%= form_for_filter(Entity, as: :sample_filter, block: :after, html: { method: :get, id: :sample_filter}) do %>
  <%= label_tag 'custom_input' %>
  <%= text_field_tag 'custom_input' %>
<% end %>

also form_for_filter include form action options:

options[:url] ||= url_for(
  :controller => controller.controller_name,
  :action => controller.action_name
)

Table sorting link

<%= sort_link(Entity, :sorting_field) %>

Controller

In your index method:

@entites = Entity.filtered(params[:sample_filter])

I18n

SampleFilter uses labels with I18n:

en:
  sample_filter:
    entity:
      fields:
        title: Title
        amount: Amount
        created_at: Created At
        kind: Type
        status: Status
        confirmed: Confirmed status
        sort: Sorting
      list:
        kind:
          all_item: All colors
          red: Red color
          green: Green color
        status:
          all_item: All statuses
          active: Active status
          inactive: Inactive status
        confirmed:
          all_item: All
          'true': Confirmed
          'false': Unconfirmed
        sort:
          id:
            asc: id ↓
            desc: id ↑
          created_at:
            asc: created_at ↓
            desc: created_at ↑
          amount:
            asc: amount ↓
            desc: amount ↑
      buttons:
        submit: Submit
        clear_form: Clear Form

License

The gem is available as open source under the terms of the MIT License.