{"id":15353782,"url":"https://github.com/westonganger/search_architect","last_synced_at":"2025-04-15T05:57:46.984Z","repository":{"id":66556543,"uuid":"325421091","full_name":"westonganger/search_architect","owner":"westonganger","description":"Dead simple, powerful and fully customizable searching for your Rails or ActiveRecord models and associations.","archived":false,"fork":false,"pushed_at":"2023-10-31T23:53:37.000Z","size":90,"stargazers_count":6,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-15T05:57:41.543Z","etag":null,"topics":["activerecord","activerecord-models","rails","rails-search","ruby","search","search-scope","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/westonganger.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-12-30T01:02:56.000Z","updated_at":"2023-02-03T15:26:30.000Z","dependencies_parsed_at":null,"dependency_job_id":"dd124657-adc3-4b95-a3e0-0ded6d8db4ee","html_url":"https://github.com/westonganger/search_architect","commit_stats":{"total_commits":23,"total_committers":1,"mean_commits":23.0,"dds":0.0,"last_synced_commit":"6d7fe3494254f5bed0d8fcb562dfc2bb3e592ba1"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/westonganger%2Fsearch_architect","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/westonganger%2Fsearch_architect/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/westonganger%2Fsearch_architect/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/westonganger%2Fsearch_architect/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/westonganger","download_url":"https://codeload.github.com/westonganger/search_architect/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249016325,"owners_count":21198832,"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","activerecord-models","rails","rails-search","ruby","search","search-scope","sql"],"created_at":"2024-10-01T12:15:11.504Z","updated_at":"2025-04-15T05:57:46.967Z","avatar_url":"https://github.com/westonganger.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Search Architect\n\n\u003ca href=\"https://badge.fury.io/rb/search_architect\" target=\"_blank\"\u003e\u003cimg height=\"21\" style='border:0px;height:21px;' border='0' src=\"https://badge.fury.io/rb/search_architect.svg\" alt=\"Gem Version\"\u003e\u003c/a\u003e\n\u003ca href='https://github.com/westonganger/search_architect/actions' target='_blank'\u003e\u003cimg src=\"https://github.com/westonganger/search_architect/actions/workflows/test.yml/badge.svg?branch=master\" style=\"max-width:100%;\" height='21' style='border:0px;height:21px;' border='0' alt=\"CI Status\"\u003e\u003c/a\u003e\n\u003ca href='https://rubygems.org/gems/search_architect' target='_blank'\u003e\u003cimg height='21' style='border:0px;height:21px;' src='https://img.shields.io/gem/dt/search_architect?color=brightgreen\u0026label=Rubygems%20Downloads' border='0' alt='RubyGems Downloads' /\u003e\u003c/a\u003e\n\nDead simple, powerful and fully customizable searching for your Rails or ActiveRecord models and associations. Capable of searching any attribute type using SQL type casting.\n\nWhy This Library:\n\nIf you are considering using the `ransack` gem, then you should think again because `ransack` is a very dirty solution that completely integrates the Searching, Sorting, and Views as requirements of eachother. Not having these features separated hurts your ability to customize and modify your code. Don't fall into this trap. This gem is just one concern with one scope. If you want to customize it later you can simply copy the code directly into your project.\n\n\n# Installation\n\n```ruby\ngem 'search_architect'\n```\n\nThen add `include SearchArchitect` to your ApplicationRecord or models.\n\n# Defining Search Scopes\n\nYou can define any search scopes on your model using the following:\n\n```ruby\nclass Comment \u003c ApplicationRecord\n  include SearchArchitect\n  \n  has_many :comments\nend\n\nclass Comment \u003c ApplicationRecord\n  include SearchArchitect\n  \n  belongs_to :user\n  belongs_to :post\nend\n\nclass Post \u003c ApplicationRecord\n  include SearchArchitect\n  \n  has_many :comments\n\n  search_scope :search, attributes: [\n    :title,\n    :content,\n    :number, ### non-string fields are automatically converted to a searchable type using sql CAST method\n    \"CAST((#{self.table_name}.number+100) AS CHAR)\", ### Plain SQL fully supported\n    :created_at, ### automatically converts date/time fields to searchable string type using sql CAST method, uses default db output format by default\n\n    comments: [\n      :number,\n      :content,\n\n      user: [\n        \"users.name\",\n\n        comments: [\n          {table_alias: \"users_comments\"}, ### Must manually specify a table_alias when we have multiple associations referencing the same table\n          \"users_comments.content\", \n        ],\n      ]\n    ],\n  ]\n  \n  search_scope :search_with_locale, sql_variables: [:locale], attributes: [\n    \"#{self.table_name}.name_translations -\u003e\u003e :locale\", # specify any variables as symbols, Ex. :locale\n  ]\n  \n  search_scope :search_custom_date_format, attributes: [\n    # PostgreSQL, Oracle\n    \"TO_CHAR(#{self.table_name}.approved_at, 'YYYY-mm-dd')\",\n    \n    # MySQL\n    \"DATE_FORMAT(#{self.table_name}.approved_at, '%Y-%m-%d')\",\n    \n    # SQLite\n    \"strftime(#{self.table_name}.approved_at, '%Y-%m-%d')\",\n  ]\n\nend\n```\n\nYou would now have access to the following searching methods:\n\n```ruby\nposts = Post.search(params[:search])\n\nposts = Post.search_with_locale(params[:search], sql_variables: {locale: @current_locale})\n```\n\n# Search Types\n\nWe includes two different searching types:\n\n### Full String Search\n\nConsiders entire string as one search. In my experience this is the natural choice however the multi-search proves to be very powerful.\n\n```ruby\nposts = Post.search(params[:search], search_type: :full_search)\n### OR\nposts = Post.search(params[:search]) # defaults to :full_search\n```\n\n### Multi Word Full-text Search\n\nRecommended. Split words on whitespace characters, Quoting is allowed to combine words\n\nThe following type of queries are supported:\n\n- `foo` (rows must include foo)\n- `foo bar` (rows must include both foo and bar)\n- `\"foo bar\"` (rows must include the phrase \"foo bar\")\n\n```ruby\nposts = Post.search(params[:search], search_type: :multi_search)\n```\n\n# Comparison Operators\n\nDifferent comparison operators can be specified by adding the `:comparison_operator` argument\n\n```ruby\nposts = Post.search(params[:search], comparison_operator: '=')\n```\n\nThe default is `ILIKE` if Postgresql or `LIKE` if non-postgres. Current valid options are: `ILIKE`, `LIKE`, and `=`\n\n# SQL Type Casting Cheatsheet\n\n- Most Types:\n  - `CAST(posts.number AS CHAR)`\n  - `CAST(posts.created_at AS CHAR)` - uses default db output format by default\n- Custom Date/Time Formatting:\n  - Postgresql, Oracle\n    - `TO_CHAR(posts.created_at, 'YYYY-mm-dd')`\n  - MySQL\n    - `DATE_FORMAT(posts.created_at, '%Y-%m-%d')`\n  - SQLite\n    `strftime(posts.created_at, '%Y-%m-%d')`\n\n#### Limitation: Boolean columns\n\nBoolean columns are only searched by their true/false value. Searching boolean fields by the column name is not possible because apparently SQL has the restriction where you cannot use `CASE` statements within `WHERE` clauses. \n\nFor example if you were trying to search a `boolean` by the string of its column name:\n\n`CASE WHEN users.admin IS TRUE THEN 'admin' ELSE  '' END`\n\nYou will find it extremely difficult to work around this. Instead I strongly recommend handling your booleans filtering logic seperately from your search logic.\n\n# Search Form / Views\n\nWe do not provide built in view templates because this is a major restriction to applications. If your looking for starter template feel free to use the following example:\n\n- [examples/_search_form.html.slim](./examples/_search_form.html.slim)\n\n\n# Key Models Provided \u0026 Additional Customizations\n\nA key aspect of this library is its simplicity and small API. For major functionality customizations we encourage you to first delete this gem and then copy this gems code directly into your repository.\n\nI strongly encourage you to read the code for this library to understand how it works within your project so that you are capable of customizing the functionality later.\n\n- [lib/search_architect/concerns/search_scope_concern.rb](./lib/search_architect/concerns/search_scope_concern.rb)\n\n# Credits\n\nCreated \u0026 Maintained by [Weston Ganger](https://westonganger.com) - [@westonganger](https://github.com/westonganger)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwestonganger%2Fsearch_architect","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwestonganger%2Fsearch_architect","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwestonganger%2Fsearch_architect/lists"}