{"id":19710487,"url":"https://github.com/rootstrap/arqo","last_synced_at":"2025-04-29T17:31:17.837Z","repository":{"id":38440562,"uuid":"273835514","full_name":"rootstrap/arqo","owner":"rootstrap","description":"Easing the query object pattern in Rails applications","archived":false,"fork":false,"pushed_at":"2023-01-23T18:59:33.000Z","size":48,"stargazers_count":51,"open_issues_count":2,"forks_count":1,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-22T13:49:42.472Z","etag":null,"topics":["activerecord","hacktoberfest","query-object-pattern","query-objects","rails"],"latest_commit_sha":null,"homepage":"https://rootstrap.com","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/rootstrap.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-06-21T04:44:14.000Z","updated_at":"2024-10-22T19:54:42.000Z","dependencies_parsed_at":"2023-02-13T02:15:32.241Z","dependency_job_id":null,"html_url":"https://github.com/rootstrap/arqo","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rootstrap%2Farqo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rootstrap%2Farqo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rootstrap%2Farqo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rootstrap%2Farqo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rootstrap","download_url":"https://codeload.github.com/rootstrap/arqo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251549199,"owners_count":21607367,"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","hacktoberfest","query-object-pattern","query-objects","rails"],"created_at":"2024-11-11T22:07:31.354Z","updated_at":"2025-04-29T17:31:17.331Z","avatar_url":"https://github.com/rootstrap.png","language":"Ruby","readme":"# ARQO ![ARQO](docs/images/logo.png)\n\nARQO (Active Record Query Objects) is a minimal gem that let you use Query Objects in an easy and Rails friendly way. It leverages `ActiveRecord` features and tries to make query objects as intuitive as possible for developers. In combination with the documentation we hope `ARQO` helps people keep their projects well structured and healthy.\n\n![CI](https://github.com/rootstrap/arqo/workflows/ci/badge.svg)\n[![Maintainability](https://api.codeclimate.com/v1/badges/5ed2b32bfdf09746bd82/maintainability)](https://codeclimate.com/github/rootstrap/arqo/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/5ed2b32bfdf09746bd82/test_coverage)](https://codeclimate.com/github/rootstrap/arqo/test_coverage)\n\n## Table of Contents\n\n- [Motivation](#motivation)\n  - [Why ARQO?](#why-arqo)\n- [Installation](#installation)\n- [Usage](#usage)\n  - [Setting up a query object](#setting-up-a-query-object)\n  - [Deriving the model](#deriving-the-model)\n  - [Chaining scopes](#chaining-scopes)\n  - [Generators](#generators)\n- [Development](#development)\n- [Contributing](#contributing)\n- [License](#license)\n- [Code of Conduct](#code-of-conduct)\n- [Credits](#credits)\n\n## Motivation\n\n`ActiveRecord` provides us with an amazing abstraction of the database structure, allowing us to write queries in a simple way. Unfortunately, models can grow large for several reasons, one of them being adding a lot of scopes or placing querying logic in methods.\n\nFor this reason is that we created `ARQO`, so that the query logic is placed into specific objects responsible for building queries while not losing any of the benefits that Rails gives us.\n\n### Why ARQO?\n\n- It is really simple, but still enough to have the best of Rails \u0026 query objects.\n\n- It will dynamically add scopes to your `ActiveRecord::Relation` instances, clearly making a separation of concerns by not making them accessible through the model.\n\n- It supports chaining methods defined in the query object just like when using Rails `scope`s.\n\n- It centralizes the querying logic of your models in a single source of truth.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'arqo'\n```\n\nAnd then execute:\n\n    $ bundle install\n\nOr install it yourself as:\n\n    $ gem install arqo\n\n## Usage\n\nIn the following sections we explain some basic usage and the API provided by the gem.\n\n### Setting up a query object\n\nIn order to use an `ARQO` query object, you need to inherit from `ARQO::Query` and define the `Scope` module inside it. Methods should be defined within the `Scope` module like this:\n```ruby\n# app/queries/user_query.rb\n\nclass UserQuery \u003c ARQO::Query\n  module Scope\n    def active_last_week\n      where('last_active_at \u003e ?', 1.week.ago)\n    end\n  end\nend\n```\n\nAnd then you can use it from anywhere in your code.\n```ruby\nUserQuery.new.active_last_week\n```\n\n## Deriving the model\nIn this previous example, the model was derived from the query object name. In case it's not derivable you should provide the `ActiveRecord::Relation` when initializing the query object, for example if you have:\n```ruby\n# app/queries/custom_named_query.rb\n\nclass CustomNamedQuery \u003c ARQO::Query\n  module Scope\n    def active_last_week\n      where('last_active_at \u003e ?', 1.week.ago)\n    end\n  end\nend\n```\n\nyou can use it like this:\n```ruby\nCustomNamedQuery.new(User.all).active_last_week\n```\n\nyou can also set the model class or relation to query from by overriding a simple method, like:\n\n```ruby\nclass CustomNamedQuery \u003c ARQO::Query\n  module Scope\n    # ...\n  end\n\n  private\n\n  def associated_relation\n    User # you can also do something like User.some_scope\n  end\nend\n```\n\n## Chaining scopes\nOf course you can chain everything together, methods defined in the query object and scopes defined in the model or by Rails.\n```ruby\n# app/queries/user_query.rb\n\nclass UserQuery \u003c ARQO::Query\n  module Scope\n    def active_last_week\n      where('last_active_at \u003e ?', 1.week.ago)\n    end\n\n    def not_deleted\n      where(deleted_at: nil)\n    end\n  end\nend\n```\n\nAnd then you chain everything together and it will just work :)\n```ruby\nUserQuery.new.where.not(name: nil).active_last_week.not_deleted.order(:id)\n```\n\n## Generators\n\nTo create the query object we can use the rails generator tool. For that, we just run:\n\n    $ rails generate query User\n\nAnd it will create your UserQuery object at `app/queries` folder. If you have set Rspec as your test framework, the corresponding spec file will be also created at `spec/queries`.\n\n:warning: Rspec is the only test framework supported for now.\n\nIf your query object is based on another class, you can set the `associated_relation` attribute to automatically override the `associated_relation` method.\n\n    $ rails generate query CustomUser --associated_relation=User\n\n### Model Generator\n\nTo generate the query object when you create your rails models, enable the query generators at your application config file adding the following line:\n\n```ruby\n# config/application.rb\n\nmodule App\n  class Application \u003c Rails::Application\n    ...\n\n    config.generators do |g|\n      ...\n      g.query true # Added line\n    end\n  end\nend\n```\n\nNow, if you run the model generator:\n\n    $ rails generate model User\n\nThe query object and spec will be created as well as the model, migrations, test files, etc.\n\nAnother alternative, it is to add the `--query` option at the end of the command:\n\n    $ rails generate model User --query\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/rootstrap/arqo. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/rootstrap/arqo/blob/master/CODE_OF_CONDUCT.md).\n\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone interacting in the ARQO project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/rootstrap/arqo/blob/master/CODE_OF_CONDUCT.md).\n\n## Logo attribution\nLogo made by [iconixar](https://www.flaticon.com/free-icon/archery_3080892) from [www.flaticon.com](https://www.flaticon.com/)\n\n## Credits\n\nARQO is maintained by [Rootstrap](http://www.rootstrap.com) with the help of our [contributors](https://github.com/rootstrap/arqo/contributors).\n\n[\u003cimg src=\"https://s3-us-west-1.amazonaws.com/rootstrap.com/img/rs.png\" width=\"100\"/\u003e](http://www.rootstrap.com)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frootstrap%2Farqo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frootstrap%2Farqo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frootstrap%2Farqo/lists"}