arel-helpers 2.0.1 → 2.0.2

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: ea16c7f7e25c5cfbfffefce34a8c9533b0843476
4
- data.tar.gz: 1166b9bbb46d3752cd1dfbbbdca2af9722a1dd71
3
+ metadata.gz: 2f4827c570f384201da3e8d5b4cb4ba128647338
4
+ data.tar.gz: 41862cd346409a675ce805be9be7b9bf870f1fad
5
5
  SHA512:
6
- metadata.gz: d84deff5d029af7d5309206e2ad6d085b606cdf4e8f71f75339ccd1dad828f03fdf625021110fcbf20768fb7d77122877a649d9eaea533fc043acdd9f683d28f
7
- data.tar.gz: e3fa334933b5792e55d222696401c53bda1ace66ec973da7c27b5fe738f8506269434265cceda4aefb38b71c1bc771288520ebebbdd391004bd0ee088d26deef
6
+ metadata.gz: 74e3d838e06893f725de391c378367693fb1b6215124eeae4ae0f9357347b044fd35d53eafc38673f0fab9c60c05b340c33d543496eec7be259d6230f20b8f80
7
+ data.tar.gz: 7aac06ec8e7433ed527cb81ab85aa269affb9bbc760b144565c35f36386d5bfee44264bc7c1e48bb015ca9ff6b36c037c594d06b7b54e84e336caab1f483b1b1
data/History.txt CHANGED
@@ -18,3 +18,6 @@
18
18
 
19
19
  * Define ArelHelpers.join_association so people can use join_association functionality without relying on autoloading. (@peeja, github issue #8)
20
20
 
21
+ == 2.0.2
22
+
23
+ * Fix issue causing CollectionProxy#[] to return Arel::Attribute objects instead of model instances. See https://github.com/camertron/arel-helpers/pull/11
data/README.md CHANGED
@@ -65,7 +65,7 @@ Things start to get messy however if you want to do an outer join instead of the
65
65
  Post
66
66
  .joins(
67
67
  Post.arel_table.join(Comments.arel_table, Arel::OuterJoin)
68
- .on(Post[:id].eq(Coments[:post_id]))
68
+ .on(Post[:id].eq(Comments[:post_id]))
69
69
  .join_sources
70
70
  )
71
71
  ```
data/lib/arel-helpers.rb CHANGED
@@ -2,6 +2,20 @@
2
2
 
3
3
  require 'active_record'
4
4
 
5
+ begin
6
+ ar_version = if ActiveRecord::VERSION.const_defined?(:STRING)
7
+ ActiveRecord::VERSION::STRING
8
+ else
9
+ ActiveRecord.version.version
10
+ end
11
+
12
+ if ar_version >= '4.0.0'
13
+ require 'arel-helpers/ext/collection_proxy'
14
+ end
15
+ rescue
16
+ puts 'ArelHelpers was unable to determine the version of ActiveRecord. You may encounter unexpected behavior.'
17
+ end
18
+
5
19
  module ArelHelpers
6
20
  autoload :JoinAssociation, "arel-helpers/join_association"
7
21
  autoload :ArelTable, "arel-helpers/arel_table"
@@ -0,0 +1,35 @@
1
+ # encoding: UTF-8
2
+
3
+ # ActiveRecord as of version 4.0 defines a CollectionProxy class that
4
+ # lazily fetches records from the database. If posts has many comments,
5
+ # then post.comments returns a CollectionProxy that can be iterated over.
6
+ # A query to fetch the pertinent records isn't fired until the iteration
7
+ # starts, so post.comments itself isn't enough to cause a trip to the
8
+ # database.
9
+ #
10
+ # ArelHelpers adds a [] method to ActiveRecord::Relation which means it
11
+ # also adds the same method to CollectionProxy, since CollectionProxy
12
+ # inherits from Relation. For some reason, CollectionProxy doesn't define
13
+ # its own [] method but instead lets ActiveRecord's complicated Delegation
14
+ # module handle calls to it via method_missing. In the post.comments case
15
+ # illustrated above, a call to [] is handled by this method_missing:
16
+ # https://github.com/rails/rails/blob/4-1-stable/activerecord/lib/active_record/relation/delegation.rb#L91
17
+ # Here, @klass refers to the Comment model, which means [] calls on an
18
+ # instance of CollectionProxy actually get re-routed. Instead of calling
19
+ # [] on a CollectionProxy like we were expecting, it calls [] on Comment,
20
+ # and therefore on the mixed-in ArelHelpers::ArelTable. The final result
21
+ # is that the caller gets back an Arel::Attribute instead of the instance
22
+ # of Comment they were expecting.
23
+ #
24
+ # This "simple" monkey patch defines [] on CollectionProxy so the convoluted
25
+ # method_missing logic in Delegation doesn't get triggered, and therefore
26
+ # doesn't end up returning an unexpected Arel::Attribute.
27
+ module ActiveRecord
28
+ module Associations
29
+ class CollectionProxy < Relation
30
+ def [](index)
31
+ to_a[index]
32
+ end
33
+ end
34
+ end
35
+ end
@@ -1,5 +1,5 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  module ArelHelpers
4
- VERSION = "2.0.1"
4
+ VERSION = "2.0.2"
5
5
  end
@@ -10,4 +10,15 @@ describe ArelHelpers::ArelTable do
10
10
  post_id.relation.name.should == "posts"
11
11
  end
12
12
  end
13
+
14
+ it "should not interfere with associations" do
15
+ post = Post.create(title: "I'm a little teapot")
16
+ post.comments[0].should be_nil
17
+ end
18
+
19
+ it "should allow retrieving associated records" do
20
+ post = Post.create(title: "I'm a little teapot")
21
+ comment = post.comments.create
22
+ post.reload.comments[0].id.should == comment.id
23
+ end
13
24
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arel-helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cameron Dutro
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-28 00:00:00.000000000 Z
11
+ date: 2014-12-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -38,23 +38,24 @@ executables: []
38
38
  extensions: []
39
39
  extra_rdoc_files: []
40
40
  files:
41
- - Gemfile
42
- - History.txt
43
- - README.md
44
- - Rakefile
45
- - arel-helpers.gemspec
46
- - lib/arel-helpers.rb
47
41
  - lib/arel-helpers/arel_table.rb
42
+ - lib/arel-helpers/ext/collection_proxy.rb
48
43
  - lib/arel-helpers/join_association.rb
49
44
  - lib/arel-helpers/query_builder.rb
50
45
  - lib/arel-helpers/version.rb
46
+ - lib/arel-helpers.rb
51
47
  - spec/arel_table_spec.rb
52
- - spec/env.rb
53
48
  - spec/env/migrations.rb
54
49
  - spec/env/models.rb
50
+ - spec/env.rb
55
51
  - spec/join_association_spec.rb
56
52
  - spec/query_builder_spec.rb
57
53
  - spec/spec_helper.rb
54
+ - Gemfile
55
+ - History.txt
56
+ - README.md
57
+ - Rakefile
58
+ - arel-helpers.gemspec
58
59
  homepage: http://github.com/camertron
59
60
  licenses: []
60
61
  metadata: {}
@@ -74,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
75
  version: '0'
75
76
  requirements: []
76
77
  rubyforge_project:
77
- rubygems_version: 2.2.2
78
+ rubygems_version: 2.0.14
78
79
  signing_key:
79
80
  specification_version: 4
80
81
  summary: Useful tools to help construct database queries with ActiveRecord and Arel.