arel-to-sql 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +6 -0
- data/.travis.yml +10 -0
- data/Appraisals +10 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +32 -0
- data/README.md +54 -0
- data/Rakefile +8 -0
- data/arel-to-sql.gemspec +25 -0
- data/gemfiles/arel_6.x.gemfile +7 -0
- data/gemfiles/arel_6.x.gemfile.lock +33 -0
- data/gemfiles/arel_8.x.gemfile +7 -0
- data/gemfiles/arel_8.x.gemfile.lock +33 -0
- data/lib/arel-to-sql.rb +32 -0
- data/lib/arel-to-sql/version.rb +6 -0
- data/test/test-arel-to-sql.rb +40 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ced3e993cfedbf35b948f0d3dbff0158173d3f62
|
4
|
+
data.tar.gz: 15d6cfd1eafc9928024bc9a1a1bd34852761f934
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2290d4129a6058fe60c0a0f525d709c31a26f96ce28c44def78a1067d6551f0593a91709da666a3f7fdc5636d7171567d63513224eb3a07f3f6798a082d39319
|
7
|
+
data.tar.gz: 398582888eab6b525bb17ac49fd180b7521ed570fd37f084decf1720758168d7f29fb4cde1b7c445211da9e381da3915bb9398bdffefac58b48878d5aec31dd7
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Appraisals
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
arel-to-sql (1.0.0)
|
5
|
+
arel (>= 6.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
appraisal (2.1.0)
|
11
|
+
bundler
|
12
|
+
rake
|
13
|
+
thor (>= 0.14.0)
|
14
|
+
arel (8.0.0)
|
15
|
+
power_assert (1.0.1)
|
16
|
+
rake (12.0.0)
|
17
|
+
test-unit (3.2.3)
|
18
|
+
power_assert
|
19
|
+
thor (0.19.4)
|
20
|
+
|
21
|
+
PLATFORMS
|
22
|
+
ruby
|
23
|
+
|
24
|
+
DEPENDENCIES
|
25
|
+
appraisal (~> 2.1)
|
26
|
+
arel-to-sql!
|
27
|
+
bundler (~> 1.7)
|
28
|
+
rake (~> 12.0)
|
29
|
+
test-unit (~> 3.2)
|
30
|
+
|
31
|
+
BUNDLED WITH
|
32
|
+
1.14.3
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
## Adds `to_sql` to arel table and attribute.
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/arel-to-sql.svg)](https://badge.fury.io/rb/arel-to-sql)
|
4
|
+
[![Build Status](https://travis-ci.org/yivo/arel-to-sql.svg?branch=master)](https://travis-ci.org/yivo/arel-to-sql)
|
5
|
+
|
6
|
+
## About
|
7
|
+
`Arel::Table` and `Àrel::Attributes::Attribute` receive `to_sql` method.
|
8
|
+
|
9
|
+
**Example**
|
10
|
+
```ruby
|
11
|
+
table = Arel::Table.new(:users)
|
12
|
+
table.to_sql # => `users`
|
13
|
+
table[:name].to_sql # => `users`.`name`
|
14
|
+
````
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
**Before**
|
18
|
+
```ruby
|
19
|
+
# Calculate total money paid.
|
20
|
+
sql = "SELECT SUM(orders.price * (100 - orders.discount) / 100) as total
|
21
|
+
FROM orders
|
22
|
+
INNER JOIN payments ON payments.order_id = orders.id;"
|
23
|
+
```
|
24
|
+
**After**
|
25
|
+
```ruby
|
26
|
+
# Calculate total money paid.
|
27
|
+
t_order = Order.arel_table
|
28
|
+
t_payment = Payment.arel_table
|
29
|
+
sql = "SELECT SUM(#{t_order[:price]} * (100 - #{t_order[:discount]}) / 100) as total
|
30
|
+
FROM #{t_order}
|
31
|
+
INNER JOIN #{t_payment} ON #{t_payment[:order_id]} = #{t_order[:id]};"
|
32
|
+
```
|
33
|
+
|
34
|
+
## Installing gem
|
35
|
+
Add to your Gemfile:
|
36
|
+
```ruby
|
37
|
+
gem 'arel-to-sql', '~> 1.0'
|
38
|
+
```
|
39
|
+
|
40
|
+
## Running Tests
|
41
|
+
Install bundler:
|
42
|
+
```bash
|
43
|
+
gem install bundler
|
44
|
+
```
|
45
|
+
|
46
|
+
Install dependencies:
|
47
|
+
```bash
|
48
|
+
cd arel-to-sql && bundle
|
49
|
+
```
|
50
|
+
|
51
|
+
Run tests:
|
52
|
+
```bash
|
53
|
+
cd arel-to-sql && appraisal rake test
|
54
|
+
```
|
data/Rakefile
ADDED
data/arel-to-sql.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require File.expand_path('../lib/arel-to-sql/version', __FILE__)
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = 'arel-to-sql'
|
8
|
+
s.version = ArelToSQL::VERSION
|
9
|
+
s.author = 'Yaroslav Konoplov'
|
10
|
+
s.email = 'eahome00@gmail.com'
|
11
|
+
s.summary = 'Adds to_sql to arel table and attribute.'
|
12
|
+
s.description = 'Adds to_sql to arel table and attribute.'
|
13
|
+
s.homepage = 'https://github.com/yivo/arel-to-sql'
|
14
|
+
s.license = 'MIT'
|
15
|
+
|
16
|
+
s.files = `git ls-files -z`.split("\x0")
|
17
|
+
s.test_files = `git ls-files -z -- {test,spec,features}/*`.split("\x0")
|
18
|
+
s.require_paths = ['lib']
|
19
|
+
|
20
|
+
s.add_dependency 'arel', '>= 6.0'
|
21
|
+
s.add_development_dependency 'rake', '~> 12.0'
|
22
|
+
s.add_development_dependency 'test-unit', '~> 3.2'
|
23
|
+
s.add_development_dependency 'bundler', '~> 1.7'
|
24
|
+
s.add_development_dependency 'appraisal', '~> 2.1'
|
25
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
PATH
|
2
|
+
remote: ..
|
3
|
+
specs:
|
4
|
+
arel-to-sql (1.0.0)
|
5
|
+
arel (>= 6.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
appraisal (2.1.0)
|
11
|
+
bundler
|
12
|
+
rake
|
13
|
+
thor (>= 0.14.0)
|
14
|
+
arel (6.0.4)
|
15
|
+
power_assert (1.0.1)
|
16
|
+
rake (12.0.0)
|
17
|
+
test-unit (3.2.3)
|
18
|
+
power_assert
|
19
|
+
thor (0.19.4)
|
20
|
+
|
21
|
+
PLATFORMS
|
22
|
+
ruby
|
23
|
+
|
24
|
+
DEPENDENCIES
|
25
|
+
appraisal (~> 2.1)
|
26
|
+
arel (~> 6.0)
|
27
|
+
arel-to-sql!
|
28
|
+
bundler (~> 1.7)
|
29
|
+
rake (~> 12.0)
|
30
|
+
test-unit (~> 3.2)
|
31
|
+
|
32
|
+
BUNDLED WITH
|
33
|
+
1.14.3
|
@@ -0,0 +1,33 @@
|
|
1
|
+
PATH
|
2
|
+
remote: ..
|
3
|
+
specs:
|
4
|
+
arel-to-sql (1.0.0)
|
5
|
+
arel (>= 6.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
appraisal (2.1.0)
|
11
|
+
bundler
|
12
|
+
rake
|
13
|
+
thor (>= 0.14.0)
|
14
|
+
arel (8.0.0)
|
15
|
+
power_assert (1.0.1)
|
16
|
+
rake (12.0.0)
|
17
|
+
test-unit (3.2.3)
|
18
|
+
power_assert
|
19
|
+
thor (0.19.4)
|
20
|
+
|
21
|
+
PLATFORMS
|
22
|
+
ruby
|
23
|
+
|
24
|
+
DEPENDENCIES
|
25
|
+
appraisal (~> 2.1)
|
26
|
+
arel (~> 8.0)
|
27
|
+
arel-to-sql!
|
28
|
+
bundler (~> 1.7)
|
29
|
+
rake (~> 12.0)
|
30
|
+
test-unit (~> 3.2)
|
31
|
+
|
32
|
+
BUNDLED WITH
|
33
|
+
1.14.3
|
data/lib/arel-to-sql.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module ArelToSQL
|
5
|
+
module TableExtension
|
6
|
+
def to_sql(engine = nil)
|
7
|
+
engine ||= respond_to?(:engine) ? self.engine : Arel::Table.engine
|
8
|
+
engine.connection.quote_table_name(table_name)
|
9
|
+
end
|
10
|
+
|
11
|
+
alias to_s to_sql
|
12
|
+
end
|
13
|
+
|
14
|
+
module AttributeExtension
|
15
|
+
def to_sql(engine = nil)
|
16
|
+
return name if Arel::Nodes::SqlLiteral === name
|
17
|
+
|
18
|
+
engine ||= relation.respond_to?(:engine) ? relation.engine : Arel::Table.engine
|
19
|
+
|
20
|
+
engine.connection.quote_table_name(relation.table_alias || relation.table_name) +
|
21
|
+
'.' +
|
22
|
+
engine.connection.quote_column_name(name)
|
23
|
+
end
|
24
|
+
|
25
|
+
alias to_s to_sql
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
require 'arel'
|
30
|
+
|
31
|
+
Arel::Attributes::Attribute.include ArelToSQL::AttributeExtension
|
32
|
+
Arel::Table.include ArelToSQL::TableExtension
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler'
|
5
|
+
Bundler.require :default, :development, :test
|
6
|
+
|
7
|
+
class FakeRecord
|
8
|
+
class Connection
|
9
|
+
def quote_table_name(name)
|
10
|
+
"`#{name}`"
|
11
|
+
end
|
12
|
+
|
13
|
+
def quote_column_name(name)
|
14
|
+
"`#{name}`"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class << self
|
19
|
+
attr_accessor :connection
|
20
|
+
end
|
21
|
+
|
22
|
+
self.connection = Connection.new
|
23
|
+
end
|
24
|
+
|
25
|
+
class ArelToSQLTest < Test::Unit::TestCase
|
26
|
+
def test_table_to_sql
|
27
|
+
table = Arel::Table.new(:users)
|
28
|
+
assert_equal '`users`', table.to_sql
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_attribute_to_sql
|
32
|
+
table = Arel::Table.new(:users)
|
33
|
+
assert_equal '`users`.`name`', table[:name].to_sql
|
34
|
+
end
|
35
|
+
|
36
|
+
def setup
|
37
|
+
super
|
38
|
+
Arel::Table.engine = FakeRecord
|
39
|
+
end
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: arel-to-sql
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yaroslav Konoplov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-03-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: arel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '6.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '6.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '12.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '12.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: test-unit
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.2'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.2'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.7'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.7'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: appraisal
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '2.1'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '2.1'
|
83
|
+
description: Adds to_sql to arel table and attribute.
|
84
|
+
email: eahome00@gmail.com
|
85
|
+
executables: []
|
86
|
+
extensions: []
|
87
|
+
extra_rdoc_files: []
|
88
|
+
files:
|
89
|
+
- ".gitignore"
|
90
|
+
- ".travis.yml"
|
91
|
+
- Appraisals
|
92
|
+
- Gemfile
|
93
|
+
- Gemfile.lock
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- arel-to-sql.gemspec
|
97
|
+
- gemfiles/arel_6.x.gemfile
|
98
|
+
- gemfiles/arel_6.x.gemfile.lock
|
99
|
+
- gemfiles/arel_8.x.gemfile
|
100
|
+
- gemfiles/arel_8.x.gemfile.lock
|
101
|
+
- lib/arel-to-sql.rb
|
102
|
+
- lib/arel-to-sql/version.rb
|
103
|
+
- test/test-arel-to-sql.rb
|
104
|
+
homepage: https://github.com/yivo/arel-to-sql
|
105
|
+
licenses:
|
106
|
+
- MIT
|
107
|
+
metadata: {}
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 2.6.8
|
125
|
+
signing_key:
|
126
|
+
specification_version: 4
|
127
|
+
summary: Adds to_sql to arel table and attribute.
|
128
|
+
test_files:
|
129
|
+
- test/test-arel-to-sql.rb
|