Skip to content

Commit

Permalink
Rails 3.1, RSpec 2 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Jul 26, 2011
1 parent b1a5bee commit f703f88
Show file tree
Hide file tree
Showing 17 changed files with 189 additions and 168 deletions.
17 changes: 11 additions & 6 deletions Gemfile
Expand Up @@ -3,15 +3,20 @@ source :rubygems
# path '/Users/mislav/.coral/rails-rails'
# git 'git://github.com/rails/rails.git'

gem 'activerecord', '~> 3.0.0.rc'
gem 'activeresource', '~> 3.0.0.rc'
gem 'actionpack', '~> 3.0.0.rc'
gem 'rake'
gem 'activerecord', '~> 3.1.0.rc'
gem 'activeresource', '~> 3.1.0.rc'
gem 'actionpack', '~> 3.1.0.rc'
gem 'sequel', '~> 3.8.0'
gem 'rspec', '~> 1.3.0'
gem 'rspec', '~> 2.6.0'
gem 'mocha', '~> 0.9.8'
gem 'sqlite3-ruby', '~> 1.2.5'
gem 'sqlite3', '~> 1.3.3'
gem 'dm-core', '~> 0.10.2'
gem 'do_sqlite3', '~> 0.10.1.1'
gem 'mysql', '~> 2.8.1', :group => :mysql
gem 'pg', '~> 0.8.0', :group => :pg
gem 'ruby-debug', '~> 0.10.3', :group => :debug

group :debug do
gem 'ruby-debug', :platforms => :mri_18
gem 'ruby-debug19', :platforms => :mri_19
end
117 changes: 75 additions & 42 deletions Gemfile.lock
@@ -1,80 +1,113 @@
GEM
remote: http://rubygems.org/
specs:
abstract (1.0.0)
actionpack (3.0.0)
activemodel (= 3.0.0)
activesupport (= 3.0.0)
builder (~> 2.1.2)
erubis (~> 2.6.6)
i18n (~> 0.4.1)
rack (~> 1.2.1)
rack-mount (~> 0.6.12)
rack-test (~> 0.5.4)
tzinfo (~> 0.3.23)
activemodel (3.0.0)
activesupport (= 3.0.0)
builder (~> 2.1.2)
i18n (~> 0.4.1)
activerecord (3.0.0)
activemodel (= 3.0.0)
activesupport (= 3.0.0)
arel (~> 1.0.0)
tzinfo (~> 0.3.23)
activeresource (3.0.0)
activemodel (= 3.0.0)
activesupport (= 3.0.0)
activesupport (3.0.0)
actionpack (3.1.0.rc5)
activemodel (= 3.1.0.rc5)
activesupport (= 3.1.0.rc5)
builder (~> 3.0.0)
erubis (~> 2.7.0)
i18n (~> 0.6)
rack (~> 1.3.1)
rack-cache (~> 1.0.2)
rack-mount (~> 0.8.1)
rack-test (~> 0.6.0)
sprockets (~> 2.0.0.beta.12)
activemodel (3.1.0.rc5)
activesupport (= 3.1.0.rc5)
bcrypt-ruby (~> 2.1.4)
builder (~> 3.0.0)
i18n (~> 0.6)
activerecord (3.1.0.rc5)
activemodel (= 3.1.0.rc5)
activesupport (= 3.1.0.rc5)
arel (~> 2.1.4)
tzinfo (~> 0.3.29)
activeresource (3.1.0.rc5)
activemodel (= 3.1.0.rc5)
activesupport (= 3.1.0.rc5)
activesupport (3.1.0.rc5)
multi_json (~> 1.0)
addressable (2.2.0)
arel (1.0.1)
activesupport (~> 3.0.0)
builder (2.1.2)
archive-tar-minitar (0.5.2)
arel (2.1.4)
bcrypt-ruby (2.1.4)
builder (3.0.0)
columnize (0.3.1)
data_objects (0.10.1)
addressable (~> 2.1)
diff-lcs (1.1.2)
dm-core (0.10.2)
addressable (~> 2.1)
extlib (~> 0.9.14)
do_sqlite3 (0.10.1.1)
data_objects (= 0.10.1)
erubis (2.6.6)
abstract (>= 1.0.0)
erubis (2.7.0)
extlib (0.9.15)
i18n (0.4.1)
hike (1.2.0)
i18n (0.6.0)
linecache (0.43)
linecache19 (0.5.12)
ruby_core_source (>= 0.1.4)
mocha (0.9.8)
rake
multi_json (1.0.3)
mysql (2.8.1)
pg (0.8.0)
rack (1.2.1)
rack-mount (0.6.13)
rack (1.3.2)
rack-cache (1.0.2)
rack (>= 0.4)
rack-mount (0.8.1)
rack (>= 1.0.0)
rack-test (0.5.4)
rack-test (0.6.0)
rack (>= 1.0)
rake (0.8.7)
rspec (1.3.0)
rspec (2.6.0)
rspec-core (~> 2.6.0)
rspec-expectations (~> 2.6.0)
rspec-mocks (~> 2.6.0)
rspec-core (2.6.4)
rspec-expectations (2.6.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.6.0)
ruby-debug (0.10.3)
columnize (>= 0.1)
ruby-debug-base (~> 0.10.3.0)
ruby-debug-base (0.10.3)
linecache (>= 0.3)
ruby-debug-base19 (0.11.25)
columnize (>= 0.3.1)
linecache19 (>= 0.5.11)
ruby_core_source (>= 0.1.4)
ruby-debug19 (0.11.6)
columnize (>= 0.3.1)
linecache19 (>= 0.5.11)
ruby-debug-base19 (>= 0.11.19)
ruby_core_source (0.1.5)
archive-tar-minitar (>= 0.5.2)
sequel (3.8.0)
sqlite3-ruby (1.2.5)
tzinfo (0.3.23)
sprockets (2.0.0.beta.12)
hike (~> 1.2)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
sqlite3 (1.3.3)
tilt (1.3.2)
tzinfo (0.3.29)

PLATFORMS
ruby

DEPENDENCIES
actionpack (~> 3.0.0.rc)
activerecord (~> 3.0.0.rc)
activeresource (~> 3.0.0.rc)
actionpack (~> 3.1.0.rc)
activerecord (~> 3.1.0.rc)
activeresource (~> 3.1.0.rc)
dm-core (~> 0.10.2)
do_sqlite3 (~> 0.10.1.1)
mocha (~> 0.9.8)
mysql (~> 2.8.1)
pg (~> 0.8.0)
rspec (~> 1.3.0)
ruby-debug (~> 0.10.3)
rake
rspec (~> 2.6.0)
ruby-debug
ruby-debug19
sequel (~> 3.8.0)
sqlite3-ruby (~> 1.2.5)
sqlite3 (~> 1.3.3)
16 changes: 13 additions & 3 deletions Rakefile
@@ -1,10 +1,20 @@
require 'rake/rdoctask'
require 'rspec/core/rake_task'

load 'spec/tasks.rake'

desc 'Default: run specs.'
task :default => :spec

desc 'Run ALL OF the specs'
RSpec::Core::RakeTask.new(:spec) do |t|
# t.ruby_opts = '-w'
end

namespace :spec do
desc "Run Rails specs"
RSpec::Core::RakeTask.new(:rails) do |t|
t.pattern = %w'spec/finders/active_record_spec.rb spec/view_helpers/action_view_spec.rb'
end
end

desc 'Generate RDoc documentation for the will_paginate plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_files.include('README.rdoc', 'LICENSE', 'CHANGELOG.rdoc').
Expand Down
13 changes: 8 additions & 5 deletions lib/will_paginate/finders/active_record.rb
Expand Up @@ -28,14 +28,17 @@ module ActiveRecord

# In Rails, this is automatically called to mix-in pagination functionality to ActiveRecord.
def self.enable!
::ActiveRecord::Base.class_eval do
extend ActiveRecord
::ActiveRecord::Base.extend ActiveRecord

klasses = [::ActiveRecord::Relation]
if defined? ::ActiveRecord::Associations::CollectionProxy
klasses << ::ActiveRecord::Associations::CollectionProxy
else
klasses << ::ActiveRecord::Associations::AssociationCollection
end

# support pagination on associations and scopes
[::ActiveRecord::Relation, ::ActiveRecord::Associations::AssociationCollection].each do |klass|
klass.send(:include, ActiveRecord)
end
klasses.each { |klass| klass.send(:include, ActiveRecord) }
end

# Wraps +find_by_sql+ by simply adding LIMIT and OFFSET to your SQL string
Expand Down
2 changes: 1 addition & 1 deletion lib/will_paginate/view_helpers/action_view.rb
Expand Up @@ -86,7 +86,7 @@ def infer_collection_from_controller
protected

def default_url_params
{ :escape => false }
{}
end

def url(page)
Expand Down
3 changes: 2 additions & 1 deletion spec/console_fixtures.rb
Expand Up @@ -24,6 +24,7 @@
end

# load all fixtures
Fixtures.create_fixtures(ActiverecordTestConnector::FIXTURES_PATH, ActiveRecord::Base.connection.tables)
ActiverecordTestConnector::Fixtures.create_fixtures \
ActiverecordTestConnector::FIXTURES_PATH, ActiveRecord::Base.connection.tables

WillPaginate::Finders::ActiveRecord.enable!
24 changes: 19 additions & 5 deletions spec/finders/active_record_spec.rb
Expand Up @@ -176,7 +176,7 @@
lambda {
Developer.paginate :page => 1, :per_page => 1, :include => :projects
$query_sql.last.should_not include(' JOIN ')
}.should run_queries(4)
}.should run_queries(3..4)
end

it "should keep :include for count when they are referenced in :conditions" do
Expand All @@ -199,7 +199,7 @@

expected = Topic.find :all,
:include => 'replies',
:conditions => ["project_id = #{project.id} AND replies.content LIKE ?", 'Nice%'],
:conditions => ["project_id = ? AND replies.content LIKE ?", project.id, 'Nice%'],
:limit => 10

result.should == expected
Expand Down Expand Up @@ -359,19 +359,33 @@ def initialize(num)
end

def matches?(block)
run(block)

if @expected_count.respond_to? :include?
@expected_count.include? @count
else
@count == @expected_count
end
end

def run(block)
$query_count = 0
$query_sql = []
block.call
@queries = $query_sql
ensure
@queries = $query_sql.dup
@count = $query_count
@count == @expected_count
end

def performed_queries
@queries
end

def failure_message
"expected #{@expected_count} queries, got #{@count}\n#{@queries.join("\n")}"
end

def negative_failure_message
"expected query count not to be #{$expected_count}"
"expected query count not to be #{@expected_count}"
end
end
32 changes: 14 additions & 18 deletions spec/finders/activerecord_test_connector.rb
Expand Up @@ -3,7 +3,18 @@
require 'active_record/fixtures'
require 'active_support/multibyte' # needed for Ruby 1.9.1

$query_count = $query_sql = nil
$query_count = 0
$query_sql = []

ignore_sql = /^(?:PRAGMA|SELECT (?:currval|CAST|@@IDENTITY|@@ROWCOUNT)|SHOW FIELDS)\b|\bFROM sqlite_master\b/

ActiveSupport::Notifications.subscribe(/^sql\./) do |*args|
payload = args.last
unless payload[:name] =~ /^Fixture/ or payload[:sql] =~ ignore_sql
$query_count += 1
$query_sql << payload[:sql]
end
end

module ActiverecordTestConnector
extend self
Expand All @@ -13,6 +24,8 @@ module ActiverecordTestConnector

FIXTURES_PATH = File.expand_path('../../fixtures', __FILE__)

Fixtures = defined?(ActiveRecord::Fixtures) ? ActiveRecord::Fixtures : ::Fixtures

# Set our defaults
self.connected = false
self.able_to_connect = true
Expand Down Expand Up @@ -48,7 +61,6 @@ def setup_connection

ActiveRecord::Base.configurations = { db => configuration }
ActiveRecord::Base.establish_connection(db)
prepare ActiveRecord::Base.connection
end

def load_schema
Expand All @@ -57,22 +69,6 @@ def load_schema
load File.join(FIXTURES_PATH, 'schema.rb')
end
end

def prepare(conn)
class << conn
IGNORED_SQL = /^(?:PRAGMA|SELECT (?:currval|CAST|@@IDENTITY|@@ROWCOUNT)|SHOW FIELDS)\b/

def execute_with_counting(sql, name = nil, &block)
if $query_count and IGNORED_SQL !~ sql
$query_count += 1
$query_sql << sql
end
execute_without_counting(sql, name, &block)
end

alias_method_chain :execute, :counting
end
end

module FixtureSetup
def fixtures(*tables)
Expand Down
10 changes: 5 additions & 5 deletions spec/fixtures/replies.yml
Expand Up @@ -2,28 +2,28 @@ witty_retort:
id: 1
topic_id: 1
content: Birdman is better!
created_at: <%= 6.hours.ago.to_s(:db) %>
created_at: <%= 6.hours.ago.utc.to_s(:db) %>

another:
id: 2
topic_id: 2
content: Nuh uh!
created_at: <%= 1.hour.ago.to_s(:db) %>
created_at: <%= 1.hour.ago.utc.to_s(:db) %>

spam:
id: 3
topic_id: 1
content: Nice site!
created_at: <%= 1.hour.ago.to_s(:db) %>
created_at: <%= 1.hour.ago.utc.to_s(:db) %>

decisive:
id: 4
topic_id: 4
content: "I'm getting to the bottom of this"
created_at: <%= 30.minutes.ago.to_s(:db) %>
created_at: <%= 30.minutes.ago.utc.to_s(:db) %>

brave:
id: 5
topic_id: 4
content: "AR doesn't scare me a bit"
created_at: <%= 10.minutes.ago.to_s(:db) %>
created_at: <%= 10.minutes.ago.utc.to_s(:db) %>

0 comments on commit f703f88

Please sign in to comment.