{"id":16737628,"url":"https://github.com/shioyama/wharel","last_synced_at":"2025-04-13T08:23:35.280Z","repository":{"id":56898109,"uuid":"134799409","full_name":"shioyama/wharel","owner":"shioyama","description":"Arel made simple","archived":false,"fork":false,"pushed_at":"2021-10-09T06:55:38.000Z","size":49,"stargazers_count":99,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-10T13:12:16.774Z","etag":null,"topics":["activerecord","arel","rails","sql"],"latest_commit_sha":null,"homepage":"https://dejimata.com/2018/5/30/arel-with-wharel","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/shioyama.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":"2018-05-25T03:37:45.000Z","updated_at":"2025-04-03T14:22:57.000Z","dependencies_parsed_at":"2022-08-21T02:20:17.351Z","dependency_job_id":null,"html_url":"https://github.com/shioyama/wharel","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shioyama%2Fwharel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shioyama%2Fwharel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shioyama%2Fwharel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shioyama%2Fwharel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shioyama","download_url":"https://codeload.github.com/shioyama/wharel/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248681839,"owners_count":21144758,"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","arel","rails","sql"],"created_at":"2024-10-13T00:27:12.896Z","updated_at":"2025-04-13T08:23:35.223Z","avatar_url":"https://github.com/shioyama.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Wharel\n\n[![Gem Version](https://badge.fury.io/rb/wharel.svg)][gem]\n[![Build Status](https://github.com/shioyama/wharel/actions/workflows/ruby.yml/badge.svg)][actions]\n\n[gem]: https://rubygems.org/gems/wharel\n[actions]: https://github.com/shioyama/wharel/actions\n\nWharel helps you write concise Arel queries with ActiveRecord using Virtual\nRows inspired by\n[Sequel](http://sequel.jeremyevans.net/rdoc/files/doc/virtual_rows_rdoc.html).\n\nAlthough similar in spirit to gems like\n[Squeel](https://github.com/activerecord-hackery/squeel) and\n[BabySqueel](https://github.com/rzane/baby_squeel), which provide sophisticated\nblock-based interfaces for querying with Arel, Wharel is much much smaller. In\nfact, the core of the gem is only [30 lines\nlong](https://github.com/shioyama/wharel/blob/master/lib/wharel.rb)! It uses a\nsingle `BasicObject` as a [clean\nroom](https://www.sethvargo.com/the-cleanroom-pattern/) to evaluate\nthe query block. That's really all there is to it.\n\nFor a more detailed explanation of the implementation, see [this blog\npost](https://dejimata.com/2018/5/30/arel-with-wharel).\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'wharel', '~\u003e 1.0.0'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install wharel\n\n## Usage\n\nSuppose we have a model `Post`:\n\n```ruby\nclass Post \u003c ApplicationRecord\n  has_many :comments\nend\n```\n\nAnd let's assume our `Post` has columns `title` and `content`.\n\nNow, if we wanted to find all the posts with a title which matched the string\n\"foo\" and content which matched the string \"bar\", we'd have to resort to\nsomething like this:\n\n```ruby\ntitle = Post.arel_table[:title]\ncontent = Post.arel_table[:content]\nPost.where(title.matches(\"foo\").and(content.matches(\"bar\")))\n```\n\nWith Wharel, you can drop the boilerplate and just use a block:\n\n```ruby\nPost.where { title.matches(\"foo\").and(content.matches(\"bar\")) }\n```\n\nWharel will map `title` and `content` in the block to the appropriate Arel\nattribute for the column.\n\nWharel also supports most other query methods, e.g. `not`:\n\n```ruby\nPost.where.not { title.eq(\"foo\") }\n```\n\n... and `order`:\n\n```ruby\nPost.order { title.lower }\n```\n\nWharel also supports `select`, `having`, `pluck`, `pick`, `group` and `or` in the same way.\n\nNow suppose we have another model `Comment` with a column `content`, and a\n`Post` `has_many :comments`:\n\n```ruby\nclass Post \u003c ApplicationRecord\n  has_many :comments\nend\n\nclass Comment \u003c ApplicationRecord\n  belongs_to :post\nend\n```\n\nNow we want to find all comments which match the title of the comment's post.\nWith standard Arel, you could do this with:\n\n```ruby\nposts = Post.arel_table\ncomments = Comment.arel_table\nComment.joins(:post).where(comments[:content].matches(posts[:title]))\n```\n\nUsing Wharel, you can pass an argument to blocks to handle this case:\n\n```ruby\nComment.joins(:post).where { |c| Post.where { |p| c.content.matches(p.title) } }\n```\n\nMuch better!\n\n## Contributing\n\nNotice something wrong or a feature missing? Post an\n[issue](https://github.com/shioyama/wharel/issues) or create a [pull\nrequest](https://github.com/shioyama/wharel/pulls).\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshioyama%2Fwharel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshioyama%2Fwharel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshioyama%2Fwharel/lists"}