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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7caa6de6e09401386429457aabaaebbe22cc7644
4
- data.tar.gz: 9f60d6693ff7bb7fe28e81b89d96544c30754388
3
+ metadata.gz: 875e70afa83c9d6c1c5b74713162b2eef19c94b1
4
+ data.tar.gz: 562a0e972c495904a876f5cfcbf08dbadf71b058
5
5
  SHA512:
6
- metadata.gz: 7eedcd17a17eb46a6cbef05be4470eb96fe9cf0e462cddf6b5313de9dea179c7497a8d83207923c0684a6088f3edb6022fc1468dfa02741e8bec725cdfd2f10f
7
- data.tar.gz: e99be0b2d82076d2c24b898fd6ae08dac884fb2d34378dcb9778e36e1b2ebcd3ae7d52a16862a5eaba342157255bb06d26038f5c68e85e3f3b8fcee6aa00c90c
6
+ metadata.gz: 2a364dbb1ccbe4937fa23892678261cdfabc4846111499748f244701f210d7a8996465f80f8d14d7c9add1ab4706f3b4a71d48736caa12ffed20f6b6c5f223f1
7
+ data.tar.gz: 746357d316c5eb82e29952f2a5fd54ffebae6e550acb7e8eebdf7fcc9bc6562e37975217afde0f8f4e441495b5789c907d0f2a0e22f398ef06d7913e842de0fe
data/History.txt CHANGED
@@ -29,3 +29,7 @@
29
29
  == 2.1.1
30
30
 
31
31
  * Fixing issue causing ArelTable instances to get returned when accessing records inside an ActiveRecord::Relation. (@svoynow, #18)
32
+
33
+ == 2.2.0
34
+
35
+ * Adding polymorphic join support for Rails 4.2.
@@ -56,30 +56,55 @@ module ArelHelpers
56
56
 
57
57
  join_dependency.join_constraints([]).map do |constraint|
58
58
  right = if block_given?
59
- yield constraint.left.name.to_sym, constraint.right
60
- else
61
- constraint.right
62
- end
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([]).map do |constraint|
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
- yield join.left.name.to_sym, join.right
76
- else
77
- join.right
78
- end
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
@@ -1,5 +1,5 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  module ArelHelpers
4
- VERSION = "2.1.1"
4
+ VERSION = "2.2.0"
5
5
  end
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
@@ -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.1.1
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: 2015-10-31 00:00:00.000000000 Z
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: 4.0.13
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: 4.0.13
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.4.8
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.