better_ar 0.0.5 → 0.0.6
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 +0 -2
- data/better_ar.gemspec +3 -3
- data/lib/better_ar.rb +72 -74
- data/lib/better_ar/version.rb +1 -1
- data/test/helper.rb +2 -1
- data/test/test_finder_methods.rb +24 -14
- metadata +3 -29
data/HISTORY.txt
CHANGED
data/README.markdown
CHANGED
@@ -25,8 +25,6 @@ is the same as
|
|
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
|
|
28
|
-
This library completely removes the old .all, .first and .count class methods from ActiveRecord::Base. As these methods were just delegated to ActiveRecord::Base.scoped they can still be called.
|
29
|
-
|
30
28
|
### Legacy
|
31
29
|
|
32
30
|
If the params contain :condition it will fall back to the legacy method.
|
data/better_ar.gemspec
CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
20
|
s.require_paths = ["lib"]
|
21
21
|
|
22
|
-
s.add_development_dependency 'activerecord', ['
|
23
|
-
s.add_development_dependency 'mocha'
|
24
|
-
s.add_development_dependency 'yard'
|
22
|
+
s.add_development_dependency 'activerecord', ['~> 3.0.3']
|
23
|
+
# s.add_development_dependency 'mocha'
|
24
|
+
# s.add_development_dependency 'yard'
|
25
25
|
end
|
data/lib/better_ar.rb
CHANGED
@@ -1,30 +1,34 @@
|
|
1
1
|
module BetterAr
|
2
|
-
module ClassMethods
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
3
|
+
# Breaks down the hash to do a {ActiveRecord::Relation} query
|
4
|
+
#
|
5
|
+
# example:
|
6
|
+
# User.all(:age => 10, :limit! => 2, :offset! => 3, :order! => :name)
|
7
|
+
#
|
8
|
+
# is the same as:
|
9
|
+
# User.where(:age => 10).limit(2).offset(3).order(:name)
|
10
|
+
#
|
11
|
+
# if the key :conditions is present it will fall back to legacy
|
12
|
+
#
|
13
|
+
# 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
|
+
#
|
15
|
+
# Implicit joins are supported.
|
16
|
+
# example:
|
17
|
+
# User.all(:records => {:name => 'test'})
|
18
|
+
#
|
19
|
+
# is the same as:
|
20
|
+
# User.joins(:records).where(:records => {:name => 'test'})
|
21
|
+
#
|
22
|
+
# @param [Hash]
|
23
|
+
# Optional
|
24
|
+
# @return [ActiveRecord::Relation]
|
25
|
+
def all(opts = {})
|
26
|
+
if opts.empty?
|
27
|
+
super()
|
28
|
+
elsif opts.key?(:conditions)
|
29
|
+
super(opts)
|
30
|
+
else
|
31
|
+
relation = clone
|
28
32
|
|
29
33
|
unless opts.key?(:conditions)
|
30
34
|
unless opts.empty?
|
@@ -48,62 +52,56 @@ module BetterAr
|
|
48
52
|
else
|
49
53
|
relation.where(opts)
|
50
54
|
end
|
51
|
-
else
|
52
|
-
relation.all(opts)
|
53
55
|
end
|
54
56
|
end
|
57
|
+
end
|
55
58
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
59
|
+
# Forces a limit of 1 on the collection
|
60
|
+
#
|
61
|
+
# example:
|
62
|
+
# User.first(:age => 10, :name => 'Brian')
|
63
|
+
#
|
64
|
+
# is the same as:
|
65
|
+
# User.where(:age => 10, :name => 'Brian').limit(1).first
|
66
|
+
#
|
67
|
+
# if the key :conditions is present it will fall back to legacy
|
68
|
+
#
|
69
|
+
# @param [Hash]
|
70
|
+
# Optional follows same convention as {#all}
|
71
|
+
# @return [ActiveRecord::Base]
|
72
|
+
def first(opts = {})
|
73
|
+
if opts.empty?
|
74
|
+
super()
|
75
|
+
elsif opts.key?(:conditions)
|
76
|
+
super(opts)
|
77
|
+
else
|
78
|
+
all(opts.merge(:limit! => 1)).first
|
75
79
|
end
|
80
|
+
end
|
76
81
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
82
|
+
# Does a count on the query
|
83
|
+
#
|
84
|
+
# example:
|
85
|
+
# User.count(:age => 20)
|
86
|
+
#
|
87
|
+
# is the same as:
|
88
|
+
# User.where(:age => 20).count
|
89
|
+
#
|
90
|
+
# if the key :conditions is present it will fall back to legacy
|
91
|
+
#
|
92
|
+
# @param [Hash]
|
93
|
+
# Optional follows same convention as {#all}
|
94
|
+
# @return [Integer]
|
95
|
+
def count(opts = {})
|
96
|
+
if opts.empty?
|
97
|
+
super()
|
98
|
+
elsif opts.key?(:conditions)
|
99
|
+
super(opts)
|
100
|
+
else
|
101
|
+
all(opts).count
|
96
102
|
end
|
97
103
|
end
|
98
104
|
|
99
105
|
end
|
100
106
|
|
101
|
-
ActiveRecord::
|
102
|
-
class << self
|
103
|
-
remove_method :all
|
104
|
-
remove_method :first
|
105
|
-
remove_method :count
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
ActiveRecord::Base.send(:extend, BetterAr::ClassMethods)
|
107
|
+
ActiveRecord::Relation.send(:include, BetterAr)
|
data/lib/better_ar/version.rb
CHANGED
data/test/helper.rb
CHANGED
data/test/test_finder_methods.rb
CHANGED
@@ -6,12 +6,6 @@ describe 'Enhanced Finder Methods' do
|
|
6
6
|
end
|
7
7
|
|
8
8
|
describe '.all' do
|
9
|
-
it 'should return all records when no params' do
|
10
|
-
User.all.to_sql.must_be_like %{
|
11
|
-
SELECT "users".* FROM "users"
|
12
|
-
}
|
13
|
-
end
|
14
|
-
|
15
9
|
it 'extracts the non-where scopes and applies' do
|
16
10
|
test_sql = User.all(:limit! => 1, :offset! => 2, :order! => :name, :age => 10).to_sql
|
17
11
|
expected_sql = User.where(:age => 10).limit(1).offset(2).order(:name).to_sql
|
@@ -44,19 +38,35 @@ describe 'Enhanced Finder Methods' do
|
|
44
38
|
describe 'hash contains :conditions' do
|
45
39
|
before do
|
46
40
|
@expected = User.create(:name => 'test')
|
47
|
-
User.create(:name => 'no test')
|
41
|
+
@user = User.create(:name => 'no test')
|
48
42
|
end
|
49
43
|
|
50
|
-
|
51
|
-
|
52
|
-
|
44
|
+
describe 'with conditions' do
|
45
|
+
it 'falls back for .all' do
|
46
|
+
User.all(:conditions => "name = 'test'").must_equal [@expected]
|
47
|
+
end
|
53
48
|
|
54
|
-
|
55
|
-
|
49
|
+
it 'falls back for .first' do
|
50
|
+
User.first(:conditions => "name = 'test'").must_equal @expected
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'falls back for .count' do
|
54
|
+
User.count(:conditions => "name = 'test'").must_equal 1
|
55
|
+
end
|
56
56
|
end
|
57
57
|
|
58
|
-
|
59
|
-
|
58
|
+
describe 'without conditions' do
|
59
|
+
it 'falls back for .all' do
|
60
|
+
User.all.must_equal [@expected, @user]
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'falls back for .first' do
|
64
|
+
User.first.must_equal @expected
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'falls back for .count' do
|
68
|
+
User.count.must_equal 2
|
69
|
+
end
|
60
70
|
end
|
61
71
|
end
|
62
72
|
|
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
|
+
- 6
|
9
|
+
version: 0.0.6
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Brian Cardarella
|
@@ -23,7 +23,7 @@ dependencies:
|
|
23
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
24
24
|
none: false
|
25
25
|
requirements:
|
26
|
-
- -
|
26
|
+
- - ~>
|
27
27
|
- !ruby/object:Gem::Version
|
28
28
|
segments:
|
29
29
|
- 3
|
@@ -32,32 +32,6 @@ dependencies:
|
|
32
32
|
version: 3.0.3
|
33
33
|
type: :development
|
34
34
|
version_requirements: *id001
|
35
|
-
- !ruby/object:Gem::Dependency
|
36
|
-
name: mocha
|
37
|
-
prerelease: false
|
38
|
-
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
-
none: false
|
40
|
-
requirements:
|
41
|
-
- - ">="
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
segments:
|
44
|
-
- 0
|
45
|
-
version: "0"
|
46
|
-
type: :development
|
47
|
-
version_requirements: *id002
|
48
|
-
- !ruby/object:Gem::Dependency
|
49
|
-
name: yard
|
50
|
-
prerelease: false
|
51
|
-
requirement: &id003 !ruby/object:Gem::Requirement
|
52
|
-
none: false
|
53
|
-
requirements:
|
54
|
-
- - ">="
|
55
|
-
- !ruby/object:Gem::Version
|
56
|
-
segments:
|
57
|
-
- 0
|
58
|
-
version: "0"
|
59
|
-
type: :development
|
60
|
-
version_requirements: *id003
|
61
35
|
description: Better Active Record finders
|
62
36
|
email:
|
63
37
|
- bcardarella@gmail.com
|