diff options
| author | Joshua Peek <josh@joshpeek.com> | 2010-01-13 20:21:04 -0600 | 
|---|---|---|
| committer | Joshua Peek <josh@joshpeek.com> | 2010-01-13 20:26:01 -0600 | 
| commit | be968ecd8b93f128a01427f76c888c291cbc239b (patch) | |
| tree | 764143daabc9310ef8d81ab1c0fd64280ea3e0b4 /actionpack/lib/action_dispatch | |
| parent | 0f51e45307508d72f65b733dbbbd1c62f27a8bef (diff) | |
| download | rails-be968ecd8b93f128a01427f76c888c291cbc239b.tar.gz rails-be968ecd8b93f128a01427f76c888c291cbc239b.tar.bz2 rails-be968ecd8b93f128a01427f76c888c291cbc239b.zip | |
Respect resources_path_names and :path_names options in new dsl
Diffstat (limited to 'actionpack/lib/action_dispatch')
| -rw-r--r-- | actionpack/lib/action_dispatch/routing/mapper.rb | 129 | ||||
| -rw-r--r-- | actionpack/lib/action_dispatch/routing/route_set.rb | 11 | 
2 files changed, 96 insertions, 44 deletions
| diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index bad90df2e3..277cb48e50 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -247,53 +247,35 @@ module ActionDispatch              options[:controller] = args.first            end -          if path = options.delete(:path) -            path_set = true -            path, @scope[:path] = @scope[:path], Mapper.normalize_path(@scope[:path].to_s + path.to_s) -          else -            path_set = false -          end - -          if name_prefix = options.delete(:name_prefix) -            name_prefix_set = true -            name_prefix, @scope[:name_prefix] = @scope[:name_prefix], (@scope[:name_prefix] ? "#{@scope[:name_prefix]}_#{name_prefix}" : name_prefix) -          else -            name_prefix_set = false -          end +          recover = {} -          if namespace = options.delete(:namespace) -            namespace_set = true -            namespace, @scope[:namespace] = @scope[:namespace], (@scope[:namespace] ? "#{@scope[:namespace]}/#{namespace}" : namespace) -          else -            namespace_set = false +          options[:constraints] ||= {} +          unless options[:constraints].is_a?(Hash) +            block, options[:constraints] = options[:constraints], {}            end -          if controller = options.delete(:controller) -            controller_set = true -            controller, @scope[:controller] = @scope[:controller], (@scope[:namespace] ? "#{@scope[:namespace]}/#{controller}" : controller) -          else -            controller_set = false +          scope_options.each do |option| +            if value = options.delete(option) +              recover[option] = @scope[option] +              @scope[option]  = send("merge_#{option}_scope", @scope[option], value) +            end            end -          constraints = options.delete(:constraints) || {} -          unless constraints.is_a?(Hash) -            block, constraints = constraints, {} -          end +          recover[:block] = @scope[:blocks] +          @scope[:blocks] = merge_blocks_scope(@scope[:blocks], block) -          constraints, @scope[:constraints] = @scope[:constraints], (@scope[:constraints] || {}).merge(constraints) -          blocks, @scope[:blocks] = @scope[:blocks], (@scope[:blocks] || []) + [block] -          options, @scope[:options] = @scope[:options], (@scope[:options] || {}).merge(options) +          recover[:options] = @scope[:options] +          @scope[:options]  = merge_options_scope(@scope[:options], options)            yield            self          ensure -          @scope[:path]        = path        if path_set -          @scope[:name_prefix] = name_prefix if name_prefix_set -          @scope[:namespace]   = namespace   if namespace_set -          @scope[:controller]  = controller  if controller_set -          @scope[:options]     = options -          @scope[:blocks]      = blocks -          @scope[:constraints] = constraints +          scope_options.each do |option| +            @scope[option] = recover[option] if recover.has_key?(option) +          end + +          @scope[:options] = recover[:options] +          @scope[:blocks]  = recover[:block]          end          def controller(controller) @@ -322,6 +304,43 @@ module ActionDispatch            args.push(options)            super(*args)          end + +        private +          def scope_options +            @scope_options ||= private_methods.grep(/^merge_(.+)_scope$/) { $1.to_sym } +          end + +          def merge_path_scope(parent, child) +            Mapper.normalize_path(parent.to_s + child.to_s) +          end + +          def merge_name_prefix_scope(parent, child) +            parent ? "#{parent}_#{child}" : child +          end + +          def merge_namespace_scope(parent, child) +            parent ? "#{parent}/#{child}" : child +          end + +          def merge_controller_scope(parent, child) +            @scope[:namespace] ? "#{@scope[:namespace]}/#{child}" : child +          end + +          def merge_resources_path_names_scope(parent, child) +            merge_options_scope(parent, child) +          end + +          def merge_constraints_scope(parent, child) +            merge_options_scope(parent, child) +          end + +          def merge_blocks_scope(parent, child) +            (parent || []) + [child] +          end + +          def merge_options_scope(parent, child) +            (parent || {}).merge(child) +          end        end        module Resources @@ -391,6 +410,11 @@ module ActionDispatch            end          end +        def initialize(*args) +          super +          @scope[:resources_path_names] = @set.resources_path_names +        end +          def resource(*resources, &block)            options = resources.extract_options! @@ -400,6 +424,13 @@ module ActionDispatch              return self            end +          if path_names = options.delete(:path_names) +            scope(:resources_path_names => path_names) do +              resource(resources, options) +            end +            return self +          end +            resource = SingletonResource.new(resources.pop, options)            if @scope[:scope_level] == :resources @@ -417,8 +448,8 @@ module ActionDispatch                post   "(.:format)",      :to => :create if resource.actions.include?(:create)                put    "(.:format)",      :to => :update if resource.actions.include?(:update)                delete "(.:format)",      :to => :destroy if resource.actions.include?(:destroy) -              get    "/new(.:format)",  :to => :new,  :as => "new_#{resource.singular}" if resource.actions.include?(:new) -              get    "/edit(.:format)", :to => :edit, :as => "edit_#{resource.singular}" if resource.actions.include?(:edit) +              get    "/#{action_path(:new)}(.:format)",  :to => :new,  :as => "new_#{resource.singular}" if resource.actions.include?(:new) +              get    "/#{action_path(:edit)}(.:format)", :to => :edit, :as => "edit_#{resource.singular}" if resource.actions.include?(:edit)              end            end @@ -434,6 +465,13 @@ module ActionDispatch              return self            end +          if path_names = options.delete(:path_names) +            scope(:resources_path_names => path_names) do +              resources(resources, options) +            end +            return self +          end +            resource = Resource.new(resources.pop, options)            if @scope[:scope_level] == :resources @@ -452,7 +490,7 @@ module ActionDispatch                  post "(.:format)", :to => :create  if resource.actions.include?(:create)                  with_exclusive_name_prefix :new do -                  get "/new(.:format)", :to => :new, :as => resource.singular if resource.actions.include?(:new) +                  get "/#{action_path(:new)}(.:format)", :to => :new, :as => resource.singular if resource.actions.include?(:new)                  end                end @@ -463,7 +501,7 @@ module ActionDispatch                    delete "(.:format)", :to => :destroy if resource.actions.include?(:destroy)                    with_exclusive_name_prefix :edit do -                    get "/edit(.:format)", :to => :edit, :as => resource.singular if resource.actions.include?(:edit) +                    get "/#{action_path(:edit)}(.:format)", :to => :edit, :as => resource.singular if resource.actions.include?(:edit)                    end                  end                end @@ -517,13 +555,15 @@ module ActionDispatch              return self            end +          resources_path_names = options.delete(:path_names) +            if args.first.is_a?(Symbol)              action = args.first              if CRUD_ACTIONS.include?(action)                return match("(.:format)", options.merge(:to => action))              else                with_exclusive_name_prefix(action) do -                return match("/#{action}(.:format)", options.merge(:to => action)) +                return match("/#{action_path(action, resources_path_names)}(.:format)", options.merge(:to => action))                end              end            end @@ -550,6 +590,11 @@ module ActionDispatch            end          private +          def action_path(name, path_names = nil) +            path_names ||= @scope[:resources_path_names] +            path_names[name.to_sym] || name.to_s +          end +            def with_exclusive_name_prefix(prefix)              begin                old_name_prefix = @scope[:name_prefix] diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 90893aa0e6..660d28dbec 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -189,12 +189,12 @@ module ActionDispatch              #     options[:use_defaults] = false              #     options.merge!(args)              #   end -            #  +            #              #   url_for(options)              # end              @module.module_eval <<-END_EVAL, __FILE__, __LINE__ + 1                def #{selector}(*args) -                if args.empty? || Hash === args.first  +                if args.empty? || Hash === args.first                    options = #{hash_access_method}(args.first || {})                  else                    options = #{hash_access_method}(args.extract_options!) @@ -225,9 +225,16 @@ module ActionDispatch        attr_accessor :routes, :named_routes        attr_accessor :disable_clear_and_finalize +      def self.default_resources_path_names +        { :new => 'new', :edit => 'edit' } +      end + +      attr_accessor :resources_path_names +        def initialize          self.routes = []          self.named_routes = NamedRouteCollection.new +        self.resources_path_names = self.class.default_resources_path_names          @disable_clear_and_finalize = false        end | 
