lazy_named_routes_helpers 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ rdoc
2
+ *.gem
3
+ .bundle
4
+ Gemfile.lock
5
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in gc_hacks.gemspec
4
+ gemspec
data/LICENCSE ADDED
@@ -0,0 +1,23 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2009 {XING AG}[http://www.xing.com/]
4
+ Copyright (c) 2009 {Stefan Kaes}[http://railsexpress.de]
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
23
+
data/README.rdoc ADDED
@@ -0,0 +1,27 @@
1
+ = Lazy Routing Helper Generation for Named Routes
2
+
3
+ A plugin for Rails 3.x which defers generation of routing helpers for named routes, such
4
+ as ..._path and ..._url, to when they're first accessed.
5
+
6
+ This can reduce the initial footprint of your Rails application, especially for ruby
7
+ 1.8.7, and and will speed up starting a rails console.
8
+
9
+ == Author
10
+
11
+ Stefan Kaes <skaes@railexpress.de>
12
+
13
+
14
+ == Usage
15
+
16
+ Require the railtie somewhere in application.rb
17
+
18
+ require "lazy_named_routes_helpers/railtie"
19
+
20
+
21
+ == Installation
22
+
23
+ gem install lazy_named_routes_helpers
24
+
25
+ or let bundler manage it.
26
+
27
+
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake'
5
+ require 'rake/testtask'
6
+ require 'rake/rdoctask'
7
+
8
+ Rake::RDocTask.new do |rdoc|
9
+ rdoc.rdoc_dir = 'rdoc'
10
+ rdoc.title = 'lazy_named_routes_helpers'
11
+ rdoc.options << '--line-numbers' << '--inline-source' << '--quiet'
12
+ rdoc.rdoc_files.include('README*')
13
+ rdoc.rdoc_files.include('lib/**/*.rb')
14
+ end
15
+
16
+ task :default => :rdoc
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "lazy_named_routes_helpers/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "lazy_named_routes_helpers"
7
+ s.version = LazyNamedRoutesHelpers::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Stefan Kaes"]
10
+ s.email = ["skaes@railsexpress.de"]
11
+ s.homepage = "https://github.com/skaes/lazy_named_routes_helpers"
12
+ s.summary = "Generate rails helper methods for accessing named routes on demand"
13
+ s.description = "Generate rails helper methods for accessing named routes on demand"
14
+
15
+ s.rubyforge_project = "lazy_named_routes_helpers"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,12 @@
1
+ # load monkey patches to rails routing code according to Rails version
2
+ # log a warning if the rails version is not supported
3
+
4
+ ActiveSupport.on_load(:action_controller) do
5
+ case Rails.version
6
+ when /^3\.0/
7
+ require 'lazy_named_routes_helpers/routing_monkey_patches_rails_3_0'
8
+ else
9
+ Rails.logger.warn "Rails version not compatible with lazy routing helper generation"
10
+ end
11
+ end
12
+
@@ -0,0 +1,133 @@
1
+ # patches for Rails 3.0 to generate named route helpers lazily
2
+ # modified: actionpack/lib/action_dispatch/routing/route_set.rb
3
+ # modified: actionpack/lib/action_dispatch/assertions/routing.rb
4
+ # modified: actionpack/lib/action_view/test_case.rb
5
+
6
+ require 'action_dispatch/routing/route_set'
7
+ module ActionDispatch
8
+ module Routing
9
+ class RouteSet
10
+ class NamedRouteCollection
11
+
12
+ # new method needed for test processing
13
+ def helper_method?(method_name)
14
+ return false unless method_name.to_s =~ /^(.+)\_(url|path)$/
15
+ get($1) != nil
16
+ end
17
+
18
+ def clear!
19
+ # puts "patched clear! (#{object_id})"
20
+ @routes = {}
21
+ @helpers = []
22
+
23
+ @module ||= Module.new do
24
+ instance_methods.each { |selector| remove_method(selector) }
25
+ end
26
+
27
+ # puts "installing method_missing handler for named routes"
28
+ @module.module_eval <<-'end_code', __FILE__, __LINE__ + 1
29
+ private
30
+ def __named_routes__; Rails.application.routes.named_routes; end
31
+ def method_missing(method_name, *args, &block)
32
+ begin
33
+ # puts "missing url helper method #{method_name}"
34
+ super
35
+ rescue NameError
36
+ raise unless method_name.to_s =~ /^(.+)\_(url|path)$/
37
+ # puts "redefining #{method_name}"
38
+ name, kind = $1.to_sym, $2.to_sym
39
+ raise "route not found: #{name}" unless route = __named_routes__[name]
40
+ opts = {:only_path => (kind == :path)}
41
+ hash = route.defaults.merge(:use_route => name).merge(opts)
42
+ __named_routes__.__send__(:define_hash_access, route, name, kind, hash)
43
+ __named_routes__.__send__(:define_url_helper, route, name, kind, hash)
44
+ __send__(method_name, *args, &block)
45
+ end
46
+ end
47
+ end_code
48
+ end #clear!
49
+ alias clear clear!
50
+
51
+ def add(name, route)
52
+ routes[name.to_sym] = route
53
+ # define_named_route_methods(name, route)
54
+ end
55
+ alias []= add
56
+
57
+ $generated_code = File.open("/tmp/generated_routing_code.rb", "w") if ENV['RAILS_DEBUG_ROUTING_CODE'].to_i==1
58
+
59
+ def add_generated_code(code, tag, file, line)
60
+ if $generated_code
61
+ # $generated_code.puts "# route: #{@requirements.inspect}"
62
+ $generated_code.puts code
63
+ $generated_code.puts
64
+ $generated_code.flush
65
+ end
66
+ # We use module_eval to avoid leaks
67
+ @module.module_eval code, "generated code/#{tag}/(#{file}:#{line})"
68
+ end
69
+
70
+ def define_hash_access(route, name, kind, options)
71
+ selector = hash_access_name(name, kind)
72
+ code = <<-END_EVAL
73
+ def #{selector}(options = nil)
74
+ options ? #{options.inspect}.merge(options) : #{options.inspect}
75
+ end
76
+ protected :#{selector}
77
+ END_EVAL
78
+ add_generated_code code, "hash_access", __FILE__, __LINE__
79
+ helpers << selector
80
+ end
81
+
82
+ def define_url_helper(route, name, kind, options)
83
+ selector = url_helper_name(name, kind)
84
+ hash_access_method = hash_access_name(name, kind)
85
+ code = <<-END_EVAL
86
+ def #{selector}(*args)
87
+ options = #{hash_access_method}(args.extract_options!)
88
+ if args.any?
89
+ options[:_positional_args] = args
90
+ options[:_positional_keys] = #{route.segment_keys.inspect}
91
+ end
92
+ url_for(options)
93
+ end
94
+ END_EVAL
95
+ add_generated_code code, "url_helper", __FILE__, __LINE__
96
+ helpers << selector
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
102
+
103
+ if Rails.env.test?
104
+
105
+ require 'action_dispatch/testing/assertions/routing'
106
+ module ActionDispatch
107
+ module Assertions
108
+ module RoutingAssertions
109
+ # ROUTES TODO: These assertions should really work in an integration context
110
+ def method_missing(selector, *args, &block)
111
+ if @controller && @routes && @routes.named_routes.helper_method?(selector)
112
+ @controller.__send__(selector, *args, &block)
113
+ else
114
+ super
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
120
+
121
+ require 'action_view/test_case'
122
+ module ActionView
123
+ class TestCase
124
+ def method_missing(selector, *args, &block)
125
+ if @controller.respond_to?(:_routes) && @controller._routes.named_routes.helper_method?(selector)
126
+ @controller.__send__(selector, *args, &block)
127
+ else
128
+ super
129
+ end
130
+ end
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,3 @@
1
+ module LazyNamedRoutesHelpers
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lazy_named_routes_helpers
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Stefan Kaes
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-02-19 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Generate rails helper methods for accessing named routes on demand
23
+ email:
24
+ - skaes@railsexpress.de
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - LICENCSE
35
+ - README.rdoc
36
+ - Rakefile
37
+ - lazy_named_routes_helpers.gemspec
38
+ - lib/lazy_named_routes_helpers/railtie.rb
39
+ - lib/lazy_named_routes_helpers/routing_monkey_patches_rails_3_0.rb
40
+ - lib/lazy_named_routes_helpers/version.rb
41
+ has_rdoc: true
42
+ homepage: https://github.com/skaes/lazy_named_routes_helpers
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project: lazy_named_routes_helpers
71
+ rubygems_version: 1.3.7
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Generate rails helper methods for accessing named routes on demand
75
+ test_files: []
76
+