Permalink
Browse files

Gems works

  • Loading branch information...
1 parent 91a86a1 commit 63d7c82d0ceffb619169f81b08242c29fe9ee534 @JGailor committed Dec 26, 2008
Showing with 95 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. 0 Changelog
  3. 0 LICENSE
  4. +13 −0 Rakefile
  5. +22 −0 lib/except.rb
  6. +19 −0 test/except_spec.rb
  7. +29 −0 test/models.rb
  8. +9 −0 test/spec_helper.rb
View
@@ -0,0 +1,3 @@
+.autotest
+*~
+test/test.db
View
No changes.
View
No changes.
View
@@ -0,0 +1,13 @@
+require 'rubygems'
+require 'rake'
+require 'spec/rake/spectask'
+
+task :default => :spec
+
+desc "Run all specs in spec directory (excluding plugin specs)"
+Spec::Rake::SpecTask.new(:spec) do |t|
+ t.libs << "lib"
+ t.spec_files = FileList['test/**/*_spec.rb']
+ t.verbose = true
+end
+
View
@@ -0,0 +1,22 @@
+require 'rubygems'
+require 'activerecord'
+
+module Except
+ def self.included(base)
+ class << base
+ def find_with_except(*args)
+ options = args.last.is_a?(::Hash) ? args.pop : {}
+ if exceptions = options.delete(:except)
+ options[:select] = self.column_names.find_all {|name| !exceptions.include?(name)}.join(", ")
+ end
+ args << options
+ find_without_except(*args)
+ end
+
+ alias_method :find_without_except, :find
+ alias_method :find, :find_with_except
+ end
+ end
+end
+
+ActiveRecord::Base.send(:include, Except)
View
@@ -0,0 +1,19 @@
+require File.join(File.dirname(__FILE__), "spec_helper")
+
+describe User do
+ before :each do
+ @user = User.create!(:email => "test@test.com", :password => "password")
+ end
+
+ it "should work normally when no except clause is given" do
+ User.first.should == @user
+ end
+
+ it "should not throw an error when using the except clause" do
+ lambda{User.find(:all, :except => "password")}.should_not raise_error
+ end
+
+ it "should only bring back all the fields except for the ones in the except clause" do
+ User.first(:except => "password").attribute_names.should_not include(:password)
+ end
+end
View
@@ -0,0 +1,29 @@
+ActiveRecord::Base.establish_connection(
+ :adapter => 'sqlite3',
+ :database => File.join(File.dirname(__FILE__), "test.db")
+)
+
+class CreateSchema < ActiveRecord::Migration
+ def self.up
+ create_table :users, :force => true do |t|
+ t.string :email, :password
+ t.timestamps
+ end
+
+ create_table :profiles, :force => true do |t|
+ t.string :firstname, :lastname
+ t.integer :user_id, :age
+ t.timestamps
+ end
+ end
+end
+
+CreateSchema.suppress_messages { CreateSchema.migrate(:up) }
+
+class User < ActiveRecord::Base
+ has_one :profile
+end
+
+class Profile < ActiveRecord::Base
+ belongs_to :user
+end
View
@@ -0,0 +1,9 @@
+$: << File.join(File.dirname(__FILE__), "..", "lib")
+$: << File.join(File.dirname(__FILE__))
+
+require 'rubygems'
+require 'test/unit'
+require 'spec'
+require 'activerecord'
+require 'except'
+require 'models'

0 comments on commit 63d7c82

Please sign in to comment.