eager_loadable_polymorphic_association 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in eager_loadable_polymorphic_association.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 moro
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.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # EagerLoadablePolymorphicAssociation
2
+
3
+ add eager loading functionality to ActiveRecord's polymorphic association.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'eager_loadable_polymorphic_association'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install eager_loadable_polymorphic_association
18
+
19
+ ## Usage
20
+
21
+ ```
22
+ class Paste < ActiveRecord::Base
23
+ extend EagerLoadablePolymorphicAssociation
24
+
25
+ belongs_to :item, polymorphic: true
26
+ eager_loadable_polymorphic_association :item, [:snippet, :picture]
27
+ end
28
+
29
+ Paste.with_item.all # => eager load associated snippet or picture
30
+ ```
31
+
32
+ 1. extend this module
33
+ 2. decrare `eager\_loadable\_polymorphic\_association` during class definition.
34
+ 1. first argument is polymorphic association name
35
+ 2. second, pass an array which contains associated objects class names (item_type.under_score).
36
+ 3. step 2 defines new scope `with\_item`, which is the scope to eager load polymorphic objects.
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec) do |t|
7
+ t.pattern = "spec/**/*_spec.rb"
8
+ end
9
+
10
+ task :default => [:spec]
11
+
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'eager_loadable_polymorphic_association/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "eager_loadable_polymorphic_association"
8
+ gem.version = EagerLoadablePolymorphicAssociation::VERSION
9
+ gem.authors = ["moro"]
10
+ gem.email = ["moronatural@gmail.com"]
11
+ gem.description = %q{add eager loading functionality to ActiveRecord's polymorphic association.}
12
+ gem.summary = %q{add eager loading functionality to ActiveRecord's polymorphic association.}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "activerecord", [">= 3.0"]
21
+
22
+ gem.add_development_dependency "rspec", [">= 2.0"]
23
+ gem.add_development_dependency "rake"
24
+ gem.add_development_dependency "sqlite3"
25
+ gem.add_development_dependency "database_cleaner"
26
+ end
@@ -0,0 +1,62 @@
1
+ require 'active_support/core_ext/string/strip'
2
+ require 'eager_loadable_polymorphic_association/version'
3
+
4
+ module EagerLoadablePolymorphicAssociation
5
+ class AssociationWriter
6
+ def initialize(association, types)
7
+ raise ArgumentError, "#{association} is not polymorphic association" unless association.options[:polymorphic]
8
+ @association = association
9
+ @types = types
10
+ end
11
+
12
+ def belong_to_them(ref_from)
13
+ @types.each do |t|
14
+ ref_from.belongs_to t, readonly: true, foreign_key: fk, conditions: condition_sql(t.to_s.classify.constantize)
15
+ end
16
+ end
17
+
18
+ def define_scope(ref_from)
19
+ ref_from.scope "with_#{@association.name}", ref_from.includes(*@types)
20
+ end
21
+
22
+ def override_accessor(ref_from)
23
+ ref_from.class_eval <<-RUBY.strip_heredoc
24
+ def #{@association.name}
25
+ concreate_item = association(self[#{ft.dump}].underscore.to_sym)
26
+ polymorphic_item = association(:#{@association.name})
27
+ if concreate_item.loaded? && !polymorphic_item.loaded?
28
+ polymorphic_item.target = concreate_item.target
29
+ end
30
+ super
31
+ end
32
+ RUBY
33
+ end
34
+
35
+ private
36
+
37
+ # aliasses
38
+ def ft
39
+ @association.foreign_type
40
+ end
41
+
42
+ def fk
43
+ @association.foreign_key
44
+ end
45
+
46
+ def tbl
47
+ @association.active_record.quoted_table_name
48
+ end
49
+
50
+ def condition_sql(klass)
51
+ ["EXISTS(SELECT 1 FROM #{tbl} WHERE #{tbl}.#{ft} = ? AND #{tbl}.#{fk} = #{klass.quoted_table_name}.#{klass.primary_key})", klass.name]
52
+ end
53
+ end
54
+
55
+ def eager_loadable_polymorphic_association(association_name, types)
56
+ assoc = AssociationWriter.new(reflections[association_name.to_sym], types)
57
+
58
+ assoc.belong_to_them(self)
59
+ assoc.define_scope(self)
60
+ assoc.override_accessor(self)
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ module EagerLoadablePolymorphicAssociation
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,33 @@
1
+ require 'active_record'
2
+
3
+ ActiveRecord::Base.establish_connection({
4
+ adapter: 'sqlite3',
5
+ database: ':memory:'
6
+ })
7
+
8
+ ActiveRecord::Schema.define(:version => 1) do
9
+ create_table 'pastes', force: true do |t|
10
+ t.belongs_to :item, polymorphic: true
11
+ t.timestamps
12
+ end
13
+
14
+ create_table 'snippets', force: true do |t|
15
+ t.text 'content', null: false
16
+ t.timestamps
17
+ end
18
+
19
+ create_table 'pictures', force: true do |t|
20
+ t.string :url, null: false
21
+ t.timestamps
22
+ end
23
+ end
24
+
25
+ class Paste < ActiveRecord::Base
26
+ belongs_to :item, polymorphic: true
27
+ end
28
+
29
+ class Snippet < ActiveRecord::Base
30
+ end
31
+
32
+ class Picture < ActiveRecord::Base
33
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+ require 'eager_loadable_polymorphic_association'
4
+
5
+ describe EagerLoadablePolymorphicAssociation do
6
+ Paste.class_eval do
7
+ extend EagerLoadablePolymorphicAssociation
8
+
9
+ eager_loadable_polymorphic_association :item, [:snippet, :picture]
10
+ end
11
+
12
+ context 'included into Paste' do
13
+ let!(:snippet) do
14
+ Snippet.create!(content: 'Lorem porem').tap {|item| Paste.create(item: item) }
15
+ end
16
+
17
+ let!(:picture) {
18
+ Picture.create!(url: 'http://example.com/example.jpeg').tap {|item| Paste.create(item: item) }
19
+ }
20
+
21
+ before do
22
+ @loaded_pastes = Paste.with_item.order(:id).all
23
+ ActiveRecord::Base.connection.should_not_receive(:select_all)
24
+ end
25
+
26
+ specify { @loaded_pastes.map(&:item).should =~ [snippet, picture] }
27
+ end
28
+ end
29
+
@@ -0,0 +1,15 @@
1
+ $: << File.expand_path('../lib', File.dirname(__FILE__))
2
+ require 'active_record_models'
3
+
4
+ require 'database_cleaner'
5
+ DatabaseCleaner.strategy = :transaction
6
+
7
+ RSpec.configure do |config|
8
+ config.mock_with :rspec
9
+ config.before do
10
+ DatabaseCleaner.start
11
+ end
12
+ config.after do
13
+ DatabaseCleaner.clean
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eager_loadable_polymorphic_association
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - moro
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '2.0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '2.0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: sqlite3
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: database_cleaner
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: add eager loading functionality to ActiveRecord's polymorphic association.
95
+ email:
96
+ - moronatural@gmail.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - Gemfile
103
+ - LICENSE.txt
104
+ - README.md
105
+ - Rakefile
106
+ - eager_loadable_polymorphic_association.gemspec
107
+ - lib/eager_loadable_polymorphic_association.rb
108
+ - lib/eager_loadable_polymorphic_association/version.rb
109
+ - spec/active_record_models.rb
110
+ - spec/eager_loadable_polymorphic_association/paste_spec.rb
111
+ - spec/spec_helper.rb
112
+ homepage: ''
113
+ licenses: []
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ! '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ segments:
125
+ - 0
126
+ hash: -864253679892836643
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ! '>='
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ segments:
134
+ - 0
135
+ hash: -864253679892836643
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 1.8.23
139
+ signing_key:
140
+ specification_version: 3
141
+ summary: add eager loading functionality to ActiveRecord's polymorphic association.
142
+ test_files:
143
+ - spec/active_record_models.rb
144
+ - spec/eager_loadable_polymorphic_association/paste_spec.rb
145
+ - spec/spec_helper.rb