ar_outer_joins 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b08109bce18501fcb96033fa12ba0233f2fa185c
4
+ data.tar.gz: ab591b631e058e82c867d026712e7fbf389cb39b
5
+ SHA512:
6
+ metadata.gz: 703f36b3498b00aa953c2133e915923267911fa5aaacef7de096842b1a58f1fb89b2b09f2d8b880b4fcf3a6b4abb1297ef0135b5f242d36e3f4ee877f5935460
7
+ data.tar.gz: 5417ff4dd08024915f596eb70c9646158d125119553951d7397b91ff7ea3d2419d90fdb5ad1f884cd29da3f4033acf4172a9c5fafc1dddc51c5bd02f18b42425
@@ -0,0 +1,7 @@
1
+ appraise "ar3.2" do
2
+ gem "activerecord", "~>3.2.0"
3
+ end
4
+
5
+ appraise "ar4" do
6
+ gem "activerecord", "4.0.0.beta1"
7
+ end
data/Gemfile CHANGED
@@ -1,4 +1,6 @@
1
1
  source 'http://rubygems.org'
2
2
 
3
+ gem "appraisal"
4
+
3
5
  # Specify your gem's dependencies in ar_outer_joins.gemspec
4
6
  gemspec
data/README.md CHANGED
@@ -20,7 +20,7 @@ require "ar_outer_joins"
20
20
  class Product
21
21
  belongs_to :category
22
22
 
23
- def published
23
+ def self.published
24
24
  outer_joins(:category).where("categories.published = ? OR products.published = ?", true, true)
25
25
  end
26
26
  end
data/Rakefile CHANGED
@@ -1,2 +1,16 @@
1
1
  #!/usr/bin/env rake
2
+
3
+ require "rubygems"
4
+ require "bundler/setup"
5
+ require "appraisal"
6
+ require "rspec/core/rake_task"
7
+
2
8
  require "bundler/gem_tasks"
9
+
10
+ desc "Run all examples"
11
+ RSpec::Core::RakeTask.new(:spec) do |t|
12
+ #t.rspec_path = 'bin/rspec'
13
+ t.rspec_opts = %w[--color]
14
+ end
15
+
16
+ task :default => :spec
@@ -15,7 +15,7 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = ArOuterJoins::VERSION
17
17
 
18
- gem.add_dependency "activerecord", "~>3.2"
18
+ gem.add_dependency "activerecord", ">=3.2"
19
19
  gem.add_development_dependency "rspec"
20
20
  gem.add_development_dependency "sqlite3"
21
21
  gem.add_development_dependency "pry"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "appraisal"
6
+ gem "activerecord", "~>3.2.0"
7
+
8
+ gemspec :path=>"../"
@@ -0,0 +1,57 @@
1
+ PATH
2
+ remote: /Users/dev/Projects/ar_outer_joins
3
+ specs:
4
+ ar_outer_joins (0.0.1)
5
+ activerecord (>= 3.2)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ activemodel (3.2.12)
11
+ activesupport (= 3.2.12)
12
+ builder (~> 3.0.0)
13
+ activerecord (3.2.12)
14
+ activemodel (= 3.2.12)
15
+ activesupport (= 3.2.12)
16
+ arel (~> 3.0.2)
17
+ tzinfo (~> 0.3.29)
18
+ activesupport (3.2.12)
19
+ i18n (~> 0.6)
20
+ multi_json (~> 1.0)
21
+ appraisal (0.5.1)
22
+ bundler
23
+ rake
24
+ arel (3.0.2)
25
+ builder (3.0.4)
26
+ coderay (1.0.9)
27
+ diff-lcs (1.2.1)
28
+ i18n (0.6.4)
29
+ method_source (0.8.1)
30
+ multi_json (1.6.1)
31
+ pry (0.9.12)
32
+ coderay (~> 1.0.5)
33
+ method_source (~> 0.8)
34
+ slop (~> 3.4)
35
+ rake (10.0.3)
36
+ rspec (2.13.0)
37
+ rspec-core (~> 2.13.0)
38
+ rspec-expectations (~> 2.13.0)
39
+ rspec-mocks (~> 2.13.0)
40
+ rspec-core (2.13.0)
41
+ rspec-expectations (2.13.0)
42
+ diff-lcs (>= 1.1.3, < 2.0)
43
+ rspec-mocks (2.13.0)
44
+ slop (3.4.3)
45
+ sqlite3 (1.3.7)
46
+ tzinfo (0.3.36)
47
+
48
+ PLATFORMS
49
+ ruby
50
+
51
+ DEPENDENCIES
52
+ activerecord (~> 3.2.0)
53
+ appraisal
54
+ ar_outer_joins!
55
+ pry
56
+ rspec
57
+ sqlite3
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "appraisal"
6
+ gem "activerecord", "4.0.0.beta1"
7
+
8
+ gemspec :path=>"../"
@@ -0,0 +1,65 @@
1
+ PATH
2
+ remote: /Users/dev/Projects/ar_outer_joins
3
+ specs:
4
+ ar_outer_joins (0.0.1)
5
+ activerecord (>= 3.2)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ activemodel (4.0.0.beta1)
11
+ activesupport (= 4.0.0.beta1)
12
+ builder (~> 3.1.0)
13
+ activerecord (4.0.0.beta1)
14
+ activemodel (= 4.0.0.beta1)
15
+ activerecord-deprecated_finders (~> 0.0.3)
16
+ activesupport (= 4.0.0.beta1)
17
+ arel (~> 4.0.0.beta1)
18
+ activerecord-deprecated_finders (0.0.3)
19
+ activesupport (4.0.0.beta1)
20
+ i18n (~> 0.6.2)
21
+ minitest (~> 4.2)
22
+ multi_json (~> 1.3)
23
+ thread_safe (~> 0.1)
24
+ tzinfo (~> 0.3.33)
25
+ appraisal (0.5.1)
26
+ bundler
27
+ rake
28
+ arel (4.0.0.beta1)
29
+ atomic (1.0.1)
30
+ builder (3.1.4)
31
+ coderay (1.0.9)
32
+ diff-lcs (1.2.1)
33
+ i18n (0.6.4)
34
+ method_source (0.8.1)
35
+ minitest (4.6.2)
36
+ multi_json (1.6.1)
37
+ pry (0.9.12)
38
+ coderay (~> 1.0.5)
39
+ method_source (~> 0.8)
40
+ slop (~> 3.4)
41
+ rake (10.0.3)
42
+ rspec (2.13.0)
43
+ rspec-core (~> 2.13.0)
44
+ rspec-expectations (~> 2.13.0)
45
+ rspec-mocks (~> 2.13.0)
46
+ rspec-core (2.13.0)
47
+ rspec-expectations (2.13.0)
48
+ diff-lcs (>= 1.1.3, < 2.0)
49
+ rspec-mocks (2.13.0)
50
+ slop (3.4.3)
51
+ sqlite3 (1.3.7)
52
+ thread_safe (0.1.0)
53
+ atomic
54
+ tzinfo (0.3.36)
55
+
56
+ PLATFORMS
57
+ ruby
58
+
59
+ DEPENDENCIES
60
+ activerecord (= 4.0.0.beta1)
61
+ appraisal
62
+ ar_outer_joins!
63
+ pry
64
+ rspec
65
+ sqlite3
@@ -8,7 +8,9 @@ module ArOuterJoins
8
8
  association_joins, regular_joins = joins.partition do |join|
9
9
  join.is_a?(Hash) or join.is_a?(Array) or join.is_a?(Symbol)
10
10
  end
11
- Join.new(self).apply(*association_joins).joins(*regular_joins)
11
+ join_set = Join.new(self).apply(*association_joins)
12
+ join_set = join_set.joins(*regular_joins) unless regular_joins.empty?
13
+ join_set
12
14
  end
13
15
  end
14
16
 
@@ -20,7 +20,15 @@ module ArOuterJoins
20
20
  end
21
21
 
22
22
  def apply(*args)
23
- klass.joins(generate(*args))
23
+ scope = if klass.all.is_a?(ActiveRecord::Relation) then klass.all else klass.scoped end
24
+ joins = scope.joins_values.select.map { |j| j.to_sql if j.respond_to?(:to_sql) }
25
+ generate(*args).flatten.inject(scope) do |scope, join|
26
+ if joins.include?(join.to_sql)
27
+ scope
28
+ else
29
+ scope.joins(join)
30
+ end
31
+ end
24
32
  end
25
33
  end
26
34
  end
@@ -24,7 +24,11 @@ module ArOuterJoins
24
24
  on = Arel::Nodes::On.new(table[association.foreign_key].eq(joined_table[primary_key]))
25
25
  [Arel::Nodes::OuterJoin.new(joined_table, on)]
26
26
  when :has_and_belongs_to_many
27
- join_model_table = Arel::Table.new(association.options[:join_table])
27
+ join_model_table = if association.respond_to?(:join_table)
28
+ Arel::Table.new(association.join_table)
29
+ else
30
+ Arel::Table.new(association.options[:join_table])
31
+ end
28
32
  joined_primary_key = association.klass.primary_key
29
33
 
30
34
  on1 = Arel::Nodes::On.new(join_model_table[association.foreign_key].eq(table[primary_key]))
@@ -1,3 +1,3 @@
1
1
  module ArOuterJoins
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -39,7 +39,7 @@ describe ActiveRecord::Base do
39
39
  product2 = Product.create! :category => category2
40
40
  product3 = Product.create! :published => true
41
41
  query = Product.outer_joins(:category).where("categories.name = ? OR products.published = ?", "Shirts", true)
42
- query.all.should =~ [product2, product3]
42
+ query.to_a.should =~ [product2, product3]
43
43
  end
44
44
 
45
45
  it "joins several associations" do
@@ -51,7 +51,7 @@ describe ActiveRecord::Base do
51
51
  product3 = Product.create! :published => true
52
52
  product4 = Product.create! :site => site1
53
53
  query = Product.outer_joins(:category, :site).where("sites.name = ? OR categories.name = ? OR products.published = ?", "Elabs", "Shirts", true)
54
- query.all.should =~ [product2, product3, product4]
54
+ query.to_a.should =~ [product2, product3, product4]
55
55
  end
56
56
  end
57
57
 
@@ -66,7 +66,7 @@ describe ActiveRecord::Base do
66
66
  Image.create! :product => product2
67
67
 
68
68
  query = Product.outer_joins(:image).where("images.highres = ? OR products.published = ?", true, true)
69
- query.all.should =~ [product1, product3]
69
+ query.to_a.should =~ [product1, product3]
70
70
  end
71
71
  end
72
72
 
@@ -81,7 +81,7 @@ describe ActiveRecord::Base do
81
81
  LineItem.create! :product => product2
82
82
 
83
83
  query = Product.outer_joins(:line_items).where("line_items.price = ? OR products.published = ?", 4, true)
84
- query.all.should =~ [product1, product3]
84
+ query.to_a.should =~ [product1, product3]
85
85
  end
86
86
  end
87
87
 
@@ -97,7 +97,7 @@ describe ActiveRecord::Base do
97
97
 
98
98
 
99
99
  query = Product.outer_joins(:tags).where("tags.name = ? OR products.published = ?", "Red", true)
100
- query.all.should =~ [product2, product3, product4]
100
+ query.to_a.should =~ [product2, product3, product4]
101
101
  end
102
102
  end
103
103
 
@@ -116,7 +116,7 @@ describe ActiveRecord::Base do
116
116
  LineItem.create! :product => product3
117
117
 
118
118
  query = Product.outer_joins(:baskets).where("baskets.purchased = ? OR products.published = ?", true, true)
119
- query.all.should =~ [product1, product3]
119
+ query.to_a.should =~ [product1, product3]
120
120
  end
121
121
  end
122
122
 
@@ -135,7 +135,7 @@ describe ActiveRecord::Base do
135
135
  LineItem.create! :product => product3
136
136
 
137
137
  query = Product.outer_joins(:line_items => :basket).where("baskets.purchased = ? OR products.published = ?", true, true)
138
- query.all.should =~ [product1, product3]
138
+ query.to_a.should =~ [product1, product3]
139
139
  end
140
140
 
141
141
  it "allows hashes with arrays" do
@@ -154,7 +154,15 @@ describe ActiveRecord::Base do
154
154
  Discount.create! :line_item => line_item3, :percentage => 80
155
155
 
156
156
  query = Product.outer_joins(:line_items => [:basket, :discounts]).where("baskets.purchased = ? OR products.published = ? OR discounts.percentage > ?", true, true, 50)
157
- query.all.should =~ [product1, product3, product4]
157
+ query.to_a.should =~ [product1, product3, product4]
158
+ end
159
+
160
+ it "does not perform join more than once" do
161
+ query = Product.outer_joins(:line_items => [:basket, :discounts]).outer_joins("INNER JOIN images ON products.id = images.product_id")
162
+ query = query.outer_joins(:line_items => [:discounts])
163
+ query = query.outer_joins(:line_items)
164
+ query = query.outer_joins(:line_items => :basket)
165
+ query.to_a.should be_empty
158
166
  end
159
167
  end
160
168
 
@@ -167,7 +175,7 @@ describe ActiveRecord::Base do
167
175
  product3 = Product.create! :published => true
168
176
  query = Product.outer_joins("LEFT OUTER JOIN categories ON products.category_id = categories.id")
169
177
  query = query.where("categories.name = ? OR products.published = ?", "Shirts", true)
170
- query.all.should =~ [product2, product3]
178
+ query.to_a.should =~ [product2, product3]
171
179
  end
172
180
 
173
181
  it "allows multiple joins" do
@@ -178,7 +186,7 @@ describe ActiveRecord::Base do
178
186
  product3 = Product.create! :published => true
179
187
  query = Product.outer_joins(:line_items, "LEFT OUTER JOIN categories ON products.category_id = categories.id")
180
188
  query = query.where("categories.name = ? OR products.published = ?", "Shirts", true)
181
- query.all.should =~ [product2, product3]
189
+ query.to_a.should =~ [product2, product3]
182
190
  end
183
191
 
184
192
  it "allows arel joins" do
@@ -193,7 +201,7 @@ describe ActiveRecord::Base do
193
201
 
194
202
  query = Product.outer_joins(join)
195
203
  query = query.where("categories.name = ? OR products.published = ?", "Shirts", true)
196
- query.all.should =~ [product2, product3]
204
+ query.to_a.should =~ [product2, product3]
197
205
  end
198
206
  end
199
207
  end
@@ -48,6 +48,10 @@ ActiveRecord::Base.connection.create_table :images do |t|
48
48
  end
49
49
 
50
50
  RSpec.configure do |config|
51
+ config.expect_with :rspec do |c|
52
+ c.syntax = [:should, :expect]
53
+ end
54
+
51
55
  config.around do |example|
52
56
  ActiveRecord::Base.transaction do
53
57
  example.run
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar_outer_joins
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
5
- prerelease:
4
+ version: 0.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Jonas Nicklas
@@ -10,52 +9,64 @@ authors:
10
9
  autorequire:
11
10
  bindir: bin
12
11
  cert_chain: []
13
- date: 2012-11-02 00:00:00.000000000 Z
12
+ date: 2014-09-08 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: activerecord
17
- requirement: &2159798540 !ruby/object:Gem::Requirement
18
- none: false
16
+ requirement: !ruby/object:Gem::Requirement
19
17
  requirements:
20
- - - ~>
18
+ - - ">="
21
19
  - !ruby/object:Gem::Version
22
20
  version: '3.2'
23
21
  type: :runtime
24
22
  prerelease: false
25
- version_requirements: *2159798540
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '3.2'
26
28
  - !ruby/object:Gem::Dependency
27
29
  name: rspec
28
- requirement: &2159798120 !ruby/object:Gem::Requirement
29
- none: false
30
+ requirement: !ruby/object:Gem::Requirement
30
31
  requirements:
31
- - - ! '>='
32
+ - - ">="
32
33
  - !ruby/object:Gem::Version
33
34
  version: '0'
34
35
  type: :development
35
36
  prerelease: false
36
- version_requirements: *2159798120
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
37
42
  - !ruby/object:Gem::Dependency
38
43
  name: sqlite3
39
- requirement: &2159797660 !ruby/object:Gem::Requirement
40
- none: false
44
+ requirement: !ruby/object:Gem::Requirement
41
45
  requirements:
42
- - - ! '>='
46
+ - - ">="
43
47
  - !ruby/object:Gem::Version
44
48
  version: '0'
45
49
  type: :development
46
50
  prerelease: false
47
- version_requirements: *2159797660
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
48
56
  - !ruby/object:Gem::Dependency
49
57
  name: pry
50
- requirement: &2159797200 !ruby/object:Gem::Requirement
51
- none: false
58
+ requirement: !ruby/object:Gem::Requirement
52
59
  requirements:
53
- - - ! '>='
60
+ - - ">="
54
61
  - !ruby/object:Gem::Version
55
62
  version: '0'
56
63
  type: :development
57
64
  prerelease: false
58
- version_requirements: *2159797200
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
59
70
  description: Adds the missing outer_joins method to ActiveRecord
60
71
  email:
61
72
  - jonas.nicklas@gmail.com
@@ -64,11 +75,16 @@ executables: []
64
75
  extensions: []
65
76
  extra_rdoc_files: []
66
77
  files:
67
- - .gitignore
78
+ - ".gitignore"
79
+ - Appraisals
68
80
  - Gemfile
69
81
  - README.md
70
82
  - Rakefile
71
- - ar_outer_joins.gemspec
83
+ - ar_outer_join.gemspec
84
+ - gemfiles/ar3.2.gemfile
85
+ - gemfiles/ar3.2.gemfile.lock
86
+ - gemfiles/ar4.gemfile
87
+ - gemfiles/ar4.gemfile.lock
72
88
  - lib/ar_outer_joins.rb
73
89
  - lib/ar_outer_joins/join.rb
74
90
  - lib/ar_outer_joins/join_builder.rb
@@ -77,27 +93,26 @@ files:
77
93
  - spec/spec_helper.rb
78
94
  homepage: http://github.com/elabs/ar_outer_joins
79
95
  licenses: []
96
+ metadata: {}
80
97
  post_install_message:
81
98
  rdoc_options: []
82
99
  require_paths:
83
100
  - lib
84
101
  required_ruby_version: !ruby/object:Gem::Requirement
85
- none: false
86
102
  requirements:
87
- - - ! '>='
103
+ - - ">="
88
104
  - !ruby/object:Gem::Version
89
105
  version: '0'
90
106
  required_rubygems_version: !ruby/object:Gem::Requirement
91
- none: false
92
107
  requirements:
93
- - - ! '>='
108
+ - - ">="
94
109
  - !ruby/object:Gem::Version
95
110
  version: '0'
96
111
  requirements: []
97
112
  rubyforge_project:
98
- rubygems_version: 1.8.10
113
+ rubygems_version: 2.2.2
99
114
  signing_key:
100
- specification_version: 3
115
+ specification_version: 4
101
116
  summary: outer_joins for ActiveRecord
102
117
  test_files:
103
118
  - spec/outer_join_spec.rb