Skip to content

Commit

Permalink
updated spec for rails2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbuddy committed May 18, 2010
1 parent e51c7b6 commit b422edd
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 28 deletions.
14 changes: 6 additions & 8 deletions Rakefile
Expand Up @@ -9,7 +9,7 @@ YARD::Rake::YardocTask.new do |t|
t.options = ['--markup=markdown'] # optional t.options = ['--markup=markdown'] # optional
end 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 namespace(:spec) do
Spec::Rake::SpecTask.new(:private) do |t| Spec::Rake::SpecTask.new(:private) do |t|
t.spec_opts ||= [] t.spec_opts ||= []
Expand All @@ -24,7 +24,7 @@ namespace(:spec) do
sh('unzip -qq spec/rails2_2/vendor.zip -dspec/rails2_2') sh('unzip -qq spec/rails2_2/vendor.zip -dspec/rails2_2')
end end


Spec::Rake::SpecTask.new(:spec) do |t| Spec::Rake::SpecTask.new(:only_spec) do |t|
t.spec_opts ||= [] t.spec_opts ||= []
t.spec_opts << "-rubygems" t.spec_opts << "-rubygems"
t.spec_opts << "--options" << "spec/spec.opts" t.spec_opts << "--options" << "spec/spec.opts"
Expand All @@ -34,9 +34,8 @@ namespace(:spec) do
task :cleanup do task :cleanup do
sh('rm -rf spec/rails2_2/vendor') sh('rm -rf spec/rails2_2/vendor')
end end


task :spec => :unzip task :spec => [:unzip, :only_spec, :cleanup]
task :cleanup => :spec
end end


namespace(:rails2_3) do namespace(:rails2_3) do
Expand All @@ -45,7 +44,7 @@ namespace(:spec) do
sh('unzip -qq spec/rails2_3/vendor.zip -dspec/rails2_3') sh('unzip -qq spec/rails2_3/vendor.zip -dspec/rails2_3')
end end


Spec::Rake::SpecTask.new(:spec) do |t| Spec::Rake::SpecTask.new(:only_spec) do |t|
t.spec_opts ||= [] t.spec_opts ||= []
t.spec_opts << "-rubygems" t.spec_opts << "-rubygems"
t.spec_opts << "--options" << "spec/spec.opts" t.spec_opts << "--options" << "spec/spec.opts"
Expand All @@ -55,8 +54,7 @@ namespace(:spec) do
sh('rm -rf spec/rails2_3/vendor') sh('rm -rf spec/rails2_3/vendor')
end end


task :spec => :unzip task :spec => [:unzip, :only_spec, :cleanup]
task :cleanup => :spec
end end




Expand Down
26 changes: 19 additions & 7 deletions lib/usher/interface/rails23.rb
Expand Up @@ -93,11 +93,11 @@ def install_helpers(destinations = [ActionController::Base, ActionView::Base], r
Array(destinations).each do |d| d.module_eval { include Helpers } Array(destinations).each do |d| d.module_eval { include Helpers }
@router.named_routes.keys.each do |name| @router.named_routes.keys.each do |name|
@module.module_eval <<-end_eval # We use module_eval to avoid leaks @module.module_eval <<-end_eval # We use module_eval to avoid leaks
def #{name}_url(options = {}) def #{name}_url(*args)
ActionController::Routing::Routes.generate(options, {}, :generate, :#{name}) UsherRailsRouter.generate(args, {}, :generate, :#{name})
end end
def #{name}_path(options = {}) def #{name}_path(*args)
ActionController::Routing::Routes.generate(options, {}, :generate, :#{name}) UsherRailsRouter.generate(args, {}, :generate, :#{name})
end end
end_eval end_eval
end end
Expand All @@ -107,12 +107,24 @@ def helpers
{ } { }
end end
" "
unless @module.const_defined?(:UsherRailsRouter)
@module.const_set(:UsherRailsRouter, self)
end

@router.named_routes.helpers.__send__(:extend, @module) @router.named_routes.helpers.__send__(:extend, @module)
end end
end end


def generate(options, recall = {}, method = :generate, route_name = nil) def generate(args, recall = {}, method = :generate, route_name = nil)
route = if(route_name) 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] @router.named_routes[route_name]
else else
merged_options = options merged_options = options
Expand All @@ -125,7 +137,7 @@ def generate(options, recall = {}, method = :generate, route_name = nil)
case method case method
when :generate when :generate
merged_options ||= recall.merge(options) 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.slice!(-1) if url[-1] == ?/
url url
else else
Expand Down
12 changes: 6 additions & 6 deletions lib/usher/node.rb
Expand Up @@ -102,8 +102,8 @@ def route_set
@route_set ||= root.route_set @route_set ||= root.route_set
end end


def find(request_object, original_path, path, params = [], gathered_meta = []) def find(request_object, original_path, path, params = [], gathered_meta = nil)
gathered_meta.concat(meta) if meta (gathered_meta ||= []).concat(meta) if meta
# terminates or is partial # terminates or is partial
if terminating_path = pick_terminate(request_object) and (path.empty? || terminating_path.route.partial_match?) if terminating_path = pick_terminate(request_object) and (path.empty? || terminating_path.route.partial_match?)
response = terminating_path.route.partial_match? ? response = terminating_path.route.partial_match? ?
Expand Down Expand Up @@ -147,18 +147,18 @@ def find(request_object, original_path, path, params = [], gathered_meta = [])
elsif request_method_type elsif request_method_type
if route_set.priority_lookups? if route_set.priority_lookups?
route_candidates = [] 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 route_candidates << ret
end 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 route_candidates << ret
end end
route_candidates.sort!{|r1, r2| r1.path.route.priority <=> r2.path.route.priority} route_candidates.sort!{|r1, r2| r1.path.route.priority <=> r2.path.route.priority}
request_method_respond(route_candidates.last, request_method_type) request_method_respond(route_candidates.last, request_method_type)
else 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 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) request_method_respond(ret, request_method_type)
else else
request_method_respond(nil, request_method_type) request_method_respond(nil, request_method_type)
Expand Down
16 changes: 9 additions & 7 deletions lib/usher/util/rails.rb
Expand Up @@ -10,16 +10,18 @@ def self.activate
ActionController::Routing.module_eval "remove_const(:Routes); Routes = Usher::Interface.for(:rails23)" ActionController::Routing.module_eval "remove_const(:Routes); Routes = Usher::Interface.for(:rails23)"


when '2.2' when '2.2'
class Usher::Interface::Rails22::Mapper Usher::Interface::Rails22::Mapper.module_eval("include ActionController::Resources")
include ActionController::Resources
end
ActionController::Routing.module_eval "remove_const(:Routes); Routes = Usher::Interface.for(:rails22)" ActionController::Routing.module_eval "remove_const(:Routes); Routes = Usher::Interface.for(:rails22)"


when '2.0' when '2.0'
class Usher::Interface::Rails20::Mapper Usher::Interface::Rails20::Mapper.module_eval("include ActionController::Resources")
include ActionController::Resources ActionController::Routing.module_eval <<-CODE
end 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 ActionController::Routing.module_eval <<-CODE
remove_const(:Routes); remove_const(:Routes);
interface = Usher::Interface.for(:rails20); interface = Usher::Interface.for(:rails20);
Expand Down
2 changes: 2 additions & 0 deletions spec/rails2_3/compat.rb
@@ -1,2 +1,4 @@
require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper")) require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
require File.expand_path(File.join(File.dirname(__FILE__), 'vendor', 'rails', 'config', 'environment')) require File.expand_path(File.join(File.dirname(__FILE__), 'vendor', 'rails', 'config', 'environment'))

Usher::Util::Rails.activate
8 changes: 8 additions & 0 deletions spec/rails2_3/generate_spec.rb
Expand Up @@ -25,4 +25,12 @@
route_set.generate({:action => 'thingy'}, {:controller => 'sample', :action => 'index', :id => 123}, :generate).should == '/sample/thingy' route_set.generate({:action => 'thingy'}, {:controller => 'sample', :action => 'index', :id => 123}, :generate).should == '/sample/thingy'
end 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 end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
@@ -1,6 +1,8 @@
libdir = File.expand_path("lib") libdir = File.expand_path("lib")
$:.unshift(libdir) unless $:.include?(libdir) $:.unshift(libdir) unless $:.include?(libdir)


require 'usher'

module CallWithMockRequestMixin module CallWithMockRequestMixin
def call_with_mock_request(url = "/sample", method = "GET", params = Hash.new) def call_with_mock_request(url = "/sample", method = "GET", params = Hash.new)
params.merge!(:method => method) params.merge!(:method => method)
Expand Down

0 comments on commit b422edd

Please sign in to comment.