arel-helpers 2.1.1 → 2.2.0
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/lib/arel-helpers/join_association.rb +34 -9
- data/lib/arel-helpers/version.rb +1 -1
- data/spec/env.rb +5 -1
- data/spec/env/migrations.rb +27 -0
- data/spec/env/models.rb +21 -0
- data/spec/join_association_spec.rb +10 -0
- metadata +13 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 875e70afa83c9d6c1c5b74713162b2eef19c94b1
|
4
|
+
data.tar.gz: 562a0e972c495904a876f5cfcbf08dbadf71b058
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a364dbb1ccbe4937fa23892678261cdfabc4846111499748f244701f210d7a8996465f80f8d14d7c9add1ab4706f3b4a71d48736caa12ffed20f6b6c5f223f1
|
7
|
+
data.tar.gz: 746357d316c5eb82e29952f2a5fd54ffebae6e550acb7e8eebdf7fcc9bc6562e37975217afde0f8f4e441495b5789c907d0f2a0e22f398ef06d7913e842de0fe
|
data/History.txt
CHANGED
@@ -56,30 +56,55 @@ module ArelHelpers
|
|
56
56
|
|
57
57
|
join_dependency.join_constraints([]).map do |constraint|
|
58
58
|
right = if block_given?
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
59
|
+
yield constraint.left.name.to_sym, constraint.right
|
60
|
+
else
|
61
|
+
constraint.right
|
62
|
+
end
|
63
63
|
|
64
64
|
join_type.new(constraint.left, right)
|
65
65
|
end
|
66
66
|
end
|
67
67
|
|
68
|
+
# ActiveRecord 4.2 moves bind variables out of the join classes
|
69
|
+
# and into the relation. For this reason, a method like
|
70
|
+
# join_association isn't able to add to the list of bind variables
|
71
|
+
# dynamically. To get around the problem, this method must return
|
72
|
+
# a string.
|
68
73
|
def join_association_4_2(table, association, join_type)
|
69
74
|
associations = association.is_a?(Array) ? association : [association]
|
70
75
|
join_dependency = ActiveRecord::Associations::JoinDependency.new(table, associations, [])
|
71
76
|
|
72
|
-
join_dependency.join_constraints([])
|
77
|
+
constraints = join_dependency.join_constraints([])
|
78
|
+
|
79
|
+
binds = constraints.flat_map do |info|
|
80
|
+
info.binds.map { |bv| table.connection.quote(*bv.reverse) }
|
81
|
+
end
|
82
|
+
|
83
|
+
joins = constraints.flat_map do |constraint|
|
73
84
|
constraint.joins.map do |join|
|
74
85
|
right = if block_given?
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
86
|
+
yield join.left.name.to_sym, join.right
|
87
|
+
else
|
88
|
+
join.right
|
89
|
+
end
|
79
90
|
|
80
91
|
join_type.new(join.left, right)
|
81
92
|
end
|
82
93
|
end
|
94
|
+
|
95
|
+
join_strings = joins.map do |join|
|
96
|
+
to_sql(join, table, binds)
|
97
|
+
end
|
98
|
+
|
99
|
+
join_strings.join(' ')
|
100
|
+
end
|
101
|
+
|
102
|
+
private
|
103
|
+
|
104
|
+
def to_sql(node, table, binds)
|
105
|
+
visitor = table.connection.visitor
|
106
|
+
collect = visitor.accept(node, Arel::Collectors::Bind.new)
|
107
|
+
collect.substitute_binds(binds).join
|
83
108
|
end
|
84
109
|
end
|
85
110
|
end
|
data/lib/arel-helpers/version.rb
CHANGED
data/spec/env.rb
CHANGED
@@ -28,6 +28,10 @@ module ArelHelpers
|
|
28
28
|
CreateAuthorsTable.new.change
|
29
29
|
CreateFavoritesTable.new.change
|
30
30
|
CreateCollabPostsTable.new.change
|
31
|
+
CreateCardsTable.new.change
|
32
|
+
CreateCardLocationsTable.new.change
|
33
|
+
CreateLocationsTable.new.change
|
34
|
+
CreateCommunityTicketsTable.new.change
|
31
35
|
end
|
32
36
|
|
33
37
|
def reset
|
@@ -37,4 +41,4 @@ module ArelHelpers
|
|
37
41
|
|
38
42
|
end
|
39
43
|
end
|
40
|
-
end
|
44
|
+
end
|
data/spec/env/migrations.rb
CHANGED
@@ -40,3 +40,30 @@ class CreateCollabPostsTable < ActiveRecord::Migration
|
|
40
40
|
end
|
41
41
|
end
|
42
42
|
end
|
43
|
+
|
44
|
+
class CreateCardsTable < ActiveRecord::Migration
|
45
|
+
def change
|
46
|
+
create_table :cards
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
class CreateCardLocationsTable < ActiveRecord::Migration
|
51
|
+
def change
|
52
|
+
create_table :card_locations do |t|
|
53
|
+
t.references :location
|
54
|
+
t.references :card, polymorphic: true
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class CreateLocationsTable < ActiveRecord::Migration
|
60
|
+
def change
|
61
|
+
create_table :locations
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
class CreateCommunityTicketsTable < ActiveRecord::Migration
|
66
|
+
def change
|
67
|
+
create_table :community_tickets
|
68
|
+
end
|
69
|
+
end
|
data/spec/env/models.rb
CHANGED
@@ -27,3 +27,24 @@ class CollabPost < ActiveRecord::Base
|
|
27
27
|
include ArelHelpers::ArelTable
|
28
28
|
has_and_belongs_to_many :authors
|
29
29
|
end
|
30
|
+
|
31
|
+
class Card < ActiveRecord::Base
|
32
|
+
has_many :card_locations
|
33
|
+
end
|
34
|
+
|
35
|
+
class CardLocation < ActiveRecord::Base
|
36
|
+
belongs_to :location
|
37
|
+
belongs_to :card, polymorphic: true
|
38
|
+
end
|
39
|
+
|
40
|
+
class Location < ActiveRecord::Base
|
41
|
+
has_many :card_locations
|
42
|
+
has_many :community_tickets, {
|
43
|
+
through: :card_locations, source: :card, source_type: 'CommunityTicket'
|
44
|
+
}
|
45
|
+
end
|
46
|
+
|
47
|
+
class CommunityTicket < ActiveRecord::Base
|
48
|
+
has_many :card_locations, as: :card, source_type: 'CommunityTicket'
|
49
|
+
has_many :locations, through: :card_locations
|
50
|
+
end
|
@@ -50,6 +50,16 @@ describe ArelHelpers do
|
|
50
50
|
CollabPost.joins(ArelHelpers.join_association(CollabPost, :authors)).to_sql.should ==
|
51
51
|
'SELECT "collab_posts".* FROM "collab_posts" INNER JOIN "authors_collab_posts" ON "authors_collab_posts"."collab_post_id" = "collab_posts"."id" INNER JOIN "authors" ON "authors"."id" = "authors_collab_posts"."author_id"'
|
52
52
|
end
|
53
|
+
|
54
|
+
it 'handles polymorphic through associations' do
|
55
|
+
relation = Location.joins(
|
56
|
+
ArelHelpers.join_association(Location, :community_tickets)
|
57
|
+
)
|
58
|
+
|
59
|
+
relation.to_sql.should == 'SELECT "locations".* FROM "locations" ' +
|
60
|
+
'INNER JOIN "card_locations" ON "card_locations"."location_id" = "locations"."id" AND "card_locations"."card_type" = \'CommunityTicket\' ' +
|
61
|
+
'INNER JOIN "community_tickets" ON "community_tickets"."id" = "card_locations"."card_id"'
|
62
|
+
end
|
53
63
|
end
|
54
64
|
end
|
55
65
|
|
metadata
CHANGED
@@ -1,29 +1,35 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: arel-helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cameron Dutro
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-01-21 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:
|
19
|
+
version: 3.1.0
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '5'
|
20
23
|
type: :runtime
|
21
24
|
prerelease: false
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
23
26
|
requirements:
|
24
|
-
- -
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.1.0
|
30
|
+
- - "<"
|
25
31
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
32
|
+
version: '5'
|
27
33
|
description: Useful tools to help construct database queries with ActiveRecord and
|
28
34
|
Arel.
|
29
35
|
email:
|
@@ -69,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
69
75
|
version: '0'
|
70
76
|
requirements: []
|
71
77
|
rubyforge_project:
|
72
|
-
rubygems_version: 2.
|
78
|
+
rubygems_version: 2.2.3
|
73
79
|
signing_key:
|
74
80
|
specification_version: 4
|
75
81
|
summary: Useful tools to help construct database queries with ActiveRecord and Arel.
|