polyamorous 1.1.0 → 1.2.0

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.
Files changed (31) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +27 -6
  3. data/Gemfile +15 -13
  4. data/README.md +7 -2
  5. data/lib/polyamorous.rb +30 -12
  6. data/lib/polyamorous/{activerecord_3_and_4.0 → activerecord_3_and_4.0_ruby_1.9}/join_association.rb +28 -27
  7. data/lib/polyamorous/{activerecord_3_and_4.0 → activerecord_3_and_4.0_ruby_1.9}/join_dependency.rb +7 -4
  8. data/lib/polyamorous/activerecord_4.1_ruby_1.9/join_association.rb +2 -0
  9. data/lib/polyamorous/activerecord_4.1_ruby_1.9/join_dependency.rb +4 -0
  10. data/lib/polyamorous/activerecord_4.1_ruby_2/join_association.rb +2 -0
  11. data/lib/polyamorous/activerecord_4.1_ruby_2/join_dependency.rb +3 -0
  12. data/lib/polyamorous/activerecord_4.1_ruby_2/make_joins.rb +11 -0
  13. data/lib/polyamorous/activerecord_4.2_ruby_1.9/join_association.rb +46 -0
  14. data/lib/polyamorous/activerecord_4.2_ruby_1.9/join_dependency.rb +94 -0
  15. data/lib/polyamorous/activerecord_4.2_ruby_2/join_association.rb +37 -0
  16. data/lib/polyamorous/{activerecord_4.1 → activerecord_4.2_ruby_2}/join_dependency.rb +39 -44
  17. data/lib/polyamorous/swapping_reflection_class.rb +11 -0
  18. data/lib/polyamorous/version.rb +1 -1
  19. data/polyamorous.gemspec +4 -3
  20. data/spec/polyamorous/join_association_spec.rb +3 -3
  21. data/spec/polyamorous/join_dependency_spec.rb +3 -3
  22. data/spec/polyamorous/join_spec.rb +2 -2
  23. data/spec/spec_helper.rb +10 -2
  24. data/spec/support/schema.rb +47 -51
  25. data/spec/support/shared_examples/join_association_3_and_4.0.rb +6 -3
  26. data/spec/support/shared_examples/join_association_4.1.rb +9 -4
  27. data/spec/support/shared_examples/join_dependency_3_and_4.0.rb +22 -11
  28. data/spec/support/shared_examples/join_dependency_4.1.rb +25 -6
  29. metadata +41 -12
  30. data/lib/polyamorous/activerecord_4.1/join_association.rb +0 -75
  31. data/lib/polyamorous/activerecord_4.2/join_dependency.rb +0 -12
@@ -1,8 +1,10 @@
1
- shared_examples "Join Association on ActiveRecord 4.1" do
1
+ shared_examples 'Join Association on ActiveRecord 4.1' do
2
2
  let(:join_dependency) { new_join_dependency Note, {} }
3
3
  let(:reflection) { Note.reflect_on_association(:notable) }
4
4
  let(:parent) { join_dependency.join_root }
5
- let(:join_association) { new_join_association(reflection, parent.children, Article) }
5
+ let(:join_association) {
6
+ new_join_association(reflection, parent.children, Article)
7
+ }
6
8
 
7
9
  subject {
8
10
  join_dependency.build_join_association_respecting_polymorphism(
@@ -12,7 +14,9 @@ shared_examples "Join Association on ActiveRecord 4.1" do
12
14
 
13
15
  it 'respects polymorphism on equality test' do
14
16
  expect(subject).to eq(
15
- join_dependency.build_join_association_respecting_polymorphism(reflection, parent, Person)
17
+ join_dependency.build_join_association_respecting_polymorphism(
18
+ reflection, parent, Person
19
+ )
16
20
  )
17
21
  expect(subject).not_to eq(
18
22
  join_dependency.build_join_association_respecting_polymorphism(
@@ -23,7 +27,8 @@ shared_examples "Join Association on ActiveRecord 4.1" do
23
27
 
24
28
  it 'leaves the orginal reflection intact for thread safety' do
25
29
  reflection.instance_variable_set(:@klass, Article)
26
- join_association.swapping_reflection_klass(reflection, Person) do |new_reflection|
30
+ join_association
31
+ .swapping_reflection_klass(reflection, Person) do |new_reflection|
27
32
  expect(new_reflection.options).not_to equal reflection.options
28
33
  expect(new_reflection.options).not_to have_key(:polymorphic)
29
34
  expect(new_reflection.klass).to eq(Person)
@@ -3,36 +3,43 @@ shared_examples "Join Dependency on ActiveRecord 3 and 4.0" do
3
3
  subject { new_join_dependency Person, :articles => :comments }
4
4
 
5
5
  specify { expect(subject.join_associations.size).to eq(2) }
6
- specify { expect(subject.join_associations).to be_all { |a| a.join_type == Polyamorous::InnerJoin } }
6
+ specify { expect(subject.join_associations)
7
+ .to be_all { |a| a.join_type == Polyamorous::InnerJoin } }
7
8
  end
8
9
 
9
10
  context 'with has_many :through association' do
10
11
  subject { new_join_dependency Person, :authored_article_comments }
11
12
 
12
13
  specify { expect(subject.join_associations.size).to eq(1) }
13
- specify { expect(subject.join_associations.first.table_name).to eq 'comments' }
14
+ specify { expect(subject.join_associations.first.table_name)
15
+ .to eq 'comments' }
14
16
  end
15
17
 
16
18
  context 'with outer join' do
17
19
  subject { new_join_dependency Person, new_join(:articles, :outer) }
18
20
 
19
21
  specify { expect(subject.join_associations.size).to eq(1) }
20
- specify { expect(subject.join_associations).to be_all { |a| a.join_type == Polyamorous::OuterJoin } }
22
+ specify { expect(subject.join_associations)
23
+ .to be_all { |a| a.join_type == Polyamorous::OuterJoin } }
21
24
  end
22
25
 
23
26
  context 'with nested outer joins' do
24
- subject { new_join_dependency Person, new_join(:articles, :outer) => new_join(:comments, :outer) }
27
+ subject { new_join_dependency Person,
28
+ new_join(:articles, :outer) => new_join(:comments, :outer) }
25
29
 
26
30
  specify { expect(subject.join_associations.size).to eq(2) }
27
- specify { expect(subject.join_associations).to be_all { |a| a.join_type == Polyamorous::OuterJoin } }
31
+ specify { expect(subject.join_associations)
32
+ .to be_all { |a| a.join_type == Polyamorous::OuterJoin } }
28
33
  end
29
34
 
30
35
  context 'with polymorphic belongs_to join' do
31
36
  subject { new_join_dependency Note, new_join(:notable, :inner, Person) }
32
37
 
33
38
  specify { expect(subject.join_associations.size).to eq(1) }
34
- specify { expect(subject.join_associations).to be_all { |a| a.join_type == Polyamorous::InnerJoin } }
35
- specify { expect(subject.join_associations.first.table_name).to eq 'people' }
39
+ specify { expect(subject.join_associations)
40
+ .to be_all { |a| a.join_type == Polyamorous::InnerJoin } }
41
+ specify { expect(subject.join_associations.first.table_name)
42
+ .to eq 'people' }
36
43
 
37
44
  it 'finds a join association respecting polymorphism' do
38
45
  parent = subject.join_base
@@ -44,11 +51,15 @@ shared_examples "Join Dependency on ActiveRecord 3 and 4.0" do
44
51
  end
45
52
 
46
53
  context 'with polymorphic belongs_to join and nested symbol join' do
47
- subject { new_join_dependency Note, new_join(:notable, :inner, Person) => :comments }
54
+ subject { new_join_dependency Note,
55
+ new_join(:notable, :inner, Person) => :comments }
48
56
 
49
57
  specify { expect(subject.join_associations.size).to eq(2) }
50
- specify { expect(subject.join_associations).to be_all { |a| a.join_type == Polyamorous::InnerJoin } }
51
- specify { expect(subject.join_associations.first.table_name).to eq 'people' }
52
- specify { expect(subject.join_associations[1].table_name).to eq 'comments' }
58
+ specify { expect(subject.join_associations)
59
+ .to be_all { |a| a.join_type == Polyamorous::InnerJoin } }
60
+ specify { expect(subject.join_associations.first.table_name)
61
+ .to eq 'people' }
62
+ specify { expect(subject.join_associations[1].table_name)
63
+ .to eq 'comments' }
53
64
  end
54
65
  end
@@ -1,31 +1,45 @@
1
1
  shared_examples "Join Dependency on ActiveRecord 4.1" do
2
2
  context 'with symbol joins' do
3
3
  subject { new_join_dependency Person, :articles => :comments }
4
+
4
5
  specify { expect(subject.join_root.drop(1).size).to eq(2) }
6
+ specify { expect(subject.join_root.drop(1).map(&:join_type))
7
+ .to be_all { Polyamorous::InnerJoin } }
5
8
  end
6
9
 
7
10
  context 'with has_many :through association' do
8
11
  subject { new_join_dependency Person, :authored_article_comments }
9
12
 
10
13
  specify { expect(subject.join_root.drop(1).size).to eq(1) }
11
- specify { expect(subject.join_root.drop(1).first.table_name).to eq 'comments' }
14
+ specify { expect(subject.join_root.drop(1).first.table_name)
15
+ .to eq 'comments' }
12
16
  end
13
17
 
14
18
  context 'with outer join' do
15
19
  subject { new_join_dependency Person, new_join(:articles, :outer) }
20
+
16
21
  specify { expect(subject.join_root.drop(1).size).to eq(1) }
22
+ specify { expect(subject.join_root.drop(1).first.join_type)
23
+ .to eq Polyamorous::OuterJoin }
17
24
  end
18
25
 
19
26
  context 'with nested outer joins' do
20
- subject { new_join_dependency Person, new_join(:articles, :outer) => new_join(:comments, :outer) }
27
+ subject { new_join_dependency Person,
28
+ new_join(:articles, :outer) => new_join(:comments, :outer) }
29
+
21
30
  specify { expect(subject.join_root.drop(1).size).to eq(2) }
31
+ specify { expect(subject.join_root.drop(1).map(&:join_type))
32
+ .to be_all { Polyamorous::OuterJoin } }
22
33
  end
23
34
 
24
35
  context 'with polymorphic belongs_to join' do
25
36
  subject { new_join_dependency Note, new_join(:notable, :inner, Person) }
26
37
 
27
38
  specify { expect(subject.join_root.drop(1).size).to eq(1) }
28
- specify { expect(subject.join_root.drop(1).first.table_name).to eq 'people' }
39
+ specify { expect(subject.join_root.drop(1).first.join_type)
40
+ .to eq Polyamorous::InnerJoin }
41
+ specify { expect(subject.join_root.drop(1).first.table_name)
42
+ .to eq 'people' }
29
43
 
30
44
  it 'finds a join association respecting polymorphism' do
31
45
  parent = subject.join_root
@@ -37,10 +51,15 @@ shared_examples "Join Dependency on ActiveRecord 4.1" do
37
51
  end
38
52
 
39
53
  context 'with polymorphic belongs_to join and nested symbol join' do
40
- subject { new_join_dependency Note, new_join(:notable, :inner, Person) => :comments }
54
+ subject { new_join_dependency Note,
55
+ new_join(:notable, :inner, Person) => :comments }
41
56
 
42
57
  specify { expect(subject.join_root.drop(1).size).to eq(2) }
43
- specify { expect(subject.join_root.drop(1).first.table_name).to eq 'people' }
44
- specify { expect(subject.join_root.drop(1)[1].table_name).to eq 'comments' }
58
+ specify { expect(subject.join_root.drop(1).map(&:join_type))
59
+ .to be_all { Polyamorous::InnerJoin } }
60
+ specify { expect(subject.join_root.drop(1).first.table_name)
61
+ .to eq 'people' }
62
+ specify { expect(subject.join_root.drop(1)[1].table_name)
63
+ .to eq 'comments' }
45
64
  end
46
65
  end
metadata CHANGED
@@ -1,14 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polyamorous
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ernie Miller
8
+ - Ryan Bigg
9
+ - Jon Atack
10
+ - Xiang Li
8
11
  autorequire:
9
12
  bindir: bin
10
13
  cert_chain: []
11
- date: 2014-07-17 00:00:00.000000000 Z
14
+ date: 2015-04-05 00:00:00.000000000 Z
12
15
  dependencies:
13
16
  - !ruby/object:Gem::Dependency
14
17
  name: activerecord
@@ -84,7 +87,10 @@ description: "\n This is just an extraction from Ransack/Squeel. You probably
84
87
  want to use this\n directly. It extends ActiveRecord's associations to support
85
88
  polymorphic belongs_to\n associations.\n "
86
89
  email:
87
- - ernie@metautonomo.us
90
+ - ernie@erniemiller.org
91
+ - radarlistener@gmail.com
92
+ - jonnyatack@gmail.com
93
+ - bigxiang@gmail.com
88
94
  executables: []
89
95
  extensions: []
90
96
  extra_rdoc_files: []
@@ -96,12 +102,19 @@ files:
96
102
  - README.md
97
103
  - Rakefile
98
104
  - lib/polyamorous.rb
99
- - lib/polyamorous/activerecord_3_and_4.0/join_association.rb
100
- - lib/polyamorous/activerecord_3_and_4.0/join_dependency.rb
101
- - lib/polyamorous/activerecord_4.1/join_association.rb
102
- - lib/polyamorous/activerecord_4.1/join_dependency.rb
103
- - lib/polyamorous/activerecord_4.2/join_dependency.rb
105
+ - lib/polyamorous/activerecord_3_and_4.0_ruby_1.9/join_association.rb
106
+ - lib/polyamorous/activerecord_3_and_4.0_ruby_1.9/join_dependency.rb
107
+ - lib/polyamorous/activerecord_4.1_ruby_1.9/join_association.rb
108
+ - lib/polyamorous/activerecord_4.1_ruby_1.9/join_dependency.rb
109
+ - lib/polyamorous/activerecord_4.1_ruby_2/join_association.rb
110
+ - lib/polyamorous/activerecord_4.1_ruby_2/join_dependency.rb
111
+ - lib/polyamorous/activerecord_4.1_ruby_2/make_joins.rb
112
+ - lib/polyamorous/activerecord_4.2_ruby_1.9/join_association.rb
113
+ - lib/polyamorous/activerecord_4.2_ruby_1.9/join_dependency.rb
114
+ - lib/polyamorous/activerecord_4.2_ruby_2/join_association.rb
115
+ - lib/polyamorous/activerecord_4.2_ruby_2/join_dependency.rb
104
116
  - lib/polyamorous/join.rb
117
+ - lib/polyamorous/swapping_reflection_class.rb
105
118
  - lib/polyamorous/tree_node.rb
106
119
  - lib/polyamorous/version.rb
107
120
  - polyamorous.gemspec
@@ -120,8 +133,9 @@ files:
120
133
  - spec/support/shared_examples/join_association_4.1.rb
121
134
  - spec/support/shared_examples/join_dependency_3_and_4.0.rb
122
135
  - spec/support/shared_examples/join_dependency_4.1.rb
123
- homepage: http://github.com/ernie/polyamorous
124
- licenses: []
136
+ homepage: https://github.com/activerecord-hackery/polyamorous
137
+ licenses:
138
+ - MIT
125
139
  metadata: {}
126
140
  post_install_message:
127
141
  rdoc_options: []
@@ -139,8 +153,23 @@ required_rubygems_version: !ruby/object:Gem::Requirement
139
153
  version: '0'
140
154
  requirements: []
141
155
  rubyforge_project: polyamorous
142
- rubygems_version: 2.2.2
156
+ rubygems_version: 2.4.6
143
157
  signing_key:
144
158
  specification_version: 4
145
159
  summary: Loves/is loved by polymorphic belongs_to associations, Ransack, Squeel, MetaSearch...
146
- test_files: []
160
+ test_files:
161
+ - spec/blueprints/articles.rb
162
+ - spec/blueprints/comments.rb
163
+ - spec/blueprints/notes.rb
164
+ - spec/blueprints/people.rb
165
+ - spec/blueprints/tags.rb
166
+ - spec/helpers/polyamorous_helper.rb
167
+ - spec/polyamorous/join_association_spec.rb
168
+ - spec/polyamorous/join_dependency_spec.rb
169
+ - spec/polyamorous/join_spec.rb
170
+ - spec/spec_helper.rb
171
+ - spec/support/schema.rb
172
+ - spec/support/shared_examples/join_association_3_and_4.0.rb
173
+ - spec/support/shared_examples/join_association_4.1.rb
174
+ - spec/support/shared_examples/join_dependency_3_and_4.0.rb
175
+ - spec/support/shared_examples/join_dependency_4.1.rb
@@ -1,75 +0,0 @@
1
- module Polyamorous
2
- module JoinAssociationExtensions
3
- def self.included(base)
4
- base.class_eval do
5
- attr_reader :join_type
6
- alias_method_chain :initialize, :polymorphism
7
- if base.method_defined?(:active_record)
8
- alias_method :base_klass, :active_record
9
- end
10
-
11
- if ActiveRecord::VERSION::STRING =~ /^3\.0\./
12
- alias_method_chain :association_join, :polymorphism
13
- else
14
- alias_method_chain :build_constraint, :polymorphism
15
- end
16
- end
17
- end
18
-
19
- def initialize_with_polymorphism(reflection, children, polymorphic_class = nil, join_type = Arel::Nodes::InnerJoin)
20
- @join_type = join_type
21
- if polymorphic_class && ::ActiveRecord::Base > polymorphic_class
22
- swapping_reflection_klass(reflection, polymorphic_class) do |reflection|
23
- initialize_without_polymorphism(reflection, children)
24
- self.reflection.options[:polymorphic] = true
25
- end
26
- else
27
- initialize_without_polymorphism(reflection, children)
28
- end
29
- end
30
-
31
- def swapping_reflection_klass(reflection, klass)
32
- new_reflection = reflection.clone
33
- new_reflection.instance_variable_set(:@options, reflection.options.clone)
34
- new_reflection.options.delete(:polymorphic)
35
- new_reflection.instance_variable_set(:@klass, klass)
36
- yield new_reflection
37
- end
38
-
39
- # Reference https://github.com/rails/rails/commit/9b15db51b78028bfecdb85595624de4b838adbd1
40
- # NOTE Not sure we still need it?
41
- def ==(other)
42
- base_klass == other.base_klass
43
- end
44
-
45
- def build_constraint_with_polymorphism(klass, table, key, foreign_table, foreign_key)
46
- if reflection.options[:polymorphic]
47
- build_constraint_without_polymorphism(klass, table, key, foreign_table, foreign_key).and(
48
- foreign_table[reflection.foreign_type].eq(reflection.klass.name)
49
- )
50
- else
51
- build_constraint_without_polymorphism(klass, table, key, foreign_table, foreign_key)
52
- end
53
- end
54
-
55
- def association_join_with_polymorphism
56
- return @join if @Join
57
-
58
- @join = association_join_without_polymorphism
59
-
60
- if reflection.macro == :belongs_to && reflection.options[:polymorphic]
61
- aliased_table = Arel::Table.new(table_name, :as => @aliased_table_name,
62
- :engine => arel_engine,
63
- :columns => klass.columns)
64
-
65
- parent_table = Arel::Table.new(parent.table_name, :as => parent.aliased_table_name,
66
- :engine => arel_engine,
67
- :columns => parent.base_klass.columns)
68
-
69
- @join << parent_table[reflection.options[:foreign_type]].eq(reflection.klass.name)
70
- end
71
-
72
- @join
73
- end
74
- end
75
- end
@@ -1,12 +0,0 @@
1
- require 'polyamorous/activerecord_4.1/join_dependency'
2
-
3
- module Polyamorous
4
- module JoinDependencyExtensions
5
- def make_joins(parent, child)
6
- tables = child.tables
7
- info = make_constraints parent, child, tables, child.join_type || Arel::Nodes::InnerJoin
8
-
9
- [info] + child.children.flat_map { |c| make_joins(child, c) }
10
- end
11
- end
12
- end