resources_id_replace 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +63 -0
- data/Rakefile +15 -0
- data/lib/resources_id_replace.rb +17 -0
- data/lib/resources_id_replace/version.rb +3 -0
- data/resources_id_replace.gemspec +21 -0
- data/test/resources_id_replace_test.rb +52 -0
- metadata +93 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Toru KAWAMURA
|
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,63 @@
|
|
1
|
+
# resources_id_replace
|
2
|
+
|
3
|
+
Replace param name of resources' id.
|
4
|
+
Useful for making user friendly URL.
|
5
|
+
|
6
|
+
## Example
|
7
|
+
|
8
|
+
You can use `:replace_id_with` option for `resources`.
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
resources :users, :replace_id_with => 'name'
|
12
|
+
```
|
13
|
+
|
14
|
+
```
|
15
|
+
users GET /users(.:format) users#index
|
16
|
+
POST /users(.:format) users#create
|
17
|
+
new_user GET /users/new(.:format) users#new
|
18
|
+
edit_user GET /users/:name/edit(.:format) users#edit
|
19
|
+
user GET /users/:name(.:format) users#show
|
20
|
+
PUT /users/:name(.:format) users#update
|
21
|
+
DELETE /users/:name(.:format) users#destroy
|
22
|
+
```
|
23
|
+
|
24
|
+
Good with the following codes.
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
class User < ActiveRecord::Base
|
28
|
+
def to_param
|
29
|
+
name
|
30
|
+
end
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
class UsersController < ApplicationController
|
36
|
+
def show
|
37
|
+
@user = User.find_by_name!(params[:name])
|
38
|
+
end
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
|
43
|
+
## Installation
|
44
|
+
|
45
|
+
Add this line to your application's Gemfile:
|
46
|
+
|
47
|
+
gem 'resources_id_replace'
|
48
|
+
|
49
|
+
And then execute:
|
50
|
+
|
51
|
+
$ bundle
|
52
|
+
|
53
|
+
Or install it yourself as:
|
54
|
+
|
55
|
+
$ gem install resources_id_replace
|
56
|
+
|
57
|
+
## Contributing
|
58
|
+
|
59
|
+
1. Fork it
|
60
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
61
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
62
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
63
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'bundler/gem_tasks'
|
3
|
+
|
4
|
+
require 'rake/testtask'
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << 'lib'
|
8
|
+
t.libs << 'test'
|
9
|
+
t.pattern = 'test/**/*_test.rb'
|
10
|
+
t.verbose = false
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
task :default => :test
|
15
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "resources_id_replace/version"
|
2
|
+
require 'action_dispatch'
|
3
|
+
|
4
|
+
ActionDispatch::Routing::Mapper::Resources::Resource.class_eval do
|
5
|
+
def id_replacer
|
6
|
+
(@options[:replace_id_with] || 'id').to_s
|
7
|
+
end
|
8
|
+
|
9
|
+
def member_scope
|
10
|
+
"#{path}/:#{id_replacer}"
|
11
|
+
end
|
12
|
+
|
13
|
+
def nested_scope
|
14
|
+
"#{path}/:#{singular}_#{id_replacer}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
ActionDispatch::Routing::Mapper::Resources::RESOURCE_OPTIONS << :replace_id_with
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'resources_id_replace/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "resources_id_replace"
|
8
|
+
gem.version = ResourcesIdReplace::VERSION
|
9
|
+
gem.authors = ["Toru KAWAMURA"]
|
10
|
+
gem.email = ["tkawa@4bit.net"]
|
11
|
+
gem.description = %q{Replace param name of resources' id}
|
12
|
+
gem.summary = %q{Replace param name of resources' id}
|
13
|
+
gem.homepage = "https://github.com/tkawa/resources_id_replace"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
17
|
+
gem.require_paths = ["lib"]
|
18
|
+
|
19
|
+
gem.add_dependency 'actionpack', '>= 3.0.0'
|
20
|
+
gem.add_development_dependency 'rake'
|
21
|
+
end
|
@@ -0,0 +1,52 @@
|
|
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 'resources_id_replace'
|
11
|
+
|
12
|
+
class UsersController < ActionController::Base
|
13
|
+
def show
|
14
|
+
head params[:name] ? :ok : :not_found
|
15
|
+
end
|
16
|
+
end
|
17
|
+
class CommentsController < ActionController::Base
|
18
|
+
def index
|
19
|
+
head params[:user_name] ? :ok : :not_found
|
20
|
+
end
|
21
|
+
def show
|
22
|
+
head params[:user_name] && params[:id] ? :ok : :not_found
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class ResourcesIdReplaceTest < ActionDispatch::IntegrationTest
|
27
|
+
Routes = ActionDispatch::Routing::RouteSet.new.tap do |app|
|
28
|
+
app.draw do
|
29
|
+
resources :users, :replace_id_with => 'name' do
|
30
|
+
resources :comments
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
include Routes.url_helpers
|
36
|
+
def app; Routes end
|
37
|
+
|
38
|
+
test "accessing user by name instead of id" do
|
39
|
+
get "/users/tkawa"
|
40
|
+
assert_equal "200", @response.code
|
41
|
+
end
|
42
|
+
|
43
|
+
test "accessing subresource by name instead of id" do
|
44
|
+
get "/users/tkawa/comments"
|
45
|
+
assert_equal "200", @response.code
|
46
|
+
end
|
47
|
+
|
48
|
+
test "accessing member of subresource by id" do
|
49
|
+
get "/users/tkawa/comments/1"
|
50
|
+
assert_equal "200", @response.code
|
51
|
+
end
|
52
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: resources_id_replace
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Toru KAWAMURA
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-29 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: actionpack
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.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: 3.0.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
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
|
+
description: Replace param name of resources' id
|
47
|
+
email:
|
48
|
+
- tkawa@4bit.net
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- lib/resources_id_replace.rb
|
59
|
+
- lib/resources_id_replace/version.rb
|
60
|
+
- resources_id_replace.gemspec
|
61
|
+
- test/resources_id_replace_test.rb
|
62
|
+
homepage: https://github.com/tkawa/resources_id_replace
|
63
|
+
licenses: []
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
hash: -2010132676723267050
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
hash: -2010132676723267050
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 1.8.24
|
89
|
+
signing_key:
|
90
|
+
specification_version: 3
|
91
|
+
summary: Replace param name of resources' id
|
92
|
+
test_files:
|
93
|
+
- test/resources_id_replace_test.rb
|