Adds to_sql
to arel table and attribute.
About
Arel::Table
and Àrel::Attributes::Attribute
receive to_sql
method.
Example
table = Arel::Table.new(:users)
table.to_sql # => `users`
table[:name].to_sql # => `users`.`name`
Usage
Before
# Calculate total money paid.
sql = "SELECT SUM(orders.price * (100 - orders.discount) / 100) as total
FROM orders
INNER JOIN payments ON payments.order_id = orders.id;"
After
# Calculate total money paid.
t_order = Order.arel_table
t_payment = Payment.arel_table
sql = "SELECT SUM(#{t_order[:price]} * (100 - #{t_order[:discount]}) / 100) as total
FROM #{t_order}
INNER JOIN #{t_payment} ON #{t_payment[:order_id]} = #{t_order[:id]};"
Installing gem
Add to your Gemfile:
gem 'arel-to-sql', '~> 1.0'
Running Tests
Install bundler:
gem install bundler
Install dependencies:
cd arel-to-sql && bundle
Run tests:
cd arel-to-sql && appraisal rake test