Skip to content
This repository
Browse code

Removed deprecated_mapper - we don't need it anymore

  • Loading branch information...
commit faba03850acc8adaf9c51004193c85191a4623e8 1 parent eac8b9c
Łukasz Strzałkowski authored August 09, 2010 drogus committed September 05, 2010
525  actionpack/lib/action_dispatch/routing/deprecated_mapper.rb
... ...
@@ -1,525 +0,0 @@
1  
-require 'active_support/core_ext/object/blank'
2  
-require 'active_support/core_ext/object/with_options'
3  
-require 'active_support/core_ext/object/try'
4  
-
5  
-module ActionDispatch
6  
-  module Routing
7  
-    class RouteSet
8  
-      attr_accessor :controller_namespaces
9  
-
10  
-      CONTROLLER_REGEXP = /[_a-zA-Z0-9]+/
11  
-
12  
-      def controller_constraints
13  
-        @controller_constraints ||= begin
14  
-          namespaces = controller_namespaces + in_memory_controller_namespaces
15  
-          source = namespaces.map { |ns| "#{Regexp.escape(ns)}/#{CONTROLLER_REGEXP.source}" }
16  
-          source << CONTROLLER_REGEXP.source
17  
-          Regexp.compile(source.sort.reverse.join('|'))
18  
-        end
19  
-      end
20  
-
21  
-      def in_memory_controller_namespaces
22  
-        namespaces = Set.new
23  
-        ActionController::Base.descendants.each do |klass|
24  
-          next if klass.anonymous?
25  
-          namespaces << klass.name.underscore.split('/')[0...-1].join('/')
26  
-        end
27  
-        namespaces.delete('')
28  
-        namespaces
29  
-      end
30  
-    end
31  
-
32  
-    class DeprecatedMapper #:nodoc:
33  
-      def initialize(set) #:nodoc:
34  
-        ActiveSupport::Deprecation.warn "You are using the old router DSL which will be removed in Rails 3.1. " <<
35  
-          "Please check how to update your routes file at: http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/"
36  
-        @set = set
37  
-      end
38  
-
39  
-      def connect(path, options = {})
40  
-        options = options.dup
41  
-
42  
-        if conditions = options.delete(:conditions)
43  
-          conditions = conditions.dup
44  
-          method = [conditions.delete(:method)].flatten.compact
45  
-          method.map! { |m|
46  
-            m = m.to_s.upcase
47  
-
48  
-            if m == "HEAD"
49  
-              raise ArgumentError, "HTTP method HEAD is invalid in route conditions. Rails processes HEAD requests the same as GETs, returning just the response headers"
50  
-            end
51  
-
52  
-            unless HTTP_METHODS.include?(m.downcase.to_sym)
53  
-              raise ArgumentError, "Invalid HTTP method specified in route conditions"
54  
-            end
55  
-
56  
-            m
57  
-          }
58  
-
59  
-          if method.length > 1
60  
-            method = Regexp.union(*method)
61  
-          elsif method.length == 1
62  
-            method = method.first
63  
-          else
64  
-            method = nil
65  
-          end
66  
-        end
67  
-
68  
-        path_prefix = options.delete(:path_prefix)
69  
-        name_prefix = options.delete(:name_prefix)
70  
-        namespace  = options.delete(:namespace)
71  
-
72  
-        name = options.delete(:_name)
73  
-        name = "#{name_prefix}#{name}" if name_prefix
74  
-
75  
-        requirements = options.delete(:requirements) || {}
76  
-        defaults = options.delete(:defaults) || {}
77  
-        options.each do |k, v|
78  
-          if v.is_a?(Regexp)
79  
-            if value = options.delete(k)
80  
-              requirements[k.to_sym] = value
81  
-            end
82  
-          else
83  
-            value = options.delete(k)
84  
-            defaults[k.to_sym] = value.is_a?(Symbol) ? value : value.to_param
85  
-          end
86  
-        end
87  
-
88  
-        requirements.each do |_, requirement|
89  
-          if requirement.source =~ %r{\A(\\A|\^)|(\\Z|\\z|\$)\Z}
90  
-            raise ArgumentError, "Regexp anchor characters are not allowed in routing requirements: #{requirement.inspect}"
91  
-          end
92  
-          if requirement.multiline?
93  
-            raise ArgumentError, "Regexp multiline option not allowed in routing requirements: #{requirement.inspect}"
94  
-          end
95  
-        end
96  
-
97  
-        requirements[:controller] ||= @set.controller_constraints
98  
-
99  
-        if defaults[:controller]
100  
-          defaults[:action] ||= 'index'
101  
-          defaults[:controller] = defaults[:controller].to_s
102  
-          defaults[:controller] = "#{namespace}#{defaults[:controller]}" if namespace
103  
-        end
104  
-
105  
-        if defaults[:action]
106  
-          defaults[:action] = defaults[:action].to_s
107  
-        end
108  
-
109  
-        if path.is_a?(String)
110  
-          path = "#{path_prefix}/#{path}" if path_prefix
111  
-          path = path.gsub('.:format', '(.:format)')
112  
-          path = optionalize_trailing_dynamic_segments(path, requirements, defaults)
113  
-          glob = $1.to_sym if path =~ /\/\*(\w+)$/
114  
-          path = ::Rack::Mount::Utils.normalize_path(path)
115  
-
116  
-          if glob && !defaults[glob].blank?
117  
-            raise ActionController::RoutingError, "paths cannot have non-empty default values"
118  
-          end
119  
-        end
120  
-
121  
-        app = Routing::RouteSet::Dispatcher.new(:defaults => defaults, :glob => glob)
122  
-
123  
-        conditions = {}
124  
-        conditions[:request_method] = method if method
125  
-        conditions[:path_info] = path if path
126  
-
127  
-        @set.add_route(app, conditions, requirements, defaults, name)
128  
-      end
129  
-
130  
-      def optionalize_trailing_dynamic_segments(path, requirements, defaults) #:nodoc:
131  
-        path = (path =~ /^\//) ? path.dup : "/#{path}"
132  
-        optional, segments = true, []
133  
-
134  
-        required_segments = requirements.keys
135  
-        required_segments -= defaults.keys.compact
136  
-
137  
-        old_segments = path.split('/')
138  
-        old_segments.shift
139  
-        length = old_segments.length
140  
-
141  
-        old_segments.reverse.each_with_index do |segment, index|
142  
-          required_segments.each do |required|
143  
-            if segment =~ /#{required}/
144  
-              optional = false
145  
-              break
146  
-            end
147  
-          end
148  
-
149  
-          if optional
150  
-            if segment == ":id" && segments.include?(":action")
151  
-              optional = false
152  
-            elsif segment == ":controller" || segment == ":action" || segment == ":id"
153  
-              # Ignore
154  
-            elsif !(segment =~ /^:\w+$/) &&
155  
-                !(segment =~ /^:\w+\(\.:format\)$/)
156  
-              optional = false
157  
-            elsif segment =~ /^:(\w+)$/
158  
-              if defaults.has_key?($1.to_sym)
159  
-                defaults.delete($1.to_sym) if defaults[$1.to_sym].nil?
160  
-              else
161  
-                optional = false
162  
-              end
163  
-            end
164  
-          end
165  
-
166  
-          if optional && index < length - 1
167  
-            segments.unshift('(/', segment)
168  
-            segments.push(')')
169  
-          elsif optional
170  
-            segments.unshift('/(', segment)
171  
-            segments.push(')')
172  
-          else
173  
-            segments.unshift('/', segment)
174  
-          end
175  
-        end
176  
-
177  
-        segments.join
178  
-      end
179  
-      private :optionalize_trailing_dynamic_segments
180  
-
181  
-      # Creates a named route called "root" for matching the root level request.
182  
-      def root(options = {})
183  
-        if options.is_a?(Symbol)
184  
-          if source_route = @set.named_routes.routes[options]
185  
-            options = source_route.defaults.merge({ :conditions => source_route.conditions })
186  
-          end
187  
-        end
188  
-        named_route("root", '', options)
189  
-      end
190  
-
191  
-      def named_route(name, path, options = {}) #:nodoc:
192  
-        options[:_name] = name
193  
-        connect(path, options)
194  
-      end
195  
-
196  
-      def namespace(name, options = {}, &block)
197  
-        if options[:namespace]
198  
-          with_options({:path_prefix => "#{options.delete(:path_prefix)}/#{name}", :name_prefix => "#{options.delete(:name_prefix)}#{name}_", :namespace => "#{options.delete(:namespace)}#{name}/" }.merge(options), &block)
199  
-        else
200  
-          with_options({:path_prefix => name, :name_prefix => "#{name}_", :namespace => "#{name}/" }.merge(options), &block)
201  
-        end
202  
-      end
203  
-
204  
-      def method_missing(route_name, *args, &proc) #:nodoc:
205  
-        super unless args.length >= 1 && proc.nil?
206  
-        named_route(route_name, *args)
207  
-      end
208  
-
209  
-      INHERITABLE_OPTIONS = :namespace, :shallow
210  
-
211  
-      class Resource #:nodoc:
212  
-        DEFAULT_ACTIONS = :index, :create, :new, :edit, :show, :update, :destroy
213  
-
214  
-        attr_reader :collection_methods, :member_methods, :new_methods
215  
-        attr_reader :path_prefix, :name_prefix, :path_segment
216  
-        attr_reader :plural, :singular
217  
-        attr_reader :options, :defaults
218  
-
219  
-        def initialize(entities, options, defaults)
220  
-          @plural   ||= entities
221  
-          @singular ||= options[:singular] || plural.to_s.singularize
222  
-          @path_segment = options.delete(:as) || @plural
223  
-
224  
-          @options = options
225  
-          @defaults = defaults
226  
-
227  
-          arrange_actions
228  
-          add_default_actions
229  
-          set_allowed_actions
230  
-          set_prefixes
231  
-        end
232  
-
233  
-        def controller
234  
-          @controller ||= "#{options[:namespace]}#{(options[:controller] || plural).to_s}"
235  
-        end
236  
-
237  
-        def requirements(with_id = false)
238  
-          @requirements   ||= @options[:requirements] || {}
239  
-          @id_requirement ||= { :id => @requirements.delete(:id) || /[^#{Routing::SEPARATORS.join}]+/ }
240  
-
241  
-          with_id ? @requirements.merge(@id_requirement) : @requirements
242  
-        end
243  
-
244  
-        def conditions
245  
-          @conditions ||= @options[:conditions] || {}
246  
-        end
247  
-
248  
-        def path
249  
-          @path ||= "#{path_prefix}/#{path_segment}"
250  
-        end
251  
-
252  
-        def new_path
253  
-          new_action   = self.options[:path_names][:new] if self.options[:path_names]
254  
-          new_action ||= self.defaults[:path_names][:new]
255  
-          @new_path  ||= "#{path}/#{new_action}"
256  
-        end
257  
-
258  
-        def shallow_path_prefix
259  
-          @shallow_path_prefix ||= @options[:shallow] ? @options[:namespace].try(:sub, /\/$/, '') : path_prefix
260  
-        end
261  
-
262  
-        def member_path
263  
-          @member_path ||= "#{shallow_path_prefix}/#{path_segment}/:id"
264  
-        end
265  
-
266  
-        def nesting_path_prefix
267  
-          @nesting_path_prefix ||= "#{shallow_path_prefix}/#{path_segment}/:#{singular}_id"
268  
-        end
269  
-
270  
-        def shallow_name_prefix
271  
-          @shallow_name_prefix ||= @options[:shallow] ? @options[:namespace].try(:gsub, /\//, '_') : name_prefix
272  
-        end
273  
-
274  
-        def nesting_name_prefix
275  
-          "#{shallow_name_prefix}#{singular}_"
276  
-        end
277  
-
278  
-        def action_separator
279  
-          @action_separator ||= ActionController::Base.resource_action_separator
280  
-        end
281  
-
282  
-        def uncountable?
283  
-          @singular.to_s == @plural.to_s
284  
-        end
285  
-
286  
-        def has_action?(action)
287  
-          !DEFAULT_ACTIONS.include?(action) || action_allowed?(action)
288  
-        end
289  
-
290  
-        protected
291  
-          def arrange_actions
292  
-            @collection_methods = arrange_actions_by_methods(options.delete(:collection))
293  
-            @member_methods     = arrange_actions_by_methods(options.delete(:member))
294  
-            @new_methods        = arrange_actions_by_methods(options.delete(:new))
295  
-          end
296  
-
297  
-          def add_default_actions
298  
-            add_default_action(member_methods, :get, :edit)
299  
-            add_default_action(new_methods, :get, :new)
300  
-          end
301  
-
302  
-          def set_allowed_actions
303  
-            only, except = @options.values_at(:only, :except)
304  
-            @allowed_actions ||= {}
305  
-
306  
-            if only == :all || except == :none
307  
-              only = nil
308  
-              except = []
309  
-            elsif only == :none || except == :all
310  
-              only = []
311  
-              except = nil
312  
-            end
313  
-
314  
-            if only
315  
-              @allowed_actions[:only] = Array(only).map {|a| a.to_sym }
316  
-            elsif except
317  
-              @allowed_actions[:except] = Array(except).map {|a| a.to_sym }
318  
-            end
319  
-          end
320  
-
321  
-          def action_allowed?(action)
322  
-            only, except = @allowed_actions.values_at(:only, :except)
323  
-            (!only || only.include?(action)) && (!except || !except.include?(action))
324  
-          end
325  
-
326  
-          def set_prefixes
327  
-            @path_prefix = options.delete(:path_prefix)
328  
-            @name_prefix = options.delete(:name_prefix)
329  
-          end
330  
-
331  
-          def arrange_actions_by_methods(actions)
332  
-            (actions || {}).inject({}) do |flipped_hash, (key, value)|
333  
-              (flipped_hash[value] ||= []) << key
334  
-              flipped_hash
335  
-            end
336  
-          end
337  
-
338  
-          def add_default_action(collection, method, action)
339  
-            (collection[method] ||= []).unshift(action)
340  
-          end
341  
-      end
342  
-
343  
-      class SingletonResource < Resource #:nodoc:
344  
-        def initialize(entity, options, defaults)
345  
-          @singular = @plural = entity
346  
-          options[:controller] ||= @singular.to_s.pluralize
347  
-          super
348  
-        end
349  
-
350  
-        alias_method :shallow_path_prefix, :path_prefix
351  
-        alias_method :shallow_name_prefix, :name_prefix
352  
-        alias_method :member_path,         :path
353  
-        alias_method :nesting_path_prefix, :path
354  
-      end
355  
-
356  
-      def resources(*entities, &block)
357  
-        options = entities.extract_options!
358  
-        entities.each { |entity| map_resource(entity, options.dup, &block) }
359  
-      end
360  
-
361  
-      def resource(*entities, &block)
362  
-        options = entities.extract_options!
363  
-        entities.each { |entity| map_singleton_resource(entity, options.dup, &block) }
364  
-      end
365  
-
366  
-      private
367  
-        def map_resource(entities, options = {}, &block)
368  
-          resource = Resource.new(entities, options, :path_names => @set.resources_path_names)
369  
-
370  
-          with_options :controller => resource.controller do |map|
371  
-            map_associations(resource, options)
372  
-
373  
-            if block_given?
374  
-              with_options(options.slice(*INHERITABLE_OPTIONS).merge(:path_prefix => resource.nesting_path_prefix, :name_prefix => resource.nesting_name_prefix), &block)
375  
-            end
376  
-
377  
-            map_collection_actions(map, resource)
378  
-            map_default_collection_actions(map, resource)
379  
-            map_new_actions(map, resource)
380  
-            map_member_actions(map, resource)
381  
-          end
382  
-        end
383  
-
384  
-        def map_singleton_resource(entities, options = {}, &block)
385  
-          resource = SingletonResource.new(entities, options, :path_names => @set.resources_path_names)
386  
-
387  
-          with_options :controller => resource.controller do |map|
388  
-            map_associations(resource, options)
389  
-
390  
-            if block_given?
391  
-              with_options(options.slice(*INHERITABLE_OPTIONS).merge(:path_prefix => resource.nesting_path_prefix, :name_prefix => resource.nesting_name_prefix), &block)
392  
-            end
393  
-
394  
-            map_collection_actions(map, resource)
395  
-            map_new_actions(map, resource)
396  
-            map_member_actions(map, resource)
397  
-            map_default_singleton_actions(map, resource)
398  
-          end
399  
-        end
400  
-
401  
-        def map_associations(resource, options)
402  
-          map_has_many_associations(resource, options.delete(:has_many), options) if options[:has_many]
403  
-
404  
-          path_prefix = "#{options.delete(:path_prefix)}#{resource.nesting_path_prefix}"
405  
-          name_prefix = "#{options.delete(:name_prefix)}#{resource.nesting_name_prefix}"
406  
-
407  
-          Array(options[:has_one]).each do |association|
408  
-            resource(association, options.slice(*INHERITABLE_OPTIONS).merge(:path_prefix => path_prefix, :name_prefix => name_prefix))
409  
-          end
410  
-        end
411  
-
412  
-        def map_has_many_associations(resource, associations, options)
413  
-          case associations
414  
-          when Hash
415  
-            associations.each do |association,has_many|
416  
-              map_has_many_associations(resource, association, options.merge(:has_many => has_many))
417  
-            end
418  
-          when Array
419  
-            associations.each do |association|
420  
-              map_has_many_associations(resource, association, options)
421  
-            end
422  
-          when Symbol, String
423  
-            resources(associations, options.slice(*INHERITABLE_OPTIONS).merge(:path_prefix => resource.nesting_path_prefix, :name_prefix => resource.nesting_name_prefix, :has_many => options[:has_many]))
424  
-          else
425  
-          end
426  
-        end
427  
-
428  
-        def map_collection_actions(map, resource)
429  
-          resource.collection_methods.each do |method, actions|
430  
-            actions.each do |action|
431  
-              [method].flatten.each do |m|
432  
-                action_path = resource.options[:path_names][action] if resource.options[:path_names].is_a?(Hash)
433  
-                action_path ||= action
434  
-
435  
-                map_resource_routes(map, resource, action, "#{resource.path}#{resource.action_separator}#{action_path}", "#{action}_#{resource.name_prefix}#{resource.plural}", m)
436  
-              end
437  
-            end
438  
-          end
439  
-        end
440  
-
441  
-        def map_default_collection_actions(map, resource)
442  
-          index_route_name = "#{resource.name_prefix}#{resource.plural}"
443  
-
444  
-          if resource.uncountable?
445  
-            index_route_name << "_index"
446  
-          end
447  
-
448  
-          map_resource_routes(map, resource, :index, resource.path, index_route_name)
449  
-          map_resource_routes(map, resource, :create, resource.path, index_route_name)
450  
-        end
451  
-
452  
-        def map_default_singleton_actions(map, resource)
453  
-          map_resource_routes(map, resource, :create, resource.path, "#{resource.shallow_name_prefix}#{resource.singular}")
454  
-        end
455  
-
456  
-        def map_new_actions(map, resource)
457  
-          resource.new_methods.each do |method, actions|
458  
-            actions.each do |action|
459  
-              route_path = resource.new_path
460  
-              route_name = "new_#{resource.name_prefix}#{resource.singular}"
461  
-
462  
-              unless action == :new
463  
-                route_path = "#{route_path}#{resource.action_separator}#{action}"
464  
-                route_name = "#{action}_#{route_name}"
465  
-              end
466  
-
467  
-              map_resource_routes(map, resource, action, route_path, route_name, method)
468  
-            end
469  
-          end
470  
-        end
471  
-
472  
-        def map_member_actions(map, resource)
473  
-          resource.member_methods.each do |method, actions|
474  
-            actions.each do |action|
475  
-              [method].flatten.each do |m|
476  
-                action_path = resource.options[:path_names][action] if resource.options[:path_names].is_a?(Hash)
477  
-                action_path ||= @set.resources_path_names[action] || action
478  
-
479  
-                map_resource_routes(map, resource, action, "#{resource.member_path}#{resource.action_separator}#{action_path}", "#{action}_#{resource.shallow_name_prefix}#{resource.singular}", m, { :force_id => true })
480  
-              end
481  
-            end
482  
-          end
483  
-
484  
-          route_path = "#{resource.shallow_name_prefix}#{resource.singular}"
485  
-          map_resource_routes(map, resource, :show, resource.member_path, route_path)
486  
-          map_resource_routes(map, resource, :update, resource.member_path, route_path)
487  
-          map_resource_routes(map, resource, :destroy, resource.member_path, route_path)
488  
-        end
489  
-
490  
-        def map_resource_routes(map, resource, action, route_path, route_name = nil, method = nil, resource_options = {} )
491  
-          if resource.has_action?(action)
492  
-            action_options = action_options_for(action, resource, method, resource_options)
493  
-            formatted_route_path = "#{route_path}.:format"
494  
-
495  
-            if route_name && @set.named_routes[route_name.to_sym].nil?
496  
-              map.named_route(route_name, formatted_route_path, action_options)
497  
-            else
498  
-              map.connect(formatted_route_path, action_options)
499  
-            end
500  
-          end
501  
-        end
502  
-
503  
-        def add_conditions_for(conditions, method)
504  
-          {:conditions => conditions.dup}.tap do |options|
505  
-            options[:conditions][:method] = method unless method == :any
506  
-          end
507  
-        end
508  
-
509  
-        def action_options_for(action, resource, method = nil, resource_options = {})
510  
-          default_options = { :action => action.to_s }
511  
-          require_id = !resource.kind_of?(SingletonResource)
512  
-          force_id = resource_options[:force_id] && !resource.kind_of?(SingletonResource)
513  
-
514  
-          case default_options[:action]
515  
-            when "index", "new"; default_options.merge(add_conditions_for(resource.conditions, method || :get)).merge(resource.requirements)
516  
-            when "create";       default_options.merge(add_conditions_for(resource.conditions, method || :post)).merge(resource.requirements)
517  
-            when "show", "edit"; default_options.merge(add_conditions_for(resource.conditions, method || :get)).merge(resource.requirements(require_id))
518  
-            when "update";       default_options.merge(add_conditions_for(resource.conditions, method || :put)).merge(resource.requirements(require_id))
519  
-            when "destroy";      default_options.merge(add_conditions_for(resource.conditions, method || :delete)).merge(resource.requirements(require_id))
520  
-            else                 default_options.merge(add_conditions_for(resource.conditions, method)).merge(resource.requirements(force_id))
521  
-          end
522  
-        end
523  
-    end
524  
-  end
525  
-end

0 notes on commit faba038

Please sign in to comment.
Something went wrong with that request. Please try again.