{"id":15512641,"url":"https://github.com/bsm/sortable-by","last_synced_at":"2025-04-07T13:31:18.067Z","repository":{"id":36190037,"uuid":"40494235","full_name":"bsm/sortable-by","owner":"bsm","description":null,"archived":false,"fork":false,"pushed_at":"2024-08-02T01:42:03.000Z","size":45,"stargazers_count":3,"open_issues_count":3,"forks_count":4,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-22T19:02:18.609Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/bsm.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-08-10T16:49:11.000Z","updated_at":"2022-03-11T11:24:55.000Z","dependencies_parsed_at":"2023-02-10T16:55:16.550Z","dependency_job_id":null,"html_url":"https://github.com/bsm/sortable-by","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bsm%2Fsortable-by","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bsm%2Fsortable-by/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bsm%2Fsortable-by/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bsm%2Fsortable-by/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bsm","download_url":"https://codeload.github.com/bsm/sortable-by/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247661651,"owners_count":20975094,"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":[],"created_at":"2024-10-02T09:53:45.802Z","updated_at":"2025-04-07T13:31:17.739Z","avatar_url":"https://github.com/bsm.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"# Sortable By\n\n[![Test](https://github.com/bsm/sortable-by/actions/workflows/test.yml/badge.svg)](https://github.com/bsm/sortable-by/actions/workflows/test.yml)\n[![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)\n\nActiveRecord plugin to parse the sort order from a query parameter, match against a white-list and generate a scope. Useful for [JSON-API][jsonapi] compatibility.\n\n[jsonapi]: http://jsonapi.org/format/#fetching-sorting\n\n## Installation\n\nAdd `gem 'sortable-by'` to your Gemfile.\n\n## Usage\n\nSimple:\n\n```ruby\nclass Post \u003c ActiveRecord::Base\n  sortable_by :title, :id\nend\n\nPost.sorted_by('title')          # =\u003e ORDER BY LOWER(posts.title) ASC\nPost.sorted_by('-title')         # =\u003e ORDER BY LOWER(posts.title) DESC\nPost.sorted_by('title,-id')      # =\u003e ORDER BY LOWER(posts.title) ASC, id DESC\nPost.sorted_by(['title', '-id']) # =\u003e ORDER BY LOWER(posts.title) ASC, id DESC\nPost.sorted_by('bad,title')      # =\u003e ORDER BY LOWER(posts.title) ASC\nPost.sorted_by(nil)              # =\u003e ORDER BY LOWER(posts.title) ASC\n```\n\nCase-sensitive:\n\n```ruby\nclass Post \u003c ActiveRecord::Base\n  sortable_by do |x|\n    x.field :title, case_sensitive: true\n    x.field :id\n  end\nend\n\nPost.sorted_by('title') # =\u003e ORDER BY posts.title ASC\n```\n\nWith custom default:\n\n```ruby\nclass Post \u003c ActiveRecord::Base\n  sortable_by :id, :topic, :created_at, default: 'topic,-created_at'\nend\n\nPost.sorted_by(nil) # =\u003e ORDER BY LOWER(posts.topic) ASC, posts.created_at DESC\n```\n\nComposition:\n\n```ruby\nclass App \u003c ActiveRecord::Base\n  sortable_by :name, default: '-version' do |x|\n    x.field :version, as: %i[major minor patch]]\n  end\nend\n\nApp.sorted_by('version') # =\u003e ORDER BY apps.major ASC, apps.minor ASC, apps.patch ASC\nApp.sorted_by(nil)       # =\u003e ORDER BY apps.major DESC, apps.minor DESC, apps.patch DESC\n```\n\nAssociations (custom scope):\n\n```ruby\nclass Product \u003c ActiveRecord::Base\n  belongs_to :shop\n  sortable_by do |x|\n    x.field :shop, as: Shop.arel_table[:name], scope: -\u003e { joins(:shop) }\n  end\nend\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbsm%2Fsortable-by","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbsm%2Fsortable-by","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbsm%2Fsortable-by/lists"}