arel-helpers 2.1.0 → 2.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/History.txt +4 -0
- data/README.md +31 -14
- data/lib/arel-helpers/arel_table.rb +5 -1
- data/lib/arel-helpers/version.rb +1 -1
- data/spec/arel_table_spec.rb +6 -0
- metadata +16 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7caa6de6e09401386429457aabaaebbe22cc7644
|
4
|
+
data.tar.gz: 9f60d6693ff7bb7fe28e81b89d96544c30754388
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7eedcd17a17eb46a6cbef05be4470eb96fe9cf0e462cddf6b5313de9dea179c7497a8d83207923c0684a6088f3edb6022fc1468dfa02741e8bec725cdfd2f10f
|
7
|
+
data.tar.gz: e99be0b2d82076d2c24b898fd6ae08dac884fb2d34378dcb9778e36e1b2ebcd3ae7d52a16862a5eaba342157255bb06d26038f5c68e85e3f3b8fcee6aa00c90c
|
data/History.txt
CHANGED
data/README.md
CHANGED
@@ -42,12 +42,29 @@ Post.where(Post[:id].eq(1))
|
|
42
42
|
Using pure Arel is one of the only ways to do an outer join with ActiveRecord. For example, let's say we have these two models:
|
43
43
|
|
44
44
|
```ruby
|
45
|
+
class Author < ActiveRecord::Base
|
46
|
+
has_many :posts
|
47
|
+
|
48
|
+
# attribute id
|
49
|
+
# attribute username
|
50
|
+
end
|
51
|
+
|
45
52
|
class Post < ActiveRecord::Base
|
53
|
+
belongs_to :author
|
46
54
|
has_many :comments
|
55
|
+
|
56
|
+
# attribute id
|
57
|
+
# attribute author_id
|
58
|
+
# attribute subject
|
47
59
|
end
|
48
60
|
|
49
61
|
class Comment < ActiveRecord::Base
|
50
62
|
belongs_to :post
|
63
|
+
belongs_to :author
|
64
|
+
|
65
|
+
# attribute id
|
66
|
+
# attribute post_id
|
67
|
+
# attribute author_id
|
51
68
|
end
|
52
69
|
```
|
53
70
|
|
@@ -62,18 +79,19 @@ ActiveRecord introspects the association between posts and comments and automati
|
|
62
79
|
Things start to get messy however if you want to do an outer join instead of the default inner join. Your query might look like this:
|
63
80
|
|
64
81
|
```ruby
|
65
|
-
Post
|
66
|
-
.
|
67
|
-
Post.
|
68
|
-
|
69
|
-
|
70
|
-
)
|
82
|
+
Post.joins(
|
83
|
+
Post.arel_table.join(Comment.arel_table, Arel::Nodes::OuterJoin)
|
84
|
+
.on(Post[:id].eq(Comment[:post_id]))
|
85
|
+
.join_sources
|
86
|
+
)
|
71
87
|
```
|
72
88
|
|
73
89
|
Such verbose. Much code. Very bloat. Wow. We've lost all the awesome association introspection that ActiveRecord would otherwise have given us. Enter `ArelHelpers.join_association`:
|
74
90
|
|
75
91
|
```ruby
|
76
|
-
Post.joins(
|
92
|
+
Post.joins(
|
93
|
+
ArelHelpers.join_association(Post, :comments, Arel::Nodes::OuterJoin)
|
94
|
+
)
|
77
95
|
```
|
78
96
|
|
79
97
|
Easy peasy.
|
@@ -82,7 +100,7 @@ Easy peasy.
|
|
82
100
|
|
83
101
|
```ruby
|
84
102
|
Post.joins(
|
85
|
-
ArelHelpers.join_association(Post, :comments, Arel::OuterJoin) do |assoc_name, join_conditions|
|
103
|
+
ArelHelpers.join_association(Post, :comments, Arel::Nodes::OuterJoin) do |assoc_name, join_conditions|
|
86
104
|
join_conditions.and(Post[:author_id].eq(4))
|
87
105
|
end
|
88
106
|
)
|
@@ -93,12 +111,11 @@ But wait, there's more! Include the `ArelHelpers::JoinAssociation` concern into
|
|
93
111
|
```ruby
|
94
112
|
include ArelHelpers::JoinAssociation
|
95
113
|
|
96
|
-
Post
|
97
|
-
.
|
98
|
-
Post.
|
99
|
-
|
100
|
-
|
101
|
-
)
|
114
|
+
Post.joins(
|
115
|
+
Post.join_association(:comments, Arel::Nodes::OuterJoin) do |assoc_name, join_conditions|
|
116
|
+
join_conditions.and(Post[:author_id].eq(4))
|
117
|
+
end
|
118
|
+
)
|
102
119
|
```
|
103
120
|
|
104
121
|
### Query Builders
|
@@ -7,6 +7,10 @@ module ArelHelpers
|
|
7
7
|
|
8
8
|
module ClassMethods
|
9
9
|
|
10
|
+
if ActiveRecord.const_defined?(:Delegation)
|
11
|
+
ActiveRecord::Delegation.delegate :[], to: :to_a
|
12
|
+
end
|
13
|
+
|
10
14
|
def [](name)
|
11
15
|
arel_table[name]
|
12
16
|
end
|
@@ -14,4 +18,4 @@ module ArelHelpers
|
|
14
18
|
end
|
15
19
|
|
16
20
|
end
|
17
|
-
end
|
21
|
+
end
|
data/lib/arel-helpers/version.rb
CHANGED
data/spec/arel_table_spec.rb
CHANGED
@@ -21,4 +21,10 @@ describe ArelHelpers::ArelTable do
|
|
21
21
|
comment = post.comments.create
|
22
22
|
post.reload.comments[0].id.should == comment.id
|
23
23
|
end
|
24
|
+
|
25
|
+
it "does not interfere with ActiveRecord::Relation objects" do
|
26
|
+
Post.all[0].should be_nil
|
27
|
+
p = Post.create(title: 'foo')
|
28
|
+
Post.all[0].id.should == p.id
|
29
|
+
end
|
24
30
|
end
|
metadata
CHANGED
@@ -1,35 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: arel-helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.1.
|
4
|
+
version: 2.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cameron Dutro
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-10-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - '
|
17
|
+
- - '='
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
20
|
-
- - <
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
version: '5'
|
19
|
+
version: 4.0.13
|
23
20
|
type: :runtime
|
24
21
|
prerelease: false
|
25
22
|
version_requirements: !ruby/object:Gem::Requirement
|
26
23
|
requirements:
|
27
|
-
- - '
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
version: 3.1.0
|
30
|
-
- - <
|
24
|
+
- - '='
|
31
25
|
- !ruby/object:Gem::Version
|
32
|
-
version:
|
26
|
+
version: 4.0.13
|
33
27
|
description: Useful tools to help construct database queries with ActiveRecord and
|
34
28
|
Arel.
|
35
29
|
email:
|
@@ -38,24 +32,24 @@ executables: []
|
|
38
32
|
extensions: []
|
39
33
|
extra_rdoc_files: []
|
40
34
|
files:
|
35
|
+
- Gemfile
|
36
|
+
- History.txt
|
37
|
+
- README.md
|
38
|
+
- Rakefile
|
39
|
+
- arel-helpers.gemspec
|
40
|
+
- lib/arel-helpers.rb
|
41
41
|
- lib/arel-helpers/arel_table.rb
|
42
42
|
- lib/arel-helpers/ext/collection_proxy.rb
|
43
43
|
- lib/arel-helpers/join_association.rb
|
44
44
|
- lib/arel-helpers/query_builder.rb
|
45
45
|
- lib/arel-helpers/version.rb
|
46
|
-
- lib/arel-helpers.rb
|
47
46
|
- spec/arel_table_spec.rb
|
47
|
+
- spec/env.rb
|
48
48
|
- spec/env/migrations.rb
|
49
49
|
- spec/env/models.rb
|
50
|
-
- spec/env.rb
|
51
50
|
- spec/join_association_spec.rb
|
52
51
|
- spec/query_builder_spec.rb
|
53
52
|
- spec/spec_helper.rb
|
54
|
-
- Gemfile
|
55
|
-
- History.txt
|
56
|
-
- README.md
|
57
|
-
- Rakefile
|
58
|
-
- arel-helpers.gemspec
|
59
53
|
homepage: http://github.com/camertron
|
60
54
|
licenses: []
|
61
55
|
metadata: {}
|
@@ -65,17 +59,17 @@ require_paths:
|
|
65
59
|
- lib
|
66
60
|
required_ruby_version: !ruby/object:Gem::Requirement
|
67
61
|
requirements:
|
68
|
-
- -
|
62
|
+
- - ">="
|
69
63
|
- !ruby/object:Gem::Version
|
70
64
|
version: '0'
|
71
65
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
66
|
requirements:
|
73
|
-
- -
|
67
|
+
- - ">="
|
74
68
|
- !ruby/object:Gem::Version
|
75
69
|
version: '0'
|
76
70
|
requirements: []
|
77
71
|
rubyforge_project:
|
78
|
-
rubygems_version: 2.
|
72
|
+
rubygems_version: 2.4.8
|
79
73
|
signing_key:
|
80
74
|
specification_version: 4
|
81
75
|
summary: Useful tools to help construct database queries with ActiveRecord and Arel.
|