{"id":19008363,"url":"https://github.com/mechanicles/ruby_simple_search","last_synced_at":"2025-07-19T21:32:47.567Z","repository":{"id":56893670,"uuid":"10145419","full_name":"mechanicles/ruby_simple_search","owner":"mechanicles","description":"The simplest way to search the data (ActiveRecord)","archived":false,"fork":false,"pushed_at":"2020-03-13T12:44:06.000Z","size":104,"stargazers_count":23,"open_issues_count":0,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-07-15T01:39:11.997Z","etag":null,"topics":["activerecord","rails","ruby","ruby-gem","search","search-algorithm","simple","sql"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mechanicles.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-05-18T18:34:03.000Z","updated_at":"2023-03-24T07:34:04.000Z","dependencies_parsed_at":"2022-08-21T01:20:19.082Z","dependency_job_id":null,"html_url":"https://github.com/mechanicles/ruby_simple_search","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/mechanicles/ruby_simple_search","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mechanicles%2Fruby_simple_search","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mechanicles%2Fruby_simple_search/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mechanicles%2Fruby_simple_search/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mechanicles%2Fruby_simple_search/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mechanicles","download_url":"https://codeload.github.com/mechanicles/ruby_simple_search/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mechanicles%2Fruby_simple_search/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266019657,"owners_count":23864916,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["activerecord","rails","ruby","ruby-gem","search","search-algorithm","simple","sql"],"created_at":"2024-11-08T18:42:19.064Z","updated_at":"2025-07-19T21:32:47.542Z","avatar_url":"https://github.com/mechanicles.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# RubySimpleSearch\n\n\n[![Build Status](https://travis-ci.org/mechanicles/ruby_simple_search.svg?branch=master)](https://travis-ci.org/mechanicles/ruby_simple_search)\n[![Maintainability](https://api.codeclimate.com/v1/badges/20e84a4c3be302b07653/maintainability)](https://codeclimate.com/github/mechanicles/ruby_simple_search/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/20e84a4c3be302b07653/test_coverage)](https://codeclimate.com/github/mechanicles/ruby_simple_search/test_coverage)\n\nThe simplest way to search the data in ActiveRecord models.\n\nIt offers simple but useful features:\n\n- [Search on the default attributes](#search-on-the-default-attributes)\n- [Override default search attributes to specific attributes ](#override-default-search-attributes-to-specific-attributes) (Credit goes to [@abdullahtariq1171](https://github.com/abdullahtariq1171))\n- [Search using patterns](#search-using-patterns)\n- [Ruby block support to extend the search query](#ruby-block-support-to-extend-the-search-query)\n- [Simple search returns an `ActiveRecord::Relation` object](#simple-search-returns-an-activerecordrelation-object)\n\nMostly on the admin side, we do have a standard text field to search the data on the table.\nSometimes we want to search through the attributes like title, content and ratings on the\npost model or email, username and description on the user model. For those searches, we use\nMySQL's or PostgreSQL's `LIKE` operator to get the results. While doing the same thing again\nand again on the different models, you add lots of duplication in your code.\n\n#### Do not repeat yourself, use RubySimpleSearch.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n    gem 'ruby_simple_search'\n\nAnd then execute:\n\n    $ bundle install\n\nOr install it yourself as:\n\n    $ gem install ruby_simple_search\n\n## Usage\n\nDefine attributes that you want to search on it\n\n```Ruby\nclass Post \u003c ActiveActiveRecord::Base\n  include RubySimpleSearch\n\n  simple_search_attributes :title, :description\nend\n```\n\n```Ruby\nclass User \u003c ActiveActiveRecord::Base\n  include RubySimpleSearch\n\n  simple_search_attributes :email, :username, :address, :age\nend\n```\n\n## Features\n\n### Search on the default attributes\nIf you don't provide any attribute at the time of searching, it will use `simple_search_attributes` from the model.\n\n```ruby\nclass User \u003c ActiveActiveRecord::Base\n  include RubySimpleSearch\n\n  simple_search_attributes :email, :username, :address\nend\n\n\nPost.simple_search('york')\n# It will search in :email, :username and :address only\n```\n\n### Override default search attributes to specific attributes\n\nIf you want to perform a specific search on particular attributes, you can pass specific attributes with `attributes` option.\n\n```ruby\nclass User \u003c ActiveActiveRecord::Base\n  include RubySimpleSearch\n\n  simple_search_attributes :email, :username, :address\nend\n\nPost.simple_search('york')\n# It will search in :email, :username and :address only\n\nPost.simple_search('york', attributes: :address)\n# It will search in :address only\n\nUser.simple_search('york', pattern: :ending, attributes: [:email, :address])\n# It will search in :email and :address only with 'ending' pattern\n```\n\n### Search using patterns\nYou can pass a `LIKE` pattern to the `simple_search` method. \n\nPatterns:\n\n- beginning\n- ending\n- containing (Default pattern)\n- plain\n\n```ruby\nPost.simple_search('york', pattern: :beginning)\n# It will search like 'york%' and finds any values that start with \"york\"\n\nPost.simple_search('york', pattern: :ending)\n# It will search like '%york' and finds any values that end with \"york\"\n\nPost.simple_search('york', pattern: :containing)\n# It will search like '%york%' and finds any values that have \"york\" in any position\n\nPost.simple_search('york', pattern: :plain)\n# It will search like 'york' and finds any values that have \"york\" word\n```\n\n### Ruby block support to extend the search query\n\n```Ruby\nUser.simple_search('35') do |search_term|\n  [\"AND age = ?\", search_term]\nend\n```\nBlock should return an array of search condition and values.\n\n### Simple search returns an `ActiveRecord::Relation` object\n\n```Ruby\nModel.simple_search('string') # =\u003e ActiveRecord::Relation object\n\nModel.simple_search('string').to_sql\n\n# OR\n\nUser.simple_search('mechanicles') do |search_term|\n  [\"AND address != ?\", search_term]\nend.to_sql\n\n# =\u003e It will return an SQL query in string format\n```\n\n## Contributing\n\n1. Fork it\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create new Pull Request\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmechanicles%2Fruby_simple_search","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmechanicles%2Fruby_simple_search","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmechanicles%2Fruby_simple_search/lists"}