better_ar 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,2 +1,5 @@
1
+ ## 0.0.4
2
+ SQL operator keys must now end with '!'
3
+
1
4
  ## 0.0.3
2
5
  Supports legacy calls to .all, .first, .count if the :conditions key is present
@@ -1,10 +1,23 @@
1
1
  # BetterAr
2
2
 
3
- BetterAr adds replaces the .all, .first and .count methods for ActiveRecord.
3
+ BetterAr replaces the .all, .first and .count methods for ActiveRecord.
4
+
5
+
6
+ ## Install
7
+
8
+ Bundler:
9
+
10
+ gem 'better_ar'
11
+
12
+ Command line:
13
+
14
+ gem install better_ar
15
+
16
+ ## Usage
4
17
 
5
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'
6
19
 
7
- User.all(:age => 10, :limit => 5, :offset => 10, :order => :name)
20
+ User.all(:age => 10, :limit! => 5, :offset! => 10, :order! => :name)
8
21
 
9
22
  is the same as
10
23
 
@@ -12,7 +25,15 @@ is the same as
12
25
 
13
26
  While this may seem less concise the advantage is being able to dynamically construct the query with a single hash in code.
14
27
 
15
- This library completely removes the old .all, .first and .count class methods on ActiveRecord::Base. As these methods were just delegated to the ActiveRecord::Base.scoped they can still be called.
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
+ ### Legacy
31
+
32
+ If the params contain :condition it will fall back to the legacy method.
33
+
34
+ ## Other
35
+
36
+ Brian Cardarella (c) 2011
16
37
 
17
- Brian Cardarella - 2011
38
+ License: MIT (see MIT-LICENSE.txt)
18
39
 
@@ -3,12 +3,14 @@ module BetterAr
3
3
 
4
4
  # Breaks down the hash to do a ActiveRecord::Relation query
5
5
  #
6
- # example: User.all(:age => 10, :limit => 2, :offset => 3, :order => :name)
6
+ # example: User.all(:age => 10, :limit! => 2, :offset! => 3, :order! => :name)
7
7
  #
8
8
  # is the same as: User.where(:age => 10).limit(2).offset(3).order(:name)
9
9
  #
10
10
  # if the key :conditions is present it will fall back to legacy
11
11
  #
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.
13
+ #
12
14
  # @param [Hash]
13
15
  # Optional
14
16
  # @return [ActiveRecord::Relation]
@@ -17,9 +19,9 @@ module BetterAr
17
19
 
18
20
  unless opts.key?(:conditions)
19
21
  unless opts.empty?
20
- [:order, :limit, :offset].each do |predicate|
22
+ opts.keys.select { |key| key.to_s =~ /!$/ }.each do |predicate|
21
23
  if value = opts.delete(predicate)
22
- relation = relation.send(predicate, value)
24
+ relation = relation.send(predicate.to_s.sub('!',''), value)
23
25
  end
24
26
  end
25
27
  relation.where(opts)
@@ -45,11 +47,11 @@ module BetterAr
45
47
  # if the key :conditions is present it will fall back to legacy
46
48
  #
47
49
  # @param [Hash]
48
- # Optional
50
+ # Optional follows same convention as .all
49
51
  # @return [ActiveRecord::Base]
50
52
  def first(opts = {})
51
53
  unless opts.key?(:conditions)
52
- all(opts.merge(:limit => 1)).first
54
+ all(opts.merge(:limit! => 1)).first
53
55
  else
54
56
  scoped.first(opts)
55
57
  end
@@ -64,7 +66,7 @@ module BetterAr
64
66
  # if the key :conditions is present it will fall back to legacy
65
67
  #
66
68
  # @param [Hash]
67
- # Optional
69
+ # Optional follows same convention as .all
68
70
  # @return [Integer]
69
71
  def count(opts = {})
70
72
  unless opts.key?(:conditions)
@@ -1,3 +1,3 @@
1
1
  module BetterAr
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -13,7 +13,7 @@ describe 'Enhanced Finder Methods' do
13
13
  end
14
14
 
15
15
  it 'extracts the non-where scopes and applies' do
16
- test_sql = User.all(:limit => 1, :offset => 2, :order => :name, :age => 10).to_sql
16
+ test_sql = User.all(:limit! => 1, :offset! => 2, :order! => :name, :age => 10).to_sql
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
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 3
9
- version: 0.0.3
8
+ - 4
9
+ version: 0.0.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Brian Cardarella