Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tkawa committed Nov 29, 2012
0 parents commit dabdd02
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'
gemspec

gem 'rails', '>= 3.0.0'
22 changes: 22 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2012 Toru KAWAMURA

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# ResourcesIdReplace

TODO: Write a gem description

## Installation

Add this line to your application's Gemfile:

gem 'resources_id_replace'

And then execute:

$ bundle

Or install it yourself as:

$ gem install resources_id_replace

## Usage

TODO: Write usage instructions here

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
15 changes: 15 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'bundler/setup'
require 'bundler/gem_tasks'

require 'rake/testtask'

Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = false
end


task :default => :test

17 changes: 17 additions & 0 deletions lib/resources_id_replace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require "resources_id_replace/version"
require 'action_dispatch'

ActionDispatch::Routing::Mapper::Resources::Resource.class_eval do
def id_replacer
(@options[:replace_id_with] || 'id').to_s
end

def member_scope
"#{path}/:#{id_replacer}"
end

def nested_scope
"#{path}/:#{singular}_#{id_replacer}"
end
end
ActionDispatch::Routing::Mapper::Resources::RESOURCE_OPTIONS << :replace_id_with
3 changes: 3 additions & 0 deletions lib/resources_id_replace/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module ResourcesIdReplace
VERSION = "0.0.1"
end
21 changes: 21 additions & 0 deletions resources_id_replace.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- encoding: utf-8 -*-
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'resources_id_replace/version'

Gem::Specification.new do |gem|
gem.name = "resources_id_replace"
gem.version = ResourcesIdReplace::VERSION
gem.authors = ["Toru KAWAMURA"]
gem.email = ["[email protected]"]
gem.description = %q{Replace path name of resources' id}
gem.summary = %q{Replace path name of resources' id}
gem.homepage = "https://github.com/tkawa/resources_id_replace"

gem.files = `git ls-files`.split($/)
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.require_paths = ["lib"]

gem.add_dependency 'actionpack', '>= 3.0.0'
gem.add_development_dependency 'rake'
end
52 changes: 52 additions & 0 deletions test/resources_id_replace_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Configure Rails Environment
ENV["RAILS_ENV"] = "test"

require 'test/unit'

require 'abstract_controller'
require 'action_controller'
require 'action_dispatch'

require 'resources_id_replace'

class UsersController < ActionController::Base
def show
head params[:name] ? :ok : :not_found
end
end
class CommentsController < ActionController::Base
def index
head params[:user_name] ? :ok : :not_found
end
def show
head params[:user_name] && params[:id] ? :ok : :not_found
end
end

class ResourcesIdReplaceTest < ActionDispatch::IntegrationTest
Routes = ActionDispatch::Routing::RouteSet.new.tap do |app|
app.draw do
resources :users, :replace_id_with => 'name' do
resources :comments
end
end
end

include Routes.url_helpers
def app; Routes end

test "accessing user by name instead of id" do
get "/users/tkawa"
assert_equal "200", @response.code
end

test "accessing subresource by name instead of id" do
get "/users/tkawa/comments"
assert_equal "200", @response.code
end

test "accessing member of subresource by id" do
get "/users/tkawa/comments/1"
assert_equal "200", @response.code
end
end

0 comments on commit dabdd02

Please sign in to comment.