No commit activity in last 3 years
No release in over 3 years
A very simple ActiveRecord search pattern
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.7
~> 10.0

Runtime

< 7, >= 4.0
 Project Readme

SimpleSearchable

This gem adds the utility methods for a basic ActiveRecord search/filtering pattern.

Installation

Add this line to your application's Gemfile:

gem 'simple_searchable'

And then execute:

$ bundle

Usage

To use, bundle the gem with your project and call searchable_by from your model(s):

class Religion < ActiveRecord::Base
  searchable_by :monotheiestic, :originated_in
  
  def self.originated_in(continents)
    where(originating_continent: continents)
  end
  
  def self.monotheistic(true_false)
    where(monotheistic: true_false)
  end
end

Now write a search form which uses the method names as parameter keys:

<%= form_tag religions_path do %>
  <%= fields_for :religion_filters do |f| %>
    <%= f.label do %>
      Monotheistic? <%= f.check_box :monotheistic, {}, true, false %>
    <% end %>
  
    <%= f.label :originated_in, "Originated" %>
    <%= f.select :originated_in, LOCATIONS %>
  <% end %>

  <%= submit_tag "Search Religions" %>
<% end %>

And get your controller to use search:

class ReligionsController < ApplicationController
  def index
    @religions = Religion.search(params[:religion_filters])
  end
end

If you want to add a new filter, add the scope to the AR class and list it in searchable_by, then add an input to your form.

Contributing

  1. Fork it ( https://github.com/meritec/simple_searchable/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request