better_ar 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY.txt CHANGED
@@ -1,3 +1,6 @@
1
+ ## 0.0.6
2
+ Moved methods as higher level module in ActiveRecord::Relation than ActiveRecord::FinderMethods. So the BetterAR modules take precedence.
3
+
1
4
  ## 0.0.5
2
5
  Implicit joins
3
6
 
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', ['>= 3.0.3']
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
- # Breaks down the hash to do a {ActiveRecord::Relation} query
5
- #
6
- # example:
7
- # User.all(:age => 10, :limit! => 2, :offset! => 3, :order! => :name)
8
- #
9
- # is the same as:
10
- # User.where(:age => 10).limit(2).offset(3).order(:name)
11
- #
12
- # if the key :conditions is present it will fall back to legacy
13
- #
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'})
22
- #
23
- # @param [Hash]
24
- # Optional
25
- # @return [ActiveRecord::Relation]
26
- def all(opts = {})
27
- relation = scoped.clone
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
- # Forces a limit of 1 on the collection
57
- #
58
- # example:
59
- # User.first(:age => 10, :name => 'Brian')
60
- #
61
- # is the same as:
62
- # User.where(:age => 10, :name => 'Brian').limit(1).first
63
- #
64
- # if the key :conditions is present it will fall back to legacy
65
- #
66
- # @param [Hash]
67
- # Optional follows same convention as {#all}
68
- # @return [ActiveRecord::Base]
69
- def first(opts = {})
70
- unless opts.key?(:conditions)
71
- all(opts.merge(:limit! => 1)).first
72
- else
73
- scoped.first(opts)
74
- end
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
- # Does a count on the query
78
- #
79
- # example:
80
- # User.count(:age => 20)
81
- #
82
- # is the same as:
83
- # User.where(:age => 20).count
84
- #
85
- # if the key :conditions is present it will fall back to legacy
86
- #
87
- # @param [Hash]
88
- # Optional follows same convention as {#all}
89
- # @return [Integer]
90
- def count(opts = {})
91
- unless opts.key?(:conditions)
92
- all(opts).count
93
- else
94
- scoped.count(opts)
95
- end
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::Base.class_eval do
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)
@@ -1,3 +1,3 @@
1
1
  module BetterAr
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
data/test/helper.rb CHANGED
@@ -1,7 +1,8 @@
1
1
  require 'rubygems'
2
2
  require 'ruby-debug'
3
3
  require 'minitest/autorun'
4
- require 'mocha'
4
+ # require 'mocha'
5
+ require 'active_support/core_ext'
5
6
  require 'active_record'
6
7
  require 'better_ar'
7
8
 
@@ -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
- it 'falls back for .all' do
51
- User.all(:conditions => "name = 'test'").must_equal [@expected]
52
- end
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
- it 'falls back for .first' do
55
- User.first(:conditions => "name = 'test'").must_equal @expected
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
- it 'falls back for .count' do
59
- User.count(:conditions => "name = 'test'").must_equal 1
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
- - 5
9
- version: 0.0.5
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