Skip to content

monade/arel_ext

Repository files navigation

Tests Gem Version

arel_ext

A set of extensions to ActiveRecord Arel.

Installation

Add the gem to your Gemfile

  gem 'arel_ext'

Add an initializer for the configuration:

ArelExt.install

And it's ready!

Usage

Simple SQL Functions invocation

You can easily invocate custom SQL functions, like string_agg:

ArelExt::Func.string_agg(Arel::Table.new('posts')[:title], ',') # => string_agg("posts"."title", ',')

Equivalent to, in pure Arel:

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:

(User[:id] == 1).to_sql # "users"."id" = 1

Equivalent to:

User.arel_table[:id].eq(1).to_sql

Easy join using arel based on associations

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:

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.

About Monade

monade

arel_ext is maintained by mònade srl.

We <3 open source software. Contact us for your next project!

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages