jakewendt-active_record_left_joins 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.rdoc +27 -0
- data/lib/active_record_left_joins.rb +101 -0
- data/lib/jakewendt-active_record_left_joins.rb +1 -0
- metadata +46 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 01a9e2cde54ac4b6de770438731c4d8441e9849a
|
4
|
+
data.tar.gz: cbf3a7f4f3c457d11f9344a452ff9a563bcfa6cf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a83faaa3d19553dd3efd0856a5264da3506b9a722a3f2b763be798b740066b1a076b1b4401c05ce29aaca4332ae5cbfa6e91f931e3d4a4e169e1ec3c0965d2a1
|
7
|
+
data.tar.gz: b590f84fab3d15ecd063bb7e66395b9dd2e51d3166115e6f78078fa6f8531e17c97ceec826acf0217da8f67ea66b197626a460ce7c0923689d95dd93c7408556
|
data/README.rdoc
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
= active_record_left_joins
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
doesn't work with has_one or has_many :through, yet
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
== Gemified with Jeweler
|
14
|
+
|
15
|
+
vi Rakefile
|
16
|
+
rake version:write
|
17
|
+
|
18
|
+
rake version:bump:patch
|
19
|
+
rake version:bump:minor
|
20
|
+
rake version:bump:major
|
21
|
+
|
22
|
+
rake gemspec
|
23
|
+
|
24
|
+
rake install
|
25
|
+
rake release
|
26
|
+
|
27
|
+
Copyright (c) 2010 [Jake Wendt], released under the MIT license
|
@@ -0,0 +1,101 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Querying
|
3
|
+
# This makes it available as the first call
|
4
|
+
delegate :left_joins, :to => :scoped
|
5
|
+
end # module Querying
|
6
|
+
class Relation
|
7
|
+
|
8
|
+
# rather than just joins(:some_association) INNER JOIN
|
9
|
+
# left_joins(:some_association) will build an OUTER LEFT JOIN
|
10
|
+
def left_joins(*args)
|
11
|
+
return self if args.compact.blank?
|
12
|
+
|
13
|
+
relation = clone
|
14
|
+
|
15
|
+
args.flatten!
|
16
|
+
|
17
|
+
# args could be just a single symbol
|
18
|
+
# ...could be an array of symbols (
|
19
|
+
# ...could be a hash (joins(:address => :address_type))
|
20
|
+
# ...could be a string (SQL)
|
21
|
+
|
22
|
+
# here I really am just expecting an array of model names as a symbols
|
23
|
+
|
24
|
+
# this only works if the primary and foreign keys are conventional
|
25
|
+
|
26
|
+
# @klass (or its reader klass() ) is the calling class (Organization)
|
27
|
+
# @table (or its reader table() ) is the calling class's Arel::Table
|
28
|
+
|
29
|
+
|
30
|
+
# StudySubject
|
31
|
+
# scope :join_patients, joins(
|
32
|
+
# Arel::Nodes::OuterJoin.new(Patient.arel_table,Arel::Nodes::On.new(
|
33
|
+
# self.arel_table[:id].eq(Patient.arel_table[:study_subject_id]))))
|
34
|
+
# scope :join_patients, left_joins(:patient) # WORKS
|
35
|
+
|
36
|
+
# Organization
|
37
|
+
# scope :without_sample_location, joins(
|
38
|
+
# Arel::Nodes::OuterJoin.new(
|
39
|
+
# SampleLocation.arel_table,Arel::Nodes::On.new(
|
40
|
+
# self.arel_table[:id].eq(SampleLocation.arel_table[:organization_id]))))
|
41
|
+
# scope :without_sample_location, left_joins(:sample_location) # WORKS
|
42
|
+
#
|
43
|
+
# Organization has one Sample Location
|
44
|
+
|
45
|
+
# scope :without_hospital, joins(
|
46
|
+
# Arel::Nodes::OuterJoin.new(
|
47
|
+
# Hospital.arel_table,Arel::Nodes::On.new(
|
48
|
+
# self.arel_table[:id].eq(Hospital.arel_table[:organization_id]))))
|
49
|
+
# scope :without_hospital, left_joins(:hospital) # WORKS
|
50
|
+
|
51
|
+
# Organization has one Hospital
|
52
|
+
|
53
|
+
|
54
|
+
# @zip_codes = ZipCode.joins(
|
55
|
+
# Arel::Nodes::OuterJoin.new(County.arel_table,Arel::Nodes::On.new(
|
56
|
+
# ZipCode.arel_table[:county_id].eq(County.arel_table[:id]))))
|
57
|
+
#
|
58
|
+
# @zip_codes = ZipCode.left_joins(:county) #??? backwards? won't work?
|
59
|
+
|
60
|
+
# ZipCode belongs to County
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
# doesn't work on has_one or has_many ... :through
|
65
|
+
|
66
|
+
|
67
|
+
args.each do |arg| # no, I'm not a pirate
|
68
|
+
reflection = klass.reflections[arg]
|
69
|
+
joining_model = reflection.klass
|
70
|
+
#
|
71
|
+
# :has_one and probably :has_many ( BUT NOT :belongs_to )
|
72
|
+
#
|
73
|
+
relation.joins_values += [
|
74
|
+
Arel::Nodes::OuterJoin.new(
|
75
|
+
joining_model.arel_table,
|
76
|
+
( reflection.macro == :belongs_to ) ?
|
77
|
+
Arel::Nodes::On.new(
|
78
|
+
table[ reflection.foreign_key ].eq(
|
79
|
+
# joining_model.arel_table[ :id ]
|
80
|
+
joining_model.arel_table[ reflection.options[:primary_key] || :id ]
|
81
|
+
)
|
82
|
+
) :
|
83
|
+
Arel::Nodes::On.new(
|
84
|
+
table[:id].eq( # won't ALWAYS be :id
|
85
|
+
joining_model.arel_table[ reflection.foreign_key ]
|
86
|
+
)
|
87
|
+
)
|
88
|
+
|
89
|
+
)
|
90
|
+
]
|
91
|
+
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
end # args.each do |arg| # no, I'm not a pirate
|
96
|
+
|
97
|
+
relation
|
98
|
+
end
|
99
|
+
|
100
|
+
end # class Relation
|
101
|
+
end # module ActiveRecord
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'active_record_left_joins'
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jakewendt-active_record_left_joins
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- George 'Jake' Wendt
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-12-03 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: active_record_left_joins
|
14
|
+
email: github@jakewendt.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files:
|
18
|
+
- README.rdoc
|
19
|
+
files:
|
20
|
+
- lib/active_record_left_joins.rb
|
21
|
+
- lib/jakewendt-active_record_left_joins.rb
|
22
|
+
- README.rdoc
|
23
|
+
homepage: http://github.com/jakewendt/active_record_left_joins
|
24
|
+
licenses: []
|
25
|
+
metadata: {}
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 2.0.14
|
43
|
+
signing_key:
|
44
|
+
specification_version: 4
|
45
|
+
summary: active_record_left_joins
|
46
|
+
test_files: []
|