diff options
| author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-01-25 10:29:38 -0800 | 
|---|---|---|
| committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-01-30 10:24:57 -0800 | 
| commit | 026c40fc1887b19af0f9a73a616d736f4ac4f5b4 (patch) | |
| tree | 399aac1f26338fd04494be7c609fc91fd89645a5 /actionpack/lib/action_dispatch/routing | |
| parent | 23b2d6069796ebf2e88261ad5667972d8b3f1e4d (diff) | |
| download | rails-026c40fc1887b19af0f9a73a616d736f4ac4f5b4.tar.gz rails-026c40fc1887b19af0f9a73a616d736f4ac4f5b4.tar.bz2 rails-026c40fc1887b19af0f9a73a616d736f4ac4f5b4.zip  | |
use polymorphism to remove conditional
Diffstat (limited to 'actionpack/lib/action_dispatch/routing')
| -rw-r--r-- | actionpack/lib/action_dispatch/routing/route_set.rb | 62 | 
1 files changed, 27 insertions, 35 deletions
diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 3c9ad349e2..e2b2914cad 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -169,7 +169,31 @@ module ActionDispatch            #            class UrlHelper              def self.create(route, options) -              new route, options +              if optimize_helper?(route) +                OptimizedUrlHelper.new(route, options) +              else +                new route, options +              end +            end + +            def self.optimize_helper?(route) +              route.requirements.except(:controller, :action).empty? +            end + +            class OptimizedUrlHelper < UrlHelper +              def initialize(route, options) +                super +              end + +              def call(t, args) +                if args.size == arg_size && !args.last.is_a?(Hash) && optimize_routes_generation?(t) +                  @options.merge!(t.url_options) if t.respond_to?(:url_options) +                  @options[:path] = eval("\"#{optimized_helper}\"") +                  ActionDispatch::Http::URL.url_for(@options) +                else +                  super +                end +              end              end              def initialize(route, options) @@ -178,10 +202,6 @@ module ActionDispatch                @route        = route              end -            def optimize_helper? -              @route.requirements.except(:controller, :action).empty? -            end -              def arg_size                @route.required_parts.size              end @@ -203,20 +223,10 @@ module ActionDispatch                string_route              end -            def url_else(t, args) +            def call(t, args)                t.url_for(handle_positional_args(t, args, @options, @segment_keys))              end -            def url_if(t, args) -              if args.size == arg_size && !args.last.is_a?(Hash) && optimize_routes_generation?(t) -                @options.merge!(t.url_options) if t.respond_to?(:url_options) -                @options[:path] = eval("\"#{optimized_helper}\"") -                ActionDispatch::Http::URL.url_for(@options) -              else -                url_else(t, args) -              end -            end -              def optimize_routes_generation?(t)                t.send(:optimize_routes_generation?)              end @@ -240,35 +250,17 @@ module ActionDispatch            def define_url_helper(route, name, options)              @module.remove_possible_method name -            #@module.module_eval <<-END_EVAL, __FILE__, __LINE__ + 1 -            #  def #{name}(*args) -            #    if #{optimize_helper?(route)} && args.size == #{route.required_parts.size} && !args.last.is_a?(Hash) && optimize_routes_generation? -            #      options = #{options.inspect} -            #      UrlHelp.new.url_if(self, options, "#{optimized_helper(route)}") -            #    else -            #      UrlHelp.new.url_else(self, args, #{options.inspect}, #{route.segment_keys.inspect}) -            #    end -            #  end -            #END_EVAL              helper = UrlHelper.create(route, options.dup) -            ohelp        = helper.optimize_helper? -              @module.module_eval do                define_method(name) do |*args| -                #helper.call t, args -                if ohelp -                  helper.url_if(self, args) -                else -                  helper.url_else(self, args) -                end +                helper.call self, args                end              end              helpers << name            end -        end        attr_accessor :formatter, :set, :named_routes, :default_scope, :router  | 
