polyamorous 0.6.4 → 1.0.0

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: 1a960cff78a0b7755b3cb84f7d44ce6fd99f0db3
4
- data.tar.gz: ed14c61ab54556d57ab445b50635b48466c36ba1
3
+ metadata.gz: f3950028cc987da204d7fa91c77f8ff80488e3a3
4
+ data.tar.gz: b43069b9cf219debd61e9e4370c313a943cd4664
5
5
  SHA512:
6
- metadata.gz: 4e826b11b91c37bbedc7bc9201c90515a5a4ba02e085ff9b0827dfe2da116bba36545526a720e205a9c4a878e66ba8c384bb6942174c038cc6d15083d5777614
7
- data.tar.gz: 2dfd89eab419e1e5301c51b30f2edff793bd721f7ce27d4a6fe4726dde9c180afcfc15a5faf703b88d80b87a6f3457c2227e4d88ea399b08f3f5da632a11945c
6
+ metadata.gz: 40bb92cc9b88a5c321f6c1538272cfd01ea01d88ec564e66f7b9ce437d9a37429be380e6e3719ee0d42df935a5acf1ecf4a7a32dc8ad6b6b92e3cc6cc0cc8c51
7
+ data.tar.gz: 053e1bfb41cbf82c4e52c3068d3e05d7f835830e2692061cdca6a2ce7a82f674d554c845a756777f0d1bef9075206a31ff208e73102892216a817ebdc11b56b7
data/.travis.yml ADDED
@@ -0,0 +1,32 @@
1
+ language: ruby
2
+
3
+ before_install:
4
+ - travis_retry gem install bundler
5
+
6
+ rvm:
7
+ - 1.9.3
8
+ - 2.0.0
9
+ - 2.1.1
10
+
11
+ env:
12
+ - RAILS=4-1-stable DB=sqlite
13
+ - RAILS=4-1-stable DB=mysql
14
+ - RAILS=4-1-stable DB=postgres
15
+ - RAILS=4-0-stable DB=sqlite
16
+ - RAILS=4-0-stable DB=mysql
17
+ - RAILS=4-0-stable DB=postgres
18
+ - RAILS=3-2-stable DB=sqlite
19
+ - RAILS=3-2-stable DB=mysql
20
+ - RAILS=3-2-stable DB=postgres
21
+ - RAILS=3-1-stable DB=sqlite
22
+ - RAILS=3-1-stable DB=mysql
23
+ - RAILS=3-1-stable DB=postgres
24
+ - RAILS=3-0-stable DB=sqlite
25
+ - RAILS=3-0-stable DB=mysql
26
+ - RAILS=3-0-stable DB=postgres
27
+
28
+ before_script:
29
+ - mysql -e 'create database ransack collate utf8_general_ci;'
30
+ - mysql -e 'use ransack;show variables like "%character%";show variables like "%collation%";'
31
+ - psql -c 'create database ransack;' -U postgres
32
+
data/Gemfile CHANGED
@@ -1,21 +1,9 @@
1
- source "http://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
  gemspec
3
3
 
4
4
  gem 'rake'
5
5
 
6
6
  rails = ENV['RAILS'] || 'master'
7
- arel = ENV['AREL'] || 'master'
8
-
9
- arel_opts = case arel
10
- when /\// # A path
11
- {:path => arel}
12
- when /^v/ # A tagged version
13
- {:git => 'git://github.com/rails/arel.git', :tag => arel}
14
- else
15
- {:git => 'git://github.com/rails/arel.git', :branch => arel}
16
- end
17
-
18
- gem 'arel', arel_opts
19
7
 
20
8
  case rails
21
9
  when /\// # A path
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Polyamorous
2
2
 
3
- This is just a long-overdue extraction from MetaSearch 1.1.x, Ransack, and Squeel.
3
+ Polyamorous is an extraction from MetaSearch 1.1.x, Ransack, and Squeel by [Ernie Miller](http://twitter.com/erniemiller).
4
4
 
5
- Move along. Nothing to see here.
5
+ ## Copyright
6
+
7
+ Copyright © 2011-2014 [Ernie Miller](http://twitter.com/erniemiller)
data/lib/polyamorous.rb CHANGED
@@ -1,26 +1,33 @@
1
1
  require "polyamorous/version"
2
- require 'active_record'
3
2
 
4
- module Polyamorous
5
- if defined?(::ActiveRecord::Associations::JoinDependency)
6
- JoinDependency = ::ActiveRecord::Associations::JoinDependency
7
- JoinAssociation = ::ActiveRecord::Associations::JoinDependency::JoinAssociation
8
- JoinBase = ::ActiveRecord::Associations::JoinDependency::JoinBase
9
- else
10
- JoinDependency = ::ActiveRecord::Associations::ClassMethods::JoinDependency
11
- JoinAssociation = ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation
12
- JoinBase = ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinBase
3
+ if defined?(::ActiveRecord)
4
+ module Polyamorous
5
+ if defined?(::ActiveRecord::Associations::JoinDependency)
6
+ JoinDependency = ::ActiveRecord::Associations::JoinDependency
7
+ JoinAssociation = ::ActiveRecord::Associations::JoinDependency::JoinAssociation
8
+ JoinBase = ::ActiveRecord::Associations::JoinDependency::JoinBase
9
+ else
10
+ JoinDependency = ::ActiveRecord::Associations::ClassMethods::JoinDependency
11
+ JoinAssociation = ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation
12
+ JoinBase = ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinBase
13
+ end
13
14
  end
14
- end
15
15
 
16
- require 'polyamorous/join'
17
- require 'polyamorous/join_association'
18
- require 'polyamorous/join_dependency'
16
+ require 'polyamorous/join'
17
+
18
+ if ActiveRecord::VERSION::STRING >= "4.1"
19
+ require 'polyamorous/activerecord_4.1/join_association'
20
+ require 'polyamorous/activerecord_4.1/join_dependency'
21
+ else
22
+ require 'polyamorous/activerecord_3_and_4.0/join_association'
23
+ require 'polyamorous/activerecord_3_and_4.0/join_dependency'
24
+ end
19
25
 
20
- Polyamorous::JoinDependency.send(:include, Polyamorous::JoinDependencyExtensions)
21
- Polyamorous::JoinAssociation.send(:include, Polyamorous::JoinAssociationExtensions)
22
- Polyamorous::JoinBase.class_eval do
23
- if method_defined?(:active_record)
24
- alias_method :base_klass, :active_record
26
+ Polyamorous::JoinDependency.send(:include, Polyamorous::JoinDependencyExtensions)
27
+ Polyamorous::JoinAssociation.send(:include, Polyamorous::JoinAssociationExtensions)
28
+ Polyamorous::JoinBase.class_eval do
29
+ if method_defined?(:active_record)
30
+ alias_method :base_klass, :active_record
31
+ end
25
32
  end
26
33
  end
@@ -1,6 +1,5 @@
1
1
  module Polyamorous
2
2
  module JoinDependencyExtensions
3
-
4
3
  def self.included(base)
5
4
  base.class_eval do
6
5
  alias_method_chain :build, :polymorphism
@@ -13,12 +12,23 @@ module Polyamorous
13
12
 
14
13
  def graft_with_polymorphism(*associations)
15
14
  associations.each do |association|
16
- unless join_associations.detect {|a| association == a}
15
+ unless join_associations.detect { |a| association == a }
17
16
  if association.reflection.options[:polymorphic]
18
- build(Join.new(association.reflection.name, association.join_type, association.reflection.klass),
19
- association.find_parent_in(self) || join_base, association.join_type)
17
+ build(
18
+ Join.new(
19
+ association.reflection.name,
20
+ association.join_type,
21
+ association.reflection.klass
22
+ ),
23
+ association.find_parent_in(self) || join_base,
24
+ association.join_type
25
+ )
20
26
  else
21
- build(association.reflection.name, association.find_parent_in(self) || join_base, association.join_type)
27
+ build(
28
+ association.reflection.name,
29
+ association.find_parent_in(self) || join_base,
30
+ association.join_type
31
+ )
22
32
  end
23
33
  end
24
34
  end
@@ -40,11 +50,17 @@ module Polyamorous
40
50
  when Join
41
51
  parent ||= _join_parts.last
42
52
  reflection = parent.reflections[associations.name] or
43
- raise ::ActiveRecord::ConfigurationError, "Association named '#{ associations.name }' was not found; perhaps you misspelled it?"
53
+ raise ::ActiveRecord::ConfigurationError,
54
+ "Association named '#{associations.name
55
+ }' was not found; perhaps you misspelled it?"
44
56
 
45
- unless join_association = find_join_association_respecting_polymorphism(reflection, parent, associations.klass)
57
+ unless join_association = find_join_association_respecting_polymorphism(
58
+ reflection, parent, associations.klass
59
+ )
46
60
  @reflections << reflection
47
- join_association = build_join_association_respecting_polymorphism(reflection, parent, associations.klass)
61
+ join_association = build_join_association_respecting_polymorphism(
62
+ reflection, parent, associations.klass
63
+ )
48
64
  join_association.join_type = associations.type
49
65
  _join_parts << join_association
50
66
  cache_joined_association(join_association)
@@ -73,6 +89,5 @@ module Polyamorous
73
89
  JoinAssociation.new(reflection, self, parent)
74
90
  end
75
91
  end
76
-
77
92
  end
78
93
  end
@@ -0,0 +1,73 @@
1
+ module Polyamorous
2
+ module JoinAssociationExtensions
3
+ def self.included(base)
4
+ base.class_eval do
5
+ alias_method_chain :initialize, :polymorphism
6
+ if base.method_defined?(:active_record)
7
+ alias_method :base_klass, :active_record
8
+ end
9
+
10
+ if ActiveRecord::VERSION::STRING =~ /^3\.0\./
11
+ alias_method_chain :association_join, :polymorphism
12
+ else
13
+ alias_method_chain :build_constraint, :polymorphism
14
+ end
15
+ end
16
+ end
17
+
18
+ def initialize_with_polymorphism(reflection, children, polymorphic_class = nil)
19
+ if polymorphic_class && ::ActiveRecord::Base > polymorphic_class
20
+ swapping_reflection_klass(reflection, polymorphic_class) do |reflection|
21
+ initialize_without_polymorphism(reflection, children)
22
+ self.reflection.options[:polymorphic] = true
23
+ end
24
+ else
25
+ initialize_without_polymorphism(reflection, children)
26
+ end
27
+ end
28
+
29
+ def swapping_reflection_klass(reflection, klass)
30
+ new_reflection = reflection.clone
31
+ new_reflection.instance_variable_set(:@options, reflection.options.clone)
32
+ new_reflection.options.delete(:polymorphic)
33
+ new_reflection.instance_variable_set(:@klass, klass)
34
+ yield new_reflection
35
+ end
36
+
37
+ # Reference https://github.com/rails/rails/commit/9b15db51b78028bfecdb85595624de4b838adbd1
38
+ # NOTE Not sure we still need it?
39
+ def ==(other)
40
+ base_klass == other.base_klass
41
+ end
42
+
43
+ def build_constraint_with_polymorphism(klass, table, key, foreign_table, foreign_key)
44
+ if reflection.options[:polymorphic]
45
+ build_constraint_without_polymorphism(klass, table, key, foreign_table, foreign_key).and(
46
+ foreign_table[reflection.foreign_type].eq(reflection.klass.name)
47
+ )
48
+ else
49
+ build_constraint_without_polymorphism(klass, table, key, foreign_table, foreign_key)
50
+ end
51
+ end
52
+
53
+ def association_join_with_polymorphism
54
+ return @join if @Join
55
+
56
+ @join = association_join_without_polymorphism
57
+
58
+ if reflection.macro == :belongs_to && reflection.options[:polymorphic]
59
+ aliased_table = Arel::Table.new(table_name, :as => @aliased_table_name,
60
+ :engine => arel_engine,
61
+ :columns => klass.columns)
62
+
63
+ parent_table = Arel::Table.new(parent.table_name, :as => parent.aliased_table_name,
64
+ :engine => arel_engine,
65
+ :columns => parent.base_klass.columns)
66
+
67
+ @join << parent_table[reflection.options[:foreign_type]].eq(reflection.klass.name)
68
+ end
69
+
70
+ @join
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,71 @@
1
+ module Polyamorous
2
+ module JoinDependencyExtensions
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+
6
+ base.class_eval do
7
+ class << self
8
+ alias_method_chain :walk_tree, :polymorphism
9
+ end
10
+
11
+ alias_method_chain :build, :polymorphism
12
+ if base.method_defined?(:active_record)
13
+ alias_method :base_klass, :active_record
14
+ end
15
+ end
16
+ end
17
+
18
+ def build_with_polymorphism(associations, base_klass)
19
+ associations.map do |name, right|
20
+ if name.is_a? Join
21
+ reflection = find_reflection base_klass, name.name
22
+ reflection.check_validity!
23
+
24
+ if reflection.options[:polymorphic]
25
+ JoinAssociation.new reflection, build(right, name.klass || base_klass), name.klass
26
+ else
27
+ JoinAssociation.new reflection, build(right, reflection.klass), name.klass
28
+ end
29
+ else
30
+ reflection = find_reflection base_klass, name
31
+ reflection.check_validity!
32
+
33
+ if reflection.options[:polymorphic]
34
+ raise ActiveRecord::EagerLoadPolymorphicError.new(reflection)
35
+ end
36
+
37
+ JoinAssociation.new reflection, build(right, reflection.klass)
38
+ end
39
+ end
40
+ end
41
+
42
+ def find_join_association_respecting_polymorphism(reflection, parent, klass)
43
+ if association = parent.children.find { |j| j.reflection == reflection }
44
+ unless reflection.options[:polymorphic]
45
+ association
46
+ else
47
+ association if association.base_klass == klass
48
+ end
49
+ end
50
+ end
51
+
52
+ def build_join_association_respecting_polymorphism(reflection, parent, klass)
53
+ if reflection.options[:polymorphic] && klass
54
+ JoinAssociation.new(reflection, self, klass)
55
+ else
56
+ JoinAssociation.new(reflection, self)
57
+ end
58
+ end
59
+
60
+ module ClassMethods
61
+ def walk_tree_with_polymorphism(associations, hash)
62
+ case associations
63
+ when Join
64
+ hash[associations] ||= {}
65
+ else
66
+ walk_tree_without_polymorphism(associations, hash)
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -1,3 +1,3 @@
1
1
  module Polyamorous
2
- VERSION = "0.6.4"
2
+ VERSION = "1.0.0"
3
3
  end
data/polyamorous.gemspec CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |s|
20
20
  s.rubyforge_project = "polyamorous"
21
21
 
22
22
  s.add_dependency 'activerecord', '>= 3.0'
23
- s.add_development_dependency 'rspec', '~> 2.6.0'
23
+ s.add_development_dependency 'rspec', '~> 2.14.0'
24
24
  s.add_development_dependency 'machinist', '~> 1.0.6'
25
25
  s.add_development_dependency 'faker', '~> 0.9.5'
26
26
  s.add_development_dependency 'sqlite3', '~> 1.3.3'
@@ -1,4 +1,13 @@
1
1
  module PolyamorousHelper
2
+ if ActiveRecord::VERSION::STRING >= "4.1"
3
+ def new_join_association(reflection, children, klass)
4
+ Polyamorous::JoinAssociation.new reflection, children, klass
5
+ end
6
+ else
7
+ def new_join_association(reflection, join_dependency, parent, klass)
8
+ Polyamorous::JoinAssociation.new reflection, join_dependency, parent, klass
9
+ end
10
+ end
2
11
 
3
12
  def new_join_dependency(klass, associations = {})
4
13
  Polyamorous::JoinDependency.new klass, associations, []
@@ -2,41 +2,10 @@ require 'spec_helper'
2
2
 
3
3
  module Polyamorous
4
4
  describe JoinAssociation do
5
- let(:join_dependency) { new_join_dependency Note, {} }
6
- let(:parent) { join_dependency.join_base }
7
- let(:reflection) { Note.reflect_on_association(:notable) }
8
- let(:join_association) { JoinAssociation.new(reflection, join_dependency, parent, Article) }
9
- subject {
10
- join_dependency.build_join_association_respecting_polymorphism(
11
- reflection, parent, Person
12
- )
13
- }
14
-
15
- it 'respects polymorphism on equality test' do
16
- subject.should eq(
17
- join_dependency.build_join_association_respecting_polymorphism(
18
- reflection, parent, Person
19
- )
20
- )
21
- subject.should_not eq(
22
- join_dependency.build_join_association_respecting_polymorphism(
23
- reflection, parent, Article
24
- )
25
- )
26
- end
27
-
28
- it 'leaves the orginal reflection intact for thread safety' do
29
- reflection.instance_variable_set(:@klass, Article)
30
- join_association.swapping_reflection_klass(reflection, Person) do |new_reflection|
31
- new_reflection.options.should_not equal reflection.options
32
- new_reflection.options.should_not have_key(:polymorphic)
33
- new_reflection.klass.should == Person
34
- reflection.klass.should == Article
35
- end
36
- end
37
-
38
- it 'sets the polmorphic option to true after initializing' do
39
- join_association.options[:polymorphic].should be_true
5
+ if ActiveRecord::VERSION::STRING >= "4.1"
6
+ include_examples "Join Association on ActiveRecord 4.1"
7
+ else
8
+ include_examples "Join Association on ActiveRecord 3 and 4.0"
40
9
  end
41
10
  end
42
11
  end
@@ -2,59 +2,10 @@ require 'spec_helper'
2
2
 
3
3
  module Polyamorous
4
4
  describe JoinDependency do
5
-
6
- context 'with symbol joins' do
7
- subject { new_join_dependency Person, :articles => :comments }
8
-
9
- specify { subject.join_associations.should have(2).associations }
10
- specify { subject.join_associations.should be_all { |a| a.join_type == Arel::InnerJoin } }
11
- end
12
-
13
- context 'with has_many :through association' do
14
- subject { new_join_dependency Person, :authored_article_comments }
15
-
16
- specify { subject.join_associations.should have(1).association }
17
- specify { subject.join_associations.first.table_name.should eq 'comments' }
5
+ if ActiveRecord::VERSION::STRING >= "4.1"
6
+ include_examples "Join Dependency on ActiveRecord 4.1"
7
+ else
8
+ include_examples "Join Dependency on ActiveRecord 3 and 4.0"
18
9
  end
19
-
20
- context 'with outer join' do
21
- subject { new_join_dependency Person, new_join(:articles, :outer) }
22
-
23
- specify { subject.join_associations.should have(1).association }
24
- specify { subject.join_associations.should be_all { |a| a.join_type == Arel::OuterJoin } }
25
- end
26
-
27
- context 'with nested outer joins' do
28
- subject { new_join_dependency Person, new_join(:articles, :outer) => new_join(:comments, :outer) }
29
-
30
- specify { subject.join_associations.should have(2).associations }
31
- specify { subject.join_associations.should be_all { |a| a.join_type == Arel::OuterJoin } }
32
- end
33
-
34
- context 'with polymorphic belongs_to join' do
35
- subject { new_join_dependency Note, new_join(:notable, :inner, Person) }
36
-
37
- specify { subject.join_associations.should have(1).association }
38
- specify { subject.join_associations.should be_all { |a| a.join_type == Arel::InnerJoin } }
39
- specify { subject.join_associations.first.table_name.should eq 'people' }
40
-
41
- it 'finds a join association respecting polymorphism' do
42
- parent = subject.join_base
43
- reflection = Note.reflect_on_association(:notable)
44
- subject.find_join_association_respecting_polymorphism(
45
- reflection, parent, Person
46
- ).should eq subject.join_associations.first
47
- end
48
- end
49
-
50
- context 'with polymorphic belongs_to join and nested symbol join' do
51
- subject { new_join_dependency Note, new_join(:notable, :inner, Person) => :comments }
52
-
53
- specify { subject.join_associations.should have(2).association }
54
- specify { subject.join_associations.should be_all { |a| a.join_type == Arel::InnerJoin } }
55
- specify { subject.join_associations.first.table_name.should eq 'people' }
56
- specify { subject.join_associations[1].table_name.should eq 'comments' }
57
- end
58
-
59
10
  end
60
11
  end
data/spec/spec_helper.rb CHANGED
@@ -5,7 +5,8 @@ require 'polyamorous'
5
5
 
6
6
  Time.zone = 'Eastern Time (US & Canada)'
7
7
 
8
- Dir[File.expand_path('../{helpers,support,blueprints}/*.rb', __FILE__)].each do |f|
8
+ Dir[File.expand_path('../{helpers,support,blueprints}/**/*.rb', __FILE__)]
9
+ .each do |f|
9
10
  require f
10
11
  end
11
12
 
@@ -13,7 +14,7 @@ Sham.define do
13
14
  name { Faker::Name.name }
14
15
  title { Faker::Lorem.sentence }
15
16
  body { Faker::Lorem.paragraph }
16
- salary {|index| 30000 + (index * 1000)}
17
+ salary { |index| 30000 + (index * 1000) }
17
18
  tag_name { Faker::Lorem.words(3).join(' ') }
18
19
  note { Faker::Lorem.words(7).join(' ') }
19
20
  end
@@ -0,0 +1,39 @@
1
+ shared_examples "Join Association on ActiveRecord 3 and 4.0" do
2
+ let(:join_dependency) { new_join_dependency Note, {} }
3
+ let(:reflection) { Note.reflect_on_association(:notable) }
4
+ let(:parent) { join_dependency.join_base }
5
+ let(:join_association) { new_join_association(reflection, join_dependency, parent, Article) }
6
+
7
+ subject {
8
+ join_dependency.build_join_association_respecting_polymorphism(
9
+ reflection, parent, Person
10
+ )
11
+ }
12
+
13
+ it 'respects polymorphism on equality test' do
14
+ subject.should eq(
15
+ join_dependency.build_join_association_respecting_polymorphism(
16
+ reflection, parent, Person
17
+ )
18
+ )
19
+ subject.should_not eq(
20
+ join_dependency.build_join_association_respecting_polymorphism(
21
+ reflection, parent, Article
22
+ )
23
+ )
24
+ end
25
+
26
+ it 'leaves the orginal reflection intact for thread safety' do
27
+ reflection.instance_variable_set(:@klass, Article)
28
+ join_association.swapping_reflection_klass(reflection, Person) do |new_reflection|
29
+ new_reflection.options.should_not equal reflection.options
30
+ new_reflection.options.should_not have_key(:polymorphic)
31
+ new_reflection.klass.should == Person
32
+ reflection.klass.should == Article
33
+ end
34
+ end
35
+
36
+ it 'sets the polmorphic option to true after initializing' do
37
+ join_association.options[:polymorphic].should be_true
38
+ end
39
+ end
@@ -0,0 +1,37 @@
1
+ shared_examples "Join Association on ActiveRecord 4.1" do
2
+ let(:join_dependency) { new_join_dependency Note, {} }
3
+ let(:reflection) { Note.reflect_on_association(:notable) }
4
+ let(:parent) { join_dependency.join_root }
5
+ let(:join_association) { new_join_association(reflection, parent.children, Article) }
6
+
7
+ subject {
8
+ join_dependency.build_join_association_respecting_polymorphism(
9
+ reflection, parent, Person
10
+ )
11
+ }
12
+
13
+ it 'respects polymorphism on equality test' do
14
+ subject.should eq(
15
+ join_dependency.build_join_association_respecting_polymorphism(reflection, parent, Person)
16
+ )
17
+ subject.should_not eq(
18
+ join_dependency.build_join_association_respecting_polymorphism(
19
+ reflection, parent, Article
20
+ )
21
+ )
22
+ end
23
+
24
+ it 'leaves the orginal reflection intact for thread safety' do
25
+ reflection.instance_variable_set(:@klass, Article)
26
+ join_association.swapping_reflection_klass(reflection, Person) do |new_reflection|
27
+ new_reflection.options.should_not equal reflection.options
28
+ new_reflection.options.should_not have_key(:polymorphic)
29
+ new_reflection.klass.should == Person
30
+ reflection.klass.should == Article
31
+ end
32
+ end
33
+
34
+ it 'sets the polmorphic option to true after initializing' do
35
+ join_association.reflection.options[:polymorphic].should be_true
36
+ end
37
+ end
@@ -0,0 +1,54 @@
1
+ shared_examples "Join Dependency on ActiveRecord 3 and 4.0" do
2
+ context 'with symbol joins' do
3
+ subject { new_join_dependency Person, :articles => :comments }
4
+
5
+ specify { subject.join_associations.should have(2).associations }
6
+ specify { subject.join_associations.should be_all { |a| a.join_type == Arel::InnerJoin } }
7
+ end
8
+
9
+ context 'with has_many :through association' do
10
+ subject { new_join_dependency Person, :authored_article_comments }
11
+
12
+ specify { subject.join_associations.should have(1).association }
13
+ specify { subject.join_associations.first.table_name.should eq 'comments' }
14
+ end
15
+
16
+ context 'with outer join' do
17
+ subject { new_join_dependency Person, new_join(:articles, :outer) }
18
+
19
+ specify { subject.join_associations.should have(1).association }
20
+ specify { subject.join_associations.should be_all { |a| a.join_type == Arel::OuterJoin } }
21
+ end
22
+
23
+ context 'with nested outer joins' do
24
+ subject { new_join_dependency Person, new_join(:articles, :outer) => new_join(:comments, :outer) }
25
+
26
+ specify { subject.join_associations.should have(2).associations }
27
+ specify { subject.join_associations.should be_all { |a| a.join_type == Arel::OuterJoin } }
28
+ end
29
+
30
+ context 'with polymorphic belongs_to join' do
31
+ subject { new_join_dependency Note, new_join(:notable, :inner, Person) }
32
+
33
+ specify { subject.join_associations.should have(1).association }
34
+ specify { subject.join_associations.should be_all { |a| a.join_type == Arel::InnerJoin } }
35
+ specify { subject.join_associations.first.table_name.should eq 'people' }
36
+
37
+ it 'finds a join association respecting polymorphism' do
38
+ parent = subject.join_base
39
+ reflection = Note.reflect_on_association(:notable)
40
+ subject.find_join_association_respecting_polymorphism(
41
+ reflection, parent, Person
42
+ ).should eq subject.join_associations.first
43
+ end
44
+ end
45
+
46
+ context 'with polymorphic belongs_to join and nested symbol join' do
47
+ subject { new_join_dependency Note, new_join(:notable, :inner, Person) => :comments }
48
+
49
+ specify { subject.join_associations.should have(2).association }
50
+ specify { subject.join_associations.should be_all { |a| a.join_type == Arel::InnerJoin } }
51
+ specify { subject.join_associations.first.table_name.should eq 'people' }
52
+ specify { subject.join_associations[1].table_name.should eq 'comments' }
53
+ end
54
+ end
@@ -0,0 +1,46 @@
1
+ shared_examples "Join Dependency on ActiveRecord 4.1" do
2
+ context 'with symbol joins' do
3
+ subject { new_join_dependency Person, :articles => :comments }
4
+ specify { subject.join_root.drop(1).should have(2).associations }
5
+ end
6
+
7
+ context 'with has_many :through association' do
8
+ subject { new_join_dependency Person, :authored_article_comments }
9
+
10
+ specify { subject.join_root.drop(1).should have(1).association }
11
+ specify { subject.join_root.drop(1).first.table_name.should eq 'comments' }
12
+ end
13
+
14
+ context 'with outer join' do
15
+ subject { new_join_dependency Person, new_join(:articles, :outer) }
16
+ specify { subject.join_root.drop(1).should have(1).association }
17
+ end
18
+
19
+ context 'with nested outer joins' do
20
+ subject { new_join_dependency Person, new_join(:articles, :outer) => new_join(:comments, :outer) }
21
+ specify { subject.join_root.drop(1).should have(2).associations }
22
+ end
23
+
24
+ context 'with polymorphic belongs_to join' do
25
+ subject { new_join_dependency Note, new_join(:notable, :inner, Person) }
26
+
27
+ specify { subject.join_root.drop(1).should have(1).association }
28
+ specify { subject.join_root.drop(1).first.table_name.should eq 'people' }
29
+
30
+ it 'finds a join association respecting polymorphism' do
31
+ parent = subject.join_root
32
+ reflection = Note.reflect_on_association(:notable)
33
+ subject.find_join_association_respecting_polymorphism(
34
+ reflection, parent, Person
35
+ ).should eq subject.join_root.drop(1).first
36
+ end
37
+ end
38
+
39
+ context 'with polymorphic belongs_to join and nested symbol join' do
40
+ subject { new_join_dependency Note, new_join(:notable, :inner, Person) => :comments }
41
+
42
+ specify { subject.join_root.drop(1).should have(2).association }
43
+ specify { subject.join_root.drop(1).first.table_name.should eq 'people' }
44
+ specify { subject.join_root.drop(1)[1].table_name.should eq 'comments' }
45
+ end
46
+ end
metadata CHANGED
@@ -1,83 +1,83 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polyamorous
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.4
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ernie Miller
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-05 00:00:00.000000000 Z
11
+ date: 2014-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '3.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 2.6.0
33
+ version: 2.14.0
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 2.6.0
40
+ version: 2.14.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: machinist
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: 1.0.6
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: 1.0.6
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: faker
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ~>
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
61
  version: 0.9.5
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ~>
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: 0.9.5
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: sqlite3
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ~>
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
75
  version: 1.3.3
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ~>
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: 1.3.3
83
83
  description: "\n This is just an extraction from Ransack/Squeel. You probably don't
@@ -89,15 +89,18 @@ executables: []
89
89
  extensions: []
90
90
  extra_rdoc_files: []
91
91
  files:
92
- - .gitignore
92
+ - ".gitignore"
93
+ - ".travis.yml"
93
94
  - Gemfile
94
95
  - LICENSE
95
96
  - README.md
96
97
  - Rakefile
97
98
  - 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
98
103
  - lib/polyamorous/join.rb
99
- - lib/polyamorous/join_association.rb
100
- - lib/polyamorous/join_dependency.rb
101
104
  - lib/polyamorous/version.rb
102
105
  - polyamorous.gemspec
103
106
  - spec/blueprints/articles.rb
@@ -111,6 +114,10 @@ files:
111
114
  - spec/polyamorous/join_spec.rb
112
115
  - spec/spec_helper.rb
113
116
  - spec/support/schema.rb
117
+ - spec/support/shared_examples/join_association_3_and_4.0.rb
118
+ - spec/support/shared_examples/join_association_4.1.rb
119
+ - spec/support/shared_examples/join_dependency_3_and_4.0.rb
120
+ - spec/support/shared_examples/join_dependency_4.1.rb
114
121
  homepage: http://github.com/ernie/polyamorous
115
122
  licenses: []
116
123
  metadata: {}
@@ -120,17 +127,17 @@ require_paths:
120
127
  - lib
121
128
  required_ruby_version: !ruby/object:Gem::Requirement
122
129
  requirements:
123
- - - '>='
130
+ - - ">="
124
131
  - !ruby/object:Gem::Version
125
132
  version: '0'
126
133
  required_rubygems_version: !ruby/object:Gem::Requirement
127
134
  requirements:
128
- - - '>='
135
+ - - ">="
129
136
  - !ruby/object:Gem::Version
130
137
  version: '0'
131
138
  requirements: []
132
139
  rubyforge_project: polyamorous
133
- rubygems_version: 2.0.3
140
+ rubygems_version: 2.2.2
134
141
  signing_key:
135
142
  specification_version: 4
136
143
  summary: Loves/is loved by polymorphic belongs_to associations, Ransack, Squeel, MetaSearch...
@@ -146,3 +153,7 @@ test_files:
146
153
  - spec/polyamorous/join_spec.rb
147
154
  - spec/spec_helper.rb
148
155
  - spec/support/schema.rb
156
+ - spec/support/shared_examples/join_association_3_and_4.0.rb
157
+ - spec/support/shared_examples/join_association_4.1.rb
158
+ - spec/support/shared_examples/join_dependency_3_and_4.0.rb
159
+ - spec/support/shared_examples/join_dependency_4.1.rb