Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 786193e3d467f58b2fbecb8798ff4a39871af7af @stopdropandrew committed Jan 25, 2010
Showing with 205 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +20 −0 MIT_LICENSE
  3. +9 −0 Rakefile
  4. +5 −0 init.rb
  5. +20 −0 install.rb
  6. +37 −0 lib/find_ids.rb
  7. +5 −0 test/database.yml.example
  8. +65 −0 test/find_ids_test.rb
  9. +43 −0 test/test_helper.rb
@@ -0,0 +1 @@
+test/database.yml
@@ -0,0 +1,20 @@
+Copyright (c) 2008 The shredders at Kongregate
+
+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.
@@ -0,0 +1,9 @@
+require 'rake'
+require 'rake/testtask'
+
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
@@ -0,0 +1,5 @@
+require 'find_ids'
+
+ActiveRecord::Base.class_eval do
+ include Insert
+end
@@ -0,0 +1,20 @@
+puts <<-SHRED
+
+From the shredders at
+K O N G R E G A T E
+ _
+ / 7
+ /_(
+ |_|
+ |_|
+ |_|
+ |_| /\
+ /\|=|/ /
+ \ |_| /
+ ) _ \
+ / |_| \
+ / -=-o /
+ \ /~\_/
+ \/
+
+<<SHRED
@@ -0,0 +1,37 @@
+module ActiveRecordExtensions
+ module FindIds
+ def find_ids(options = {})
+ find_column_values(primary_key, options)
+ end
+
+ def find_column_values(column_name, options = {})
+ # Get the results as an array of tiny hashes { "id" => "1" } and flatten them out to just the ids
+ find_multiple_column_values(column_name, options).map{|record| record[column_name.to_s]}
+ end
+
+ def find_set_ids(options = {})
+ find_set_column_values(primary_key, options)
+ end
+
+ def find_set_column_values(column_name, options = {})
+ Set.new(find_column_values(column_name, options))
+ end
+
+ def find_multiple_column_values(*column_names)
+ options = column_names.extract_options!
+
+ distinct = "distinct" if options.delete(:distinct)
+ select_for_columns = column_names.map{|column_name| "#{distinct} #{quoted_table_name}.`#{column_name}`"}.join(', ')
+ sql = construct_finder_sql(options.merge( :select => select_for_columns ))
+
+ array_of_hashes = connection.select_all(sql)
+ array_of_hashes.each do |columns_values_hash|
+ columns_values_hash.each do |k,v|
+ columns_values_hash[k] = self.columns_hash[k].type_cast(v)
+ end
+ end
+ end
+ end
+end
+
+ActiveRecord::Base.extend ActiveRecordExtensions::FindIds
@@ -0,0 +1,5 @@
+adapter: mysql
+host: localhost
+database: find_ids_test
+username: andrew
+password:
@@ -0,0 +1,65 @@
+TEST_ROOT = File.dirname(__FILE__)
+require File.join(TEST_ROOT, 'test_helper')
+
+require File.join(TEST_ROOT, '..', 'lib', 'find_ids')
+
+class FindIdsTest < ActiveSupport::TestCase
+ include ActiveRecord::TestFixtures
+ self.use_instantiated_fixtures = false
+ self.use_transactional_fixtures = true
+ self.pre_loaded_fixtures = true
+
+ def test_find_ids_should_do_the_obvious
+ assert_equal User.find(:all).map(&:id), User.find_ids
+ end
+
+ def test_find_ids_shouldnt_instantiate_AR_objects
+ expected_ids = User.find(:all).map(&:id)
+
+ User.any_instance.expects(:after_initialize).never
+ assert_equal expected_ids, User.find_ids
+ end
+
+ def test_find_ids_can_handle_ordering
+ assert_equal User.find(:all, :order => 'random').map(&:id), User.find_ids(:order => 'random')
+ end
+
+ def test_find_column_values_should_do_the_obvious
+ assert_equal User.find(:all).map(&:email), User.find_column_values('email')
+ end
+
+ def test_find_column_values_shouldnt_instantiate_AR_objects
+ expected_emails = User.find(:all).map(&:email)
+
+ User.any_instance.expects(:after_initialize).never
+ assert_equal expected_emails, User.find_column_values('email')
+ end
+
+ def test_find_column_values_can_handle_ordering
+ assert_equal User.find(:all, :order => 'random').map(&:email), User.find_column_values('email', :order => 'random')
+ end
+
+ def test_find_column_values_handles_distinct
+ original_count = User.count
+ original_emails = User.find(:all, :order => 'email').map(&:email)
+
+ User.find(:all).each do |user|
+ User.create!(:email => user.email)
+ end
+
+ assert_equal original_count * 2, User.count, "There should be double the users"
+ assert_equal original_emails, User.find_column_values('email', :order => 'email', :distinct => true)
+ end
+
+ def test_find_ids_should_use_primary_key
+ assert_equal WackyUser.find(:all).map(&:wack), WackyUser.find_ids
+ end
+
+ def test_find_set_ids
+ assert_equal Set.new(User.find_ids), User.find_set_ids
+ end
+
+ def test_find_multiple_column_values
+ assert_equal User.find(:all).map{ |u| {'id' => u.id, 'email' => u.email} }, User.find_multiple_column_values('id', 'email')
+ end
+end
@@ -0,0 +1,43 @@
+require 'rubygems'
+require 'test/unit'
+require 'active_record'
+require 'active_record/fixtures'
+require 'mocha'
+require 'yaml'
+
+ActiveRecord::Base.configurations = YAML::load(File.open(File.join(TEST_ROOT,'database.yml')))
+ActiveRecord::Base.establish_connection(:find_ids_test)
+
+ActiveRecord::Schema.define do
+ create_table "users", :force => true do |t|
+ t.string "username"
+ t.string "email"
+ t.integer "reverse"
+ t.integer "random"
+ end
+
+ create_table "wacky_users", :primary_key => 'wack', :force => true do |t|
+ t.string "username"
+ end
+end
+
+class User < ActiveRecord::Base
+ def after_initialize
+ # for testing
+ end
+end
+
+class WackyUser < ActiveRecord::Base
+ set_primary_key 'wack'
+end
+
+(1..10).each do |i|
+ User.create!(
+ :username => "user#{i}",
+ :email => "user#{i}@gmail.com",
+ :random => rand(100000),
+ :reverse => 100000 - i
+ )
+
+ WackyUser.create(:username => "user#{i}")
+end

0 comments on commit 786193e

Please sign in to comment.