flowmor_router 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -4
- data/app/models/routable_record.rb +12 -2
- data/config/routes.rb +1 -1
- data/lib/flowmor_router/exceptions.rb +1 -0
- data/lib/flowmor_router/version.rb +1 -1
- data/test/dummy/app/controllers/article_controller.rb +9 -0
- data/test/dummy/app/models/article.rb +1 -1
- data/test/dummy/app/views/article/show.html.erb +1 -0
- data/test/dummy/db/migrate/20140818105255_add_published_to_articles.rb +5 -0
- data/test/dummy/db/schema.rb +2 -1
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/test.log +4062 -0
- data/test/dummy/test/integration/routable_records_test.rb +13 -0
- data/test/dummy/test/models/article_test.rb +3 -1
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5083f6f53551ab2b5b71297b68cf0051782d7467
|
4
|
+
data.tar.gz: 60b5fb5c276e951281ec0c4aa1ba7a1cc06e7349
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f2503a0cb8da3e53c654bf9b6d5d64e202d36bd55e52c2863dd0605a76baa97f417cb1410f423a754c7e1caca8a756562b296d4a910afa254675631bfe414bd3
|
7
|
+
data.tar.gz: 9ffcd42582e48d404bc521647cdb1f034c799fd8287291562656f7f03e741d52b150ef766e472598da68883866fac27e594dc62ac2a2106994bbad9f14c9a510
|
data/README.md
CHANGED
@@ -34,15 +34,16 @@ Every model instance's route is named after the model and name.
|
|
34
34
|
|
35
35
|
## State of the project
|
36
36
|
|
37
|
-
### 0.0.
|
37
|
+
### 0.0.3
|
38
|
+
### Tested and Works on Rails 4.x and Ruby 2.x
|
38
39
|
|
39
|
-
Its got enough functionality to work really well for me [(mwlang)](https://github.com/mwlang) in its current form.
|
40
|
+
Its got enough functionality to work really well for me [(mwlang)](https://github.com/mwlang) in its current form. It's a simple implementation with relatively few lines of code, adequately test covered. It works and is used in production on a handful of sites. You can see it in action on [my personal site](http://codeconnoisseur.org) and [business site](http://cybrains.net).
|
40
41
|
|
41
42
|
### Is it For You?
|
42
43
|
|
43
|
-
This isn't for everyone. It does build routes from objects in the database. It does provide functionality similar to [friendly_id](https://github.com/norman/friendly_id) or by simply redefining the id of a model with AR's #to_param. If you run multiple instances of an application, you'll need to take care of syncing when the database is updated. The simplest way to do this is by adding Post.reload_routes (for example) to the before_filter callback of the controller. Sounds like a performance killer, but its really not. Just think every time you refresh during development that the routes are reloaded!
|
44
|
-
|
44
|
+
This isn't for everyone. It does build routes from objects in the database. Rails purists will argue this method pollutes the routes space. It does provide functionality similar to [friendly_id](https://github.com/norman/friendly_id) or by simply redefining the id of a model with AR's #to_param. If you run multiple instances of an application, you'll need to take care of syncing when the database is updated. The simplest way to do this is by adding Post.reload_routes (for example) to the before_filter callback of the controller. Sounds like a performance killer, but its really not. Just think every time you refresh during development that the routes are reloaded!
|
45
45
|
|
46
|
+
On the other hand, this approach allows you a lot of flexibility to creating truly custom routes in your app. It also allows you to avoid using a global "match any" in your config/routes.rb. A use case is porting over a WordPress site to Rails where there was a highly customized permalink structure in place.
|
46
47
|
|
47
48
|
### To Install
|
48
49
|
|
@@ -143,6 +144,14 @@ end
|
|
143
144
|
|
144
145
|
If you need to get any fancier than that, then just about everything you need can be found in the [app/models/routable_record.rb](https://github.com/mwlang/flowmor_router/blob/master/app/models/routable_record.rb) implementation.
|
145
146
|
|
147
|
+
By default, all RoutableRecord instances are added to the routes table. What gets routed can be customized by overriding the :routable scope.
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
class Article < RoutableRecord
|
151
|
+
scope :routable, -> { where published: true }
|
152
|
+
# ...
|
153
|
+
end
|
154
|
+
```
|
146
155
|
### TODO and Contributing
|
147
156
|
|
148
157
|
This is largely an extraction of functionality from multiple Rails projects. As such, it has the features I needed to fully extract to the engine. However, some possible enhancements came to mind as I pulled this together:
|
@@ -1,6 +1,8 @@
|
|
1
1
|
class RoutableRecord < ActiveRecord::Base
|
2
2
|
self.abstract_class = true
|
3
3
|
|
4
|
+
scope :routable, -> {}
|
5
|
+
|
4
6
|
def self.route_model
|
5
7
|
name.underscore.downcase
|
6
8
|
end
|
@@ -90,11 +92,19 @@ class RoutableRecord < ActiveRecord::Base
|
|
90
92
|
end
|
91
93
|
|
92
94
|
def url
|
93
|
-
|
95
|
+
begin
|
96
|
+
send("#{route_name}_url", default_url_options)
|
97
|
+
rescue NoMethodError
|
98
|
+
raise FlowmorRouter::UnroutedRecord.new("#{self.inspect} was not routed.")
|
99
|
+
end
|
94
100
|
end
|
95
101
|
|
96
102
|
def path
|
97
|
-
|
103
|
+
begin
|
104
|
+
send("#{route_name}_path")
|
105
|
+
rescue NoMethodError
|
106
|
+
raise FlowmorRouter::UnroutedRecord.new("#{self.inspect} was not routed.")
|
107
|
+
end
|
98
108
|
end
|
99
109
|
|
100
110
|
def new_name_value
|
data/config/routes.rb
CHANGED
@@ -2,7 +2,7 @@ Rails.application.routes.draw do
|
|
2
2
|
|
3
3
|
# Routes from RoutableRecord descendants
|
4
4
|
RoutableRecord.descendants.each do |model|
|
5
|
-
model.
|
5
|
+
model.routable.each do |record|
|
6
6
|
get record.route,
|
7
7
|
to: record.controller_action,
|
8
8
|
defaults: { id: record.id },
|
@@ -0,0 +1 @@
|
|
1
|
+
<h1><%= @article.title %></h1>
|
data/test/dummy/db/schema.rb
CHANGED
@@ -11,13 +11,14 @@
|
|
11
11
|
#
|
12
12
|
# It's strongly recommended that you check this file into your version control system.
|
13
13
|
|
14
|
-
ActiveRecord::Schema.define(version:
|
14
|
+
ActiveRecord::Schema.define(version: 20140818105255) do
|
15
15
|
|
16
16
|
create_table "articles", force: true do |t|
|
17
17
|
t.string "title"
|
18
18
|
t.string "name"
|
19
19
|
t.datetime "created_at"
|
20
20
|
t.datetime "updated_at"
|
21
|
+
t.boolean "published"
|
21
22
|
end
|
22
23
|
|
23
24
|
create_table "news_articles", force: true do |t|
|
data/test/dummy/db/test.sqlite3
CHANGED
Binary file
|