autolink 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Burke Libbey, Thorkelson Consulting Ltd.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,23 @@
1
+ = autolink
2
+
3
+ Autolink is a small set of monkeypatches to make linking to objects in deeply-namespaced url schemas completely painless. Say you have object @d, which is almost always referred to as /a/b/@d.b.id/c/@d.c.id/d/@d.id. This plugin lets you specify that default in @d's model, then simply pass @d to rails' link helpers. So `path_for @d` now returns '/a/1/b/2/c/3/d/4'.
4
+
5
+ == Installation
6
+
7
+ Autolink only works in Rails 2.3, and has only been tested on 2.3.8. Add it as a gem in config/environment.rb:
8
+
9
+ config.gem 'autolink'
10
+
11
+ Then add this to your 'spec/spec_helper.rb', under the line that requires 'spec/rails':
12
+
13
+ require 'autolink/rspec'
14
+
15
+ Now add this to any models that you want to autolink:
16
+
17
+ def self.default_lineage(object)
18
+ [:path, :to, :model, object.parent, object]
19
+ end
20
+
21
+ This default lineage works exactly like ActionController's PolymorphicRouting lineage (since that's what it is...). The reason it's a class method rather than an instance method is to deal with some sticky mock-related issues.
22
+
23
+ I make no guarantees that this will work for you.
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'rcov/rcovtask'
6
+ Rcov::RcovTask.new do |test|
7
+ test.libs << 'spec'
8
+ test.pattern = 'spec/**/*_spec.rb'
9
+ test.verbose = true
10
+ end
11
+ rescue LoadError
12
+ task :rcov do
13
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
14
+ end
15
+ end
16
+
17
+ require 'rake/rdoctask'
18
+ Rake::RDocTask.new do |rdoc|
19
+ if File.exist?('VERSION')
20
+ version = File.read('VERSION')
21
+ else
22
+ version = ""
23
+ end
24
+
25
+ rdoc.rdoc_dir = 'rdoc'
26
+ rdoc.title = "multilogger #{version}"
27
+ rdoc.rdoc_files.include('README*')
28
+ rdoc.rdoc_files.include('lib/**/*.rb')
29
+ end
@@ -0,0 +1,19 @@
1
+ Gem::Specification.new do |gem|
2
+ gem.name = 'autolink'
3
+ gem.version = "0.0.2"
4
+
5
+ gem.author, gem.email = 'Burke Libbey', "burke@burkelibbey.org"
6
+ gem.homepage = "http://github.com/burke/autolink"
7
+
8
+ gem.summary = "Automatically generate nested paths in Rails 2.3 according to your application's setup"
9
+ gem.description = "By setting a default lineage in each model, and using these monkeypatches to Rails, the url helpers are able to generate complex urls from a single ActiveRecord::Base object. Works in 2.3.8, probably other 2.3 railses."
10
+
11
+ gem.required_ruby_version = '>= 1.8.7'
12
+
13
+ gem.has_rdoc = false
14
+
15
+ gem.files = Dir.glob("{lib,spec}/**/*") + %w(LICENSE README.rdoc Rakefile autolink.gemspec)
16
+
17
+ end
18
+
19
+
@@ -0,0 +1,3 @@
1
+ require 'autolink/action_controller_extensions'
2
+ require 'autolink/action_view_extensions'
3
+ require 'autolink/active_record_extensions'
@@ -0,0 +1,26 @@
1
+ module ActionController #:nodoc:
2
+ class Base
3
+
4
+ def autolink(obj)
5
+ polymorphic_url(obj.class.default_lineage(obj))
6
+ end
7
+
8
+ def url_for(options = {})
9
+ options ||= {}
10
+ case options
11
+ when ActiveRecord::Base # +
12
+ @@__url = @url # +
13
+ autolink(options) # +
14
+ when String
15
+ options
16
+ when Hash
17
+ @url = @@__url unless @url # +
18
+ @url.rewrite(rewrite_options(options))
19
+ else
20
+ polymorphic_url(options)
21
+ end
22
+ end
23
+
24
+ end
25
+ end
26
+
@@ -0,0 +1,28 @@
1
+ module ActionView
2
+ module Helpers
3
+ module UrlHelper
4
+ def url_for(options = {})
5
+ options ||= {}
6
+ url = case options
7
+ when ActiveRecord::Base # This method is the same as 2.3.8,
8
+ polymorphic_path(options.class.default_lineage(options)) # Except for the addition of these two lines.
9
+ when String
10
+ escape = true
11
+ options
12
+ when Hash
13
+ options = { :only_path => options[:host].nil? }.update(options.symbolize_keys)
14
+ escape = options.key?(:escape) ? options.delete(:escape) : true
15
+ @controller.send(:url_for, options)
16
+ when :back
17
+ escape = false
18
+ @controller.request.env["HTTP_REFERER"] || 'javascript:history.back()'
19
+ else
20
+ escape = false
21
+ polymorphic_path(options)
22
+ end
23
+
24
+ escape ? escape_once(url).html_safe : url
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,9 @@
1
+ class ActiveRecord::Base
2
+ def default_lineage
3
+ if self.class.respond_to?(:default_lineage)
4
+ self.class.default_lineage(self)
5
+ else
6
+ raise "default_lineage() has not been implemented for this class"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,25 @@
1
+ module Spec
2
+ module Rails
3
+ module Matchers
4
+
5
+ class RedirectTo
6
+
7
+ def expected_url
8
+ case @expected
9
+ when ActiveRecord::Base
10
+ ActionController::Base.new.autolink(@expected)
11
+ when Hash
12
+ return ActionController::UrlRewriter.new(@request, {}).rewrite(@expected)
13
+ when :back
14
+ return @request.env['HTTP_REFERER']
15
+ when %r{^\w+://.*}
16
+ return @expected
17
+ else
18
+ return "http://#{@request.host}" + (@expected.split('')[0] == '/' ? '' : '/') + @expected
19
+ end
20
+ end
21
+
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+
3
+ # require something.
4
+
5
+ gem "rspec", ">=1.2.5"
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
10
+
11
+
@@ -0,0 +1 @@
1
+ --colour
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: autolink
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Burke Libbey
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-25 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: By setting a default lineage in each model, and using these monkeypatches to Rails, the url helpers are able to generate complex urls from a single ActiveRecord::Base object. Works in 2.3.8, probably other 2.3 railses.
23
+ email: burke@burkelibbey.org
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - lib/autolink/action_controller_extensions.rb
32
+ - lib/autolink/action_view_extensions.rb
33
+ - lib/autolink/active_record_extensions.rb
34
+ - lib/autolink/rspec.rb
35
+ - lib/autolink.rb
36
+ - spec/spec_helper.rb
37
+ - spec/spec_opts.rb
38
+ - LICENSE
39
+ - README.rdoc
40
+ - Rakefile
41
+ - autolink.gemspec
42
+ has_rdoc: false
43
+ homepage: http://github.com/burke/autolink
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options: []
48
+
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 57
57
+ segments:
58
+ - 1
59
+ - 8
60
+ - 7
61
+ version: 1.8.7
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ hash: 3
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ requirements: []
72
+
73
+ rubyforge_project:
74
+ rubygems_version: 1.3.7
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Automatically generate nested paths in Rails 2.3 according to your application's setup
78
+ test_files: []
79
+