rack-rewrite-dynamic 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +162 -0
- data/Rakefile +8 -0
- data/lib/rack-rewrite-dynamic.rb +5 -0
- data/lib/rack/rewrite/dynamic/base.rb +47 -0
- data/lib/rack/rewrite/dynamic/filter_rewrite.rb +46 -0
- data/lib/rack/rewrite/dynamic/rails_route_generator.rb +11 -0
- data/lib/rack/rewrite/dynamic/rewrite.rb +66 -0
- data/lib/rack/rewrite/dynamic/rewrites.rb +30 -0
- data/lib/rack/rewrite/dynamic/version.rb +8 -0
- data/rack-rewrite-dynamic.gemspec +24 -0
- data/spec/rack/rewrite/dynamic/filter_rewrite_spec.rb +52 -0
- data/spec/rack/rewrite/dynamic/rewrite_spec.rb +62 -0
- data/spec/rack/rewrite/dynamic/rewrites_spec.rb +22 -0
- data/spec/rack/rewrite/dynamic_spec.rb +76 -0
- metadata +130 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Michal Olah
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,162 @@
|
|
1
|
+
# rack-rewrite-dynamic
|
2
|
+
|
3
|
+
Rack rewrite is a great tool if you have static based rewrites. What do
|
4
|
+
you do when dynamic slug based rewrites are needed to make your SEO
|
5
|
+
seeking client happy?
|
6
|
+
|
7
|
+
Consider the following requirements:
|
8
|
+
|
9
|
+
/slug_name/another_slug_name should be rendered by /controller/id
|
10
|
+
/filter_slug-another_filter_slug-resources_name should be renderd by
|
11
|
+
/resources_name?color_ids[]=42&color_ids[]=43&category_ids[]=42
|
12
|
+
|
13
|
+
You could try to setup custom routing using
|
14
|
+
[rack-rewrite arbitrary rewriting feature](https://github.com/jtrupiano/rack-rewrite#arbitrary-rewriting).
|
15
|
+
Rack-rewrite-dynamic wraps these use cases and provides an easy way to
|
16
|
+
create your own if you need to do so.
|
17
|
+
|
18
|
+
|
19
|
+
## Installation
|
20
|
+
|
21
|
+
Add this line to your application's Gemfile:
|
22
|
+
|
23
|
+
gem 'rack-rewrite-dynamic'
|
24
|
+
|
25
|
+
And then execute:
|
26
|
+
|
27
|
+
$ bundle
|
28
|
+
|
29
|
+
Or install it yourself as:
|
30
|
+
|
31
|
+
$ gem install rack-rewrite-dynamic
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
|
35
|
+
Rack-rewrite-dynamic currently assumes its being used as a middleware. It also assumes you have a Slug
|
36
|
+
model using the [friendly_id](https://github.com/norman/friendly_id)
|
37
|
+
gem. The slug model should have a polymorphic association to the models
|
38
|
+
that are used for SEO urls.
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
class Slug < ActiveRecord::Base
|
42
|
+
extend FriendlyId
|
43
|
+
friendly_id :content, use: [:slugged, :history]
|
44
|
+
belongs_to :sluggable, :polymorphic => true
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
Rack-rewrite-dynamic provides two types of slug based url matchers:
|
49
|
+
|
50
|
+
### Show page URL rewrites
|
51
|
+
|
52
|
+
To set up the rewrites, hook into rack-rewrite middleware in config/application.rb
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
config.middleware.insert_after "ActiveRecord::QueryCache", 'Rack::Rewrite' do |base|
|
56
|
+
rewriter = 'Rack::Rewrite::Dynamic::Rewrites'.constantize.new do
|
57
|
+
rewrite url_parts: [{'Category' => 'slug', 'IceCream' => 'slug'}]
|
58
|
+
end
|
59
|
+
rewriter.apply_rewrites(base)
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
The example rewrites /category_slug/ice_cream_slug to /ice_creams/42 so
|
64
|
+
the rails app can have
|
65
|
+
```ruby
|
66
|
+
resources :ice_creams
|
67
|
+
```
|
68
|
+
defined as usual.
|
69
|
+
|
70
|
+
### Filter URL rewrites
|
71
|
+
|
72
|
+
To set up a url rewriter use
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
config.middleware.insert_after "ActiveRecord::QueryCache", 'Rack::Rewrite' do |base|
|
76
|
+
rewriter = 'Rack::Rewrite::Dynamic::Rewrites'.constantize.new do
|
77
|
+
rewrite_filter separator: '-', target: 'cars', suffix: 'cars'
|
78
|
+
end
|
79
|
+
rewriter.apply_rewrites(base)
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
The example rewrites /color_slug-another_color_slug-category_slug-cars
|
84
|
+
to /cars?color_ids[]=42&color_ids[]=43&category_ids[]=42 so the rails
|
85
|
+
app can have
|
86
|
+
```ruby
|
87
|
+
resources :cars
|
88
|
+
```
|
89
|
+
defined as usual and use the filter attributes on the index page.
|
90
|
+
|
91
|
+
### Custom rewrites
|
92
|
+
|
93
|
+
If the two built in rewrites do not fit your need, you can create your
|
94
|
+
own. You can use one of our own rewriters as a template.
|
95
|
+
The only requirement is to have a perform instance method that
|
96
|
+
receives a match object of the url and a rack_env object containing the
|
97
|
+
request information from the rack environment. To bring in some
|
98
|
+
functionality that should be useful include our base module.
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
class MyRewriter
|
102
|
+
include Rack::Rewrite::Dynamic::Base
|
103
|
+
|
104
|
+
def perform(match, rack_env)
|
105
|
+
# some awesome rewriting
|
106
|
+
end
|
107
|
+
end
|
108
|
+
```
|
109
|
+
|
110
|
+
You can then pass them in as an argument to the rewrite method.
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
config.middleware.insert_after "ActiveRecord::QueryCache", 'Rack::Rewrite' do |base|
|
114
|
+
rewriter = 'Rack::Rewrite::Dynamic::Rewrites'.constantize.new do
|
115
|
+
rewrite {option: 'value'}, MyRewriter
|
116
|
+
end
|
117
|
+
rewriter.apply_rewrites(base)
|
118
|
+
end
|
119
|
+
```
|
120
|
+
|
121
|
+
### Configuration
|
122
|
+
|
123
|
+
If you wish to use a different slug class you may pass it in as an
|
124
|
+
argument when defining the rewrite.
|
125
|
+
|
126
|
+
```ruby
|
127
|
+
config.middleware.insert_after "ActiveRecord::QueryCache", 'Rack::Rewrite' do |base|
|
128
|
+
rewriter = 'Rack::Rewrite::Dynamic::Rewrites'.constantize.new do
|
129
|
+
rewrite url_parts: [{'Category' => 'slug', 'IceCream' => 'slug'}], slug_name: 'AnotherSlug'
|
130
|
+
end
|
131
|
+
rewriter.apply_rewrites(base)
|
132
|
+
end
|
133
|
+
```
|
134
|
+
|
135
|
+
By default the rewrites assume you have a rails application to generate
|
136
|
+
the routing. If you with to have a custom route generator, you can
|
137
|
+
supply it when defining the rewrite. It needs to respond to a
|
138
|
+
route_for(slug) message and return a string representing the url.
|
139
|
+
|
140
|
+
```ruby
|
141
|
+
class TestGenerator
|
142
|
+
def self.route_for slug
|
143
|
+
'some/path'
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
config.middleware.insert_after "ActiveRecord::QueryCache", 'Rack::Rewrite' do |base|
|
148
|
+
rewriter = 'Rack::Rewrite::Dynamic::Rewrites'.constantize.new do
|
149
|
+
rewrite url_parts: [{'Category' => 'slug', 'IceCream' => 'slug'}], route_generator_name: 'TestGenerator'
|
150
|
+
end
|
151
|
+
rewriter.apply_rewrites(base)
|
152
|
+
end
|
153
|
+
```
|
154
|
+
|
155
|
+
|
156
|
+
## Contributing
|
157
|
+
|
158
|
+
1. Fork it
|
159
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
160
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
161
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
162
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
module Rack
|
2
|
+
class Rewrite
|
3
|
+
module Dynamic
|
4
|
+
module Base
|
5
|
+
attr_reader :opts
|
6
|
+
def initialize(opts)
|
7
|
+
@opts = opts
|
8
|
+
@opts[:slug_name] ||= 'Slug'
|
9
|
+
@opts[:route_generator_name] ||= 'Rack::Rewrite::Dynamic::RailsRouteGenerator'
|
10
|
+
end
|
11
|
+
|
12
|
+
def apply_rewrite(base)
|
13
|
+
base.send :rewrite, %r{#{build_match_string}}, lambda { |match, rack_env|
|
14
|
+
perform(match, rack_env)
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
def slug_type?(slug_name, slug_type)
|
19
|
+
slug = find_sluggable(slug_name)
|
20
|
+
slug if slug && slug[:sluggable_type] == slug_type
|
21
|
+
end
|
22
|
+
def find_sluggable(friendly_id)
|
23
|
+
slug_klass.find(friendly_id)
|
24
|
+
rescue ActiveRecord::RecordNotFound
|
25
|
+
# :)
|
26
|
+
end
|
27
|
+
def slug_klass
|
28
|
+
@slug_klass ||= @opts[:slug_name].constantize
|
29
|
+
end
|
30
|
+
def route_generator_klass
|
31
|
+
@route_generator_klass ||= @opts[:route_generator_name].constantize
|
32
|
+
end
|
33
|
+
def slug_path_if_present(slug, rack_env)
|
34
|
+
if slug
|
35
|
+
route_generator_klass.route_for slug
|
36
|
+
else
|
37
|
+
rack_env['REQUEST_URI'] || rack_env['PATH_INFO']
|
38
|
+
end
|
39
|
+
end
|
40
|
+
def original_path(rack_env)
|
41
|
+
rack_env['REQUEST_URI'] || rack_env['PATH_INFO']
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require_relative 'base'
|
2
|
+
require 'active_support/all'
|
3
|
+
module Rack
|
4
|
+
class Rewrite
|
5
|
+
module Dynamic
|
6
|
+
class FilterRewrite
|
7
|
+
include Base
|
8
|
+
|
9
|
+
def perform(match, rack_env)
|
10
|
+
if !(match[1] =~ /assets/)
|
11
|
+
filter_parts = match[1].split(@opts[:separator])
|
12
|
+
if filter_parts.present?
|
13
|
+
slugs = filter_parts.map do |slug_candidate|
|
14
|
+
find_sluggable(slug_candidate)
|
15
|
+
end
|
16
|
+
if !slugs.include?(nil)
|
17
|
+
filter_params = {}
|
18
|
+
slugs.each do |s|
|
19
|
+
filter_params["#{s[:sluggable_type].underscore}_ids"] ||= []
|
20
|
+
filter_params["#{s[:sluggable_type].underscore}_ids"] << s[:sluggable_id]
|
21
|
+
end
|
22
|
+
return "/#{@opts[:target]}?#{filter_params.to_query}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
else
|
26
|
+
rack_env['REQUEST_URI'] || rack_env['PATH_INFO']
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
def build_match_string
|
32
|
+
match_string = '^'
|
33
|
+
match_string << "#{@opts[:prefix]}" if @opts[:prefix]
|
34
|
+
match_string << '\/' + "#{filter_string}"
|
35
|
+
match_string << "#{@opts[:separator]}#{@opts[:suffix]}" if @opts[:suffix] && @opts[:separator]
|
36
|
+
match_string << "\/?$"
|
37
|
+
match_string
|
38
|
+
end
|
39
|
+
def filter_string
|
40
|
+
'([^\/]+)'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require_relative 'base'
|
2
|
+
module Rack
|
3
|
+
class Rewrite
|
4
|
+
module Dynamic
|
5
|
+
class Rewrite
|
6
|
+
include Base
|
7
|
+
|
8
|
+
def perform(match, rack_env)
|
9
|
+
if !(match[1] =~ /assets/)
|
10
|
+
urls = find_urls(match, rack_env)
|
11
|
+
if urls.length > 0
|
12
|
+
urls.first
|
13
|
+
else
|
14
|
+
original_path(rack_env)
|
15
|
+
end
|
16
|
+
else
|
17
|
+
original_path(rack_env)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def find_urls(match, rack_env)
|
22
|
+
@opts[:url_parts].map do |url_def|
|
23
|
+
find_url(url_def, match, rack_env)
|
24
|
+
end.compact
|
25
|
+
end
|
26
|
+
|
27
|
+
def find_url(url_parts, match, rack_env)
|
28
|
+
slugs = url_parts.each_with_index.map do |url_part, index|
|
29
|
+
if url_part[1] == 'slug'
|
30
|
+
slug_type?(match[index+1], url_part[0])
|
31
|
+
else
|
32
|
+
'static'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
if !slugs.include?(nil)
|
36
|
+
slug_path_if_present(slugs[url_parts.length-1], rack_env)
|
37
|
+
else
|
38
|
+
nil
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
def build_match_string
|
44
|
+
match_string = "^\/"
|
45
|
+
match_string << @opts[:url_parts].first.map do |url_value, url_type|
|
46
|
+
if url_type == 'static'
|
47
|
+
"(#{url_value})"
|
48
|
+
else
|
49
|
+
suburl_string
|
50
|
+
end
|
51
|
+
end.join(suburl_separator)
|
52
|
+
match_string << @opts[:suffix] if @opts[:suffix]
|
53
|
+
match_string << "\/?$"
|
54
|
+
match_string
|
55
|
+
end
|
56
|
+
def suburl_string
|
57
|
+
'([^\/]+)'
|
58
|
+
end
|
59
|
+
def suburl_separator
|
60
|
+
'\/'
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require_relative 'rewrite'
|
2
|
+
require_relative 'filter_rewrite'
|
3
|
+
|
4
|
+
module Rack
|
5
|
+
class Rewrite
|
6
|
+
module Dynamic
|
7
|
+
class Rewrites
|
8
|
+
attr_reader :rewrites
|
9
|
+
def initialize &rewrite_block
|
10
|
+
instance_eval(&rewrite_block) if block_given?
|
11
|
+
end
|
12
|
+
|
13
|
+
def rewrite_filter(opts = {})
|
14
|
+
rewrite(opts, FilterRewrite)
|
15
|
+
end
|
16
|
+
def rewrite(opts = {}, rewrite_klass = Rewrite)
|
17
|
+
@rewrites ||= []
|
18
|
+
@rewrites << rewrite_klass.new(opts)
|
19
|
+
end
|
20
|
+
|
21
|
+
def apply_rewrites(base)
|
22
|
+
@rewrites.each do |rewrite|
|
23
|
+
rewrite.apply_rewrite(base)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rack/rewrite/dynamic/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "rack-rewrite-dynamic"
|
8
|
+
gem.version = Rack::Rewrite::Dynamic::VERSION
|
9
|
+
gem.authors = ["Michal Olah"]
|
10
|
+
gem.email = ["olahmichal@gmail.com"]
|
11
|
+
gem.description = %q{Dynamic SEO urls}
|
12
|
+
gem.summary = %q{SEO urls based on slugs}
|
13
|
+
gem.homepage = "https://github.com/bonetics/rack-rewrite-dynamic"
|
14
|
+
|
15
|
+
gem.add_dependency 'rack-rewrite'
|
16
|
+
gem.add_dependency 'activesupport'
|
17
|
+
gem.add_development_dependency 'rack'
|
18
|
+
gem.add_development_dependency "rspec"
|
19
|
+
|
20
|
+
gem.files = `git ls-files`.split($/)
|
21
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
22
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
23
|
+
gem.require_paths = ["lib"]
|
24
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require_relative '../../../../lib/rack/rewrite/dynamic/filter_rewrite'
|
2
|
+
|
3
|
+
describe Rack::Rewrite::Dynamic::FilterRewrite do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
unless defined?(ActiveRecord::RecordNotFound)
|
7
|
+
module ActiveRecord
|
8
|
+
module RecordNotFound; end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
unless defined?(Slug)
|
12
|
+
class Slug; end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
let(:opts) do
|
17
|
+
{
|
18
|
+
separator: '-', target: 'outfits', suffix: 'outfits'
|
19
|
+
}
|
20
|
+
end
|
21
|
+
subject { Rack::Rewrite::Dynamic::FilterRewrite.new(opts) }
|
22
|
+
let(:base) { mock(:base) }
|
23
|
+
|
24
|
+
its(:opts) { should eq(opts) }
|
25
|
+
|
26
|
+
it 'should apply rewrite' do
|
27
|
+
base.should_receive(:rewrite)
|
28
|
+
subject.apply_rewrite(base)
|
29
|
+
end
|
30
|
+
|
31
|
+
describe '#perform' do
|
32
|
+
let(:match) { ['', 'filter_slug-outfits'] }
|
33
|
+
let(:rack_env) { { 'REQUEST_URI' => 'some/path' } }
|
34
|
+
it 'should perform rewrite' do
|
35
|
+
subject.stub(:find_sluggable) { { sluggable_type: 'color', sluggable_id: 42 } }
|
36
|
+
subject.perform(match, rack_env).should eq('/outfits?color_ids%5B%5D=42&color_ids%5B%5D=42')
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should return the original request if assets' do
|
40
|
+
match[1] = 'assets'
|
41
|
+
subject.perform(match, rack_env).should eq('some/path')
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should not return path if slug wasn not found' do
|
45
|
+
subject.stub(:find_sluggable)
|
46
|
+
subject.perform(match, rack_env).should be_nil
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require_relative '../../../../lib/rack/rewrite/dynamic/rewrite'
|
2
|
+
|
3
|
+
describe Rack::Rewrite::Dynamic::Rewrite do
|
4
|
+
before(:each) do
|
5
|
+
unless defined?(ActiveRecord::RecordNotFound)
|
6
|
+
module ActiveRecord
|
7
|
+
module RecordNotFound; end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
unless defined?(Slug)
|
11
|
+
class Slug; end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
let(:opts) do
|
16
|
+
{
|
17
|
+
url_parts: [{'online-shops' => 'static',
|
18
|
+
'Merchant' => 'slug'}]
|
19
|
+
}
|
20
|
+
end
|
21
|
+
subject { Rack::Rewrite::Dynamic::Rewrite.new(opts) }
|
22
|
+
let(:base) { mock(:base) }
|
23
|
+
|
24
|
+
its(:opts) { should eq(opts) }
|
25
|
+
|
26
|
+
it 'should apply rewrite' do
|
27
|
+
base.should_receive(:rewrite)
|
28
|
+
subject.apply_rewrite(base)
|
29
|
+
end
|
30
|
+
|
31
|
+
describe '#perform' do
|
32
|
+
let(:match) { ['', 'merchant-slug'] }
|
33
|
+
let(:rack_env) { { 'REQUEST_URI' => 'some/path' } }
|
34
|
+
|
35
|
+
it 'should return the original request if assets' do
|
36
|
+
match[1] = 'assets'
|
37
|
+
subject.perform(match, rack_env).should eq('some/path')
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should parse the url_parts and return first match' do
|
41
|
+
subject.stub(:find_urls) { ['some/path', 'some_other/path'] }
|
42
|
+
subject.perform(match, rack_env).should eq('some/path')
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should find urls' do
|
46
|
+
subject.stub(:find_url) { 'some/path' }
|
47
|
+
subject.find_urls(match, rack_env).should == ['some/path']
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should find url' do
|
51
|
+
subject.stub(:slug_type?) { stub }
|
52
|
+
subject.stub(:slug_path_if_present) { 'some/path' }
|
53
|
+
subject.find_url(opts[:url_parts].first, match, rack_env).should eq('some/path')
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'should not return path if slug wasn not found' do
|
57
|
+
subject.stub(:slug_type?) { stub }
|
58
|
+
subject.stub(:slug_path_if_present)
|
59
|
+
subject.find_url(opts[:url_parts].first, match, rack_env).should be_nil
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require_relative '../../../../lib/rack/rewrite/dynamic/rewrites'
|
2
|
+
|
3
|
+
describe Rack::Rewrite::Dynamic::Rewrites do
|
4
|
+
|
5
|
+
it 'adds a rewrite' do
|
6
|
+
subject.rewrite
|
7
|
+
subject.rewrites.size.should > 0
|
8
|
+
end
|
9
|
+
it 'adds a rewrite_filter' do
|
10
|
+
subject.rewrite_filter
|
11
|
+
subject.rewrites.size.should > 0
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'applies rewrites' do
|
15
|
+
base = stub
|
16
|
+
|
17
|
+
subject.rewrite
|
18
|
+
subject.rewrites.each {|rewrite| rewrite.should_receive(:apply_rewrite).with(base) }
|
19
|
+
subject.apply_rewrites(base)
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require_relative '../../../lib/rack/rewrite/dynamic/rewrites'
|
2
|
+
|
3
|
+
require 'rack'
|
4
|
+
require 'rack-rewrite'
|
5
|
+
|
6
|
+
class Slug
|
7
|
+
def self.create!(attrs)
|
8
|
+
@attrs = attrs
|
9
|
+
end
|
10
|
+
def self.find(slug)
|
11
|
+
@attrs
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class TestSlug
|
16
|
+
def self.create!(attrs)
|
17
|
+
@attrs = attrs
|
18
|
+
end
|
19
|
+
def self.find(slug)
|
20
|
+
@attrs
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
class TestGenerator
|
26
|
+
def self.route_for slug
|
27
|
+
'some/path'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe Rack::Rewrite::Dynamic do
|
32
|
+
|
33
|
+
let(:app) { mock(:app)}
|
34
|
+
let(:env) { { 'PATH_INFO' => '/online-shops/murdar'} }
|
35
|
+
before :each do
|
36
|
+
Slug.create!(sluggable_type: 'Merchant', sluggable_id: 42, content: 'Murdar')
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'uses dynamic rewrites' do
|
40
|
+
Rack::Rewrite::Dynamic::Rewrite.any_instance.stub(:slug_path_if_present) { '/merchants/42' }
|
41
|
+
subject = Rack::Rewrite.new(app) do |base|
|
42
|
+
rewriter = Rack::Rewrite::Dynamic::Rewrites.new do
|
43
|
+
rewrite url_parts: [{'online-shops' => 'static', 'Merchant' => 'slug'}]
|
44
|
+
end
|
45
|
+
rewriter.apply_rewrites(base)
|
46
|
+
end
|
47
|
+
app.should_receive(:call).with({"PATH_INFO"=>"/merchants/42", "REQUEST_URI"=>"/merchants/42", "QUERY_STRING"=>""})
|
48
|
+
subject.call(env)
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'accepts a different route generator' do
|
52
|
+
subject = Rack::Rewrite.new(app) do |base|
|
53
|
+
rewriter = Rack::Rewrite::Dynamic::Rewrites.new do
|
54
|
+
rewrite url_parts: [{'online-shops' => 'static', 'Merchant' => 'slug'}], route_generator_name: 'TestGenerator'
|
55
|
+
end
|
56
|
+
rewriter.apply_rewrites(base)
|
57
|
+
end
|
58
|
+
app.should_receive(:call).with({"PATH_INFO"=>"some/path", "REQUEST_URI"=>"some/path", "QUERY_STRING"=>""})
|
59
|
+
subject.call(env)
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'accepts a different slug class' do
|
63
|
+
TestSlug.create!(sluggable_type: 'Merchant', sluggable_id: 42, content: 'Murdar')
|
64
|
+
Rack::Rewrite::Dynamic::Rewrite.any_instance.stub(:slug_path_if_present) { '/merchants/42' }
|
65
|
+
subject = Rack::Rewrite.new(app) do |base|
|
66
|
+
rewriter = Rack::Rewrite::Dynamic::Rewrites.new do
|
67
|
+
rewrite url_parts: [{'online-shops' => 'static', 'Merchant' => 'slug'}], slug_name: 'TestSlug'
|
68
|
+
end
|
69
|
+
rewriter.apply_rewrites(base)
|
70
|
+
end
|
71
|
+
app.should_receive(:call).with({"PATH_INFO"=>"/merchants/42", "REQUEST_URI"=>"/merchants/42", "QUERY_STRING"=>""})
|
72
|
+
subject.call(env)
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
end
|
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-rewrite-dynamic
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Michal Olah
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rack-rewrite
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activesupport
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rack
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Dynamic SEO urls
|
79
|
+
email:
|
80
|
+
- olahmichal@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- LICENSE.txt
|
88
|
+
- README.md
|
89
|
+
- Rakefile
|
90
|
+
- lib/rack-rewrite-dynamic.rb
|
91
|
+
- lib/rack/rewrite/dynamic/base.rb
|
92
|
+
- lib/rack/rewrite/dynamic/filter_rewrite.rb
|
93
|
+
- lib/rack/rewrite/dynamic/rails_route_generator.rb
|
94
|
+
- lib/rack/rewrite/dynamic/rewrite.rb
|
95
|
+
- lib/rack/rewrite/dynamic/rewrites.rb
|
96
|
+
- lib/rack/rewrite/dynamic/version.rb
|
97
|
+
- rack-rewrite-dynamic.gemspec
|
98
|
+
- spec/rack/rewrite/dynamic/filter_rewrite_spec.rb
|
99
|
+
- spec/rack/rewrite/dynamic/rewrite_spec.rb
|
100
|
+
- spec/rack/rewrite/dynamic/rewrites_spec.rb
|
101
|
+
- spec/rack/rewrite/dynamic_spec.rb
|
102
|
+
homepage: https://github.com/bonetics/rack-rewrite-dynamic
|
103
|
+
licenses: []
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
none: false
|
110
|
+
requirements:
|
111
|
+
- - ! '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
none: false
|
116
|
+
requirements:
|
117
|
+
- - ! '>='
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 1.8.24
|
123
|
+
signing_key:
|
124
|
+
specification_version: 3
|
125
|
+
summary: SEO urls based on slugs
|
126
|
+
test_files:
|
127
|
+
- spec/rack/rewrite/dynamic/filter_rewrite_spec.rb
|
128
|
+
- spec/rack/rewrite/dynamic/rewrite_spec.rb
|
129
|
+
- spec/rack/rewrite/dynamic/rewrites_spec.rb
|
130
|
+
- spec/rack/rewrite/dynamic_spec.rb
|