Skip to content

Commit

Permalink
fix spec/console and show nice SQL output
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Aug 9, 2010
1 parent c764e8b commit dfc3044
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
9 changes: 5 additions & 4 deletions spec/console
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env ruby
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
libs = []
libs = %w[rubygems irb/completion console_fixtures]

libs << 'irb/completion'
libs << 'console_fixtures'
args = libs.map { |lib| ['-r', lib] }.flatten
args << '--simple-prompt'

exec "#{irb} -Ilib:spec#{libs.map{ |l| " -r #{l}" }.join} --simple-prompt"
ENV['RUBYLIB'] = 'lib:spec'
exec irb, *args
23 changes: 22 additions & 1 deletion spec/console_fixtures.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
require 'bundler'
Bundler.setup

require 'will_paginate/finders/active_record'
require 'finders/activerecord_test_connector'

ActiverecordTestConnector.setup

windows = RUBY_PLATFORM =~ /(:?mswin|mingw)/
# used just for the `color` method
log_subscriber = ActiveSupport::LogSubscriber.log_subscribers.first

IGNORE_SQL = /\b(sqlite_master|sqlite_version)\b|^CREATE TABLE/

ActiveSupport::Notifications.subscribe(/^sql\./) do |*args|
data = args.last
unless data[:name] =~ /^Fixture/ or data[:sql] =~ IGNORE_SQL
if windows
puts data[:sql]
else
puts log_subscriber.send(:color, data[:sql], :cyan)
end
end
end

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

ActiverecordTestConnector.show_sql
WillPaginate::Finders::ActiveRecord.enable!
8 changes: 1 addition & 7 deletions spec/finders/activerecord_test_connector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def self.setup_connection
raise "no configuration for '#{db}'" unless configurations.key? db
configuration = configurations[db]

ActiveRecord::Base.logger = Logger.new(STDOUT) if $0 == 'irb'
# ActiveRecord::Base.logger = Logger.new(STDOUT) if $0 == 'irb'
puts "using #{configuration['adapter']} adapter"

ActiveRecord::Base.configurations = { db => configuration }
Expand Down Expand Up @@ -72,12 +72,6 @@ def execute_with_counting(sql, name = nil, &block)
end
end

def self.show_sql
ActiveSupport::Notifications.subscribe('active_record.sql') do |*args|
puts args.last[:sql]
end
end

module FixtureSetup
def fixtures(*tables)
table_names = tables.map { |t| t.to_s }
Expand Down

0 comments on commit dfc3044

Please sign in to comment.