squeel 0.8.9 → 0.8.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,13 +1,12 @@
1
- ActiveRecord::Relation.send :include, Squeel::Nodes::Aliasing
2
-
3
1
  case ActiveRecord::VERSION::MAJOR
4
2
  when 3
3
+ ActiveRecord::Relation.send :include, Squeel::Nodes::Aliasing
4
+ require 'squeel/adapters/active_record/join_dependency'
5
+
5
6
  case ActiveRecord::VERSION::MINOR
6
7
  when 0
7
8
  require 'squeel/adapters/active_record/3.0/compat'
8
9
  require 'squeel/adapters/active_record/3.0/relation'
9
- require 'squeel/adapters/active_record/3.0/join_dependency'
10
- require 'squeel/adapters/active_record/3.0/join_association'
11
10
  require 'squeel/adapters/active_record/3.0/association_preload'
12
11
  require 'squeel/adapters/active_record/3.0/context'
13
12
 
@@ -16,8 +15,6 @@ when 3
16
15
  ActiveRecord::Base.extend Squeel::Adapters::ActiveRecord::AssociationPreload
17
16
  else
18
17
  require 'squeel/adapters/active_record/relation'
19
- require 'squeel/adapters/active_record/join_dependency'
20
- require 'squeel/adapters/active_record/join_association'
21
18
  require 'squeel/adapters/active_record/preloader'
22
19
  require 'squeel/adapters/active_record/context'
23
20
 
@@ -1,4 +1,4 @@
1
- require 'active_record'
1
+ require 'polyamorous'
2
2
 
3
3
  module Squeel
4
4
  module Adapters
@@ -8,44 +8,19 @@ module Squeel
8
8
  def self.included(base)
9
9
  base.class_eval do
10
10
  alias_method_chain :build, :squeel
11
- alias_method_chain :graft, :squeel
12
11
  end
13
12
  end
14
13
 
15
- def graft_with_squeel(*associations)
16
- associations.each do |association|
17
- unless join_associations.detect {|a| association == a}
18
- if association.reflection.options[:polymorphic]
19
- build(Nodes::Join.new(association.reflection.name, association.join_type, association.reflection.klass),
20
- association.find_parent_in(self) || join_base, association.join_type)
21
- else
22
- build(association.reflection.name, association.find_parent_in(self) || join_base, association.join_type)
23
- end
24
- end
25
- end
26
- self
27
- end
28
-
29
14
  def build_with_squeel(associations, parent = nil, join_type = Arel::InnerJoin)
30
- associations = associations.symbol if Nodes::Stub === associations
31
-
32
15
  case associations
16
+ when Nodes::Stub
17
+ associations = associations.symbol
33
18
  when Nodes::Join
34
- parent ||= join_parts.last
35
- reflection = parent.reflections[associations._name] or
36
- raise ::ActiveRecord::ConfigurationError, "Association named '#{ associations._name }' was not found; perhaps you misspelled it?"
37
-
38
- unless join_association = find_join_association_respecting_polymorphism(reflection, parent, associations._klass)
39
- @reflections << reflection
40
- join_association = build_join_association_respecting_polymorphism(reflection, parent, associations._klass)
41
- join_association.join_type = associations._type
42
- @join_parts << join_association
43
- cache_joined_association(join_association)
44
- end
19
+ associations = associations._join
20
+ end
45
21
 
46
- join_association
47
- when Nodes::KeyPath
48
- parent ||= join_parts.last
22
+ if Nodes::KeyPath === associations
23
+ parent ||= _join_parts.last
49
24
  associations.path_with_endpoint.each do |key|
50
25
  parent = build(key, parent, join_type)
51
26
  end
@@ -55,24 +30,6 @@ module Squeel
55
30
  end
56
31
  end
57
32
 
58
- def find_join_association_respecting_polymorphism(reflection, parent, klass)
59
- if association = find_join_association(reflection, parent)
60
- unless reflection.options[:polymorphic]
61
- association
62
- else
63
- association if association.active_record == klass
64
- end
65
- end
66
- end
67
-
68
- def build_join_association_respecting_polymorphism(reflection, parent, klass)
69
- if reflection.options[:polymorphic] && klass
70
- JoinAssociation.new(reflection, self, parent, klass)
71
- else
72
- JoinAssociation.new(reflection, self, parent)
73
- end
74
- end
75
-
76
33
  end
77
34
  end
78
35
  end
@@ -4,50 +4,34 @@ module Squeel
4
4
  class Join
5
5
  undef_method :id if method_defined?(:id)
6
6
 
7
- # @return [Symbol] The join's association name
8
- attr_reader :_name
7
+ attr_reader :_join
9
8
 
10
- # @return [Arel::InnerJoin, Arel::OuterJoin] The ARel join type
11
- attr_reader :_type
12
-
13
- # @return [Class] The polymorphic belongs_to join class
14
- # @return [NilClass] If the join is not a polymorphic belongs_to join
15
- attr_reader :_klass
9
+ delegate :name, :type, :klass, :name=, :type=, :klass=, :to => :_join, :prefix => ''
16
10
 
17
11
  # Create a new Join node
18
12
  # @param [Symbol] name The association name
19
13
  # @param [Arel::InnerJoin, Arel::OuterJoin] type The ARel join class
20
14
  # @param [Class, String, Symbol] klass The polymorphic belongs_to class or class name
21
15
  def initialize(name, type = Arel::InnerJoin, klass = nil)
22
- @_name, @_type = name, type
23
- @_klass = convert_to_class(klass) if klass
16
+ @_join = Polyamorous::Join.new(name, type, klass)
24
17
  end
25
18
 
26
19
  # Set the join type to an inner join
27
20
  # @return [Join] The join, with an updated join type.
28
21
  def inner
29
- @_type = Arel::InnerJoin
22
+ self._type = Arel::InnerJoin
30
23
  self
31
24
  end
32
25
 
33
26
  # Set the join type to an outer join
34
27
  # @return [Join] The join, with an updated join type.
35
28
  def outer
36
- @_type = Arel::OuterJoin
29
+ self._type = Arel::OuterJoin
37
30
  self
38
31
  end
39
32
 
40
- # Set the polymorphic belongs_to class
41
- # @param [Class, String, Symbol] class_or_class_name The polymorphic belongs_to class or class name
42
- # @return [Class] The class that's just been set
43
- def _klass=(class_or_class_name)
44
- @_klass = convert_to_class(class_or_class_name)
45
- end
46
-
47
- # Returns a true value (the class itself) if a polymorphic belongs_to class has been set
48
- # @return [NilClass, Class] The class, if present.
49
33
  def polymorphic?
50
- @_klass
34
+ _klass
51
35
  end
52
36
 
53
37
  # Compare with other objects
@@ -92,22 +76,6 @@ module Squeel
92
76
  nil
93
77
  end
94
78
 
95
- private
96
-
97
- # Convert the given value into a class.
98
- # @param [Class, String, Symbol] value The value to be converted
99
- # @return [Class] The class after conversion
100
- def convert_to_class(value)
101
- case value
102
- when String, Symbol
103
- Kernel.const_get(value)
104
- when Class
105
- value
106
- else
107
- raise ArgumentError, "#{value} cannot be converted to a Class"
108
- end
109
- end
110
-
111
79
  end
112
80
  end
113
81
  end
@@ -1,3 +1,3 @@
1
1
  module Squeel
2
- VERSION = "0.8.9"
2
+ VERSION = "0.8.10"
3
3
  end
@@ -16,7 +16,7 @@ module Squeel
16
16
  end
17
17
 
18
18
  it 'contextualizes join parts with the proper alias' do
19
- table = @c.contextualize @jd.join_parts.last
19
+ table = @c.contextualize @jd._join_parts.last
20
20
  table.table_alias.should eq 'parents_people_2'
21
21
  end
22
22
 
@@ -19,7 +19,7 @@ module Squeel
19
19
  visitor = relation.predicate_visitor
20
20
 
21
21
  visitor.should be_a Visitors::PredicateVisitor
22
- table = visitor.contextualize(relation.join_dependency.join_parts.last)
22
+ table = visitor.contextualize(relation.join_dependency._join_parts.last)
23
23
  table.table_alias.should eq 'parents_people_2'
24
24
  end
25
25
 
@@ -39,7 +39,7 @@ module Squeel
39
39
  visitor = relation.attribute_visitor
40
40
 
41
41
  visitor.should be_a Visitors::AttributeVisitor
42
- table = visitor.contextualize(relation.join_dependency.join_parts.last)
42
+ table = visitor.contextualize(relation.join_dependency._join_parts.last)
43
43
  table.table_alias.should eq 'parents_people_2'
44
44
  end
45
45
 
@@ -29,6 +29,7 @@ you're feeling especially appreciative. It'd help me justify this
29
29
 
30
30
  s.add_dependency 'activerecord', '~> 3.0'
31
31
  s.add_dependency 'activesupport', '~> 3.0'
32
+ s.add_dependency 'polyamorous', '~> 0.5.0'
32
33
  s.add_development_dependency 'rspec', '~> 2.6.0'
33
34
  s.add_development_dependency 'machinist', '~> 1.0.6'
34
35
  s.add_development_dependency 'faker', '~> 0.9.5'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: squeel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.9
4
+ version: 0.8.10
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-29 00:00:00.000000000Z
12
+ date: 2011-09-03 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &70295228232080 !ruby/object:Gem::Requirement
16
+ requirement: &70152532762100 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70295228232080
24
+ version_requirements: *70152532762100
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activesupport
27
- requirement: &70295228231580 !ruby/object:Gem::Requirement
27
+ requirement: &70152532761420 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,21 @@ dependencies:
32
32
  version: '3.0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70295228231580
35
+ version_requirements: *70152532761420
36
+ - !ruby/object:Gem::Dependency
37
+ name: polyamorous
38
+ requirement: &70152532757160 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 0.5.0
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70152532757160
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: rspec
38
- requirement: &70295228231120 !ruby/object:Gem::Requirement
49
+ requirement: &70152532756300 !ruby/object:Gem::Requirement
39
50
  none: false
40
51
  requirements:
41
52
  - - ~>
@@ -43,10 +54,10 @@ dependencies:
43
54
  version: 2.6.0
44
55
  type: :development
45
56
  prerelease: false
46
- version_requirements: *70295228231120
57
+ version_requirements: *70152532756300
47
58
  - !ruby/object:Gem::Dependency
48
59
  name: machinist
49
- requirement: &70295228230660 !ruby/object:Gem::Requirement
60
+ requirement: &70152532755220 !ruby/object:Gem::Requirement
50
61
  none: false
51
62
  requirements:
52
63
  - - ~>
@@ -54,10 +65,10 @@ dependencies:
54
65
  version: 1.0.6
55
66
  type: :development
56
67
  prerelease: false
57
- version_requirements: *70295228230660
68
+ version_requirements: *70152532755220
58
69
  - !ruby/object:Gem::Dependency
59
70
  name: faker
60
- requirement: &70295228230200 !ruby/object:Gem::Requirement
71
+ requirement: &70152532754580 !ruby/object:Gem::Requirement
61
72
  none: false
62
73
  requirements:
63
74
  - - ~>
@@ -65,10 +76,10 @@ dependencies:
65
76
  version: 0.9.5
66
77
  type: :development
67
78
  prerelease: false
68
- version_requirements: *70295228230200
79
+ version_requirements: *70152532754580
69
80
  - !ruby/object:Gem::Dependency
70
81
  name: sqlite3
71
- requirement: &70295228229740 !ruby/object:Gem::Requirement
82
+ requirement: &70152532753940 !ruby/object:Gem::Requirement
72
83
  none: false
73
84
  requirements:
74
85
  - - ~>
@@ -76,7 +87,7 @@ dependencies:
76
87
  version: 1.3.3
77
88
  type: :development
78
89
  prerelease: false
79
- version_requirements: *70295228229740
90
+ version_requirements: *70152532753940
80
91
  description: ! "\n Squeel unlocks the power of ARel in your Rails 3 application
81
92
  with\n a handy block-based syntax. You can write subqueries, access named\n
82
93
  \ functions provided by your RDBMS, and more, all without writing\n SQL
@@ -101,11 +112,8 @@ files:
101
112
  - lib/squeel/adapters/active_record/3.0/association_preload.rb
102
113
  - lib/squeel/adapters/active_record/3.0/compat.rb
103
114
  - lib/squeel/adapters/active_record/3.0/context.rb
104
- - lib/squeel/adapters/active_record/3.0/join_association.rb
105
- - lib/squeel/adapters/active_record/3.0/join_dependency.rb
106
115
  - lib/squeel/adapters/active_record/3.0/relation.rb
107
116
  - lib/squeel/adapters/active_record/context.rb
108
- - lib/squeel/adapters/active_record/join_association.rb
109
117
  - lib/squeel/adapters/active_record/join_dependency.rb
110
118
  - lib/squeel/adapters/active_record/preloader.rb
111
119
  - lib/squeel/adapters/active_record/relation.rb
@@ -149,7 +157,6 @@ files:
149
157
  - spec/helpers/squeel_helper.rb
150
158
  - spec/spec_helper.rb
151
159
  - spec/squeel/adapters/active_record/context_spec.rb
152
- - spec/squeel/adapters/active_record/join_association_spec.rb
153
160
  - spec/squeel/adapters/active_record/join_dependency_spec.rb
154
161
  - spec/squeel/adapters/active_record/relation_spec.rb
155
162
  - spec/squeel/core_ext/symbol_spec.rb
@@ -202,7 +209,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
202
209
  version: '0'
203
210
  requirements: []
204
211
  rubyforge_project: squeel
205
- rubygems_version: 1.8.6
212
+ rubygems_version: 1.8.10
206
213
  signing_key:
207
214
  specification_version: 3
208
215
  summary: ActiveRecord 3, improved.
@@ -217,7 +224,6 @@ test_files:
217
224
  - spec/helpers/squeel_helper.rb
218
225
  - spec/spec_helper.rb
219
226
  - spec/squeel/adapters/active_record/context_spec.rb
220
- - spec/squeel/adapters/active_record/join_association_spec.rb
221
227
  - spec/squeel/adapters/active_record/join_dependency_spec.rb
222
228
  - spec/squeel/adapters/active_record/relation_spec.rb
223
229
  - spec/squeel/core_ext/symbol_spec.rb
@@ -1,54 +0,0 @@
1
- require 'active_record'
2
-
3
- module Squeel
4
- module Adapters
5
- module ActiveRecord
6
- class JoinAssociation < ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation
7
-
8
- def initialize(reflection, join_dependency, parent = nil, polymorphic_class = nil)
9
- if polymorphic_class && ::ActiveRecord::Base > polymorphic_class
10
- swapping_reflection_klass(reflection, polymorphic_class) do |reflection|
11
- super(reflection, join_dependency, parent)
12
- end
13
- else
14
- super(reflection, join_dependency, parent)
15
- end
16
- end
17
-
18
- def swapping_reflection_klass(reflection, klass)
19
- reflection = reflection.clone
20
- original_polymorphic = reflection.options.delete(:polymorphic)
21
- reflection.instance_variable_set(:@klass, klass)
22
- yield reflection
23
- ensure
24
- reflection.options[:polymorphic] = original_polymorphic
25
- end
26
-
27
- def ==(other)
28
- super && active_record == other.active_record
29
- end
30
-
31
- def association_join
32
- return @join if @Join
33
-
34
- @join = super
35
-
36
- if reflection.macro == :belongs_to && reflection.options[:polymorphic]
37
- aliased_table = Arel::Table.new(table_name, :as => @aliased_table_name,
38
- :engine => arel_engine,
39
- :columns => klass.columns)
40
-
41
- parent_table = Arel::Table.new(parent.table_name, :as => parent.aliased_table_name,
42
- :engine => arel_engine,
43
- :columns => parent.active_record.columns)
44
-
45
- @join << parent_table[reflection.options[:foreign_type]].eq(reflection.klass.name)
46
- end
47
-
48
- @join
49
- end
50
-
51
- end
52
- end
53
- end
54
- end
@@ -1,80 +0,0 @@
1
- require 'active_record'
2
-
3
- module Squeel
4
- module Adapters
5
- module ActiveRecord
6
- module JoinDependency
7
-
8
- def self.included(base)
9
- base.class_eval do
10
- alias_method_chain :build, :squeel
11
- alias_method_chain :graft, :squeel
12
- alias :join_parts :joins
13
- end
14
- end
15
-
16
- def graft_with_squeel(*associations)
17
- associations.each do |association|
18
- unless join_associations.detect {|a| association == a}
19
- if association.reflection.options[:polymorphic]
20
- build(Nodes::Join.new(association.reflection.name, association.join_type, association.reflection.klass),
21
- association.find_parent_in(self) || join_base, association.join_type)
22
- else
23
- build(association.reflection.name, association.find_parent_in(self) || join_base, association.join_type)
24
- end
25
- end
26
- end
27
- self
28
- end
29
-
30
- def build_with_squeel(associations, parent = nil, join_type = Arel::InnerJoin)
31
- associations = associations.symbol if Nodes::Stub === associations
32
-
33
- case associations
34
- when Nodes::Join
35
- parent ||= @joins.last
36
- reflection = parent.reflections[associations._name] or
37
- raise ::ActiveRecord::ConfigurationError, "Association named '#{ associations._name }' was not found; perhaps you misspelled it?"
38
-
39
- unless join_association = find_join_association_respecting_polymorphism(reflection, parent, associations._klass)
40
- @reflections << reflection
41
- join_association = build_join_association_respecting_polymorphism(reflection, parent, associations._klass)
42
- join_association.join_type = associations._type
43
- @joins << join_association
44
- cache_joined_association(join_association)
45
- end
46
-
47
- join_association
48
- when Nodes::KeyPath
49
- parent ||= @joins.last
50
- associations.path_with_endpoint.each do |key|
51
- parent = build(key, parent, join_type)
52
- end
53
- parent
54
- else
55
- build_without_squeel(associations, parent, join_type)
56
- end
57
- end
58
-
59
- def find_join_association_respecting_polymorphism(reflection, parent, klass)
60
- if association = find_join_association(reflection, parent)
61
- unless reflection.options[:polymorphic]
62
- association
63
- else
64
- association if association.active_record == klass
65
- end
66
- end
67
- end
68
-
69
- def build_join_association_respecting_polymorphism(reflection, parent, klass)
70
- if reflection.options[:polymorphic] && klass
71
- JoinAssociation.new(reflection, self, parent, klass)
72
- else
73
- JoinAssociation.new(reflection, self, parent)
74
- end
75
- end
76
-
77
- end
78
- end
79
- end
80
- end
@@ -1,44 +0,0 @@
1
- require 'active_record'
2
-
3
- module Squeel
4
- module Adapters
5
- module ActiveRecord
6
- class JoinAssociation < ::ActiveRecord::Associations::JoinDependency::JoinAssociation
7
-
8
- def initialize(reflection, join_dependency, parent = nil, polymorphic_class = nil)
9
- if polymorphic_class && ::ActiveRecord::Base > polymorphic_class
10
- swapping_reflection_klass(reflection, polymorphic_class) do |reflection|
11
- super(reflection, join_dependency, parent)
12
- end
13
- else
14
- super(reflection, join_dependency, parent)
15
- end
16
- end
17
-
18
- def swapping_reflection_klass(reflection, klass)
19
- reflection = reflection.clone
20
- original_polymorphic = reflection.options.delete(:polymorphic)
21
- reflection.instance_variable_set(:@klass, klass)
22
- yield reflection
23
- ensure
24
- reflection.options[:polymorphic] = original_polymorphic
25
- end
26
-
27
- def ==(other)
28
- super && active_record == other.active_record
29
- end
30
-
31
- def build_constraint(reflection, table, key, foreign_table, foreign_key)
32
- if reflection.options[:polymorphic]
33
- super.and(
34
- foreign_table[reflection.foreign_type].eq(reflection.klass.name)
35
- )
36
- else
37
- super
38
- end
39
- end
40
-
41
- end
42
- end
43
- end
44
- end
@@ -1,20 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Squeel
4
- module Adapters
5
- module ActiveRecord
6
- describe JoinAssociation do
7
- before do
8
- @jd = new_join_dependency(Note, {}, [])
9
- @notable = Note.reflect_on_association(:notable)
10
- end
11
-
12
- it 'accepts a 4th parameter to set a polymorphic class' do
13
- join_association = JoinAssociation.new(@notable, @jd, @jd.join_base, Article)
14
- join_association.reflection.klass.should eq Article
15
- end
16
-
17
- end
18
- end
19
- end
20
- end