Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 133 lines (114 sloc) 4.526 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
# Thanks to Urligence, you get some free url helpers.
#
# No matter what your controller looks like...
#
# [edit_|new_]object_url # is the equivalent of saying [edit_|new_]post_url(@post)
# [edit_|new_]object_url(some_other_object) # allows you to specify an object, but still maintain any paths or namespaces that are present
#
# collection_url # is like saying posts_url
#
# Url helpers are especially useful when working with polymorphic controllers.
#
# # /posts/1/comments
# object_url #=> /posts/1/comments/#{@comment.to_param}
# object_url(comment) #=> /posts/1/comments/#{comment.to_param}
# edit_object_url #=> /posts/1/comments/#{@comment.to_param}/edit
# collection_url #=> /posts/1/comments
#
# # /products/1/comments
# object_url #=> /products/1/comments/#{@comment.to_param}
# object_url(comment) #=> /products/1/comments/#{comment.to_param}
# edit_object_url #=> /products/1/comments/#{@comment.to_param}/edit
# collection_url #=> /products/1/comments
#
# # /comments
# object_url #=> /comments/#{@comment.to_param}
# object_url(comment) #=> /comments/#{comment.to_param}
# edit_object_url #=> /comments/#{@comment.to_param}/edit
# collection_url #=> /comments
#
# Or with namespaced, nested controllers...
#
# # /admin/products/1/options
# object_url #=> /admin/products/1/options/#{@option.to_param}
# object_url(option) #=> /admin/products/1/options/#{option.to_param}
# edit_object_url #=> /admin/products/1/options/#{@option.to_param}/edit
# collection_url #=> /admin/products/1/options
#
# You get the idea. Everything is automagical! All parameters are inferred.
#
module ResourceController
  module Helpers
    module Urls
      protected
        ['', 'edit_'].each do |type|
          symbol = type.blank? ? nil : type.gsub(/_/, '').to_sym
      
          define_method("#{type}object_url") do |*alternate_object|
            smart_url *object_url_options(symbol, alternate_object.first)
          end
      
          define_method("#{type}object_path") do |*alternate_object|
            smart_path *object_url_options(symbol, alternate_object.first)
          end
      
          define_method("hash_for_#{type}object_url") do |*alternate_object|
            hash_for_smart_url *object_url_options(symbol, alternate_object.first)
          end
      
          define_method("hash_for_#{type}object_path") do |*alternate_object|
            hash_for_smart_path *object_url_options(symbol, alternate_object.first)
          end
        end
    
        def new_object_url
          smart_url *new_object_url_options
        end
    
        def new_object_path
          smart_path *new_object_url_options
        end
    
        def hash_for_new_object_url
          hash_for_smart_url *new_object_url_options
        end
    
        def hash_for_new_object_path
          hash_for_smart_path *new_object_url_options
        end
    
        def collection_url
          smart_url *collection_url_options
        end
    
        def collection_path
          smart_path *collection_url_options
        end
    
        def hash_for_collection_url
          hash_for_smart_url *collection_url_options
        end
    
        def hash_for_collection_path
          hash_for_smart_path *collection_url_options
        end
    
        # Used internally to provide the options to smart_url from Urligence.
        #
        def collection_url_options
          namespaces + [parent_url_options, route_name.to_s.pluralize.to_sym]
        end
    
        # Used internally to provide the options to smart_url from Urligence.
        #
        def object_url_options(action_prefix = nil, alternate_object = nil)
          [action_prefix] + namespaces + [parent_url_options, [route_name.to_sym, alternate_object || object]]
        end
    
        # Used internally to provide the options to smart_url from Urligence.
        #
        def new_object_url_options
          [:new] + namespaces + [parent_url_options, route_name.to_sym]
        end
    
        def parent_url_options
          if parent?
            parent_singleton? ? parent_type.to_sym : [parent_type.to_sym, parent_object]
          else
            nil
          end
        end
    
        # Returns all of the current namespaces of the current controller, symbolized, in array form.
        #
        def namespaces
          names = self.class.name.split("::")
          names.pop
      
          names.map(&:underscore).map(&:to_sym)
        end
    end
  end
end
Something went wrong with that request. Please try again.