https://github.com/monade/arel_ext
https://github.com/monade/arel_ext
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/monade/arel_ext
- Owner: monade
- License: mit
- Created: 2023-06-23T13:14:42.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-06-23T13:27:54.000Z (about 3 years ago)
- Last Synced: 2025-01-11T06:21:36.378Z (over 1 year ago)
- Language: Ruby
- Size: 7.81 KB
- Stars: 0
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README

[](https://badge.fury.io/rb/arel_ext)
# arel_ext
A set of extensions to ActiveRecord Arel.
## Installation
Add the gem to your Gemfile
```ruby
gem 'arel_ext'
```
Add an initializer for the configuration:
```ruby
ArelExt.install
```
And it's ready!
## Usage
### Simple SQL Functions invocation
You can easily invocate custom SQL functions, like string_agg:
```ruby
ArelExt::Func.string_agg(Arel::Table.new('posts')[:title], ',') # => string_agg("posts"."title", ',')
```
Equivalent to, in pure Arel:
```ruby
args = [Arel::Table.new('posts')[:title], Arel::Nodes.build_quoted(',')]
Arel::Nodes::NamedFunction.new('string_agg', args)
```
### Simplified arel_table access
You can access to arel_table columns through #[] directly from the label.
For instance:
```ruby
(User[:id] == 1).to_sql # "users"."id" = 1
```
Equivalent to:
```ruby
User.arel_table[:id].eq(1).to_sql
```
### Easy join using arel based on associations
```ruby
class User
belongs_to :posts
end
User.arel_join(:posts).to_sql # => 'INNER JOIN "posts" ON "posts"."user_id" = "users"."id"'
User.join(User.arel_join(:posts)) # => It works!
```
You can also alias the table name:
```ruby
User.arel_join(:posts, as: 'my_posts').to_sql # => INNER JOIN "posts" "my_posts" ON "my_posts"."user_id" = "users"."id"
```
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
About Monade
----------------

arel_ext is maintained by [mònade srl](https://monade.io/en/home-en/).
We <3 open source software. [Contact us](https://monade.io/en/contact-us/) for your next project!