routing_concerns 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.md +103 -0
- data/Rakefile +38 -0
- data/lib/action_dispatch/routing/concerns.rb +56 -0
- data/lib/active_model/forbidden_attributes_protection.rb +16 -0
- data/lib/routing_concerns.rb +2 -0
- data/lib/routing_concerns/railtie.rb +1 -0
- data/lib/routing_concerns/version.rb +3 -0
- data/test/routing_concerns_test.rb +43 -0
- metadata +99 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2012 David Heinemeier Hansson
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
Routing Concerns
|
2
|
+
================
|
3
|
+
|
4
|
+
Abstract common routing resource concerns to cut down on duplication.
|
5
|
+
|
6
|
+
Code before:
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
BCX::Application.routes.draw do
|
10
|
+
resources :calendar_events do
|
11
|
+
get :past, on: :collection
|
12
|
+
resources :comments
|
13
|
+
end
|
14
|
+
|
15
|
+
resources :messages { resources :comments }
|
16
|
+
resources :forwards { resources :comments }
|
17
|
+
resources :uploads { resources :comments }
|
18
|
+
resources :documents { resources :comments }
|
19
|
+
resources :todos { resources :comments }
|
20
|
+
|
21
|
+
resources :projects, defaults: { bucket_type: 'project' } do
|
22
|
+
post :trash, :restore, on: :member
|
23
|
+
|
24
|
+
resources :messages, except: [ :new ] do
|
25
|
+
post :trash, :restore, on: :member
|
26
|
+
resources :image_attachments, only: :index
|
27
|
+
end
|
28
|
+
|
29
|
+
resources :forwards do
|
30
|
+
member do
|
31
|
+
get :content
|
32
|
+
post :trash, :restore
|
33
|
+
end
|
34
|
+
|
35
|
+
resources :image_attachments, only: :index
|
36
|
+
end
|
37
|
+
|
38
|
+
resources :uploads do
|
39
|
+
post :trash, :restore, on: :member
|
40
|
+
resources :image_attachments, only: :index
|
41
|
+
end
|
42
|
+
|
43
|
+
resources :todolists do
|
44
|
+
get :more, :completed, on: :collection
|
45
|
+
post :trash, :restore, on: :member
|
46
|
+
end
|
47
|
+
|
48
|
+
resources :todos do
|
49
|
+
post :toggle, :trash, :restore, on: :member
|
50
|
+
end
|
51
|
+
|
52
|
+
resources :comments do
|
53
|
+
post :trash, on: :member
|
54
|
+
resources :image_attachments, only: :index
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
Code after:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
BCX::Application.routes.draw do
|
64
|
+
concern :commentable do
|
65
|
+
resources :comments
|
66
|
+
end
|
67
|
+
|
68
|
+
concern :trashable do
|
69
|
+
post :trash, :restore, on: :member
|
70
|
+
end
|
71
|
+
|
72
|
+
concern :image_attachable do
|
73
|
+
resources :image_attachments, only: :index
|
74
|
+
end
|
75
|
+
|
76
|
+
resources :calendar_events, concerns: :commentable do
|
77
|
+
get :past, on: :collection
|
78
|
+
end
|
79
|
+
|
80
|
+
resources :messages, :forwards, :uploads, :documents, :todos, concerns: :commentable
|
81
|
+
|
82
|
+
resources :projects, concerns: :trashable, defaults: { bucket_type: 'project' } do
|
83
|
+
resources :messages, :uploads, :comments, concerns: [:trashable, :image_attachable]
|
84
|
+
|
85
|
+
resources :forwards, concerns: [:trashable, :image_attachable] do
|
86
|
+
get :content, on: :member
|
87
|
+
end
|
88
|
+
|
89
|
+
resources :todolists, concerns: :trashable do
|
90
|
+
get :more, :completed, on: :collection
|
91
|
+
end
|
92
|
+
|
93
|
+
resources :todos, concerns: :trashable do
|
94
|
+
post :toggle, on: :member
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
```
|
99
|
+
|
100
|
+
Compatibility
|
101
|
+
-------------
|
102
|
+
|
103
|
+
This plugin was designed as a proof-of-concept for a feature that's destined for Rails 4. It has only been tested on Rails 3.2+, but may work on earlier versions as well.
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'RoutingConcerns'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
Bundler::GemHelper.install_tasks
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
|
30
|
+
Rake::TestTask.new(:test) do |t|
|
31
|
+
t.libs << 'lib'
|
32
|
+
t.libs << 'test'
|
33
|
+
t.pattern = 'test/**/*_test.rb'
|
34
|
+
t.verbose = false
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
task :default => :test
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'action_dispatch'
|
2
|
+
|
3
|
+
module ActionDispatch::Routing::Mapper::Concerns
|
4
|
+
def concern(name, &block)
|
5
|
+
@concerns ||= {}
|
6
|
+
@concerns[name] = block
|
7
|
+
end
|
8
|
+
|
9
|
+
def concerns(*names)
|
10
|
+
Array(names).flatten.compact.each do |name|
|
11
|
+
if @concerns && concern = @concerns[name]
|
12
|
+
instance_eval(&concern)
|
13
|
+
else
|
14
|
+
raise "No concern named #{name} was found!"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module ActionDispatch::Routing::Mapper::ResourcesWithConcerns
|
21
|
+
extend ActiveSupport::Concern
|
22
|
+
|
23
|
+
included do
|
24
|
+
alias_method_chain :resource, :concerns
|
25
|
+
alias_method_chain :resources, :concerns
|
26
|
+
end
|
27
|
+
|
28
|
+
def resource_with_concerns(*resources, &block)
|
29
|
+
if (options_with_concerns = resources.last).is_a?(Hash)
|
30
|
+
named_concerns = options_with_concerns[:concerns]
|
31
|
+
|
32
|
+
resource_without_concerns(*resources) do
|
33
|
+
concerns(named_concerns)
|
34
|
+
block.call if block_given?
|
35
|
+
end
|
36
|
+
else
|
37
|
+
resource_without_concerns(*resources, &block)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def resources_with_concerns(*resources, &block)
|
42
|
+
if (options_with_concerns = resources.last).is_a?(Hash)
|
43
|
+
named_concerns = options_with_concerns[:concerns]
|
44
|
+
|
45
|
+
resources_without_concerns(*resources) do
|
46
|
+
concerns(named_concerns)
|
47
|
+
block.call if block_given?
|
48
|
+
end
|
49
|
+
else
|
50
|
+
resources_without_concerns(*resources, &block)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
ActionDispatch::Routing::Mapper.send :include, ActionDispatch::Routing::Mapper::Concerns
|
56
|
+
ActionDispatch::Routing::Mapper.send :include, ActionDispatch::Routing::Mapper::ResourcesWithConcerns
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module ActiveModel
|
2
|
+
class ForbiddenAttributes < StandardError
|
3
|
+
end
|
4
|
+
|
5
|
+
module ForbiddenAttributesProtection
|
6
|
+
def sanitize_for_mass_assignment(new_attributes, options = {})
|
7
|
+
if !new_attributes.respond_to?(:permitted?) || (new_attributes.respond_to?(:permitted?) && new_attributes.permitted?)
|
8
|
+
super
|
9
|
+
else
|
10
|
+
raise ActiveModel::ForbiddenAttributes
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
ActiveModel.autoload :ForbiddenAttributesProtection
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'rails/railtie'
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# Configure Rails Environment
|
2
|
+
ENV["RAILS_ENV"] = "test"
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
|
6
|
+
require 'abstract_controller'
|
7
|
+
require 'action_controller'
|
8
|
+
require 'action_dispatch'
|
9
|
+
|
10
|
+
require 'routing_concerns'
|
11
|
+
|
12
|
+
class CommentsController < ActionController::Base
|
13
|
+
def index
|
14
|
+
head :ok
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class RoutingConcernsTest < ActionDispatch::IntegrationTest
|
19
|
+
Routes = ActionDispatch::Routing::RouteSet.new.tap do |app|
|
20
|
+
app.draw do
|
21
|
+
concern :commentable do
|
22
|
+
resources :comments
|
23
|
+
end
|
24
|
+
|
25
|
+
resources :posts, concerns: :commentable
|
26
|
+
resource :post, concerns: :commentable
|
27
|
+
resources :comments
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
include Routes.url_helpers
|
32
|
+
def app; Routes end
|
33
|
+
|
34
|
+
test "accessing concern from resources" do
|
35
|
+
get "/posts/1/comments"
|
36
|
+
assert_equal "200", @response.code
|
37
|
+
end
|
38
|
+
|
39
|
+
test "accessing concern from resource" do
|
40
|
+
get "/post/comments"
|
41
|
+
assert_equal "200", @response.code
|
42
|
+
end
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: routing_concerns
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- David Heinemeier Hansson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: actionpack
|
16
|
+
requirement: &2152297360 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.2.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2152297360
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activemodel
|
27
|
+
requirement: &2152296340 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 3.2.0
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2152296340
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: railties
|
38
|
+
requirement: &2152295060 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 3.2.0
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *2152295060
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rake
|
49
|
+
requirement: &2152294660 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *2152294660
|
58
|
+
description:
|
59
|
+
email:
|
60
|
+
- david@heinemeierhansson.com
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- lib/action_dispatch/routing/concerns.rb
|
66
|
+
- lib/active_model/forbidden_attributes_protection.rb
|
67
|
+
- lib/routing_concerns/railtie.rb
|
68
|
+
- lib/routing_concerns/version.rb
|
69
|
+
- lib/routing_concerns.rb
|
70
|
+
- MIT-LICENSE
|
71
|
+
- Rakefile
|
72
|
+
- README.md
|
73
|
+
- test/routing_concerns_test.rb
|
74
|
+
homepage:
|
75
|
+
licenses: []
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ! '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 1.8.7
|
95
|
+
signing_key:
|
96
|
+
specification_version: 3
|
97
|
+
summary: Routing concerns for Action Pack
|
98
|
+
test_files:
|
99
|
+
- test/routing_concerns_test.rb
|