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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7f1ebc6d621b453c84faed0bb13d8308b3b41253
4
- data.tar.gz: 605fb7c44ebfaf312147b27f622e70f6c15bf15d
3
+ metadata.gz: 5083f6f53551ab2b5b71297b68cf0051782d7467
4
+ data.tar.gz: 60b5fb5c276e951281ec0c4aa1ba7a1cc06e7349
5
5
  SHA512:
6
- metadata.gz: 7c2ff21c3643e02cc1a599284c8bb88bca5d2d6dfaae291e95380bab1e6f9121aef714f4e45d201427554dbedb920ea16a331395ff483b13aa3ca0b6d2a08d4b
7
- data.tar.gz: 99dcf89a95b529de76053d9c29225d4c28d09ae094f765c9d0cdedc5c1e06df0185877169996f48e4b14f8b92df8dd4f311e46c77569efbc2591afb8eff5a968
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.1
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. Its 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
+ 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
- send("#{route_name}_url", default_url_options)
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
- send("#{route_name}_path")
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.all.each do |record|
5
+ model.routable.each do |record|
6
6
  get record.route,
7
7
  to: record.controller_action,
8
8
  defaults: { id: record.id },
@@ -1,3 +1,4 @@
1
1
  module FlowmorRouter
2
2
  class UnroutableRecord < RuntimeError; end
3
+ class UnroutedRecord < RuntimeError; end
3
4
  end
@@ -1,3 +1,3 @@
1
1
  module FlowmorRouter
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1,9 @@
1
+ class ArticleController < ApplicationController
2
+ before_action :set_article, only: [:show]
3
+
4
+ private
5
+
6
+ def set_article
7
+ @article = Article.find(params[:id])
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  class Article < RoutableRecord
2
-
2
+ scope :routable, -> { where(published: true) }
3
3
  end
@@ -0,0 +1 @@
1
+ <h1><%= @article.title %></h1>
@@ -0,0 +1,5 @@
1
+ class AddPublishedToArticles < ActiveRecord::Migration
2
+ def change
3
+ add_column :articles, :published, :boolean
4
+ end
5
+ end
@@ -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: 20140811184010) do
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|
Binary file