referrable_joins 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +2 -0
- data/lib/referrable_joins/version.rb +3 -0
- data/lib/referrable_joins.rb +108 -0
- data/referrable_joins.gemspec +19 -0
- metadata +73 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
|
3
|
+
# MONKEYPATCH This class is entirely new
|
4
|
+
class ReferrableJoin
|
5
|
+
|
6
|
+
attr_reader :association
|
7
|
+
attr_accessor :relation
|
8
|
+
|
9
|
+
def initialize(table, association)
|
10
|
+
@association = association
|
11
|
+
@relation = Arel::Table.new(table, ActiveRecord::Base)
|
12
|
+
end
|
13
|
+
|
14
|
+
def mirror_relation(other)
|
15
|
+
["engine", "columns", "table_alias"].each do |ivar|
|
16
|
+
@relation.instance_variable_set("@#{ivar}", other.instance_variable_get("@#{ivar}"))
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_s
|
21
|
+
@association.to_s
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
module QueryMethods
|
27
|
+
def build_joins(relation, joins)
|
28
|
+
association_joins = []
|
29
|
+
|
30
|
+
joins = @joins_values.map {|j| j.respond_to?(:strip) ? j.strip : j}.uniq
|
31
|
+
|
32
|
+
joins.each do |join|
|
33
|
+
# MONKEYPATCH Added ActiveRecord::ReferrableJoin to the array here.
|
34
|
+
association_joins << join if [Hash, Array, Symbol, ActiveRecord::ReferrableJoin].include?(join.class) && !array_of_strings?(join)
|
35
|
+
end
|
36
|
+
|
37
|
+
stashed_association_joins = joins.grep(ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation)
|
38
|
+
|
39
|
+
non_association_joins = (joins - association_joins - stashed_association_joins)
|
40
|
+
custom_joins = custom_join_sql(*non_association_joins)
|
41
|
+
|
42
|
+
join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, association_joins, custom_joins)
|
43
|
+
|
44
|
+
join_dependency.graft(*stashed_association_joins)
|
45
|
+
|
46
|
+
@implicit_readonly = true unless association_joins.empty? && stashed_association_joins.empty?
|
47
|
+
|
48
|
+
to_join = []
|
49
|
+
|
50
|
+
join_dependency.join_associations.each do |association|
|
51
|
+
if (association_relation = association.relation).is_a?(Array)
|
52
|
+
to_join << [association_relation.first, association.join_type, association.association_join.first]
|
53
|
+
to_join << [association_relation.last, association.join_type, association.association_join.last]
|
54
|
+
else
|
55
|
+
to_join << [association_relation, association.join_type, association.association_join]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
to_join.uniq.each do |left, join_type, right|
|
60
|
+
relation = relation.join(left, join_type).on(*right)
|
61
|
+
end
|
62
|
+
|
63
|
+
relation.join(custom_joins)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
module Associations
|
69
|
+
module ClassMethods
|
70
|
+
class JoinDependency
|
71
|
+
|
72
|
+
def build(associations, parent = nil, join_type = Arel::InnerJoin)
|
73
|
+
parent ||= @joins.last
|
74
|
+
case associations
|
75
|
+
# MONKEYPATCH Added ActiveRecord::ReferrableJoin here
|
76
|
+
when Symbol, String, ActiveRecord::ReferrableJoin
|
77
|
+
reflection = parent.reflections[associations.to_s.intern] or
|
78
|
+
raise ConfigurationError, "Association named '#{ associations }' was not found; perhaps you misspelled it\?"
|
79
|
+
unless join_association = find_join_association(reflection, parent)
|
80
|
+
@reflections << reflection
|
81
|
+
join_association = build_join_association(reflection, parent)
|
82
|
+
# <MONKEYPATCH added this conditional
|
83
|
+
if associations.kind_of?(ActiveRecord::ReferrableJoin)
|
84
|
+
associations.mirror_relation(join_association.relation)
|
85
|
+
end
|
86
|
+
# >MONKEYPATCH
|
87
|
+
join_association.join_type = join_type
|
88
|
+
@joins << join_association
|
89
|
+
cache_joined_association(join_association)
|
90
|
+
end
|
91
|
+
join_association
|
92
|
+
when Array
|
93
|
+
associations.each do |association|
|
94
|
+
build(association, parent, join_type)
|
95
|
+
end
|
96
|
+
when Hash
|
97
|
+
associations.keys.sort{|a,b|a.to_s<=>b.to_s}.each do |name|
|
98
|
+
join_association = build(name, parent, join_type)
|
99
|
+
build(associations[name], join_association, join_type)
|
100
|
+
end
|
101
|
+
else
|
102
|
+
raise ConfigurationError, associations.inspect
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "referrable_joins/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "referrable_joins"
|
7
|
+
s.version = ReferrableJoins::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Burke Libbey"]
|
10
|
+
s.email = ["burke@burkelibbey.org"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{Adds the ability to refer to columns in a specific auto-generated join using the Arel::Table class}
|
13
|
+
s.description = %q{Adds the ability to refer to columns in a specific auto-generated join using the Arel::Table class}
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: referrable_joins
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Burke Libbey
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-04-03 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Adds the ability to refer to columns in a specific auto-generated join using the Arel::Table class
|
23
|
+
email:
|
24
|
+
- burke@burkelibbey.org
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- Rakefile
|
35
|
+
- lib/referrable_joins.rb
|
36
|
+
- lib/referrable_joins/version.rb
|
37
|
+
- referrable_joins.gemspec
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: ""
|
40
|
+
licenses: []
|
41
|
+
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
hash: 3
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.6.0
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Adds the ability to refer to columns in a specific auto-generated join using the Arel::Table class
|
72
|
+
test_files: []
|
73
|
+
|