Permalink
Browse files

Initial spec tests in place

  • Loading branch information...
Sean Grove
Sean Grove committed Feb 9, 2010
1 parent 8288898 commit 764db7503db501b5f5ddd47ec57543f0f6c16ff3
Showing with 164 additions and 55 deletions.
  1. +40 −24 lib/action_controller_ext.rb
  2. +24 −19 lib/action_view_ext.rb
  3. +17 −12 lib/active_record_ext.rb
  4. +1 −0 lib/spec_storm.rb
  5. +1 −0 spec/debug.log
  6. +13 −0 spec/spec_helper.rb
  7. +68 −0 spec/spec_storm_spec.rb
@@ -1,31 +1,47 @@
ActionController::Base.class_eval do
- alias_method :original_url_for, :url_for
-
- def url_for(options = {})
- options.merge!( {:db_prefix => ActiveRecord::Base.table_name_prefix} ) unless options.class != Hash
- original_url_for options
- end
-
- alias_method :original_process, :process
-
- def process(request, response, method = :perform_action, *arguments) #:nodoc:
- raise StandardError.new("db_prefix cannot be nil in SpecStorm mode!") if request.params[:db_prefix].nil?
- ActiveRecord::Base.table_name_prefix = request.params[:db_prefix]
- ActiveRecord::Base.reset_all_table_names
-
- original_process(request, response, method, *arguments)
+ puts "Not Patching ActionController" if self.const_defined?("SpecStormLoaded")
+ unless self.const_defined?("SpecStormLoaded")
+ puts "Patching ActionController::Base"
+ SpecStormLoaded = true
+
+ alias_method :original_url_for, :url_for
+
+ def url_for(options = {})
+ options.merge!( {:db_prefix => ActiveRecord::Base.table_name_prefix} ) unless options.class != Hash
+ original_url_for options
+ end
+
+ alias_method :original_process, :process
+
+ def process(request, response, method = :perform_action, *arguments) #:nodoc:
+ raise StandardError.new("db_prefix cannot be nil in SpecStorm mode!") if request.params[:db_prefix].nil?
+ ActiveRecord::Base.table_name_prefix = request.params[:db_prefix]
+ ActiveRecord::Base.reset_all_table_names
+
+ original_process(request, response, method, *arguments)
+ end
+
+ alias_method :original_redirect_to, :redirect_to
+
+ def redirect_to(options = {}, response_status = {}) #:doc:
+ raise ActionControllerError.new("Cannot redirect to nil!") if options.nil?
+
+ case options
+ when String
+ options += "?db_prefix=#{ActiveRecord::Base.table_name_prefix}" unless options.include? "db_prefix="
+ end
+
+ original_redirect_to( options, response_status )
+ end
end
+end
- alias_method :original_redirect_to, :redirect_to
-
- def redirect_to(options = {}, response_status = {}) #:doc:
- raise ActionControllerError.new("Cannot redirect to nil!") if options.nil?
+ActionController::UrlRewriter.class_eval do
+ alias_method :original_rewrite_path, :rewrite_path
- case options
- when String
- options += "?db_prefix=#{ActiveRecord::Base.table_name_prefix}" unless options.include? "db_prefix="
- end
+ def rewrite_path(options)
+ options.merge!( {:db_prefix => ActiveRecord::Base.table_name_prefix} ) unless options.class != Hash or options.has_key?(:db_prefix)
- original_redirect_to( options, response_status )
+ original_rewrite_path(options)
end
end
View
@@ -1,26 +1,31 @@
ActionView::Helpers::UrlHelper.class_eval do
- alias_method :original_url_to, :url_for
+ unless self.const_defined?("SpecStormLoaded")
+ puts "Patching ActiveView::Base"
+ SpecStormLoaded = true
+
+ alias_method :original_url_to, :url_for
- def url_for(options = {})
- # TODO: Regex stuff to only append this when necessary
- case options
+ def url_for(options = {})
+ # TODO: Regex stuff to only append this when necessary
+ case options
when String
- return "#{original_url_to( options )}?db_prefix=#{ActiveRecord::Base.table_name_prefix}" unless options.include? "db_prefix="
+ return "#{original_url_to( options )}?db_prefix=#{ActiveRecord::Base.table_name_prefix}" unless options.include? "db_prefix="
+ end
+
+ original_url_to options
end
-
- original_url_to options
- end
-
- alias_method :original_link_to, :link_to
-
- def link_to(*args, &block)
- logger.debug "link_to:\t#{args.inspect}"
-
- arguments = args.join(",")
- if block_given?
- return eval("original_link_to( #{arguments}")
+
+ alias_method :original_link_to, :link_to
+
+ def link_to(*args, &block)
+ logger.debug "link_to:\t#{args.inspect}"
+
+ arguments = args.join(",")
+ if block_given?
+ return eval("original_link_to( #{arguments}")
+ end
+
+ original_link_to *args
end
-
- original_link_to *args
end
end
View
@@ -1,19 +1,24 @@
ActiveRecord::Base.instance_eval do
- def prefix_and_reset_all_table_names_to(prefix)
- ActiveRecord::Base.table_name_prefix = prefix
- ActiveRecord::Base.reset_all_table_names
- end
+ unless self.const_defined?("SpecStormLoaded")
+ puts "Patching ActiveRecord::Base"
+ SpecStormLoaded = true
- def reset_all_table_names
- subclasses.each do |sc|
- sc.reset_table_name
- puts "Reset #{sc}..."
+ def prefix_and_reset_all_table_names_to(prefix)
+ ActiveRecord::Base.table_name_prefix = prefix
+ ActiveRecord::Base.reset_all_table_names
+ end
+
+ def reset_all_table_names
+ subclasses.each do |sc|
+ sc.reset_table_name
+ puts "Reset #{sc}..."
+ end
end
- end
- def show_all_subclasses
- subclasses.each do |sc|
- puts "#{sc} -> #{sc.table_name}"
+ def show_all_subclasses
+ subclasses.each do |sc|
+ puts "#{sc} -> #{sc.table_name}"
+ end
end
end
end
View
@@ -1,4 +1,5 @@
# SpecStorm
+puts "Parsing SpecStorm loader"
module SpecStorm
class SpecStormError < StandardError; end #:nodoc
class NoDBPrefixSpecified < SpecStormError; end #:nodoc
View
@@ -0,0 +1 @@
+# Logfile created on Mon Feb 08 17:36:42 -0800 2010 by logger.rb/22285
View
@@ -0,0 +1,13 @@
+module SpecStorm
+ USE_NAMESPACE_HACK = true
+end
+
+begin
+ require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
+rescue LoadError
+ puts "You need to install rspec in your base app"
+ exit
+end
+
+plugin_spec_dir = File.dirname(__FILE__)
+ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
View
@@ -0,0 +1,68 @@
+require File.dirname(__FILE__) + '/spec_helper'
+
+# For ActiveRecord extension testing
+class Category < ActiveRecord::Base; end
+class Categorization < ActiveRecord::Base; end
+class Smarts < ActiveRecord::Base; end
+class CreditCard < ActiveRecord::Base
+ class PinNumber < ActiveRecord::Base
+ class CvvCode < ActiveRecord::Base; end
+ class SubCvvCode < CvvCode; end
+ end
+ class SubPinNumber < PinNumber; end
+ class Brand < Category; end
+end
+
+
+describe SpecStorm do
+ context "ActionController" do
+ before(:each) do
+ @db_prefix = "dummy_prefix_"
+ set_table_prefix_to @db_prefix
+
+ @request = ActionController::TestRequest.new
+ @params = {}
+ @rewriter = ActionController::UrlRewriter.new(@request, @params)
+ end
+
+ it "should have a blank db prefix if enabled but not set" do
+ clear_table_prefix
+
+ rewritten_url = @rewriter.rewrite(:protocol => 'https://', :controller => 'c', :action => 'a', :id => 'i')
+ rewritten_url.should == "https://test.host/c/a/i?db_prefix="
+ end
+
+ it "should inject a db prefix when generating a url" do
+ rewritten_url = @rewriter.rewrite(:protocol => 'https://', :controller => 'c', :action => 'a', :id => 'i')
+ rewritten_url.should == "https://test.host/c/a/i?db_prefix=dummy_prefix_"
+ end
+
+ it "should not override a pre-existing db_prefix when generating a url" do
+ rewritten_url = @rewriter.rewrite(:protocol => 'https://', :controller => 'c', :action => 'a', :id => 'i', :db_prefix => "preexisting_dummy_prefix_")
+ rewritten_url.should == "https://test.host/c/a/i?db_prefix=preexisting_dummy_prefix_"
+ end
+ end
+
+ context "ActiveRecord" do
+ before(:each) do
+ @db_prefix = "dummy_prefix_"
+ set_table_prefix_to @db_prefix
+ end
+
+ it "should modify database table name prefixes" do
+ puts "#{Category.table_name_prefix}|#{Category.table_name}|#{Category.table_name_suffix}"
+ "#{Category.table_name}".should == "dummy_prefix_categories"
+ end
+ end
+
+ protected
+
+ def set_table_prefix_to db_prefix
+ @db_prefix = db_prefix
+ ActiveRecord::Base.prefix_and_reset_all_table_names_to @db_prefix
+ end
+
+ def clear_table_prefix
+ ActiveRecord::Base.prefix_and_reset_all_table_names_to ""
+ end
+end

0 comments on commit 764db75

Please sign in to comment.