From 45d8ff08a449d694f33e42ec2f97515d790e3cf7 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Sat, 28 Nov 2009 21:36:58 -0800 Subject: Remove reference to class that doesn't exist (ht: brynary) --- actionpack/lib/action_view.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view.rb b/actionpack/lib/action_view.rb index e95e84aeb5..f6f7ec0c8c 100644 --- a/actionpack/lib/action_view.rb +++ b/actionpack/lib/action_view.rb @@ -25,7 +25,7 @@ require File.join(File.dirname(__FILE__), "action_pack") module ActionView def self.load_all! - [Context, Base, InlineTemplate, TemplateError] + [Context, Base, TemplateError] end autoload :Base, 'action_view/base' -- cgit v1.2.3 From bb84cab2fceb470cd1e7fa1ceeb5e7c4c764f5de Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 29 Nov 2009 01:37:50 -0500 Subject: Update reference to deprecated constant to avoid warnings --- actionpack/lib/action_dispatch/middleware/templates/rescues/_trace.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_dispatch/middleware/templates/rescues/_trace.erb b/actionpack/lib/action_dispatch/middleware/templates/rescues/_trace.erb index f8f6b424ca..07b4919934 100644 --- a/actionpack/lib/action_dispatch/middleware/templates/rescues/_trace.erb +++ b/actionpack/lib/action_dispatch/middleware/templates/rescues/_trace.erb @@ -7,7 +7,7 @@ names = traces.collect {|name, trace| name} %> -

RAILS_ROOT: <%= defined?(RAILS_ROOT) ? RAILS_ROOT : "unset" %>

+

Rails.root: <%= defined?(Rails) && Rails.respond_to?(:root) ? Rails.root : "unset" %>

<% names.each do |name| %> -- cgit v1.2.3 From 3f025e64083c4a0cde4841254a88ef54780824f9 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 29 Nov 2009 15:23:27 -0600 Subject: Resource collection should be defined before member routes --- actionpack/lib/action_dispatch/routing/mapper.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index cfe7425a61..087d4ab478 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -75,18 +75,18 @@ module ActionDispatch with_scope_level(:resources, :name => singular) do yield if block_given? + collection do + get "", :to => :index, :as => plural + post "", :to => :create + get "new", :to => :new, :as => "new_#{singular}" + end + member do get "", :to => :show, :as => singular put "", :to => :update delete "", :to => :destroy get "edit", :to => :edit, :as => "edit_#{singular}" end - - collection do - get "", :to => :index, :as => plural - post "", :to => :create - get "new", :to => :new, :as => "new_#{singular}" - end end end end -- cgit v1.2.3 From 40ae2070d57867bac1c9e7f77c25ac7ac7b5a647 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 29 Nov 2009 16:59:44 -0600 Subject: Extract Resource and SingletonResource helper objects --- actionpack/lib/action_dispatch/routing/mapper.rb | 112 +++++++++++++++++------ 1 file changed, 84 insertions(+), 28 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 087d4ab478..9b00bd3dc0 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -2,6 +2,71 @@ module ActionDispatch module Routing class Mapper module Resources + class Resource #:nodoc: + attr_reader :plural, :singular + attr_reader :path_prefix, :name_prefix + + def initialize(entities, options = {}) + entities = entities.to_s + + @plural = entities.pluralize + @singular = entities.singularize + + @path_prefix = options[:path_prefix] + @name_prefix = options[:name_prefix] + end + + def name + plural + end + + def controller + plural + end + + def member_name + if name_prefix + "#{name_prefix}_#{singular}" + else + singular + end + end + + def collection_name + if name_prefix + "#{name_prefix}_#{plural}" + else + plural + end + end + + def new_name + if name_prefix + "new_#{name_prefix}_#{singular}" + else + "new_#{singular}" + end + end + + def edit_name + if name_prefix + "edit_#{name_prefix}_#{singular}" + else + "edit_#{singular}" + end + end + end + + class SingletonResource < Resource #:nodoc: + def initialize(entity, options = {}) + super(entity) + end + + def name + singular + end + end + def resource(*resources, &block) options = resources.last.is_a?(Hash) ? resources.pop : {} @@ -11,29 +76,27 @@ module ActionDispatch return self end - resource = resources.pop + name_prefix = @scope[:options][:name_prefix] if @scope[:options] + resource = SingletonResource.new(resources.pop, :name_prefix => name_prefix) if @scope[:scope_level] == :resources member do - resource(resource, options, &block) + resource(resource.name, options, &block) end return self end - singular = resource.to_s - plural = singular.pluralize - - controller(plural) do - namespace(resource) do - with_scope_level(:resource) do + controller(resource.controller) do + namespace(resource.name) do + with_scope_level(:resource, :name => resource.singular) do yield if block_given? - get "", :to => :show, :as => "#{singular}" + get "", :to => :show, :as => resource.member_name post "", :to => :create put "", :to => :update delete "", :to => :destroy - get "new", :to => :new, :as => "new_#{singular}" - get "edit", :to => :edit, :as => "edit_#{singular}" + get "new", :to => :new, :as => resource.new_name + get "edit", :to => :edit, :as => resource.edit_name end end end @@ -50,42 +113,35 @@ module ActionDispatch return self end - resource = resources.pop - - plural = resource.to_s - singular = plural.singularize + name_prefix = @scope[:options][:name_prefix] if @scope[:options] + resource = Resource.new(resources.pop, :name_prefix => name_prefix) if @scope[:scope_level] == :resources parent_resource = @scope[:scope_level_options][:name] with_scope_level(:member) do scope(":#{parent_resource}_id", :name_prefix => parent_resource) do - resources(resource, options, &block) + resources(resource.name, options, &block) end end return self end - if @scope[:options] && (prefix = @scope[:options][:name_prefix]) - plural = "#{prefix}_#{plural}" - singular = "#{prefix}_#{singular}" - end - - controller(resource) do - namespace(resource) do - with_scope_level(:resources, :name => singular) do + controller(resource.controller) do + namespace(resource.name) do + with_scope_level(:resources, :name => resource.singular) do yield if block_given? collection do - get "", :to => :index, :as => plural + get "", :to => :index, :as => resource.collection_name post "", :to => :create - get "new", :to => :new, :as => "new_#{singular}" + get "new", :to => :new, :as => resource.new_name end member do - get "", :to => :show, :as => singular + get "", :to => :show, :as => resource.member_name put "", :to => :update delete "", :to => :destroy - get "edit", :to => :edit, :as => "edit_#{singular}" + get "edit", :to => :edit, :as => resource.edit_name end end end -- cgit v1.2.3 From 5da01a92c741b3a9a020a4dec9ddf120c0484e20 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 29 Nov 2009 17:01:14 -0600 Subject: Make use of extract_options! --- actionpack/lib/action_dispatch/routing/mapper.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 9b00bd3dc0..9ec85daba8 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -68,7 +68,7 @@ module ActionDispatch end def resource(*resources, &block) - options = resources.last.is_a?(Hash) ? resources.pop : {} + options = resources.extract_options! if resources.length > 1 raise ArgumentError if block_given? @@ -105,7 +105,7 @@ module ActionDispatch end def resources(*resources, &block) - options = resources.last.is_a?(Hash) ? resources.pop : {} + options = resources.extract_options! if resources.length > 1 raise ArgumentError if block_given? @@ -173,7 +173,7 @@ module ActionDispatch end def match(*args) - options = args.last.is_a?(Hash) ? args.pop : {} + options = args.extract_options! args.push(options) case options.delete(:on) @@ -203,7 +203,7 @@ module ActionDispatch module Scoping def scope(*args) - options = args.last.is_a?(Hash) ? args.pop : {} + options = args.extract_options! constraints = options.delete(:constraints) || {} unless constraints.is_a?(Hash) @@ -300,7 +300,7 @@ module ActionDispatch end def match(*args) - options = args.last.is_a?(Hash) ? args.pop : {} + options = args.extract_options! if args.length > 1 args.each { |path| match(path, options) } @@ -384,7 +384,7 @@ module ActionDispatch private def map_method(method, *args, &block) - options = args.last.is_a?(Hash) ? args.pop : {} + options = args.extract_options! options[:via] = method args.push(options) match(*args, &block) -- cgit v1.2.3 From 312c3bfa247249a1562eb9d04c335f4d38a18b28 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 29 Nov 2009 17:39:37 -0600 Subject: Break down long match routing method --- actionpack/lib/action_dispatch/routing/mapper.rb | 76 +++++++++++++++++------- 1 file changed, 53 insertions(+), 23 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 9ec85daba8..57bbb55c1c 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -202,6 +202,12 @@ module ActionDispatch end module Scoping + def self.extended(object) + object.instance_eval do + @scope = {} + end + end + def scope(*args) options = args.extract_options! @@ -249,9 +255,24 @@ module ActionDispatch def constraints(constraints = {}) scope(:constraints => constraints) { yield } end + + def match(*args) + options = args.extract_options! + options = (@scope[:options] || {}).merge(options) + args.push(options) + super(*args) + end end class Constraints + def new(app, constraints = []) + if constraints.any? + super(app, constraints) + else + app + end + end + def initialize(app, constraints = []) @app, @constraints = app, constraints end @@ -273,7 +294,6 @@ module ActionDispatch def initialize(set) @set = set - @scope = {} extend Scoping extend Resources @@ -313,7 +333,6 @@ module ActionDispatch path = args.first - options = (@scope[:options] || {}).merge(options) conditions, defaults = {}, {} path = nil if path == "" @@ -345,29 +364,12 @@ module ActionDispatch conditions[:request_method] = Regexp.union(*via) end - defaults[:controller] = @scope[:controller].to_s if @scope[:controller] - - if options[:to].respond_to?(:call) - app = options[:to] - defaults.delete(:controller) - defaults.delete(:action) - elsif options[:to].is_a?(String) - defaults[:controller], defaults[:action] = options[:to].split('#') - elsif options[:to].is_a?(Symbol) - defaults[:action] = options[:to].to_s - end - app ||= Routing::RouteSet::Dispatcher.new(:defaults => defaults) + defaults[:controller] ||= @scope[:controller].to_s if @scope[:controller] - if app.is_a?(Routing::RouteSet::Dispatcher) - unless defaults.include?(:controller) || segment_keys.include?("controller") - raise ArgumentError, "missing :controller" - end - unless defaults.include?(:action) || segment_keys.include?("action") - raise ArgumentError, "missing :action" - end - end + app = initialize_app_endpoint(options, defaults) + validate_defaults!(app, defaults, segment_keys) + app = Constraints.new(app, blocks) - app = Constraints.new(app, blocks) if blocks.any? @set.add_route(app, conditions, requirements, defaults, options[:as]) self @@ -383,6 +385,34 @@ module ActionDispatch end private + def initialize_app_endpoint(options, defaults) + app = nil + + if options[:to].respond_to?(:call) + app = options[:to] + defaults.delete(:controller) + defaults.delete(:action) + elsif options[:to].is_a?(String) + defaults[:controller], defaults[:action] = options[:to].split('#') + elsif options[:to].is_a?(Symbol) + defaults[:action] = options[:to].to_s + end + + app || Routing::RouteSet::Dispatcher.new(:defaults => defaults) + end + + def validate_defaults!(app, defaults, segment_keys) + return unless app.is_a?(Routing::RouteSet::Dispatcher) + + unless defaults.include?(:controller) || segment_keys.include?("controller") + raise ArgumentError, "missing :controller" + end + + unless defaults.include?(:action) || segment_keys.include?("action") + raise ArgumentError, "missing :action" + end + end + def map_method(method, *args, &block) options = args.extract_options! options[:via] = method -- cgit v1.2.3 From f69f9820ee84f32bb53d001efd6ebc79517fb0e1 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 29 Nov 2009 17:45:12 -0600 Subject: Wrap up http related routing helpers --- actionpack/lib/action_dispatch/routing/mapper.rb | 70 +++++++++++++----------- 1 file changed, 37 insertions(+), 33 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 57bbb55c1c..8dfac30ac0 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -264,6 +264,42 @@ module ActionDispatch end end + module HttpHelpers + def get(*args, &block) + map_method(:get, *args, &block) + end + + def post(*args, &block) + map_method(:post, *args, &block) + end + + def put(*args, &block) + map_method(:put, *args, &block) + end + + def delete(*args, &block) + map_method(:delete, *args, &block) + end + + def redirect(path, options = {}) + status = options[:status] || 301 + lambda { |env| + req = Rack::Request.new(env) + url = req.scheme + '://' + req.host + path + [status, {'Location' => url, 'Content-Type' => 'text/html'}, ['Moved Permanently']] + } + end + + private + def map_method(method, *args, &block) + options = args.extract_options! + options[:via] = method + args.push(options) + match(*args, &block) + self + end + end + class Constraints def new(app, constraints = []) if constraints.any? @@ -295,26 +331,11 @@ module ActionDispatch def initialize(set) @set = set + extend HttpHelpers extend Scoping extend Resources end - def get(*args, &block) - map_method(:get, *args, &block) - end - - def post(*args, &block) - map_method(:post, *args, &block) - end - - def put(*args, &block) - map_method(:put, *args, &block) - end - - def delete(*args, &block) - map_method(:delete, *args, &block) - end - def root(options = {}) match '/', options.merge(:as => :root) end @@ -375,15 +396,6 @@ module ActionDispatch self end - def redirect(path, options = {}) - status = options[:status] || 301 - lambda { |env| - req = Rack::Request.new(env) - url = req.scheme + '://' + req.host + path - [status, {'Location' => url, 'Content-Type' => 'text/html'}, ['Moved Permanently']] - } - end - private def initialize_app_endpoint(options, defaults) app = nil @@ -412,14 +424,6 @@ module ActionDispatch raise ArgumentError, "missing :action" end end - - def map_method(method, *args, &block) - options = args.extract_options! - options[:via] = method - args.push(options) - match(*args, &block) - self - end end end end -- cgit v1.2.3 From 075f50d62cd02c2cc14b145cdb34bc9ee85cc83c Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 29 Nov 2009 18:17:14 -0600 Subject: Fix some nested resource generation tests --- actionpack/lib/action_dispatch/routing/mapper.rb | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 8dfac30ac0..34d75e55b6 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -80,15 +80,19 @@ module ActionDispatch resource = SingletonResource.new(resources.pop, :name_prefix => name_prefix) if @scope[:scope_level] == :resources - member do - resource(resource.name, options, &block) + parent_resource = @scope[:scope_level_options][:name] + parent_named_prefix = @scope[:scope_level_options][:name_prefix] + with_scope_level(:member) do + scope(":#{parent_resource}_id", :name_prefix => parent_named_prefix) do + resource(resource.name, options, &block) + end end return self end controller(resource.controller) do namespace(resource.name) do - with_scope_level(:resource, :name => resource.singular) do + with_scope_level(:resource, :name => resource.singular, :name_prefix => resource.member_name) do yield if block_given? get "", :to => :show, :as => resource.member_name @@ -118,8 +122,9 @@ module ActionDispatch if @scope[:scope_level] == :resources parent_resource = @scope[:scope_level_options][:name] + parent_named_prefix = @scope[:scope_level_options][:name_prefix] with_scope_level(:member) do - scope(":#{parent_resource}_id", :name_prefix => parent_resource) do + scope(":#{parent_resource}_id", :name_prefix => parent_named_prefix) do resources(resource.name, options, &block) end end @@ -128,7 +133,7 @@ module ActionDispatch controller(resource.controller) do namespace(resource.name) do - with_scope_level(:resources, :name => resource.singular) do + with_scope_level(:resources, :name => resource.singular, :name_prefix => resource.member_name) do yield if block_given? collection do -- cgit v1.2.3