Permalink
Browse files

updated spec for rails2.3

  • Loading branch information...
1 parent e51c7b6 commit b422edd2c1db3fa883876c1cba3acdb16c747c47 @joshbuddy committed May 18, 2010
Showing with 52 additions and 28 deletions.
  1. +6 −8 Rakefile
  2. +19 −7 lib/usher/interface/rails23.rb
  3. +6 −6 lib/usher/node.rb
  4. +9 −7 lib/usher/util/rails.rb
  5. +2 −0 spec/rails2_3/compat.rb
  6. +8 −0 spec/rails2_3/generate_spec.rb
  7. +2 −0 spec/spec_helper.rb
View
@@ -9,7 +9,7 @@ YARD::Rake::YardocTask.new do |t|
t.options = ['--markup=markdown'] # optional
end
-task :spec => ['spec:private', 'spec:rails2_2:cleanup', 'spec:rails2_3:cleanup']
+task :spec => ['spec:private', 'spec:rails2_2:spec', 'spec:rails2_3:spec']
namespace(:spec) do
Spec::Rake::SpecTask.new(:private) do |t|
t.spec_opts ||= []
@@ -24,7 +24,7 @@ namespace(:spec) do
sh('unzip -qq spec/rails2_2/vendor.zip -dspec/rails2_2')
end
- Spec::Rake::SpecTask.new(:spec) do |t|
+ Spec::Rake::SpecTask.new(:only_spec) do |t|
t.spec_opts ||= []
t.spec_opts << "-rubygems"
t.spec_opts << "--options" << "spec/spec.opts"
@@ -34,9 +34,8 @@ namespace(:spec) do
task :cleanup do
sh('rm -rf spec/rails2_2/vendor')
end
-
- task :spec => :unzip
- task :cleanup => :spec
+
+ task :spec => [:unzip, :only_spec, :cleanup]
end
namespace(:rails2_3) do
@@ -45,7 +44,7 @@ namespace(:spec) do
sh('unzip -qq spec/rails2_3/vendor.zip -dspec/rails2_3')
end
- Spec::Rake::SpecTask.new(:spec) do |t|
+ Spec::Rake::SpecTask.new(:only_spec) do |t|
t.spec_opts ||= []
t.spec_opts << "-rubygems"
t.spec_opts << "--options" << "spec/spec.opts"
@@ -55,8 +54,7 @@ namespace(:spec) do
sh('rm -rf spec/rails2_3/vendor')
end
- task :spec => :unzip
- task :cleanup => :spec
+ task :spec => [:unzip, :only_spec, :cleanup]
end
@@ -93,11 +93,11 @@ def install_helpers(destinations = [ActionController::Base, ActionView::Base], r
Array(destinations).each do |d| d.module_eval { include Helpers }
@router.named_routes.keys.each do |name|
@module.module_eval <<-end_eval # We use module_eval to avoid leaks
- def #{name}_url(options = {})
- ActionController::Routing::Routes.generate(options, {}, :generate, :#{name})
+ def #{name}_url(*args)
+ UsherRailsRouter.generate(args, {}, :generate, :#{name})
end
- def #{name}_path(options = {})
- ActionController::Routing::Routes.generate(options, {}, :generate, :#{name})
+ def #{name}_path(*args)
+ UsherRailsRouter.generate(args, {}, :generate, :#{name})
end
end_eval
end
@@ -107,12 +107,24 @@ def helpers
{ }
end
"
+ unless @module.const_defined?(:UsherRailsRouter)
+ @module.const_set(:UsherRailsRouter, self)
+ end
+
@router.named_routes.helpers.__send__(:extend, @module)
end
end
- def generate(options, recall = {}, method = :generate, route_name = nil)
- route = if(route_name)
+ def generate(args, recall = {}, method = :generate, route_name = nil)
+ if args.is_a?(Hash)
+ options = args
+ args = nil
+ else
+ args = Array(args)
+ options = args.last.is_a?(Hash) ? args.pop : {}
+ end
+
+ route = if route_name
@router.named_routes[route_name]
else
merged_options = options
@@ -125,7 +137,7 @@ def generate(options, recall = {}, method = :generate, route_name = nil)
case method
when :generate
merged_options ||= recall.merge(options)
- url = generate_url(route, merged_options)
+ url = generate_url(route, args ? args << merged_options : merged_options)
url.slice!(-1) if url[-1] == ?/
url
else
View
@@ -102,8 +102,8 @@ def route_set
@route_set ||= root.route_set
end
- def find(request_object, original_path, path, params = [], gathered_meta = [])
- gathered_meta.concat(meta) if meta
+ def find(request_object, original_path, path, params = [], gathered_meta = nil)
+ (gathered_meta ||= []).concat(meta) if meta
# terminates or is partial
if terminating_path = pick_terminate(request_object) and (path.empty? || terminating_path.route.partial_match?)
response = terminating_path.route.partial_match? ?
@@ -147,18 +147,18 @@ def find(request_object, original_path, path, params = [], gathered_meta = [])
elsif request_method_type
if route_set.priority_lookups?
route_candidates = []
- if specific_node = request[request_object.send(request_method_type)] and ret = specific_node.find(request_object, original_path, path.dup, params.dup, gathered_meta.dup)
+ if specific_node = request[request_object.send(request_method_type)] and ret = specific_node.find(request_object, original_path, path.dup, params.dup, gathered_meta && gathered_meta.dup)
route_candidates << ret
end
- if general_node = request[nil] and ret = general_node.find(request_object, original_path, path.dup, params.dup, gathered_meta.dup)
+ if general_node = request[nil] and ret = general_node.find(request_object, original_path, path.dup, params.dup, gathered_meta && gathered_meta.dup)
route_candidates << ret
end
route_candidates.sort!{|r1, r2| r1.path.route.priority <=> r2.path.route.priority}
request_method_respond(route_candidates.last, request_method_type)
else
- if specific_node = request[request_object.send(request_method_type)] and ret = specific_node.find(request_object, original_path, path.dup, params.dup, gathered_meta.dup)
+ if specific_node = request[request_object.send(request_method_type)] and ret = specific_node.find(request_object, original_path, path.dup, params.dup, gathered_meta && gathered_meta.dup)
ret
- elsif general_node = request[nil] and ret = general_node.find(request_object, original_path, path.dup, params.dup, gathered_meta.dup)
+ elsif general_node = request[nil] and ret = general_node.find(request_object, original_path, path.dup, params.dup, gathered_meta && gathered_meta.dup)
request_method_respond(ret, request_method_type)
else
request_method_respond(nil, request_method_type)
View
@@ -10,16 +10,18 @@ def self.activate
ActionController::Routing.module_eval "remove_const(:Routes); Routes = Usher::Interface.for(:rails23)"
when '2.2'
- class Usher::Interface::Rails22::Mapper
- include ActionController::Resources
- end
+ Usher::Interface::Rails22::Mapper.module_eval("include ActionController::Resources")
ActionController::Routing.module_eval "remove_const(:Routes); Routes = Usher::Interface.for(:rails22)"
when '2.0'
- class Usher::Interface::Rails20::Mapper
- include ActionController::Resources
- end
-
+ Usher::Interface::Rails20::Mapper.module_eval("include ActionController::Resources")
+ ActionController::Routing.module_eval <<-CODE
+ remove_const(:Routes);
+ interface = Usher::Interface.for(:rails20);
+ interface.configuration_file = File.join(RAILS_ROOT, 'config', 'routes.rb')
+ Routes = interface;
+ CODE
+ when '3.0'
ActionController::Routing.module_eval <<-CODE
remove_const(:Routes);
interface = Usher::Interface.for(:rails20);
View
@@ -1,2 +1,4 @@
require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
require File.expand_path(File.join(File.dirname(__FILE__), 'vendor', 'rails', 'config', 'environment'))
+
+Usher::Util::Rails.activate
@@ -25,4 +25,12 @@
route_set.generate({:action => 'thingy'}, {:controller => 'sample', :action => 'index', :id => 123}, :generate).should == '/sample/thingy'
end
+ it "should generate routes based on name with a hash" do
+ route_set.add_named_route(:test, '/:test1/:test2', :controller => 'sample', :action => 'index')
+ generator = Class.new
+ route_set.install_helpers(generator, true)
+ generator.new.test_url('one', 'two').should == '/one/two'
+ generator.new.test_url('one', 'two', :three => 'four').should == '/one/two?three=four'
+ end
+
end
View
@@ -1,6 +1,8 @@
libdir = File.expand_path("lib")
$:.unshift(libdir) unless $:.include?(libdir)
+require 'usher'
+
module CallWithMockRequestMixin
def call_with_mock_request(url = "/sample", method = "GET", params = Hash.new)
params.merge!(:method => method)

0 comments on commit b422edd

Please sign in to comment.