Report abuse

Default to 'HTML'


			
# Update the define_url_helper method to append the 'html' format if it's required and not provided
module ActionController
  module Routing
    class RouteSet
      class NamedRouteCollection
        def define_url_helper(route, name, kind, options)
          selector = url_helper_name(name, kind)

          # The segment keys used for positional paramters
          segment_keys = route.segments.collect do |segment|
            segment.key if segment.respond_to? :key
          end.compact
          hash_access_method = hash_access_name(name, kind)
          @module.send :module_eval, <<-end_eval # We use module_eval to avoid leaks
            def #{selector}(*args)
              opts = if args.empty? || Hash === args.first
                tmp = args.first || {}
                #{'tmp[:format] = \'html\'' if segment_keys.include?(:format)}
                tmp
              else
                #{'args << \'html\' unless args.include?(\'html\')' if segment_keys.include?(:format)}
                args.zip(#{segment_keys.inspect}).inject({}) do |h, (v, k)|
                  h[k] = v
                  h
                end
              end
              url_for(#{hash_access_method}(opts))
            end
          end_eval
          @module.send(:protected, selector)
          helpers << selector
        end
      end
    end
  end
end

Define only the routes we want. No backwards compatibility.


			
# Overwriting the resource module to setup only the routes that we want to use.
# we also append the .:format value where necessary
module ActionController
  module Resources
    private

      # Handled setting up the action for :collection => { } actions.
      def map_collection_actions(map, resource)
        resource.collection_methods.each do |method, actions|
          actions.each do |action|
            action_options = action_options_for(action, resource, method)
            map.named_route("#{action}_#{resource.name_prefix}#{resource.plural}", "#{resource.path}#{resource.action_separator}#{action}.:format", action_options)
            # this is the ;action route (ie: /widgets/1;featured). I don't like it, so I've commented it out.
            # the new alternative is /widgets/1/featured which I prefer. It's defined on the line right above this
            # map.connect("#{resource.path};#{action}.:format", action_options)
          end
        end
      end

      def map_default_collection_actions(map, resource)
        # index route. /widgets.:format
        map.named_route("#{resource.name_prefix}#{resource.plural}", "#{resource.path}.:format", action_options_for("index", resource))
        # create route. we don't bother with .:format because it's never displayed to the client
        map.connect(resource.path, action_options_for("create", resource))
      end

      # create the default routes for a singleton. 
      def map_default_singleton_actions(map, resource)
        # we don't bother with .:format because it's never displayed to the client
        map.connect(resource.path, action_options_for("create", resource))
      end

      # Handled setting up the action for :new => { } actions. 'new' is a default new action
      def map_new_actions(map, resource)
        resource.new_methods.each do |method, actions|
          actions.each do |action|
            action_options = action_options_for(action, resource, method)
            if action == :new
              # setup the basic 'new' route. ie: new_widget => /widgets/new.:format
              map.named_route("new_#{resource.name_prefix}#{resource.singular}", "#{resource.new_path}.:format", action_options)
            else
              map.named_route("#{action}_new_#{resource.name_prefix}#{resource.singular}", "#{resource.new_path}#{resource.action_separator}#{action}.:format", action_options)
              # this is the ;action route (ie: /widgets/1;edit). I don't like it, so I've commented it out.
              # the new alternative is /widgets/1/edit which I prefer. It's defined on the line right above this
              # map.connect("#{resource.new_path};#{action}", action_options)
            end
          end
        end
      end

      # Handled setting up the action for :member => { } actions. 'edit' is a default member action
      def map_member_actions(map, resource)
        resource.member_methods.each do |method, actions|
          actions.each do |action|
            # setup the additional member action routes. ie: edit_widget => /widget/:id/edit
            map.named_route(
              "#{action}_#{resource.name_prefix}#{resource.singular}", 
              "#{resource.member_path}#{resource.action_separator}#{action}.:format", 
              action_options_for(action, resource, method)
            )
          end
        end

        # setup the singular named route. ie: widget
        # this ends up being widget_path(widget) => /widget/:id.:format
        map.named_route("#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}.:format", action_options_for("show", resource))

        # Update and delete aren't setup as named routes because they would collide
        # Here we just setup standard routes for the update and destroy methods
        # we don't bother with .:format on update and delete because they are never shown in the browser
        map.connect(resource.member_path, action_options_for("update", resource))
        map.connect(resource.member_path, action_options_for("destroy", resource))
      end

  end
end