better_ar 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/HISTORY.txt +3 -0
- data/TODO +1 -0
- data/lib/better_ar/finder_methods.rb +28 -24
- data/lib/better_ar/version.rb +1 -1
- data/test/test_finder_methods.rb +7 -0
- metadata +4 -3
data/HISTORY.txt
CHANGED
@@ -29,30 +29,9 @@ module BetterAr
|
|
29
29
|
super(opts)
|
30
30
|
else
|
31
31
|
relation = clone
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
opts.keys.select { |key| key.to_s =~ /!$/ }.each do |predicate|
|
36
|
-
if value = opts.delete(predicate)
|
37
|
-
relation = relation.send(predicate.to_s.sub('!',''), value)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
reflect_on_all_associations.map(&:name).each do |name|
|
42
|
-
if opts.key?(name)
|
43
|
-
relation = relation.joins(name)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
relation.where(opts)
|
48
|
-
end
|
49
|
-
|
50
|
-
if opts.empty?
|
51
|
-
relation
|
52
|
-
else
|
53
|
-
relation.where(opts)
|
54
|
-
end
|
55
|
-
end
|
32
|
+
relation = better_ar_apply_sql_clauses(relation, opts)
|
33
|
+
relation = better_ar_apply_associations(relation, opts)
|
34
|
+
relation.where(opts)
|
56
35
|
end
|
57
36
|
end
|
58
37
|
|
@@ -78,6 +57,31 @@ module BetterAr
|
|
78
57
|
all(opts.merge(:limit! => 1)).first
|
79
58
|
end
|
80
59
|
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def better_ar_apply_sql_clauses(relation, opts = {})
|
64
|
+
opts.keys.select { |key| key.to_s =~ /!$/ }.each do |clause|
|
65
|
+
if value = opts.delete(clause)
|
66
|
+
relation = relation.send(clause.to_s.sub('!',''), value)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
relation
|
71
|
+
end
|
72
|
+
|
73
|
+
def better_ar_apply_associations(relation, opts = {})
|
74
|
+
(reflect_on_all_associations.map(&:name) & better_ar_join_keys(opts)).each do |name|
|
75
|
+
relation = relation.joins(name)
|
76
|
+
end
|
77
|
+
|
78
|
+
relation
|
79
|
+
end
|
80
|
+
|
81
|
+
def better_ar_join_keys(opts = {})
|
82
|
+
opts.keys.select { |key| !opts[key].kind_of?(ActiveRecord::Base) }
|
83
|
+
end
|
84
|
+
|
81
85
|
end
|
82
86
|
end
|
83
87
|
|
data/lib/better_ar/version.rb
CHANGED
data/test/test_finder_methods.rb
CHANGED
@@ -17,6 +17,13 @@ describe 'ActiveRecord::Relation Finder Methods' do
|
|
17
17
|
expected_sql = User.joins(:records).where(:records => { :name => 'test' }).to_sql
|
18
18
|
test_sql.must_be_like expected_sql
|
19
19
|
end
|
20
|
+
|
21
|
+
it 'will ignore implicit joins if value is an instance of ActiveRecord::Base' do
|
22
|
+
user = User.create
|
23
|
+
test_sql = Record.all(:user => user).to_sql
|
24
|
+
expected_sql = Record.where(:user => user).to_sql
|
25
|
+
test_sql.must_be_like expected_sql
|
26
|
+
end
|
20
27
|
end
|
21
28
|
|
22
29
|
describe '.first' do
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 8
|
9
|
+
version: 0.0.8
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Brian Cardarella
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-02-
|
17
|
+
date: 2011-02-15 00:00:00 -05:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -49,6 +49,7 @@ files:
|
|
49
49
|
- MIT-LICENSE.txt
|
50
50
|
- README.markdown
|
51
51
|
- Rakefile
|
52
|
+
- TODO
|
52
53
|
- better_ar.gemspec
|
53
54
|
- lib/better_ar.rb
|
54
55
|
- lib/better_ar/association_collection.rb
|