polymorphic_preloader 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4dd69a1e51664e96c696043f891baa22fc411721
4
+ data.tar.gz: 1a0b2a19bcd376494a53cafea94f83c2f0493e2f
5
+ SHA512:
6
+ metadata.gz: 9f2a7928d774cecaa05f983fc479ed798498b4173bcd5dd9739743b952258ffbaaa7ed47d8de4f933b98206a84da61b9519209712349ba59c3135e69eee03d06
7
+ data.tar.gz: 62d275ed66ac8ae974e89bc4f6126bdd84dd30fcc9b5570a6409735bebbef41f43261062f508271cf7078229e5cae3b9dbde6769f2e72ae17ec7ee205d0c1395
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
@@ -0,0 +1,8 @@
1
+ # Submitting a Pull Request
2
+
3
+ 1. Fork it ( https://github.com/antstorm/polymorphic_preloader/fork )
4
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
5
+ 3. Implement your feature and write tests for it (`bundle exec rake`)
6
+ 4. Commit your changes (`git commit -am 'Add some feature'`)
7
+ 5. Push to the branch (`git push origin my-new-feature`)
8
+ 6. Create a new Pull Request
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'railties', '>= 4.0'
4
+ gem 'activerecord', '>= 4.0', require: 'active_record/associations/preloader'
5
+
6
+ gemspec
7
+
8
+ gem 'rake', '~> 10.0'
9
+ gem 'rails', '>= 4.0'
10
+ gem 'rspec-rails'
11
+ gem 'pry'
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Anthony Dmitriyev
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,57 @@
1
+ # PolymorphicPreloader
2
+
3
+ This gem does exactly what it says — it preloads nested polymorphic associations on ActiveRecord objects.
4
+
5
+ Here's a common example:
6
+
7
+ ```ruby
8
+ class Transaction < ActiveRecord::Base
9
+ belongs_to :user
10
+ belongs_to :purchase, polymorphic: true
11
+ end
12
+
13
+ class Product < ActiveRecord::Base
14
+ has_many :transactions, as: :purchase
15
+ belongs_to :company
16
+ end
17
+
18
+ class Service < ActiveRecord::Base
19
+ has_many :transactions, as: :purchase
20
+ belongs_to :provider
21
+ end
22
+ ```
23
+
24
+ Suppose we want to show all the products and services that user has purchased. Apparently we want to eager-load `products` with `companies` and `services` with `providers`.
25
+ ActiveRecord won't allow you to do `includes(purchase: [ :company, :provider ])`. Here's where PolymorphicPreloader comes to help.
26
+
27
+ ## Installation
28
+
29
+ Add this line to your application's Gemfile:
30
+
31
+ ```ruby
32
+ gem 'polymorphic_preloader', github: 'antstorm/polymorphic_preloader', branch: 'initial-version'
33
+ ```
34
+
35
+ And then execute:
36
+
37
+ $ bundle
38
+
39
+ Or install it yourself as:
40
+
41
+ $ gem install polymorphic_preloader
42
+
43
+ ## Usage
44
+
45
+ Considering the above example:
46
+
47
+ ```ruby
48
+ transactions = current_user.transactions.includes(:purchase)
49
+
50
+ PolymorphicPreloader.new(transactions, :purchase).preload!(product: :company, service: :provider)
51
+ ```
52
+
53
+ ## TODO
54
+
55
+ - Ensure good test coverage
56
+ - Tidy up and release as a gem
57
+ - Hook into ActiveRecord for a delayed preloading and nicer interface (e.g. `includes_polymorphic()`)
@@ -0,0 +1,15 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ begin
5
+ Bundler.setup(:default, :development)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.puts e.message
8
+ $stderr.puts "Run `bundle install` to install missing gems"
9
+ exit e.status_code
10
+ end
11
+
12
+ RSpec::Core::RakeTask.new
13
+
14
+ task default: :spec
15
+ task test: :spec
@@ -0,0 +1,26 @@
1
+ require 'polymorphic_preloader/version'
2
+
3
+ class PolymorphicPreloader
4
+ attr_accessor :grouped_objects, :association_name
5
+
6
+ def initialize(objects, association_name)
7
+ self.association_name = association_name
8
+ self.grouped_objects = objects.group_by { |object| object.send("#{association_name}_type") }
9
+ end
10
+
11
+ def preload!(associations)
12
+ grouped_objects.each_pair do |object_type, objects_with_same_association|
13
+ target_associations = associations[object_type.to_s.underscore.to_sym]
14
+
15
+ if target_associations.present?
16
+ preloader.preload objects_with_same_association, association_name => target_associations
17
+ end
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def preloader
24
+ @preloader ||= ActiveRecord::Associations::Preloader.new
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ class PolymorphicPreloader
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,19 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'polymorphic_preloader/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'polymorphic_preloader'
8
+ spec.version = PolymorphicPreloader::VERSION
9
+ spec.authors = ['Anthony Dmitriyev']
10
+ spec.email = ['antstorm@gmail.com']
11
+ spec.summary = 'Eager loading nested polymorphic associations in ActiveRecord'
12
+ spec.description = 'Adds the ability to preload nested polymorphic associations in your ActiveRecord queries'
13
+ spec.homepage = 'https://github.com/antstorm/polymorphic_preloader'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split("\n")
17
+ spec.test_files = `git ls-files -- spec/*`.split("\n")
18
+ spec.require_paths = ['lib']
19
+ end
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ describe PolymorphicPreloader do
4
+ subject { PolymorphicPreloader }
5
+
6
+ it 'can be initialized' do
7
+ preloader = subject.new([], :key)
8
+
9
+ expect(preloader).to be_instance_of(PolymorphicPreloader)
10
+ end
11
+
12
+ it 'stores a local reference to ActiveRecord preloader' do
13
+ preloader = subject.new([], :key)
14
+
15
+ expect(preloader.send(:preloader)).to be_instance_of(ActiveRecord::Associations::Preloader)
16
+ end
17
+
18
+ it 'stores association key' do
19
+ preloader = subject.new([], :polymorphic)
20
+
21
+ expect(preloader.association_name).to be_eql(:polymorphic)
22
+ end
23
+
24
+ it 'groups objects by polymorphic association' do
25
+ object_1 = double
26
+ allow(object_1).to receive(:polymorphic_type) { 'First' }
27
+
28
+ object_2 = double
29
+ allow(object_2).to receive(:polymorphic_type) { 'Second' }
30
+
31
+ preloader = subject.new([object_1, object_2], :polymorphic)
32
+
33
+ expect(preloader.grouped_objects).to include('First' => [object_1])
34
+ expect(preloader.grouped_objects).to include('Second' => [object_2])
35
+ end
36
+
37
+ it 'preloads association' do
38
+ object = double
39
+ allow(object).to receive(:polymorphic_type) { 'Object' }
40
+
41
+ preloader = subject.new([object], :polymorphic)
42
+
43
+ expect(preloader.send(:preloader)).to receive(:preload).with([object], polymorphic: :association)
44
+
45
+ preloader.preload!(object: :association)
46
+ end
47
+
48
+ it 'preloads multiple associations' do
49
+ object_1 = double
50
+ allow(object_1).to receive(:polymorphic_type) { 'FirstObject' }
51
+
52
+ object_2 = double
53
+ allow(object_2).to receive(:polymorphic_type) { 'SecondObject' }
54
+
55
+ preloader = subject.new([object_1, object_2], :polymorphic)
56
+
57
+ expect(preloader.send(:preloader)).to receive(:preload).with([object_1], polymorphic: :association_1)
58
+ expect(preloader.send(:preloader)).to receive(:preload).with([object_2], polymorphic: :association_2)
59
+
60
+ preloader.preload!(first_object: :association_1, second_object: :association_2)
61
+ end
62
+
63
+ it 'does not preload not specified associations' do
64
+ object = double
65
+ allow(object).to receive(:polymorphic_type) { 'Object' }
66
+
67
+ preloader = subject.new([object], :polymorphic)
68
+
69
+ expect(preloader.send(:preloader)).not_to receive(:preload)
70
+
71
+ preloader.preload!(another_object: :association)
72
+ end
73
+ end
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ Bundler.require
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: polymorphic_preloader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Anthony Dmitriyev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-05 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Adds the ability to preload nested polymorphic associations in your ActiveRecord
14
+ queries
15
+ email:
16
+ - antstorm@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - CONTRIBUTING.md
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - lib/polymorphic_preloader.rb
28
+ - lib/polymorphic_preloader/version.rb
29
+ - polymorphic_preloader.gemspec
30
+ - spec/polymorphic_preloader_spec.rb
31
+ - spec/spec_helper.rb
32
+ homepage: https://github.com/antstorm/polymorphic_preloader
33
+ licenses:
34
+ - MIT
35
+ metadata: {}
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 2.4.5
53
+ signing_key:
54
+ specification_version: 4
55
+ summary: Eager loading nested polymorphic associations in ActiveRecord
56
+ test_files:
57
+ - spec/polymorphic_preloader_spec.rb
58
+ - spec/spec_helper.rb