better_ar 0.0.3

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/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ doc/*
6
+ .yardoc/*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm --create default@better_ar > /dev/null
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in better_ar.gemspec
4
+ gemspec
data/HISTORY.txt ADDED
@@ -0,0 +1,2 @@
1
+ ## 0.0.3
2
+ Supports legacy calls to .all, .first, .count if the :conditions key is present
data/MIT-LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2011 Brian Cardarella
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
data/README.markdown ADDED
@@ -0,0 +1,18 @@
1
+ # BetterAr
2
+
3
+ BetterAr adds replaces the .all, .first and .count methods for ActiveRecord.
4
+
5
+ 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
+
7
+ User.all(:age => 10, :limit => 5, :offset => 10, :order => :name)
8
+
9
+ is the same as
10
+
11
+ User.where(:age => 10).limit(5).offset(10).order(:name)
12
+
13
+ While this may seem less concise the advantage is being able to dynamically construct the query with a single hash in code.
14
+
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.
16
+
17
+ Brian Cardarella - 2011
18
+
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'rake'
2
+ require 'bundler'
3
+ Bundler::GemHelper.install_tasks
4
+
5
+ require 'rake/testtask'
6
+ Rake::TestTask.new(:test) do |test|
7
+ test.libs << 'lib' << 'test'
8
+ test.pattern = 'test/**/test_*.rb'
9
+ test.verbose = true
10
+ end
11
+
12
+ task :default => :test
data/better_ar.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "better_ar/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "better_ar"
7
+ s.version = BetterAr::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Brian Cardarella"]
10
+ s.email = ["bcardarella@gmail.com"]
11
+ s.homepage = "https://github.com/bcardarella/better_ar"
12
+ s.summary = %q{Better Active Record finders}
13
+ s.description = %q{Better Active Record finders}
14
+
15
+ s.rubyforge_project = "better_ar"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_development_dependency 'activerecord', ['>= 3.0.3']
23
+ s.add_development_dependency 'mocha'
24
+ s.add_development_dependency 'yard'
25
+ end
data/lib/better_ar.rb ADDED
@@ -0,0 +1,88 @@
1
+ module BetterAr
2
+ module ClassMethods
3
+
4
+ # Breaks down the hash to do a ActiveRecord::Relation query
5
+ #
6
+ # example: User.all(:age => 10, :limit => 2, :offset => 3, :order => :name)
7
+ #
8
+ # is the same as: User.where(:age => 10).limit(2).offset(3).order(:name)
9
+ #
10
+ # if the key :conditions is present it will fall back to legacy
11
+ #
12
+ # @param [Hash]
13
+ # Optional
14
+ # @return [ActiveRecord::Relation]
15
+ def all(opts = {})
16
+ relation = scoped.clone
17
+
18
+ unless opts.key?(:conditions)
19
+ unless opts.empty?
20
+ [:order, :limit, :offset].each do |predicate|
21
+ if value = opts.delete(predicate)
22
+ relation = relation.send(predicate, value)
23
+ end
24
+ end
25
+ relation.where(opts)
26
+ end
27
+
28
+ if opts.empty?
29
+ relation
30
+ else
31
+ relation.where(opts)
32
+ end
33
+ else
34
+ relation.all(opts)
35
+ end
36
+
37
+ end
38
+
39
+ # Forces a limit of 1 on the collection
40
+ #
41
+ # example: User.first(:age => 10, :name => 'Brian')
42
+ #
43
+ # is the same as: User.where(:age => 10, :name => 'Brian').limit(1).first
44
+ #
45
+ # if the key :conditions is present it will fall back to legacy
46
+ #
47
+ # @param [Hash]
48
+ # Optional
49
+ # @return [ActiveRecord::Base]
50
+ def first(opts = {})
51
+ unless opts.key?(:conditions)
52
+ all(opts.merge(:limit => 1)).first
53
+ else
54
+ scoped.first(opts)
55
+ end
56
+ end
57
+
58
+ # Does a count on the query
59
+ #
60
+ # example: User.count(:age => 20)
61
+ #
62
+ # is the same as: User.where(:age => 20).count
63
+ #
64
+ # if the key :conditions is present it will fall back to legacy
65
+ #
66
+ # @param [Hash]
67
+ # Optional
68
+ # @return [Integer]
69
+ def count(opts = {})
70
+ unless opts.key?(:conditions)
71
+ all(opts).count
72
+ else
73
+ scoped.count(opts)
74
+ end
75
+ end
76
+ end
77
+
78
+ end
79
+
80
+ ActiveRecord::Base.class_eval do
81
+ class << self
82
+ remove_method :all
83
+ remove_method :first
84
+ remove_method :count
85
+ end
86
+ end
87
+
88
+ ActiveRecord::Base.send(:extend, BetterAr::ClassMethods)
@@ -0,0 +1,3 @@
1
+ module BetterAr
2
+ VERSION = "0.0.3"
3
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,23 @@
1
+ require 'rubygems'
2
+ require 'ruby-debug'
3
+ require 'minitest/autorun'
4
+ require 'mocha'
5
+ require 'active_record'
6
+ require 'better_ar'
7
+
8
+ class Object
9
+ def must_be_like other
10
+ self.gsub(/\s+/, ' ').strip.must_equal other.gsub(/\s+/, ' ').strip
11
+ end
12
+ end
13
+
14
+ ActiveRecord::Base.establish_connection(
15
+ :adapter => defined?(JRUBY_VERSION) ? 'jdbcsqlite3' : 'sqlite3',
16
+ :database => ':memory:'
17
+ )
18
+
19
+ users_table = %{CREATE TABLE users (id INTEGER PRIMARY KEY, age INTEGER, name TEXT);}
20
+ ActiveRecord::Base.connection.execute(users_table)
21
+
22
+ class User < ActiveRecord::Base; end
23
+
@@ -0,0 +1,58 @@
1
+ require 'helper'
2
+
3
+ describe 'Enhanced Finder Methods' do
4
+ after do
5
+ User.destroy_all
6
+ end
7
+
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
+ it 'extracts the non-where scopes and applies' do
16
+ test_sql = User.all(:limit => 1, :offset => 2, :order => :name, :age => 10).to_sql
17
+ expected_sql = User.where(:age => 10).limit(1).offset(2).order(:name).to_sql
18
+ test_sql.must_be_like expected_sql
19
+ end
20
+ end
21
+
22
+ describe '.first' do
23
+ it 'calls #first on .all' do
24
+ expected = User.create(:age => 10)
25
+ User.create(:age => 11)
26
+ User.first(:age => 10).must_equal expected
27
+ end
28
+ end
29
+
30
+ describe '.count' do
31
+ it 'calls #count on .all' do
32
+ User.create(:age => 10)
33
+ User.create(:age => 11)
34
+ User.count(:age => 10).must_equal 1
35
+ end
36
+ end
37
+
38
+ describe 'hash contains :conditions' do
39
+ before do
40
+ @expected = User.create(:name => 'test')
41
+ User.create(:name => 'no test')
42
+ end
43
+
44
+ it 'falls back for .all' do
45
+ User.all(:conditions => "name = 'test'").must_equal [@expected]
46
+ end
47
+
48
+ it 'falls back for .first' do
49
+ User.first(:conditions => "name = 'test'").must_equal @expected
50
+ end
51
+
52
+ it 'falls back for .count' do
53
+ User.count(:conditions => "name = 'test'").must_equal 1
54
+ end
55
+ end
56
+
57
+ end
58
+
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: better_ar
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 3
9
+ version: 0.0.3
10
+ platform: ruby
11
+ authors:
12
+ - Brian Cardarella
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-27 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activerecord
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 3
30
+ - 0
31
+ - 3
32
+ version: 3.0.3
33
+ type: :development
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
+ description: Better Active Record finders
62
+ email:
63
+ - bcardarella@gmail.com
64
+ executables: []
65
+
66
+ extensions: []
67
+
68
+ extra_rdoc_files: []
69
+
70
+ files:
71
+ - .gitignore
72
+ - .rvmrc
73
+ - Gemfile
74
+ - HISTORY.txt
75
+ - MIT-LICENSE.txt
76
+ - README.markdown
77
+ - Rakefile
78
+ - better_ar.gemspec
79
+ - lib/better_ar.rb
80
+ - lib/better_ar/version.rb
81
+ - test/helper.rb
82
+ - test/test_finder_methods.rb
83
+ has_rdoc: true
84
+ homepage: https://github.com/bcardarella/better_ar
85
+ licenses: []
86
+
87
+ post_install_message:
88
+ rdoc_options: []
89
+
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ segments:
98
+ - 0
99
+ version: "0"
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ segments:
106
+ - 0
107
+ version: "0"
108
+ requirements: []
109
+
110
+ rubyforge_project: better_ar
111
+ rubygems_version: 1.3.7
112
+ signing_key:
113
+ specification_version: 3
114
+ summary: Better Active Record finders
115
+ test_files:
116
+ - test/helper.rb
117
+ - test/test_finder_methods.rb