Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

remove adapters from main repo

  • Loading branch information...
commit 5e94044e4647d94f9846791290e88cc37b175125 1 parent 828a22d
@defunkt authored
Showing with 0 additions and 1,829 deletions.
  1. +0 −8 adapters/ambitious_activeldap/Manifest
  2. +0 −31 adapters/ambitious_activeldap/Rakefile
  3. +0 −8 adapters/ambitious_activeldap/lib/ambition/adapters/active_ldap.rb
  4. +0 −15 adapters/ambitious_activeldap/lib/ambition/adapters/active_ldap/base.rb
  5. +0 −27 adapters/ambitious_activeldap/lib/ambition/adapters/active_ldap/query.rb
  6. +0 −65 adapters/ambitious_activeldap/lib/ambition/adapters/active_ldap/select.rb
  7. +0 −95 adapters/ambitious_activeldap/test/enumerable_test.rb
  8. +0 −17 adapters/ambitious_activeldap/test/helper.rb
  9. +0 −7 adapters/ambitious_activeldap/test/integration_test.rb
  10. +0 −83 adapters/ambitious_activeldap/test/select_test.rb
  11. +0 −18 adapters/ambitious_activerecord/LICENSE
  12. +0 −21 adapters/ambitious_activerecord/Manifest
  13. +0 −234 adapters/ambitious_activerecord/README
  14. +0 −39 adapters/ambitious_activerecord/Rakefile
  15. +0 −11 adapters/ambitious_activerecord/lib/ambition/adapters/active_record.rb
  16. +0 −57 adapters/ambitious_activerecord/lib/ambition/adapters/active_record/base.rb
  17. +0 −57 adapters/ambitious_activerecord/lib/ambition/adapters/active_record/query.rb
  18. +0 −99 adapters/ambitious_activerecord/lib/ambition/adapters/active_record/select.rb
  19. +0 −19 adapters/ambitious_activerecord/lib/ambition/adapters/active_record/slice.rb
  20. +0 −43 adapters/ambitious_activerecord/lib/ambition/adapters/active_record/sort.rb
  21. +0 −39 adapters/ambitious_activerecord/lib/ambition/adapters/active_record/statements.rb
  22. +0 −69 adapters/ambitious_activerecord/test/benchmark.rb
  23. +0 −36 adapters/ambitious_activerecord/test/chaining_test.rb
  24. +0 −19 adapters/ambitious_activerecord/test/count_test.rb
  25. +0 −88 adapters/ambitious_activerecord/test/enumerable_test.rb
  26. +0 −47 adapters/ambitious_activerecord/test/helper.rb
  27. +0 −62 adapters/ambitious_activerecord/test/join_test.rb
  28. +0 −36 adapters/ambitious_activerecord/test/profiler.rb
  29. +0 −11 adapters/ambitious_activerecord/test/ruby_test.rb
  30. +0 −259 adapters/ambitious_activerecord/test/select_test.rb
  31. +0 −43 adapters/ambitious_activerecord/test/slice_test.rb
  32. +0 −54 adapters/ambitious_activerecord/test/sort_test.rb
  33. +0 −43 adapters/ambitious_activerecord/test/source_test.rb
  34. +0 −69 adapters/ambitious_activerecord/test/types_test.rb
View
8 adapters/ambitious_activeldap/Manifest
@@ -1,8 +0,0 @@
-lib/ambition/adapters/active_ldap/base.rb
-lib/ambition/adapters/active_ldap/query.rb
-lib/ambition/adapters/active_ldap/select.rb
-test/enumerable_test.rb
-test/helper.rb
-test/integration_test.rb
-test/select_test.rb
-Manifest
View
31 adapters/ambitious_activeldap/Rakefile
@@ -1,31 +0,0 @@
-require 'rake'
-
-Version = '0.1.0'
-
-begin
- require 'rubygems'
- gem 'echoe', '>=2.7'
- ENV['RUBY_FLAGS'] = ""
- require 'echoe'
-
- Echoe.new('ambitious-activeldap') do |p|
- p.dependencies << 'ruby-activeldap >=0.8.3.1'
- p.summary = "An ambitious adapter for ActiveLDAP"
- p.author = 'Chris Wanstrath'
- p.email = "chris@ozmm.org"
-
- p.project = 'ambition'
- p.url = "http://ambition.rubyforge.org/"
- p.test_pattern = 'test/*_test.rb'
- p.version = Version
- p.dependencies << 'ambition >=0.5.0'
- end
-
-rescue LoadError
- puts "Not doing any of the Echoe gemmy stuff, because you don't have the specified gem versions"
-end
-
-desc 'Install as a gem'
-task :install_gem do
- puts `rake manifest package && gem install pkg/ambitious-activeldap-#{Version}.gem`
-end
View
8 adapters/ambitious_activeldap/lib/ambition/adapters/active_ldap.rb
@@ -1,8 +0,0 @@
-require 'ambition'
-require 'active_ldap'
-require 'ambition/adapters/active_ldap/query'
-require 'ambition/adapters/active_ldap/base'
-require 'ambition/adapters/active_ldap/select'
-
-ActiveLdap::Base.extend Ambition::API
-ActiveLdap::Base.ambition_adapter = Ambition::Adapters::ActiveLdap
View
15 adapters/ambitious_activeldap/lib/ambition/adapters/active_ldap/base.rb
@@ -1,15 +0,0 @@
-module Ambition
- module Adapters
- module ActiveLdap
- class Base
- def sanitize(object)
- case object
- when true then 'TRUE'
- when false then 'FALSE'
- else object.to_s
- end
- end
- end
- end
- end
-end
View
27 adapters/ambitious_activeldap/lib/ambition/adapters/active_ldap/query.rb
@@ -1,27 +0,0 @@
-module Ambition
- module Adapters
- module ActiveLdap
- class Query
-
- def kick
- owner.find(:all, to_hash)
- end
-
- def size
- raise "Not Implemented"
- end
-
- def to_hash
- hash = {}
- hash[:filter] = to_s unless to_s.empty?
- hash
- end
-
- def to_s
- Array(clauses[:select]).join
- clauses[:select].first.to_s
- end
- end
- end
- end
-end
View
65 adapters/ambitious_activeldap/lib/ambition/adapters/active_ldap/select.rb
@@ -1,65 +0,0 @@
-module Ambition
- module Adapters
- module ActiveLdap
- class Select < Base
- def call(*methods)
- method = methods.first.to_s
- if method[-1] == ??
- "(#{method[0...-1]}=#{sanitize true})"
- else
- method
- end
- end
-
- def chained_call(*methods)
- call(*methods)
- end
-
- def both(left, right)
- "(&#{left}#{sanitize right})"
- end
-
- def either(left, right)
- "(|#{left}#{sanitize right})"
- end
-
- def ==(left, right)
- "(#{left}=#{sanitize right})"
- end
-
- # !=
- def not_equal(left, right)
- "(!(#{left}=#{sanitize right}))"
- end
-
- def =~(left, right)
- end
-
- # !~
- def not_regexp(left, right)
- end
-
- def <(left, right)
- self.<=(left, right)
- end
-
- def >(left, right)
- self.>=(left, right)
- end
-
- def >=(left, right)
- "(#{left}>=#{sanitize right})"
- end
-
- def <=(left, right)
- "(#{left}<=#{sanitize right})"
- end
-
- def include?(left, right)
- bits = left.map { |item| "(#{right}=#{item})" }
- "(|#{bits})"
- end
- end
- end
- end
-end
View
95 adapters/ambitious_activeldap/test/enumerable_test.rb
@@ -1,95 +0,0 @@
-require File.dirname(__FILE__) + '/helper'
-
-context "Each" do
- specify "simple ==" do
- hash = { :filter => "(uid=mking)" }
- LDAPUser.expects(:find).with(:all, hash).returns([])
- LDAPUser.select { |m| m.uid == 'mking' }.each do |user|
- puts user.cn
- end
- end
-
- xspecify "limit and conditions" do
- hash = { :limit => 5, :filter => "(uid=mking)" }
- LDAPUser.expects(:find).with(:all, hash).returns([])
- LDAPUser.select { |m| m.uid == 'mking' }.first(5).each do |user|
- puts user.cn
- end
- end
-
- xspecify "limit and conditions and order" do
- hash = { :limit => 5, :filter => "(uid=mking)", :sort_by => 'sn' }
- LDAPUser.expects(:find).with(:all, hash).returns([])
- LDAPUser.select { |m| m.uid == 'mking' }.sort_by { |m| m.sn }.first(5).each do |user|
- puts user.cn
- end
- end
-
- xspecify "limit and order" do
- hash = { :limit => 5, :sort_by => 'sn' }
- LDAPUser.expects(:find).with(:all, hash).returns([])
- LDAPUser.sort_by { |m| m.sn }.first(5).each do |user|
- puts user.name
- end
- end
-end
-
-context "Enumerable Methods" do
- specify "map" do
- hash = { :filter => "(uid=mking)" }
- LDAPUser.expects(:find).with(:all, hash).returns([])
- LDAPUser.select { |m| m.uid == 'mking' }.map { |u| u.name }
- end
-
- specify "each_with_index" do
- hash = { :filter => "(uid=mking)" }
- LDAPUser.expects(:find).with(:all, hash).returns([])
- LDAPUser.select { |m| m.uid == 'mking' }.each_with_index do |user, i|
- puts "#{i}: #{user.name}"
- end
- end
-
- # specify "any?" do
- # LDAPUser.expects(:count).with(:conditions => "users.age > 21").returns(1)
- # LDAPUser.any? { |u| u.age > 21 }.should == true
- # end
-
-# specify "all?" do
-# LDAPUser.expects(:count).at_least_once.returns(10, 20)
-# LDAPUser.all? { |u| u.age > 21 }.should == false
-#
-# LDAPUser.expects(:count).at_least_once.returns(10, 10)
-# LDAPUser.all? { |u| u.age > 21 }.should == true
-# end
-#
-# specify "empty?" do
-# LDAPUser.expects(:count).with(:conditions => "users.age > 21").returns(1)
-# LDAPUser.select { |u| u.age > 21 }.empty?.should.equal false
-#
-# LDAPUser.expects(:count).with(:conditions => "users.age > 21").returns(0)
-# LDAPUser.select { |u| u.age > 21 }.empty?.should.equal true
-# end
-#
- specify "entries" do
- LDAPUser.expects(:find).with(:all, {})
- LDAPUser.entries
-
- hash = { :filter => "(uid=mking)" }
- LDAPUser.expects(:find).with(:all, hash).returns([])
- LDAPUser.select { |m| m.uid == 'mking' }.entries
- end
-
- specify "to_a" do
- LDAPUser.expects(:find).with(:all, {})
- LDAPUser.to_a
- end
-#
-# xspecify "each_slice" do
-# end
-#
-# xspecify "max" do
-# end
-#
-# xspecify "min" do
-# end
-end
View
17 adapters/ambitious_activeldap/test/helper.rb
@@ -1,17 +0,0 @@
-%w( rubygems test/spec mocha redgreen English ).each { |f| require f }
-
-$LOAD_PATH.unshift *[ File.dirname(__FILE__) + '/../lib', File.dirname(__FILE__) + '/../../../lib' ]
-require 'ambition/adapters/active_ldap'
-
-ActiveLdap::Base.class_eval do
- def self.find(*args)
- 'dummy find method'
- end
-end
-
-class LDAPUser < ActiveLdap::Base
- def self.table_name
- # in real life would call the base class method on the ActiveLdap model class
- 'ou=people,dc=automatthew,dc=com'
- end
-end
View
7 adapters/ambitious_activeldap/test/integration_test.rb
@@ -1,7 +0,0 @@
-require File.dirname(__FILE__) + '/helper'
-
-context "LDAP model after including LDAP Adapter" do
- specify "should still have original find method" do
- LDAPUser.find(:all).should == "dummy find method"
- end
-end
View
83 adapters/ambitious_activeldap/test/select_test.rb
@@ -1,83 +0,0 @@
-require File.dirname(__FILE__) + '/helper'
-
-context "LDAP Adapter :: Select" do
- context "Filter (using select)" do
- specify "simple ==" do
- filter = LDAPUser.select { |m| m.name == 'jon' }.to_s
- filter.should == "(name=jon)"
- end
-
- specify "simple !=" do
- filter = LDAPUser.select { |m| m.name != 'jon' }.to_s
- filter.should == "(!(name=jon))"
- end
-
- specify "simple == && ==" do
- filter = LDAPUser.select { |m| m.name == 'jon' && m.age == 21 }.to_s
- filter.should == "(&(name=jon)(age=21))"
- end
-
- specify "simple == || ==" do
- filter = LDAPUser.select { |m| m.name == 'jon' || m.age == 21 }.to_s
- filter.should == "(|(name=jon)(age=21))"
- end
-
- specify "mixed && and ||" do
- filter = LDAPUser.select { |m| m.name == 'jon' || m.age == 21 && m.password == 'pass' }.to_s
- filter.should == "(|(name=jon)(&(age=21)(password=pass)))"
- end
-
- specify "grouped && and ||" do
- filter = LDAPUser.select { |m| (m.name == 'jon' || m.name == 'rick') && m.age == 21 }.to_s
- filter.should == "(&(|(name=jon)(name=rick))(age=21))"
- end
-
- specify "simple >/<" do
- # LDAP apparently only supports >= and <=
- filter = LDAPUser.select { |m| m.age > 21 }.to_s
- filter.should == "(age>=21)"
-
- filter = LDAPUser.select { |m| m.age >= 21 }.to_s
- filter.should == "(age>=21)"
-
- filter = LDAPUser.select { |m| m.age < 21 }.to_s
- filter.should == "(age<=21)"
-
- filter = LDAPUser.select { |m| m.age <= 21 }.to_s
- filter.should == "(age<=21)"
- end
-
- specify "array.include? item" do
- filter = LDAPUser.select { |m| [1, 2, 3, 4].include? m.id }.to_s
- # I'm not sure whether this is idiomatic, but it works.
- filter.should == "(|(id=1)(id=2)(id=3)(id=4))"
- end
-
- specify "variable'd array.include? item" do
- array = [1, 2, 3, 4]
- filter = LDAPUser.select { |m| array.include? m.id }.to_s
- filter.should == "(|(id=1)(id=2)(id=3)(id=4))"
- end
-
- specify "simple == with variables" do
- me = 'chris'
- filter = LDAPUser.select { |m| m.name == me }.to_s
- filter.should == "(name=#{me})"
- end
-
- specify "simple == with true" do
- filter = LDAPUser.select { |m| m.disabled == true }.to_s
- filter.should == "(disabled=TRUE)"
- end
-
- xspecify "implicit true" do
- filter = LDAPUser.select { |m| m.disabled }.to_s
- filter.should == "(disabled=TRUE)"
- end
-
- specify "predicate method" do
- filter = LDAPUser.select { |m| m.disabled? }.to_s
- filter.should == "(disabled=TRUE)"
- end
- end
-end
View
18 adapters/ambitious_activerecord/LICENSE
@@ -1,18 +0,0 @@
-Copyright (c) 2007 Chris Wanstrath
-
-Permission is hereby granted, free of charge, to any person obtaining a copy of
-this software and associated documentation files (the "Software"), to deal in
-the Software without restriction, including without limitation the rights to
-use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
-the Software, and to permit persons to whom the Software is furnished to do so,
-subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
-FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
-COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
-IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
-CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
21 adapters/ambitious_activerecord/Manifest
@@ -1,21 +0,0 @@
-lib/ambition/adapters/active_record/base.rb
-lib/ambition/adapters/active_record/query.rb
-lib/ambition/adapters/active_record/select.rb
-lib/ambition/adapters/active_record/slice.rb
-lib/ambition/adapters/active_record/sort.rb
-lib/ambition/adapters/active_record/statements.rb
-lib/ambition/adapters/active_record.rb
-test/benchmark.rb
-test/chaining_test.rb
-test/count_test.rb
-test/enumerable_test.rb
-test/helper.rb
-test/join_test.rb
-test/profiler.rb
-test/ruby_test.rb
-test/select_test.rb
-test/slice_test.rb
-test/sort_test.rb
-test/source_test.rb
-test/types_test.rb
-Manifest
View
234 adapters/ambitious_activerecord/README
@@ -1,234 +0,0 @@
-h2. An Ambitious ActiveRecord Adapter
-
-I could tell you all about how awesome the internals are, or
-how fun it was to write, or how it'll make you rich and famous,
-but instead I'm just going to show you some examples.
-
-h2. Get It
-
-@$ sudo gem install ambitious-activerecord@
-
-This will suck in the adapter and its dependencies (ActiveRecord & Ambition).
-It's fully usable outside of Rails (I use it in a Camping app or two), as long
-as you're riding ActiveRecord.
-
-Now require it in your app:
-
-<pre>
-require 'rubygems'
-require 'ambition/adapters/activerecord'
-</pre>
-
-h2. Examples
-
-Basically, you write your SQL in Ruby. No, not in Ruby. As Ruby.
-
-<ruby>
-User.select { |u| u.city == 'San Francisco' }.each do |user|
- puts user.name
-end
-</ruby>
-
-And that's it.
-
-The key is that queries aren't actually run until the data they represent is
-requested. Usually this is done with what I call a kicker method. You can call them
-that, too.
-
-Kicker methods are guys like @detect@, @each@, @each_with_index@, @map@, @entries@,
-@to_a@, and @first@ (with no argument). Methods like @select@, @sort_by@, and @first@
-(with an argument) are not kicker methods and return a @Context@ object without running any SQL.
-
-Our @Context@ object has two useful methods: @to_s@ and @to_hash@. With these,
-we can check out what exactly we're building. Not everyone has @to_s@,
-though. Mostly ignore these methods and treat everything like you normally
-would.
-
-See, @to_s@:
-
-<ruby>
->> User.select { |m| m.name == 'jon' }.to_s
-=> "SELECT * FROM users WHERE users.name = 'jon'"
-</ruby>
-
-See, @to_hash@:
-
-<ruby>
->> User.select { |m| m.name == 'jon' }.to_hash
-=> { :conditions => "users.name = 'jon'" }
-</ruby>
-
-h2. Equality - select { |u| u.field == 'bob' }
-
-<ruby>
-User.select { |m| m.name == 'jon' }
-"SELECT * FROM users WHERE users.name = 'jon'"
-
-User.select { |m| m.created_at > 2.days.ago }
-"SELECT * FROM users WHERE users.created_at > '2007-09-26 20:37:47'"
-
-User.select { |m| m.name == 'jon' }
-"SELECT * FROM users WHERE users.name = 'jon'"
-
-User.select { |m| m.name != 'jon' }
-"SELECT * FROM users WHERE users.name <> 'jon'"
-
-User.select { |m| m.name == 'jon' && m.age == 21 }
-"SELECT * FROM users WHERE (users.name = 'jon' AND users.age = 21)"
-
-User.select { |m| m.name == 'jon' || m.age == 21 }
-"SELECT * FROM users WHERE (users.name = 'jon' OR users.age = 21)"
-
-User.select { |m| m.name == 'jon' || m.age == 21 && m.password == 'pass' }
-"SELECT * FROM users WHERE
- (users.name = 'jon' OR (users.age = 21 AND users.password = 'pass'))"
-
-User.select { |m| (m.name == 'jon' || m.name == 'rick') && m.age == 21 }
-"SELECT * FROM users WHERE
- ((users.name = 'jon' OR users.name = 'rick') AND users.age = 21)"
-</ruby>
-
-h2. Associations - select { |u| u.field == 'bob' && u.association.field == 'bob@bob.com' }
-
-The @to_s@ method doesn't work on associations yet, but that's okay: they can
-still query through ActiveRecord just fine.
-
-<ruby>
-User.select do |u|
- u.email == 'chris@ozmm.org' && u.profile.name == 'chris wanstrath'
-end.map(&:title)
-
-"SELECT users.id AS t0_r0, ... FROM users
- LEFT OUTER JOIN profiles ON profiles.user_id = users.id
- WHERE ((users.email = 'chris@ozmm.org' AND profiles.name = 'chris wanstrath'))"
-</ruby>
-
-h2. Comparisons - select { |u| u.age > 21 }
-
-<ruby>
-User.select { |m| m.age > 21 }
-"SELECT * FROM users WHERE users.age > 21"
-
-User.select { |m| m.age < 21 }.to_s
-"SELECT * FROM users WHERE users.age < 21"
-
-User.select { |m| [1, 2, 3, 4].include? m.id }
-"SELECT * FROM users WHERE users.id IN (1, 2, 3, 4)"
-</ruby>
-
-h2. LIKE and REGEXP (RLIKE) - select { |m| m.name =~ 'chris' }
-
-<ruby>
-User.select { |m| m.name =~ 'chris' }
-"SELECT * FROM users WHERE users.name LIKE 'chris'"
-
-User.select { |m| m.name =~ 'chri%' }
-"SELECT * FROM users WHERE users.name LIKE 'chri%'"
-
-User.select { |m| m.name !~ 'chris' }
-"SELECT * FROM users WHERE users.name NOT LIKE 'chris'"
-
-User.select { |m| !(m.name =~ 'chris') }
-"SELECT * FROM users WHERE users.name NOT LIKE 'chris'"
-
-User.select { |m| m.name =~ /chris/ }
-"SELECT * FROM users WHERE users.name REGEXP 'chris'"
-</ruby>
-
-h2. #detect
-
-<ruby>
-User.detect { |m| m.name == 'chris' }
-"SELECT * FROM users WHERE users.name = 'chris' LIMIT 1"
-</ruby>
-
-h2. LIMITs - first, first(x), [offset, limit], [range], slice
-
-<ruby>
-User.select { |m| m.name == 'jon' }.first
-"SELECT * FROM users WHERE users.name = 'jon' LIMIT 1"
-
-User.select { |m| m.name == 'jon' }.first(5)
-"SELECT * FROM users WHERE users.name = 'jon' LIMIT 5"
-
-User.select { |m| m.name == 'jon' }[10, 20]
-"SELECT * FROM users WHERE users.name = 'jon' LIMIT 10, 20"
-
-User.select { |m| m.name == 'jon' }[10..20]
-"SELECT * FROM users WHERE users.name = 'jon' LIMIT 10, 10"
-</ruby>
-
-h2. ORDER - sort_by { |u| u.field }
-
-<ruby>
-User.select { |m| m.name == 'jon' }.sort_by { |m| m.name }
-"SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.name"
-
-User.select { |m| m.name == 'jon' }.sort_by { |m| [ m.name, m.age ] }
-"SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.name, users.age"
-
-User.select { |m| m.name == 'jon' }.sort_by { |m| [ m.name, -m.age ] }
-"SELECT * FROM users WHERE users.name = 'jon'
- ORDER BY users.name, users.age DESC"
-
-User.select { |m| m.name == 'jon' }.sort_by { |m| [ -m.name, -m.age ] }
-"SELECT * FROM users WHERE users.name = 'jon'
- ORDER BY users.name DESC, users.age DESC"
-
-User.select { |m| m.name == 'jon' }.sort_by { |m| -m.age }
-"SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.age DESC"
-
-User.select { |m| m.name == 'jon' }.sort_by { |m| -m.profiles.title }
-"SELECT users.id AS t0_r0, ... FROM users
- LEFT OUTER JOIN profiles ON profiles.user_id = users.id
- WHERE (users.name = 'jon') ORDER BY profiles.title DESC"
-
-User.select { |m| m.name == 'jon' }.sort_by { rand }
-"SELECT * FROM users WHERE users.name = 'jon' ORDER BY RAND()"
-</ruby>
-
-h2. COUNT - select { |u| u.name == 'jon' }.size
-
-<ruby>
-User.select { |m| m.name == 'jon' }.size
-"SELECT count(*) AS count_all FROM users WHERE (users.name = 'jon')"
-
->> User.select { |m| m.name == 'jon' }.size
-=> 21
-</ruby>
-
-h2. Other Enumerables
-
-These methods perform COUNT() operations rather than loading your array into memory. They're all
-kickers.
-
-<ruby>
-User.any? { |m| m.name == 'jon' }
-User.all? { |m| m.name == 'jon' }
-User.select { |m| m.name == 'jon' }.empty?
-</ruby>
-
-h2. More Sugar
-
-The @downcase@ and @upcase@ methods will map to LOWER() and UPPER(), respectively.
-
-<ruby>
->> User.select { |m| m.name.downcase =~ 'jon%' }.to_s
-=> "SELECT * FROM users WHERE LOWER(users.name) LIKE 'jon%'"
-</ruby>
-
-h2. Quoting
-
-Columns and values will be quoted using ActiveRecord's quote_column_name and quote methods, if
-possible.
-
-h2. SELECT * FROM bugs
-
-Found a bug? Sweet. Add it at "the Lighthouse":http://err.lighthouseapp.com/projects/466-plugins/tickets/new.
-
-More information on Ambition:
-
- * "http://ambition.rubyforge.org":http://ambition.rubyforge.org
- * "http://groups.google.com/group/ambition-rb/":http://groups.google.com/group/ambition-rb/
-
-- Chris Wanstrath [ chris@ozmm.org ]
View
39 adapters/ambitious_activerecord/Rakefile
@@ -1,39 +0,0 @@
-require 'rake'
-
-Version = '0.1.2'
-
-begin
- require 'rubygems'
- gem 'echoe', '>=2.7'
- ENV['RUBY_FLAGS'] = ""
- require 'echoe'
-
- Echoe.new('ambitious-activerecord') do |p|
- p.dependencies << 'activerecord >=1.15.0'
- p.summary = "An ambitious adapter for ActiveRecord"
- p.author = 'Chris Wanstrath'
- p.email = "chris@ozmm.org"
-
- p.project = 'ambition'
- p.url = "http://ambition.rubyforge.org/"
- p.test_pattern = 'test/*_test.rb'
- p.version = Version
- p.dependencies << 'ambition >=0.5.1'
- end
-
-rescue LoadError
- puts "Not doing any of the Echoe gemmy stuff, because you don't have the specified gem versions"
-
- require 'rake/testtask'
- Rake::TestTask.new do |t|
- t.pattern = "test/*_test.rb"
- end
-end
-
-desc 'Install as a gem'
-task :install_gem do
- puts `rake manifest package && gem install pkg/ambitious-activerecord-#{Version}.gem`
-end
-
-task :default => :test
-
View
11 adapters/ambitious_activerecord/lib/ambition/adapters/active_record.rb
@@ -1,11 +0,0 @@
-require 'ambition'
-require 'active_record'
-require 'ambition/adapters/active_record/query'
-require 'ambition/adapters/active_record/base'
-require 'ambition/adapters/active_record/select'
-require 'ambition/adapters/active_record/sort'
-require 'ambition/adapters/active_record/slice'
-require 'ambition/adapters/active_record/statements'
-
-ActiveRecord::Base.extend Ambition::API
-ActiveRecord::Base.ambition_adapter = Ambition::Adapters::ActiveRecord
View
57 adapters/ambitious_activerecord/lib/ambition/adapters/active_record/base.rb
@@ -1,57 +0,0 @@
-require 'active_record/connection_adapters/abstract/quoting'
-
-module Ambition
- module Adapters
- module ActiveRecord
- class Base
- include ::ActiveRecord::ConnectionAdapters::Quoting
-
- def sanitize(value)
- if value.is_a? Array
- return value.map { |v| sanitize(v) }.join(', ')
- end
-
- case value
- when true, 'true'
- '1'
- when false, 'false'
- '0'
- when Regexp
- "'#{value.source}'"
- else
- if active_connection?
- ::ActiveRecord::Base.connection.quote(value)
- else
- quote(value)
- end
- end
- rescue
- "'#{value}'"
- end
-
- def quote_column_name(value)
- if active_connection?
- ::ActiveRecord::Base.connection.quote_column_name(value)
- else
- value.to_s
- end
- end
-
- def active_connection?
- ::ActiveRecord::Base.active_connection_name
- end
-
- def dbadapter_name
- ::ActiveRecord::Base.connection.adapter_name
- rescue ::ActiveRecord::ConnectionNotEstablished
- 'Abstract'
- end
-
- def statement(*args)
- @statement_instance ||= DatabaseStatements.const_get(dbadapter_name).new
- @statement_instance.send(*args)
- end
- end
- end
- end
-end
View
57 adapters/ambitious_activerecord/lib/ambition/adapters/active_record/query.rb
@@ -1,57 +0,0 @@
-module Ambition
- module Adapters
- module ActiveRecord
- class Query
- @@select = 'SELECT * FROM %s %s'
-
- def kick
- owner.find(:all, to_hash)
- end
-
- def size
- owner.count(to_hash)
- end
- alias_method :length, :size
-
- def to_hash
- hash = {}
-
- unless (where = clauses[:select]).blank?
- hash[:conditions] = Array(where)
- hash[:conditions] *= ' AND '
- end
-
- if order = clauses[:sort]
- hash[:order] = order.join(', ')
- end
-
- if Array(clauses[:slice]).last =~ /LIMIT (\d+)/
- hash[:limit] = $1.to_i
- end
-
- if Array(clauses[:slice]).last =~ /OFFSET (\d+)/
- hash[:offset] = $1.to_i
- end
-
- hash[:include] = stash[:include] if stash[:include]
-
- hash
- end
-
- def to_s
- hash = to_hash
-
- raise "Sorry, I can't construct SQL with complex joins (yet)" unless hash[:include].blank?
-
- sql = []
- sql << "WHERE #{hash[:conditions]}" unless hash[:conditions].blank?
- sql << "ORDER BY #{hash[:order]}" unless hash[:order].blank?
- sql << clauses[:slice].last unless hash[:slice].blank?
-
- @@select % [ owner.table_name, sql.join(' ') ]
- end
- alias_method :to_sql, :to_s
- end
- end
- end
-end
View
99 adapters/ambitious_activerecord/lib/ambition/adapters/active_record/select.rb
@@ -1,99 +0,0 @@
-module Ambition
- module Adapters
- module ActiveRecord
- class Select < Base
- def call(method)
- "#{owner.table_name}.#{quote_column_name method}"
- end
-
- def chained_call(*methods)
- if reflection = owner.reflections[methods.first]
- stash[:include] ||= []
- stash[:include] << methods.first
- "#{reflection.table_name}.#{quote_column_name methods.last}"
- elsif respond_to? methods[1]
- send(methods[1], methods.first)
- else
- raise "I don't understand: #{methods.inspect}"
- end
- end
-
- def both(left, right)
- "(#{left} AND #{right})"
- end
-
- def either(left, right)
- "(#{left} OR #{right})"
- end
-
- def ==(left, right)
- if right.nil?
- "#{left} IS NULL"
- else
- "#{left} = #{sanitize right}"
- end
- end
-
- # !=
- def not_equal(left, right)
- if right.nil?
- "#{left} IS NOT NULL"
- else
- "#{left} <> #{sanitize right}"
- end
- end
-
- def =~(left, right)
- if right.is_a? Regexp
- "#{left} #{statement(:regexp, right)} #{sanitize right}"
- else
- "#{left} LIKE #{sanitize right}"
- end
- end
-
- # !~
- def not_regexp(left, right)
- if right.is_a? Regexp
- "#{left} #{statement(:not_regexp, right)} #{sanitize right}"
- else
- "#{left} NOT LIKE #{sanitize right}"
- end
- end
-
- def <(left, right)
- "#{left} < #{sanitize right}"
- end
-
- def >(left, right)
- "#{left} > #{sanitize right}"
- end
-
- def >=(left, right)
- "#{left} >= #{sanitize right}"
- end
-
- def <=(left, right)
- "#{left} <= #{sanitize right}"
- end
-
- def include?(left, right)
- left = left.map { |element| sanitize element }.join(', ')
- "#{right} IN (#{left})"
- end
-
- def nil?(column)
- left = "#{owner.table_name}.#{quote_column_name column}"
- negated? ? not_equal(left, nil) : self.==(left, nil)
- end
-
- def downcase(column)
- "LOWER(#{owner.table_name}.#{quote_column_name column})"
- end
-
- def upcase(column)
- "UPPER(#{owner.table_name}.#{quote_column_name column})"
- end
- end
- end
- end
-end
View
19 adapters/ambitious_activerecord/lib/ambition/adapters/active_record/slice.rb
@@ -1,19 +0,0 @@
-module Ambition
- module Adapters
- module ActiveRecord
- class Slice < Base
- def slice(start, length=nil)
- if start.is_a? Range
- length = start.end
- length -= 1 if start.exclude_end?
- start = start.first - 1
- length -= start
- end
- out = "LIMIT #{length} "
- out << "OFFSET #{start}" if start.to_i.nonzero?
- out
- end
- end
- end
- end
-end
View
43 adapters/ambitious_activerecord/lib/ambition/adapters/active_record/sort.rb
@@ -1,43 +0,0 @@
-module Ambition
- module Adapters
- module ActiveRecord
- class Sort < Base
- def sort_by(method)
- "#{owner.table_name}.#{quote_column_name method}"
- end
-
- def reverse_sort_by(method)
- "#{owner.table_name}.#{quote_column_name method} DESC"
- end
-
- def chained_sort_by(receiver, method)
- if reflection = owner.reflections[receiver]
- stash[:include] ||= []
- stash[:include] << receiver
- "#{reflection.table_name}.#{quote_column_name method}"
- else
- raise [ receiver, method ].inspect
- end
- end
-
- def chained_reverse_sort_by(receiver, method)
- if reflection = owner.reflections[receiver]
- stash[:include] ||= []
- stash[:include] << receiver
- "#{reflection.table_name}.#{quote_column_name method} DESC"
- else
- raise [ receiver, method ].inspect
- end
- end
-
- def to_proc(symbol)
- "#{owner.table_name}.#{symbol}"
- end
-
- def rand
- 'RAND()'
- end
- end
- end
- end
-end
View
39 adapters/ambitious_activerecord/lib/ambition/adapters/active_record/statements.rb
@@ -1,39 +0,0 @@
-module Ambition
- module Adapters
- module ActiveRecord
- module DatabaseStatements
- def self.const_missing(*args)
- Abstract
- end
-
- class Abstract
- def regexp(regexp)
- 'REGEXP'
- end
-
- def not_regexp(regexp)
- 'NOT REGEXP'
- end
- end
-
- class PostgreSQL < Abstract
- def regexp(regexp)
- if regexp.options == 1
- '~*'
- else
- '~'
- end
- end
-
- def not_regexp(regexp)
- if regexp.options == 1
- '!~*'
- else
- '!~'
- end
- end
- end
- end
- end
- end
-end
View
69 adapters/ambitious_activerecord/test/benchmark.rb
@@ -1,69 +0,0 @@
-unless defined? Test
- $:.unshift File.dirname(__FILE__) + '/../../../lib'
- %w( rubygems ambition/adapters/active_record benchmark ).each { |f| require f }
-
- class User < ActiveRecord::Base
- def self.reflections
- return @reflections if @reflections
- @reflections = {}
- @reflections[:ideas] = Reflection.new(:has_many, 'user_id', :ideas, 'ideas')
- @reflections[:invites] = Reflection.new(:has_many, 'referrer_id', :invites, 'invites')
- @reflections[:profile] = Reflection.new(:has_one, 'user_id', :profile, 'profiles')
- @reflections[:account] = Reflection.new(:belongs_to, 'account_id', :account, 'accounts')
- @reflections
- end
-
- def self.table_name
- 'users'
- end
- end
-
- class Reflection < Struct.new(:macro, :primary_key_name, :name, :table_name)
- end
-
- Times = 10000
-
- Benchmark.bm(30) do |x|
- x.report 'simple select' do
- Times.times do
- User.select { |u| u.id == 20 }.to_hash
- end
- end
-
- x.report 'simple select w/ eval' do
- Times.times do
- User.select { |u| u.created_at == Time.now }.to_hash
- end
- end
-
- x.report 'dual select' do
- Times.times do
- User.select { |u| u.id == 20 && u.age > 20 }.to_hash
- end
- end
-
- x.report 'join select' do
- Times.times do
- User.select { |u| u.id == 20 && u.ideas.name =~ /stuff/ }.to_hash
- end
- end
-
- x.report 'dual select w/ sort' do
- Times.times do
- User.select { |u| u.id == 20 && u.age > 20 }.sort_by { |u| u.id }.to_hash
- end
- end
-
- x.report 'dual select w/ sort & first' do
- Times.times do
- User.select { |u| u.id == 20 && u.age > 20 }.sort_by { |u| u.id }.first(20).to_hash
- end
- end
-
- x.report "it's complicated" do
- Times.times do
- User.select { |u| (u.id == 20 && u.age > 20) || u.profile.name == 'Jon' }.sort_by { |u| [u.id, -u.name] }.first(20).to_hash
- end
- end
- end
-end
View
36 adapters/ambitious_activerecord/test/chaining_test.rb
@@ -1,36 +0,0 @@
-require File.dirname(__FILE__) + '/helper'
-
-context "ActiveRecord Adapter" do
- context "Chaining" do
- specify "should join selects with AND" do
- sql = User.select { |m| m.name == 'jon' }
- sql = sql.select { |m| m.age == 22 }
- sql.to_s.should == "SELECT * FROM users WHERE users.name = 'jon' AND users.age = 22"
- end
-
- specify "should join sort_bys with a comma" do
- sql = User.select { |m| m.name == 'jon' }
- sql = sql.sort_by { |m| m.name }
- sql = sql.sort_by { |m| m.age }
- sql.to_s.should == "SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.name, users.age"
- end
-
- specify "should join selects and sorts intelligently" do
- sql = User.select { |m| m.name == 'jon' }
- sql = sql.select { |m| m.age == 22 }
- sql = sql.sort_by { |m| -m.name }
- sql = sql.sort_by { |m| m.age }
- sql.to_s.should == "SELECT * FROM users WHERE users.name = 'jon' AND users.age = 22 ORDER BY users.name DESC, users.age"
- end
-
- specify "should join lots of selects and sorts intelligently" do
- sql = User.select { |m| m.name == 'jon' }
- sql = sql.select { |m| m.age == 22 }
- sql = sql.sort_by { |m| m.name }
- sql = sql.select { |m| m.power == true }
- sql = sql.sort_by { |m| m.email }
- sql = sql.select { |m| m.admin == true && m.email == 'chris@ozmm.org' }
- sql.to_s.should == "SELECT * FROM users WHERE users.name = 'jon' AND users.age = 22 AND users.power = 1 AND (users.admin = 1 AND users.email = 'chris@ozmm.org') ORDER BY users.name, users.email"
- end
- end
-end
View
19 adapters/ambitious_activerecord/test/count_test.rb
@@ -1,19 +0,0 @@
-require File.dirname(__FILE__) + '/helper'
-
-context "ActiveRecord Adapter" do
- context "Count" do
- setup do
- hash = { :conditions => "users.name = 'jon'" }
- User.expects(:count).with(hash)
- @sql = User.select { |m| m.name == 'jon' }
- end
-
- specify "size" do
- @sql.size
- end
-
- specify "length" do
- @sql.length
- end
- end
-end
View
88 adapters/ambitious_activerecord/test/enumerable_test.rb
@@ -1,88 +0,0 @@
-require File.dirname(__FILE__) + '/helper'
-
-context "ActiveRecord Adapter" do
- context "Each" do
- specify "simple ==" do
- hash = { :conditions => "users.age = 21" }
- User.expects(:find).with(:all, hash).returns([])
- User.select { |m| m.age == 21 }.each do |user|
- puts user.name
- end
- end
-
- specify "limit and conditions" do
- hash = { :limit => 5, :conditions => "users.age = 21" }
- User.expects(:find).with(:all, hash).returns([])
- User.select { |m| m.age == 21 }.first(5).each do |user|
- puts user.name
- end
- end
-
- specify "limit and conditions and order" do
- hash = { :limit => 5, :conditions => "users.age = 21", :order => 'users.name' }
- User.expects(:find).with(:all, hash).returns([])
- User.select { |m| m.age == 21 }.sort_by { |m| m.name }.first(5).each do |user|
- puts user.name
- end
- end
-
- specify "limit and order" do
- hash = { :limit => 5, :order => 'users.name' }
- User.expects(:find).with(:all, hash).returns([])
- User.sort_by { |m| m.name }.first(5).each do |user|
- puts user.name
- end
- end
- end
-
- context "Enumerable Methods" do
- specify "map" do
- hash = { :conditions => "users.age = 21" }
- User.expects(:find).with(:all, hash).returns([])
- User.select { |m| m.age == 21 }.map { |u| u.name }
- end
-
- specify "each_with_index" do
- hash = { :conditions => "users.age = 21" }
- User.expects(:find).with(:all, hash).returns([])
- User.select { |m| m.age == 21 }.each_with_index do |user, i|
- puts "#{i}: #{user.name}"
- end
- end
-
- specify "any?" do
- User.expects(:count).with(:conditions => "users.age > 21").returns(1)
- User.any? { |u| u.age > 21 }.should == true
- end
-
- specify "all?" do
- User.expects(:count).at_least_once.returns(10, 20)
- User.all? { |u| u.age > 21 }.should == false
-
- User.expects(:count).at_least_once.returns(10, 10)
- User.all? { |u| u.age > 21 }.should == true
- end
-
- specify "empty?" do
- User.expects(:count).with(:conditions => "users.age > 21").returns(1)
- User.select { |u| u.age > 21 }.empty?.should.equal false
-
- User.expects(:count).with(:conditions => "users.age > 21").returns(0)
- User.select { |u| u.age > 21 }.empty?.should.equal true
- end
-
- specify "entries" do
- User.expects(:find).with(:all, {})
- User.entries
-
- hash = { :conditions => "users.age = 21" }
- User.expects(:find).with(:all, hash).returns([])
- User.select { |m| m.age == 21 }.entries
- end
-
- specify "to_a" do
- User.expects(:find).with(:all, {})
- User.to_a
- end
- end
-end
View
47 adapters/ambitious_activerecord/test/helper.rb
@@ -1,47 +0,0 @@
-%w( rubygems test/spec mocha redgreen English ).each { |f| require f }
-
-$LOAD_PATH.unshift *[ File.dirname(__FILE__) + '/../lib', File.dirname(__FILE__) + '/../../../lib' ]
-require 'ambition/adapters/active_record'
-
-class User < ActiveRecord::Base
- def self.reflections
- return @reflections if @reflections
- @reflections = {}
- @reflections[:ideas] = Reflection.new(:has_many, 'user_id', :ideas, 'ideas')
- @reflections[:invites] = Reflection.new(:has_many, 'referrer_id', :invites, 'invites')
- @reflections[:profile] = Reflection.new(:has_one, 'user_id', :profile, 'profiles')
- @reflections[:account] = Reflection.new(:belongs_to, 'account_id', :account, 'accounts')
- @reflections
- end
-
- def self.table_name
- 'users'
- end
-end
-
-class Reflection < Struct.new(:macro, :primary_key_name, :name, :table_name)
-end
-
-module ActiveRecord
- module ConnectionAdapters
- class MysqlAdapter
- def initialize(*args)
- super
- end
-
- def connect(*args)
- true
- end
- end
-
- class PostgreSQLAdapter
- def connect(*args)
- true
- end
- class PGError; end
- end
-
- class FakeAdapter < AbstractAdapter
- end
- end
-end
View
62 adapters/ambitious_activerecord/test/join_test.rb
@@ -1,62 +0,0 @@
-require File.dirname(__FILE__) + '/helper'
-
-context "ActiveRecord Adapter" do
- context "Joins" do
- specify "simple == on an association" do
- sql = User.select { |m| m.account.email == 'chris@ozmm.org' }
- sql.to_hash.should == {
- :conditions => "accounts.email = 'chris@ozmm.org'",
- :include => [:account]
- }
- end
-
- specify "simple mixed == on an association" do
- sql = User.select { |m| m.name == 'chris' && m.account.email == 'chris@ozmm.org' }
- sql.to_hash.should == {
- :conditions => "(users.name = 'chris' AND accounts.email = 'chris@ozmm.org')",
- :include => [:account]
- }
- end
-
- specify "multiple associations" do
- sql = User.select { |m| m.ideas.title == 'New Freezer' || m.invites.email == 'pj@hyett.com' }
- sql.to_hash.should == {
- :conditions => "(ideas.title = 'New Freezer' OR invites.email = 'pj@hyett.com')",
- :include => [:ideas, :invites]
- }
- end
-
- specify "belongs_to" do
- sql = User.select { |m| m.account.id > 20 }
- sql.to_hash.should == {
- :conditions => "accounts.id > 20",
- :include => [:account]
- }
- end
-
- specify "complex joins have no to_s" do
- sql = User.select { |m| m.account.id > 20 }
- should.raise { sql.to_s }
- end
-
- specify "non-existant associations" do
- should.raise { User.select { |m| m.liquor.brand == 'Jack' } }
- end
-
- specify "in order" do
- sql = User.sort_by { |m| m.ideas.title }
- sql.to_hash.should == {
- :order => "ideas.title",
- :include => [:ideas]
- }
- end
-
- specify "in a more complex order" do
- sql = User.sort_by { |m| [ m.ideas.title, -m.invites.email ] }
- sql.to_hash.should == {
- :order => "ideas.title, invites.email DESC",
- :include => [:ideas, :invites]
- }
- end
- end
-end
View
36 adapters/ambitious_activerecord/test/profiler.rb
@@ -1,36 +0,0 @@
-$:.unshift File.dirname(__FILE__) + '/../../../lib'
-require 'rubygems'
-require 'ambition'
-require 'ambition/adapters/active_record'
-require 'ruby-prof'
-
-class User < ActiveRecord::Base
- def self.reflections
- return @reflections if @reflections
- @reflections = {}
- @reflections[:ideas] = Reflection.new(:has_many, 'user_id', :ideas, 'ideas')
- @reflections[:invites] = Reflection.new(:has_many, 'referrer_id', :invites, 'invites')
- @reflections[:profile] = Reflection.new(:has_one, 'user_id', :profile, 'profiles')
- @reflections[:account] = Reflection.new(:belongs_to, 'account_id', :account, 'accounts')
- @reflections
- end
-
- def self.table_name
- 'users'
- end
-end
-
-class Reflection < Struct.new(:macro, :primary_key_name, :name, :table_name)
-end
-
-result = RubyProf.profile do
- 1000.times do
- User.select { |u| (u.id == 20 && u.age > 20) || u.profile.name == 'Jon' }.sort_by { |u| [u.id, -u.name] }.first(20).to_hash
- end
-end
-
-printer = RubyProf::FlatPrinter.new(result)
-#printer = RubyProf::GraphPrinter.new(result)
-printer.print(STDOUT, 0)
-
-puts User.select { |u| (u.id == 20 && u.age > 20) || u.profile.name == 'Jon' }.sort_by { |u| [u.id, -u.name] }.first(20).to_hash.inspect
View
11 adapters/ambitious_activerecord/test/ruby_test.rb
@@ -1,11 +0,0 @@
-require File.dirname(__FILE__) + '/helper'
-
-context "ActiveRecord Adapter" do
- context "Inline Ruby" do
- xspecify "should know what to return" do
- name = 'David'
- sql = User.select { |u| name.nil? || u.name == name }.to_s
- sql.should == "SELECT * FROM users WHERE (users.name = 'David')"
- end
- end
-end
View
259 adapters/ambitious_activerecord/test/select_test.rb
@@ -1,259 +0,0 @@
-require File.dirname(__FILE__) + '/helper'
-
-context "ActiveRecord Adapter" do
- context "Select" do
- specify "simple ==" do
- sql = User.select { |m| m.name == 'jon' }.to_s
- sql.should == "SELECT * FROM users WHERE users.name = 'jon'"
- end
-
- specify "simple !=" do
- sql = User.select { |m| m.name != 'jon' }.to_s
- sql.should == "SELECT * FROM users WHERE users.name <> 'jon'"
- end
-
- specify "simple == && ==" do
- sql = User.select { |m| m.name == 'jon' && m.age == 21 }.to_s
- sql.should == "SELECT * FROM users WHERE (users.name = 'jon' AND users.age = 21)"
- end
-
- specify "simple == || ==" do
- sql = User.select { |m| m.name == 'jon' || m.age == 21 }.to_s
- sql.should == "SELECT * FROM users WHERE (users.name = 'jon' OR users.age = 21)"
- end
-
- specify "mixed && and ||" do
- sql = User.select { |m| m.name == 'jon' || m.age == 21 && m.password == 'pass' }.to_s
- sql.should == "SELECT * FROM users WHERE (users.name = 'jon' OR (users.age = 21 AND users.password = 'pass'))"
- end
-
- specify "grouped && and ||" do
- sql = User.select { |m| (m.name == 'jon' || m.name == 'rick') && m.age == 21 }.to_s
- sql.should == "SELECT * FROM users WHERE ((users.name = 'jon' OR users.name = 'rick') AND users.age = 21)"
- end
-
- specify "simple >/<" do
- sql = User.select { |m| m.age > 21 }.to_s
- sql.should == "SELECT * FROM users WHERE users.age > 21"
-
- sql = User.select { |m| m.age >= 21 }.to_s
- sql.should == "SELECT * FROM users WHERE users.age >= 21"
-
- sql = User.select { |m| m.age < 21 }.to_s
- sql.should == "SELECT * FROM users WHERE users.age < 21"
- end
-
- specify "array.include? item" do
- sql = User.select { |m| [1, 2, 3, 4].include? m.id }.to_s
- sql.should == "SELECT * FROM users WHERE users.id IN (1, 2, 3, 4)"
- end
-
- specify "variable'd array.include? item" do
- array = [1, 2, 3, 4]
- sql = User.select { |m| array.include? m.id }.to_s
- sql.should == "SELECT * FROM users WHERE users.id IN (1, 2, 3, 4)"
- end
-
- specify "simple == with variables" do
- me = 'chris'
- sql = User.select { |m| m.name == me }.to_s
- sql.should == "SELECT * FROM users WHERE users.name = '#{me}'"
- end
-
- specify "simple == with method arguments" do
- def test_it(name)
- sql = User.select { |m| m.name == name }.to_s
- sql.should == "SELECT * FROM users WHERE users.name = '#{name}'"
- end
-
- test_it('chris')
- end
-
- specify "simple == with instance variables" do
- @me = 'chris'
- sql = User.select { |m| m.name == @me }.to_s
- sql.should == "SELECT * FROM users WHERE users.name = '#{@me}'"
- end
-
- specify "simple == with instance variable method call" do
- require 'ostruct'
- @person = OpenStruct.new(:name => 'chris')
-
- sql = User.select { |m| m.name == @person.name }.to_s
- sql.should == "SELECT * FROM users WHERE users.name = '#{@person.name}'"
- end
-
- specify "simple == with global variables" do
- $my_name = 'boston'
- sql = User.select { |m| m.name == $my_name }.to_s
- sql.should == "SELECT * FROM users WHERE users.name = '#{$my_name}'"
- end
-
- specify "simple == with method call" do
- def band
- 'megadeth'
- end
-
- sql = User.select { |m| m.name == band }.to_s
- sql.should == "SELECT * FROM users WHERE users.name = '#{band}'"
- end
-
- specify "simple =~ with string" do
- sql = User.select { |m| m.name =~ 'chris' }.to_s
- sql.should == "SELECT * FROM users WHERE users.name LIKE 'chris'"
-
- sql = User.select { |m| m.name =~ 'chri%' }.to_s
- sql.should == "SELECT * FROM users WHERE users.name LIKE 'chri%'"
- end
-
- specify "simple !~ with string" do
- sql = User.select { |m| m.name !~ 'chris' }.to_s
- sql.should == "SELECT * FROM users WHERE users.name NOT LIKE 'chris'"
-
- sql = User.select { |m| !(m.name =~ 'chris') }.to_s
- sql.should == "SELECT * FROM users WHERE users.name NOT LIKE 'chris'"
- end
-
- specify "simple =~ with regexp" do
- sql = User.select { |m| m.name =~ /chris/ }.to_s
- sql.should == "SELECT * FROM users WHERE users.name REGEXP 'chris'"
- end
-
- specify "simple =~ with regexp flags" do
- sql = User.select { |m| m.name =~ /chris/i }.to_s
- sql.should == "SELECT * FROM users WHERE users.name REGEXP 'chris'"
- end
-
- specify "simple LOWER()" do
- sql = User.select { |m| m.name.downcase =~ 'chris%' }.to_s
- sql.should == "SELECT * FROM users WHERE LOWER(users.name) LIKE 'chris%'"
- end
-
- specify "simple UPPER()" do
- sql = User.select { |m| m.name.upcase =~ 'chris%' }.to_s
- sql.should == "SELECT * FROM users WHERE UPPER(users.name) LIKE 'chris%'"
- end
-
- specify "undefined equality symbol" do
- should.raise { User.select { |m| m.name =* /chris/ }.to_s }
- end
-
- specify "block variable / assigning variable conflict" do
- m = User.select { |m| m.name == 'chris' }.to_s
- m.should == "SELECT * FROM users WHERE users.name = 'chris'"
- end
-
- specify "simple == with inline ruby" do
- sql = User.select { |m| m.created_at == 2.days.ago.to_s(:db) }.to_s
- sql.should == "SELECT * FROM users WHERE users.created_at = '#{2.days.ago.to_s(:db)}'"
- end
-
- specify "deep chains" do
- should.raise do
- User.select { |m| m.created_at.something.else.perhaps }.to_s
- end
- end
-
- specify "inspect" do
- User.select { |u| u.name }.inspect.should.match %r(call #to_s or #to_hash)
- end
- end
-
- context "Detect" do
- specify "simple ==" do
- User.expects(:select).returns(mock(:first => true))
- User.detect { |m| m.name == 'chris' }
- end
-
- specify "nothing found" do
- User.expects(:select).returns(mock(:first => nil))
- User.detect { |m| m.name == 'chris' }.should.be.nil
- end
- end
-
- xcontext "[]" do
- specify "finds a single row" do
- User.expects(:find).with(1)
- User[1]
- end
- end
-
- context "PostgreSQL specific" do
- setup do
- ActiveRecord::Base.connection = ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.new 'fake_connection', 'fake_logger'
- end
-
- teardown do
- ActiveRecord::Base.remove_connection
- end
-
- specify "quoting of column name" do
- me = 'chris'
- sql = User.select { |m| m.name == me }.to_s
- sql.should == %(SELECT * FROM users WHERE users."name" = '#{me}')
- end
-
- specify "simple =~ with regexp" do
- sql = User.select { |m| m.name =~ /chris/ }.to_s
- sql.should == %(SELECT * FROM users WHERE users."name" ~ 'chris')
- end
-
- specify "insensitive =~" do
- sql = User.select { |m| m.name =~ /chris/i }.to_s
- sql.should == %(SELECT * FROM users WHERE users."name" ~* 'chris')
- end
-
- specify "negated =~" do
- sql = User.select { |m| m.name !~ /chris/ }.to_s
- sql.should == %(SELECT * FROM users WHERE users."name" !~ 'chris')
- end
-
- specify "negated insensitive =~" do
- sql = User.select { |m| m.name !~ /chris/i }.to_s
- sql.should == %(SELECT * FROM users WHERE users."name" !~* 'chris')
- end
- end
-
- context "MySQL specific" do
- setup do
- ActiveRecord::Base.connection = ActiveRecord::ConnectionAdapters::MysqlAdapter.new('connection', 'logger')
- end
-
- teardown do
- ActiveRecord::Base.connection = ActiveRecord::ConnectionAdapters::FakeAdapter.new('connection', 'logger')
- end
-
- specify "quoting of column name" do
- me = 'chris'
- sql = User.select { |m| m.name == me }.to_s
- sql.should == "SELECT * FROM users WHERE users.`name` = '#{me}'"
- end
-
- specify "simple =~ with regexp" do
- sql = User.select { |m| m.name =~ /chris/ }.to_s
- sql.should == "SELECT * FROM users WHERE users.`name` REGEXP 'chris'"
- end
-
- specify "negated =~" do
- sql = User.select { |m| m.name !~ 'chris' }.to_s
- sql.should == "SELECT * FROM users WHERE users.`name` NOT LIKE 'chris'"
- end
-
- specify "negated =~ with regexp" do
- sql = User.select { |m| m.name !~ /chris/ }.to_s
- sql.should == "SELECT * FROM users WHERE users.`name` NOT REGEXP 'chris'"
- end
- end
-
- context "Adapter without overrides" do
- setup do
- ActiveRecord::Base.connection = ActiveRecord::ConnectionAdapters::FakeAdapter.new('connection', 'logger')
- end
-
- specify "quoting of column name" do
- me = 'chris'
- sql = User.select { |m| m.name == me }.to_s
- sql.should == "SELECT * FROM users WHERE users.name = '#{me}'"
- end
- end
-end
View
43 adapters/ambitious_activerecord/test/slice_test.rb
@@ -1,43 +0,0 @@
-require File.dirname(__FILE__) + '/helper'
-
-context "ActiveRecord Adapter" do
- context "Slice" do
- setup do
- @sql = User.select { |m| m.name == 'jon' }
- end
-
- specify "first" do
- conditions = { :conditions => "users.name = 'jon'", :limit => 1 }
- User.expects(:find).with(:all, conditions)
- @sql.first
- end
-
- specify "first with argument" do
- conditions = { :conditions => "users.name = 'jon'", :limit => 5 }
- User.expects(:find).with(:all, conditions)
- @sql.first(5).entries
- end
-
- specify "[] with two elements" do
- conditions = { :conditions => "users.name = 'jon'", :limit => 20, :offset => 10 }
- User.expects(:find).with(:all, conditions)
- @sql[10, 20].entries
-
- conditions = { :conditions => "users.name = 'jon'", :limit => 20, :offset => 20 }
- User.expects(:find).with(:all, conditions)
- @sql[20, 20].entries
- end
-
- specify "slice is an alias of []" do
- conditions = { :conditions => "users.name = 'jon'", :limit => 20, :offset => 10 }
- User.expects(:find).with(:all, conditions)
- @sql.slice(10, 20).entries
- end
-
- specify "[] with range" do
- conditions = { :conditions => "users.name = 'jon'", :limit => 10, :offset => 10 }
- User.expects(:find).with(:all, conditions)
- @sql[11..20].entries
- end
- end
-end
View
54 adapters/ambitious_activerecord/test/sort_test.rb
@@ -1,54 +0,0 @@
-require File.dirname(__FILE__) + '/helper'
-
-context "ActiveRecord Adapter" do
- context "Sort" do
- setup do
- @sql = User.select { |m| m.name == 'jon' }
- end
-
- specify "simple order" do
- string = @sql.sort_by { |m| m.name }.to_s
- string.should == "SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.name"
- end
-
- specify "simple combined order" do
- string = @sql.sort_by { |m| [ m.name, m.age ] }.to_s
- string.should == "SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.name, users.age"
- end
-
- specify "simple combined order with single reverse" do
- string = @sql.sort_by { |m| [ m.name, -m.age ] }.to_s
- string.should == "SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.name, users.age DESC"
- end
-
- specify "simple combined order with two reverses" do
- string = @sql.sort_by { |m| [ -m.name, -m.age ] }.to_s
- string.should == "SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.name DESC, users.age DESC"
- end
-
- specify "reverse order with -" do
- string = @sql.sort_by { |m| -m.age }.to_s
- string.should == "SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.age DESC"
- end
-
- xspecify "reverse order with #reverse" do
- # TODO: not implemented
- string = @sql.sort_by { |m| m.age }.reverse.to_s
- string.should == "SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.age DESC"
- end
-
- specify "random order" do
- string = @sql.sort_by { rand }.to_s
- string.should == "SELECT * FROM users WHERE users.name = 'jon' ORDER BY RAND()"
- end
-
- specify "non-existent method to sort by" do
- should.raise(NoMethodError) { @sql.sort_by { foo }.to_s }
- end
-
- specify "Symbol#to_proc" do
- string = User.sort_by(&:name).to_s
- string.should == "SELECT * FROM users ORDER BY users.name"
- end
- end
-end
View
43 adapters/ambitious_activerecord/test/source_test.rb
@@ -1,43 +0,0 @@
-require File.dirname(__FILE__) + '/helper'
-require 'ostruct'
-
-context "Setting the ambition_source" do
- setup do
-# @users = [
-# OpenStruct.new(:name => 'Chris', :age => 22),
-# OpenStruct.new(:name => 'PJ', :age => 24),
-# OpenStruct.new(:name => 'Kevin', :age => 23),
-# OpenStruct.new(:name => '_why', :age => 65)
-# ]
-# User.ambition_source = @users
- end
-
- teardown do
-# User.ambition_source = nil
- end
-
- xspecify "should run all selects / detects against that collection" do
- User.detect { |u| u.name == 'Chris' }.should == @users.first
- end
-
- xspecify "should run all sorts against that collection" do
- User.sort_by { |u| -u.age }.entries.should == @users.sort_by { |u| -u.age }
- end
-
- xspecify "should chain successfully" do
- User.select { |u| u.age > 22 }.sort_by { |u| -u.age }.entries.should == [ @users[3], @users[1], @users[2] ]
- end
-
- xspecify "should be able to revert to normal" do
- block = proc { User.select { |m| m.name == 'PJ' }.first }
-
- User.expects(:find).never
- block.call.should == @users[1]
-
- conditions = { :conditions => "users.name = 'PJ'", :limit => 1 }
- User.expects(:find).with(:first, conditions)
-
- User.ambition_source = nil
- block.call
- end
-end
View
69 adapters/ambitious_activerecord/test/types_test.rb
@@ -1,69 +0,0 @@
-require File.dirname(__FILE__) + '/helper'
-
-##
-# Once dynamically, once hardcoded
-context "Different types" do
- types_hash = {
- 'string' => "'string'",
- :symbol => "'--- :symbol\n'",
- 1 => '1',
- 1.2 => '1.2',
- true => '1',
- false => '0',
- Time.now => "'#{Time.now.to_s(:db)}'",
- DateTime.now => "'#{DateTime.now.to_s(:db)}'",
- Date.today => "'#{Date.today.to_s(:db)}'"
- }
-
- types_hash.each do |type, translation|
- specify "simple using #{type}" do
- sql = User.select { |m| m.name == type }.to_s
- sql.should == "SELECT * FROM users WHERE users.name = #{translation}"
- end
- end
-
- specify "float" do
- sql = User.select { |m| m.name == 1.2 }.to_s
- sql.should == "SELECT * FROM users WHERE users.name = 1.2"
- end
-
- specify "integer" do
- sql = User.select { |m| m.name == 1 }.to_s
- sql.should == "SELECT * FROM users WHERE users.name = 1"
- end
-
- specify "true" do
- sql = User.select { |m| m.name == true }.to_s
- sql.should == "SELECT * FROM users WHERE users.name = 1"
- end
-
- specify "false" do
- sql = User.select { |m| m.name == false }.to_s
- sql.should == "SELECT * FROM users WHERE users.name = 0"
- end
-
- specify "nil" do
- sql = User.select { |m| m.name == nil }.to_s
- sql.should == "SELECT * FROM users WHERE users.name IS NULL"
- end
-
- specify "not nil" do
- sql = User.select { |m| m.name != nil }.to_s
- sql.should == "SELECT * FROM users WHERE users.name IS NOT NULL"
- end
-
- specify "nil?" do
- sql = User.select { |m| m.name.nil? }.to_s
- sql.should == "SELECT * FROM users WHERE users.name IS NULL"
- end
-
- specify "!nil?" do
- sql = User.select { |m| !m.name.nil? }.to_s
- sql.should == "SELECT * FROM users WHERE users.name IS NOT NULL"
- end
-
- specify "Time" do
- sql = User.select { |m| m.name == Time.now }.to_s
- sql.should == "SELECT * FROM users WHERE users.name = '#{Time.now.to_s(:db)}'"
- end
-end
Please sign in to comment.
Something went wrong with that request. Please try again.