better_ar 0.0.4 → 0.0.5
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/README.markdown +3 -3
- data/lib/better_ar.rb +30 -11
- data/lib/better_ar/version.rb +1 -1
- data/test/helper.rb +9 -1
- data/test/test_finder_methods.rb +6 -0
- metadata +3 -3
data/HISTORY.txt
CHANGED
data/README.markdown
CHANGED
@@ -15,13 +15,13 @@ Command line:
|
|
15
15
|
|
16
16
|
## Usage
|
17
17
|
|
18
|
-
Now a single hash can be passed to ActiveRecord::Base.all that contains not only the where values but also other scopes like 'limit' and 'order'
|
18
|
+
Now a single hash can be passed to ActiveRecord::Base.all that contains not only the where values but also other scopes like 'limit' and 'order' and also implicit joins
|
19
19
|
|
20
|
-
User.all(:age => 10, :limit! => 5, :offset! => 10, :order! => :name)
|
20
|
+
User.all(:age => 10, :limit! => 5, :offset! => 10, :order! => :name, :records => {:name => 'test'})
|
21
21
|
|
22
22
|
is the same as
|
23
23
|
|
24
|
-
User.where(:age => 10).limit(5).offset(10).order(:name)
|
24
|
+
User.joins(:records).where(:age => 10, :records => {:name => 'test'}).limit(5).offset(10).order(:name)
|
25
25
|
|
26
26
|
While this may seem less concise the advantage is being able to dynamically construct the query with a single hash in code.
|
27
27
|
|
data/lib/better_ar.rb
CHANGED
@@ -1,15 +1,24 @@
|
|
1
1
|
module BetterAr
|
2
2
|
module ClassMethods
|
3
3
|
|
4
|
-
# Breaks down the hash to do a ActiveRecord::Relation query
|
4
|
+
# Breaks down the hash to do a {ActiveRecord::Relation} query
|
5
5
|
#
|
6
|
-
# example:
|
6
|
+
# example:
|
7
|
+
# User.all(:age => 10, :limit! => 2, :offset! => 3, :order! => :name)
|
7
8
|
#
|
8
|
-
# is the same as:
|
9
|
+
# is the same as:
|
10
|
+
# User.where(:age => 10).limit(2).offset(3).order(:name)
|
9
11
|
#
|
10
12
|
# if the key :conditions is present it will fall back to legacy
|
11
13
|
#
|
12
|
-
# any key with the '!' at the end will be assumed to be a sql operator. The key should match either an ActiveRecord::Relation instance method or an ARel predicate.
|
14
|
+
# any key with the '!' at the end will be assumed to be a sql operator. The key should match either an {ActiveRecord::Relation} instance method or an ARel predicate.
|
15
|
+
#
|
16
|
+
# Implicit joins are supported.
|
17
|
+
# example:
|
18
|
+
# User.all(:records => {:name => 'test'})
|
19
|
+
#
|
20
|
+
# is the same as:
|
21
|
+
# User.joins(:records).where(:records => {:name => 'test'})
|
13
22
|
#
|
14
23
|
# @param [Hash]
|
15
24
|
# Optional
|
@@ -24,6 +33,13 @@ module BetterAr
|
|
24
33
|
relation = relation.send(predicate.to_s.sub('!',''), value)
|
25
34
|
end
|
26
35
|
end
|
36
|
+
|
37
|
+
reflect_on_all_associations.map(&:name).each do |name|
|
38
|
+
if opts.key?(name)
|
39
|
+
relation = relation.joins(name)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
27
43
|
relation.where(opts)
|
28
44
|
end
|
29
45
|
|
@@ -35,19 +51,20 @@ module BetterAr
|
|
35
51
|
else
|
36
52
|
relation.all(opts)
|
37
53
|
end
|
38
|
-
|
39
54
|
end
|
40
55
|
|
41
56
|
# Forces a limit of 1 on the collection
|
42
57
|
#
|
43
|
-
# example:
|
58
|
+
# example:
|
59
|
+
# User.first(:age => 10, :name => 'Brian')
|
44
60
|
#
|
45
|
-
# is the same as:
|
61
|
+
# is the same as:
|
62
|
+
# User.where(:age => 10, :name => 'Brian').limit(1).first
|
46
63
|
#
|
47
64
|
# if the key :conditions is present it will fall back to legacy
|
48
65
|
#
|
49
66
|
# @param [Hash]
|
50
|
-
# Optional follows same convention as
|
67
|
+
# Optional follows same convention as {#all}
|
51
68
|
# @return [ActiveRecord::Base]
|
52
69
|
def first(opts = {})
|
53
70
|
unless opts.key?(:conditions)
|
@@ -59,14 +76,16 @@ module BetterAr
|
|
59
76
|
|
60
77
|
# Does a count on the query
|
61
78
|
#
|
62
|
-
# example:
|
79
|
+
# example:
|
80
|
+
# User.count(:age => 20)
|
63
81
|
#
|
64
|
-
# is the same as:
|
82
|
+
# is the same as:
|
83
|
+
# User.where(:age => 20).count
|
65
84
|
#
|
66
85
|
# if the key :conditions is present it will fall back to legacy
|
67
86
|
#
|
68
87
|
# @param [Hash]
|
69
|
-
# Optional follows same convention as
|
88
|
+
# Optional follows same convention as {#all}
|
70
89
|
# @return [Integer]
|
71
90
|
def count(opts = {})
|
72
91
|
unless opts.key?(:conditions)
|
data/lib/better_ar/version.rb
CHANGED
data/test/helper.rb
CHANGED
@@ -17,7 +17,15 @@ ActiveRecord::Base.establish_connection(
|
|
17
17
|
)
|
18
18
|
|
19
19
|
users_table = %{CREATE TABLE users (id INTEGER PRIMARY KEY, age INTEGER, name TEXT);}
|
20
|
+
records_table = %{CREATE TABLE records (id INTEGER PRIMARY KEY, user_id INTEGER, name TEXT);}
|
20
21
|
ActiveRecord::Base.connection.execute(users_table)
|
22
|
+
ActiveRecord::Base.connection.execute(records_table)
|
21
23
|
|
22
|
-
class User < ActiveRecord::Base
|
24
|
+
class User < ActiveRecord::Base
|
25
|
+
has_many :records
|
26
|
+
end
|
27
|
+
|
28
|
+
class Record < ActiveRecord::Base
|
29
|
+
belongs_to :user
|
30
|
+
end
|
23
31
|
|
data/test/test_finder_methods.rb
CHANGED
@@ -17,6 +17,12 @@ describe 'Enhanced Finder Methods' do
|
|
17
17
|
expected_sql = User.where(:age => 10).limit(1).offset(2).order(:name).to_sql
|
18
18
|
test_sql.must_be_like expected_sql
|
19
19
|
end
|
20
|
+
|
21
|
+
it 'finds implicit joins by reflection' do
|
22
|
+
test_sql = User.all(:records => { :name => 'test' }).to_sql
|
23
|
+
expected_sql = User.joins(:records).where(:records => { :name => 'test' }).to_sql
|
24
|
+
test_sql.must_be_like expected_sql
|
25
|
+
end
|
20
26
|
end
|
21
27
|
|
22
28
|
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
|
+
- 5
|
9
|
+
version: 0.0.5
|
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-
|
17
|
+
date: 2011-02-11 00:00:00 -05:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|