Class: ActionDispatch::Routing::RouteSet::NamedRouteCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/lazy_named_routes_helpers/routing_monkey_patches_rails_3_0.rb

Instance Method Summary collapse

Instance Method Details

#add(name, route) ⇒ Object Also known as: []=



52
53
54
55
# File 'lib/lazy_named_routes_helpers/routing_monkey_patches_rails_3_0.rb', line 52

def add(name, route)
  routes[name.to_sym] = route
  # define_named_route_methods(name, route)
end

#add_generated_code(code, tag, file, line) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/lazy_named_routes_helpers/routing_monkey_patches_rails_3_0.rb', line 60

def add_generated_code(code, tag, file, line)
  if $generated_code
    # $generated_code.puts "# route: #{@requirements.inspect}"
    $generated_code.puts code
    $generated_code.puts
    $generated_code.flush
  end
  # We use module_eval to avoid leaks
  @module.module_eval code, "generated code/#{tag}/(#{file}:#{line})"
end

#clear!Object Also known as: clear



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
# File 'lib/lazy_named_routes_helpers/routing_monkey_patches_rails_3_0.rb', line 19

def clear!
  # puts "patched clear! (#{object_id})"
  @routes = {}
  @helpers = []

  @module ||= Module.new do
    instance_methods.each { |selector| remove_method(selector) }
  end

  # puts "installing method_missing handler for named routes"
  @module.module_eval <<-'end_code', __FILE__, __LINE__ + 1
    private
    def __named_routes__; Rails.application.routes.named_routes; end
    def method_missing(method_name, *args, &block)
      begin
        # puts "missing url helper method #{method_name}"
        super
      rescue NameError
        raise unless method_name.to_s =~ /^(.+)\_(url|path)$/
        # puts "redefining #{method_name}"
        name, kind = $1.to_sym, $2.to_sym
        raise "route not found: #{name}" unless route = __named_routes__[name]
        opts = {:only_path => (kind == :path)}
        hash = route.defaults.merge(:use_route => name).merge(opts)
        __named_routes__.__send__(:define_hash_access, route, name, kind, hash)
        __named_routes__.__send__(:define_url_helper, route, name, kind, hash)
        __send__(method_name, *args, &block)
      end
    end
  end_code
end

#define_hash_access(route, name, kind, options) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/lazy_named_routes_helpers/routing_monkey_patches_rails_3_0.rb', line 71

def define_hash_access(route, name, kind, options)
  selector = hash_access_name(name, kind)
  code = <<-END_EVAL
  def #{selector}(options = nil)
    options ? #{options.inspect}.merge(options) : #{options.inspect}
  end
  protected :#{selector}
  END_EVAL
  add_generated_code code, "hash_access", __FILE__, __LINE__
  helpers << selector
end

#define_url_helper(route, name, kind, options) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/lazy_named_routes_helpers/routing_monkey_patches_rails_3_0.rb', line 83

def define_url_helper(route, name, kind, options)
  selector = url_helper_name(name, kind)
  hash_access_method = hash_access_name(name, kind)
  code = <<-END_EVAL
  def #{selector}(*args)
    options = #{hash_access_method}(args.extract_options!)
    if args.any?
      options[:_positional_args] = args
      options[:_positional_keys] = #{route.segment_keys.inspect}
    end
    url_for(options)
  end
  END_EVAL
  add_generated_code code, "url_helper", __FILE__, __LINE__
  helpers << selector
end

#helper_method?(method_name) ⇒ Boolean

new method needed for test processing

Returns:

  • (Boolean)


14
15
16
17
# File 'lib/lazy_named_routes_helpers/routing_monkey_patches_rails_3_0.rb', line 14

def helper_method?(method_name)
  return false unless method_name.to_s =~ /^(.+)\_(url|path)$/
  get($1) != nil
end