speaking_url 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,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in speaking_url_resource.gemspec
4
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,102 @@
1
+ = Speaking URL
2
+
3
+ The Speaking URL Gem provides an extension for the <tt>routes.rb</tt> file of
4
+ your Rails-App to allow for arbitrary URL being used for resources. The
5
+ Gem is mainly SEO-motivated; it facilitates:
6
+
7
+ * Usage of so called "static" or "nice-looking" URLs,
8
+ * Implementation of a SEO friendly URL strategy,
9
+ * Usage of different style URLs for resources of the same type
10
+ * And (from a SEO-point maybe most importantly) Modification of URL
11
+ strategies with proper HTTP (301) redirects.
12
+
13
+ This Gem <em>does not</em>:
14
+
15
+ * generate URLs
16
+ * manage all of your App's URLs
17
+
18
+
19
+ These points have to by managed by yourself.
20
+
21
+ = Installation
22
+
23
+ Add the gem to your Rails-App by including it in your Gemfile:
24
+
25
+ gem "speaking_url"
26
+
27
+
28
+ = Usage
29
+
30
+ A quick list of steps that have to be done in order to get started
31
+ with using this Gem follows.
32
+
33
+ == Usage in your models
34
+
35
+ The Speaking Url must be setup within the models (resources) that you
36
+ want to assign a Speaking URL to.
37
+
38
+ class Person
39
+ include Mongoid::Document
40
+ include include SpeakingUrl::MongoResource
41
+ end
42
+
43
+ This adds an array field +urls+ to the Person resource, where the
44
+ current url and the url history are stored.
45
+
46
+ To add a new route to a some resource, use the +add_mapping+ method
47
+ instead of modifying the field directly.
48
+
49
+ @person.add_mapping('/some/new/mapping')
50
+
51
+
52
+
53
+ == Configuring routes
54
+
55
+ To actually deploy the new routes, the Gem ships with the
56
+ +speaking_url_resource+ method. So far it accepts no customizations; you
57
+ simply provide the resources as symbols:
58
+
59
+
60
+ speaking_url_resource :person, :product
61
+
62
+ Here both - the person and product resource - are made accessible by their
63
+ speaking url(s). You probably want to include this line as one of your last
64
+ statements (maybe before your root source), so that the other routes
65
+ have higher priority and achieve slightly better performance.
66
+
67
+
68
+
69
+ == Usage in Controllers
70
+
71
+ In the appropriate controllers actions a specific resource is no longer
72
+ fetches by it's databse ID, but by the request.path:
73
+
74
+ @search = Person.find_by_url(request.path)
75
+
76
+ You can be sure there that a resource is found because otherwise the the
77
+ controller wouldn't have been called by the routing mapper.
78
+
79
+
80
+ == Usage in views
81
+
82
+ Instead of using the default rails route helpers in your views, you want
83
+ to use the speaking url helpers:
84
+
85
+ <%= link_to "A nice person", @person.current_url %>
86
+
87
+
88
+ = Outlook and Todo
89
+
90
+ So far the Gem is pretty limited. What could be developed for following
91
+ version is:
92
+
93
+ * unit tests (asap)
94
+ * support for other persistent storage mappers
95
+ * configuration options for the routes method
96
+
97
+
98
+ = License
99
+
100
+ GPLv3. Copyright 2011 Kai Rubarth.
101
+
102
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,42 @@
1
+ require 'mongoid'
2
+
3
+ module SpeakingUrl
4
+
5
+ module MongoResource
6
+ def self.included(klass)
7
+ @@cocktails ||= []
8
+ @@cocktails.push(klass).uniq!
9
+
10
+ klass.instance_eval do
11
+ include Mongoid::Document
12
+ field :urls, :type => Array, :default => []
13
+ end
14
+
15
+ klass.extend(ClassMethods)
16
+ end
17
+
18
+ def self.cocktails
19
+ @@cocktails
20
+ end
21
+
22
+ def current_url
23
+ urls.last
24
+ end
25
+
26
+ def add_mapping(new_url)
27
+ urls << new_url
28
+ end
29
+
30
+ module ClassMethods
31
+ def find_by_url(url)
32
+ where(:urls => url).first
33
+ end
34
+ end
35
+
36
+ end
37
+
38
+ end
39
+
40
+
41
+
42
+
@@ -0,0 +1,43 @@
1
+
2
+ module ActionDispatch::Routing
3
+
4
+ class Mapper
5
+
6
+ def speaking_url_resource(*resources)
7
+
8
+ resources.map!(&:to_sym)
9
+
10
+ resources.each do |sym|
11
+
12
+ klass = Kernel.const_get(sym.to_s.classify)
13
+
14
+ current_address_constraint_class = Class.new do
15
+ @klass = klass
16
+ class << self; attr_reader :klass; end
17
+ def matches?(request)
18
+ r = self.class.klass.find_by_url(request.path)
19
+ return false if r.nil?
20
+ r.current_url == request.path
21
+ end
22
+ end
23
+
24
+ old_address_constraint_class = Class.new do
25
+ @klass = klass
26
+ class << self; attr_reader :klass; end
27
+ def matches?(request)
28
+ r = self.class.klass.find_by_url(request.path)
29
+ return false if r.nil?
30
+ r.current_url != request.path
31
+ end
32
+ end
33
+
34
+ match "*path", :to => "#{klass.name.underscore.pluralize}#show", :constraints => current_address_constraint_class.new
35
+ match "*path" => redirect{|p, req| klass.find_by_url(req.path).current_url}, :constraints => old_address_constraint_class.new
36
+
37
+ end
38
+ end
39
+ end
40
+
41
+ end
42
+
43
+
@@ -0,0 +1,3 @@
1
+ module SpeakingUrl
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,12 @@
1
+ require 'rails'
2
+ #require 'active_support/dependencies'
3
+
4
+ require 'speaking_url/mongo_resource'
5
+ require 'speaking_url/routing_extensions'
6
+
7
+ module SpeakingUrl
8
+
9
+ end
10
+
11
+
12
+
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "speaking_url/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "speaking_url"
7
+ s.version = SpeakingUrl::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Kai Rubarth"]
10
+ s.email = ["kai@doxter.de"]
11
+ s.homepage = ""
12
+ s.summary = %q{allows arbitrary url for resources and facilitates seo-friendly changing of urls.}
13
+ s.description = %q{Provides the possibility to have arbitrary URL for resources.}
14
+
15
+ s.rubyforge_project = "speaking_url"
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
+
22
+ s.add_dependency('mongoid')
23
+ s.add_development_dependency('shoulda')
24
+ s.add_development_dependency('mocha')
25
+
26
+ end
27
+
28
+
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: speaking_url
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
+ - Kai Rubarth
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-06-06 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: mongoid
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: shoulda
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: mocha
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :development
62
+ version_requirements: *id003
63
+ description: Provides the possibility to have arbitrary URL for resources.
64
+ email:
65
+ - kai@doxter.de
66
+ executables: []
67
+
68
+ extensions: []
69
+
70
+ extra_rdoc_files: []
71
+
72
+ files:
73
+ - .gitignore
74
+ - Gemfile
75
+ - README.rdoc
76
+ - Rakefile
77
+ - lib/speaking_url.rb
78
+ - lib/speaking_url/mongo_resource.rb
79
+ - lib/speaking_url/routing_extensions.rb
80
+ - lib/speaking_url/version.rb
81
+ - speaking_url.gemspec
82
+ has_rdoc: true
83
+ homepage: ""
84
+ licenses: []
85
+
86
+ post_install_message:
87
+ rdoc_options: []
88
+
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ hash: 3
97
+ segments:
98
+ - 0
99
+ version: "0"
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ hash: 3
106
+ segments:
107
+ - 0
108
+ version: "0"
109
+ requirements: []
110
+
111
+ rubyforge_project: speaking_url
112
+ rubygems_version: 1.3.7
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: allows arbitrary url for resources and facilitates seo-friendly changing of urls.
116
+ test_files: []
117
+