referrable_joins 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,39 @@
|
|
1
1
|
module ActiveRecord
|
2
2
|
|
3
|
+
class ReflectionTable
|
4
|
+
|
5
|
+
attr_reader :association
|
6
|
+
attr_accessor :relation
|
7
|
+
|
8
|
+
def [](name)
|
9
|
+
relation.columns.find { |col| col.name == name }
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(table, association)
|
13
|
+
@association = association
|
14
|
+
@relation = Arel::Table.new(table, ActiveRecord::Base)
|
15
|
+
end
|
16
|
+
|
17
|
+
def mirror_relation(other)
|
18
|
+
["engine", "columns", "table_alias"].each do |ivar|
|
19
|
+
@relation.instance_variable_set("@#{ivar}", other.instance_variable_get("@#{ivar}"))
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_s
|
24
|
+
@association.to_s
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
3
29
|
class Base
|
4
30
|
class << self
|
5
31
|
delegate :outer_joins, :inner_joins, :to => :scoped
|
32
|
+
|
33
|
+
def reflection_table(reflection)
|
34
|
+
ActiveRecord::ReflectionTable.new(self.reflections[reflection].table_name.to_sym)
|
35
|
+
end
|
36
|
+
|
6
37
|
end
|
7
38
|
end
|
8
39
|
|
@@ -70,11 +101,11 @@ module ActiveRecord
|
|
70
101
|
outer_joins = @outer_joins_values.map {|j| j.respond_to?(:strip) ? j.strip : j}.uniq
|
71
102
|
|
72
103
|
inner_joins.each do |join|
|
73
|
-
inner_association_joins << join if [Hash, Array, Symbol,
|
104
|
+
inner_association_joins << join if [Hash, Array, Symbol, ActiveRecord::ReflectionTable].include?(join.class) && !array_of_strings?(join)
|
74
105
|
end
|
75
106
|
|
76
107
|
outer_joins.each do |join|
|
77
|
-
outer_association_joins << join if [Hash, Array, Symbol,
|
108
|
+
outer_association_joins << join if [Hash, Array, Symbol, ActiveRecord::ReflectionTable].include?(join.class) && !array_of_strings?(join)
|
78
109
|
end
|
79
110
|
|
80
111
|
stashed_association_joins = (inner_joins + outer_joins).grep(ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation)
|
@@ -128,15 +159,15 @@ module ActiveRecord
|
|
128
159
|
def build(associations, parent = nil, join_type = Arel::InnerJoin)
|
129
160
|
parent ||= @joins.last
|
130
161
|
case associations
|
131
|
-
# MONKEYPATCH Added
|
132
|
-
when Symbol, String,
|
162
|
+
# MONKEYPATCH Added ActiveRecord::ReflectionTable here
|
163
|
+
when Symbol, String, ActiveRecord::ReflectionTable
|
133
164
|
reflection = parent.reflections[associations.to_s.intern] or
|
134
165
|
raise ConfigurationError, "Association named '#{ associations }' was not found; perhaps you misspelled it\?"
|
135
166
|
unless join_association = find_join_association(reflection, parent)
|
136
167
|
@reflections << reflection
|
137
168
|
join_association = build_join_association(reflection, parent)
|
138
169
|
# <MONKEYPATCH added this conditional
|
139
|
-
if associations.kind_of?(
|
170
|
+
if associations.kind_of?(ActiveRecord::ReflectionTable)
|
140
171
|
associations.mirror_relation(join_association.relation)
|
141
172
|
end
|
142
173
|
# >MONKEYPATCH
|
data/lib/referrable_joins.rb
CHANGED
@@ -1,29 +1,3 @@
|
|
1
|
-
class ReferrableJoin
|
2
|
-
|
3
|
-
attr_reader :association
|
4
|
-
attr_accessor :relation
|
5
|
-
|
6
|
-
def [](name)
|
7
|
-
relation.columns.find { |col| col.name == name }
|
8
|
-
end
|
9
|
-
|
10
|
-
def initialize(table, association)
|
11
|
-
@association = association
|
12
|
-
@relation = Arel::Table.new(table, ActiveRecord::Base)
|
13
|
-
end
|
14
|
-
|
15
|
-
def mirror_relation(other)
|
16
|
-
["engine", "columns", "table_alias"].each do |ivar|
|
17
|
-
@relation.instance_variable_set("@#{ivar}", other.instance_variable_get("@#{ivar}"))
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def to_s
|
22
|
-
@association.to_s
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
26
|
-
|
27
1
|
path = File.join(File.dirname(__FILE__), 'referrable_joins')
|
28
2
|
$:.unshift(path) unless $:.include?(path)
|
29
3
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: referrable_joins
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Burke Libbey
|