Skip to content

Commit

Permalink
RouteSet: simplify routes helpers generation code
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan committed Apr 24, 2012
1 parent 242f4d1 commit 15c5060
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions actionpack/lib/action_dispatch/routing/route_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,24 +135,32 @@ def length
end

private
def url_helper_name(name, kind = :url)
:"#{name}_#{kind}"
def url_helper_name(name, only_path)
if only_path
:"#{name}_path"
else
:"#{name}_url"
end
end

def hash_access_name(name, kind = :url)
:"hash_for_#{name}_#{kind}"
def hash_access_name(name, only_path)
if only_path
:"hash_for_#{name}_path"
else
:"hash_for_#{name}_url"
end
end

def define_named_route_methods(name, route)
{:url => {:only_path => false}, :path => {:only_path => true}}.each do |kind, opts|
hash = route.defaults.merge(:use_route => name).merge(opts)
define_hash_access route, name, kind, hash
define_url_helper route, name, kind, hash
[true, false].each do |only_path|
hash = route.defaults.merge(:use_route => name, :only_path => only_path)
define_hash_access route, name, hash
define_url_helper route, name, hash
end
end

def define_hash_access(route, name, kind, options)
selector = hash_access_name(name, kind)
def define_hash_access(route, name, options)
selector = hash_access_name(name, options[:only_path])

@module.module_eval do
remove_possible_method selector
Expand Down Expand Up @@ -187,9 +195,9 @@ def define_hash_access(route, name, kind, options)
#
# foo_url(bar, baz, bang, :sort_by => 'baz')
#
def define_url_helper(route, name, kind, options)
selector = url_helper_name(name, kind)
hash_access_method = hash_access_name(name, kind)
def define_url_helper(route, name, options)
selector = url_helper_name(name, options[:only_path])
hash_access_method = hash_access_name(name, options[:only_path])

if optimize_helper?(route)
@module.module_eval <<-END_EVAL, __FILE__, __LINE__ + 1
Expand Down

0 comments on commit 15c5060

Please sign in to comment.