dynamic_router 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: abc0a2d19bc3fb9974bba3a517de829a29fa82d1
4
- data.tar.gz: 5ec55c93c6ff0b7f08e1f52aec5e73e1c33d0820
3
+ metadata.gz: 59b9c8ce0c8e7f5607f7f6f66dce1a35f31292f2
4
+ data.tar.gz: 7b9a38b8bea748f00c1029e6e33f8264855b6fd4
5
5
  SHA512:
6
- metadata.gz: 78b07a36b939ca786e39b6ad039ac0b92508bdb551c7f2e5580639b7e74edc400aa90e07ecb63621ff434e6ea9b959a06352d83e31665fc2416f5b97a7fe37b4
7
- data.tar.gz: 917822ef9a54f88c789402700b9e360e51578dd0166ab94e07fb58e87483e47d5a023a7fed2b483d5f7b7c83abb2febec4cf3dd991c37a4aaa35a7987e3c976a
6
+ metadata.gz: 076391a6883e1a5457bb6572cb24bbda2c5332e36ecd12e8a86672fab621ca72a006cefc98351502fe7ef3df17c424d66bdb4f82e3074d4f2ecc0cf5324dddec
7
+ data.tar.gz: 3458f3b113f4d67dfe49d1ebc7baffc9da0deb32a0064fe871431d7fb66446453f7fbaacbdf0da1f10d58cff56e2acfc8adbfb1228b77f86fcdfb6c9862ea0cd
data/.gitignore CHANGED
@@ -52,4 +52,7 @@ build/
52
52
 
53
53
  # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
54
54
  .rvmrc
55
- .project
55
+ .project
56
+
57
+ ## Dummy Project
58
+ /spec/dummy/
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # DynamicRouter
2
2
 
3
- [![Build Status](https://travis-ci.org/gberdugo/dynamic_router.png?branch=master)](https://travis-ci.org/gberdugo/dynamic_router) [![Gem Version](https://badge.fury.io/rb/dynamic_router.png)](http://badge.fury.io/rb/dynamic_router) [![Coverage Status](https://coveralls.io/repos/coyosoftware/dynamic_router/badge.png)](https://coveralls.io/r/coyosoftware/dynamic_router)
3
+ [![Build Status](https://travis-ci.org/coyosoftware/dynamic_router.svg?branch=master)](https://travis-ci.org/coyosoftware/dynamic_router) [![Gem Version](https://badge.fury.io/rb/dynamic_router.png)](http://badge.fury.io/rb/dynamic_router) [![Coverage Status](https://coveralls.io/repos/coyosoftware/dynamic_router/badge.png)](https://coveralls.io/r/coyosoftware/dynamic_router)
4
4
 
5
- Add dynamic routes to your project
5
+ Add dynamic routes based on model attributes
6
6
 
7
7
  ## Installation
8
8
 
@@ -20,11 +20,48 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- TODO: Write usage instructions here
23
+ Suppose you want to create a friendly URL for a resource based on fields of an example model:
24
+
25
+ class Example < ActiveRecord::Base
26
+ # This model has a field called 'url'
27
+ end
28
+
29
+ You can create a route to a resource using this field as URL, just use the notation:
30
+
31
+ class Example < ActiveRecord::Base
32
+ # This model has a field called 'url'
33
+
34
+ has_dynamic_route Proc.new {|example| "/#{example.url}"}, "dummy#dummy_method"
35
+ end
36
+
37
+ With this notation, when you create models like:
38
+
39
+ Example.create!(:url => "abc")
40
+ Example.create!(:url => "123")
41
+
42
+ The dynamic router will create the routes "/abc" and "/123" mapping to DummyController#dummy_method
43
+
44
+ You can pass the desired HTTP method also:
45
+
46
+ class Example < ActiveRecord::Base
47
+ # This model has a field called 'url'
48
+
49
+ has_dynamic_route Proc.new {|example| "/#{example.url}"}, "dummy#dummy_method", :method => :post
50
+ end
51
+
52
+ And can specify default values to be passed, like:
53
+
54
+ class Example < ActiveRecord::Base
55
+ # This model has two fields called 'url' and 'default_field'
56
+
57
+ has_dynamic_route Proc.new {|example| "/#{example.url}"}, "dummy#dummy_method", :defaults => {:some_value => Proc.new {|example| example.default_field}}
58
+ end
59
+
60
+ The dynamic router will map ALL records of the model on the startup and will create an after_save hook to create new routes as the models are created.
24
61
 
25
62
  ## Contributing
26
63
 
27
- 1. Fork it ( https://github.com/gberdugo/dynamic_router/fork )
64
+ 1. Fork it ( https://github.com/coyosoftware/dynamic_router/fork )
28
65
  2. Create your feature branch (`git checkout -b my-new-feature`)
29
66
  3. Commit your changes (`git commit -am 'Add some feature'`)
30
67
  4. Push to the branch (`git push origin my-new-feature`)
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["gberdugo@gmail.com"]
11
11
  spec.summary = %q{Add dynamic routes to your project}
12
12
  spec.description = %q{Use this gem to add dynamic routes to your project.}
13
- spec.homepage = "http://rubygems.org/gems/dynamic_router"
13
+ spec.homepage = "https://github.com/coyosoftware/dynamic_router"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
@@ -9,7 +9,7 @@ module DynamicRouter
9
9
  klass.find_each do |model|
10
10
  defaults = DynamicRouter::ClassMethods.parse_defaults(options[:defaults], model)
11
11
 
12
- self.send(method, url.call(model).to_s, :to => target.to_s, :defaults => defaults)
12
+ DynamicRouter::ClassMethods.draw_route(self, method, url.call(model).to_s, target.to_s, defaults)
13
13
  end
14
14
  end
15
15
  end
@@ -34,11 +34,16 @@ module DynamicRouter
34
34
  def create_route
35
35
  klass = self
36
36
 
37
- Rails.application.routes.draw do
37
+ _routes = Rails.application.routes
38
+ _routes.disable_clear_and_finalize = true
39
+
40
+ _routes.draw do
38
41
  defaults = DynamicRouter::ClassMethods.parse_defaults(klass._defaults, klass)
39
42
 
40
- self.send(klass._method, klass._url.call(klass).to_s, :to => klass._target.to_s, :defaults => defaults)
43
+ DynamicRouter::ClassMethods.draw_route(self, klass._method, klass._url.call(klass).to_s, klass._target.to_s, defaults)
41
44
  end
45
+
46
+ _routes.disable_clear_and_finalize = false
42
47
  end
43
48
  end
44
49
  end
@@ -55,5 +60,9 @@ module DynamicRouter
55
60
 
56
61
  defaults
57
62
  end
63
+
64
+ def draw_route(route, method, url, target, defaults)
65
+ route.send(method, url, :to => target, :defaults => defaults) unless url.blank? || url.gsub(/\//, "").blank?
66
+ end
58
67
  end
59
68
  end
@@ -1,3 +1,3 @@
1
1
  module DynamicRouter
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -6,7 +6,9 @@ RSpec.describe DynamicRouter do
6
6
  extend DynamicRouter
7
7
  end
8
8
 
9
+ Example.destroy_all
9
10
  Example.create!(:first_path => "path_a", :second_path => "path_a_a", :default_field => "default_value")
11
+ Example.create!(:first_path => "path_a", :second_path => "path_a_c", :default_field => "default_value")
10
12
  end
11
13
 
12
14
  it "should check the the existence of the table" do
@@ -22,6 +24,7 @@ RSpec.describe DynamicRouter do
22
24
  Example.send(:has_dynamic_route, Proc.new {|example| "/#{example.first_path}/#{example.second_path}"}, "dummy#dummy_method")
23
25
 
24
26
  expect(Rails.application.routes.recognize_path('/path_a/path_a_a', :method => :get))
27
+ expect(Rails.application.routes.recognize_path('/path_a/path_a_c', :method => :get))
25
28
  end
26
29
 
27
30
  it "should accept the method option" do
@@ -43,4 +46,19 @@ RSpec.describe DynamicRouter do
43
46
 
44
47
  expect(Rails.application.routes.recognize_path('/path_a/path_a_b', :method => :get))
45
48
  end
49
+
50
+ it "should not create the route if the url is blank" do
51
+ Example.send(:has_dynamic_route, Proc.new {|example| ""}, "dummy#dummy_method")
52
+
53
+ expect(Rails.application.routes.routes.named_routes).to be_empty
54
+ end
55
+
56
+ it "should not create the route if the url eval to blank" do
57
+ Example.send(:has_dynamic_route, Proc.new {|example| "/#{example.first_path}/#{example.second_path}"}, "dummy#dummy_method")
58
+
59
+ Example.create!(:first_path => "")
60
+
61
+ expect(Rails.application.routes.routes.named_routes.size).to eq(2)
62
+ expect(Rails.application.routes.routes.size).to eq(2)
63
+ end
46
64
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynamic_router
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gustavo Berdugo
@@ -150,7 +150,6 @@ files:
150
150
  - spec/dummy/config/locales/pt_BR.yml
151
151
  - spec/dummy/config/routes.rb
152
152
  - spec/dummy/db/test.sqlite3
153
- - spec/dummy/log/test.log
154
153
  - spec/dummy/public/404.html
155
154
  - spec/dummy/public/422.html
156
155
  - spec/dummy/public/500.html
@@ -159,7 +158,7 @@ files:
159
158
  - spec/dummy/script/rails
160
159
  - spec/dynamic_router_spec.rb
161
160
  - spec/spec_helper.rb
162
- homepage: http://rubygems.org/gems/dynamic_router
161
+ homepage: https://github.com/coyosoftware/dynamic_router
163
162
  licenses:
164
163
  - MIT
165
164
  metadata: {}
@@ -207,7 +206,6 @@ test_files:
207
206
  - spec/dummy/config/locales/pt_BR.yml
208
207
  - spec/dummy/config/routes.rb
209
208
  - spec/dummy/db/test.sqlite3
210
- - spec/dummy/log/test.log
211
209
  - spec/dummy/public/404.html
212
210
  - spec/dummy/public/422.html
213
211
  - spec/dummy/public/500.html