Skip to content

Commit

Permalink
Prepare for v0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed May 15, 2015
1 parent f2b99ae commit 8a9d801
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 32 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Lotus::Router
Rack compatible HTTP router for Ruby

## v0.4.0 - 2015-05-15
### Added
- [Alfonso Uceda Pompa] Nested RESTful resource(s)

### Changed
- [Alfonso Uceda Pompa] RESTful resource(s) have a correct pluralization/singularization for variables and named routes (eg. `/books/:id` is now `:book` instead of `:books`)

## v0.3.0 - 2015-03-23

## v0.2.1 - 2015-01-30
Expand Down
64 changes: 53 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,27 @@ router.resource 'profile', controller: 'identity'
router.path(:profile) # => /profile # Will route to Identity::Show
```

#### Nested Resources

We can nest resource(s):

```ruby
router = Lotus::Router.new
router.resource :identity do
resource :avatar
resources :api_keys
end

router.path(:identity_avatar) # => /identity/avatar
router.path(:new_identity_avatar) # => /identity/avatar/new
router.path(:edit_identity_avatar) # => /identity/avatar/new

router.path(:identity_api_keys) # => /identity/api_keys
router.path(:identity_api_key) # => /identity/api_keys/:id
router.path(:new_identity_api_key) # => /identity/api_keys/new
router.path(:edit_identity_api_key) # => /identity/api_keys/:id/edit
```



### RESTful Resources:
Expand Down Expand Up @@ -438,14 +459,14 @@ It will map:
<td>/flowers/:id</td>
<td>Flowers::Show</td>
<td>:show</td>
<td>:flowers</td>
<td>:flower</td>
</tr>
<tr>
<td>GET</td>
<td>/flowers/new</td>
<td>Flowers::New</td>
<td>:new</td>
<td>:new_flowers</td>
<td>:new_flower</td>
</tr>
<tr>
<td>POST</td>
Expand All @@ -459,29 +480,29 @@ It will map:
<td>/flowers/:id/edit</td>
<td>Flowers::Edit</td>
<td>:edit</td>
<td>:edit_flowers</td>
<td>:edit_flower</td>
</tr>
<tr>
<td>PATCH</td>
<td>/flowers/:id</td>
<td>Flowers::Update</td>
<td>:update</td>
<td>:flowers</td>
<td>:flower</td>
</tr>
<tr>
<td>DELETE</td>
<td>/flowers/:id</td>
<td>Flowers::Destroy</td>
<td>:destroy</td>
<td>:flowers</td>
<td>:flower</td>
</tr>
</table>


```ruby
router.path(:flowers) # => /flowers
router.path(:flowers, id: 23) # => /flowers/23
router.path(:edit_flowers, id: 23) # => /flowers/23/edit
router.path(:flowers) # => /flowers
router.path(:flower, id: 23) # => /flowers/23
router.path(:edit_flower, id: 23) # => /flowers/23/edit
```


Expand Down Expand Up @@ -512,8 +533,8 @@ router.resources 'flowers' do
end
end

router.path(:toggle_flowers, id: 23) # => /flowers/23/toggle
router.path(:search_flowers) # => /flowers/search
router.path(:toggle_flower, id: 23) # => /flowers/23/toggle
router.path(:search_flowers) # => /flowers/search
```


Expand All @@ -523,7 +544,28 @@ Configure controller:
router = Lotus::Router.new
router.resources 'blossoms', controller: 'flowers'

router.path(:blossoms, id: 23) # => /blossoms/23 # Will route to Flowers::Show
router.path(:blossom, id: 23) # => /blossoms/23 # Will route to Flowers::Show
```

#### Nested Resources

We can nest resource(s):

```ruby
router = Lotus::Router.new
router.resources :users do
resource :avatar
resources :favorites
end

router.path(:user_avatar, user_id: 1) # => /users/1/avatar
router.path(:new_user_avatar, user_id: 1) # => /users/1/avatar/new
router.path(:edit_user_avatar, user_id: 1) # => /users/1/avatar/edit

router.path(:user_favorites, user_id: 1) # => /users/1/favorites
router.path(:user_favorite, user_id: 1, id: 2) # => /users/1/favorites/2
router.path(:new_user_favorites, user_id: 1) # => /users/1/favorites/new
router.path(:edit_user_favorites, user_id: 1, id: 2) # => /users/1/favorites/2/edit
```

## Testing
Expand Down
2 changes: 1 addition & 1 deletion lib/lotus/router/version.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Lotus
class Router
# @since 0.1.0
VERSION = '0.3.0'.freeze
VERSION = '0.4.0'.freeze
end
end
12 changes: 6 additions & 6 deletions lib/lotus/routing/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Resource
include Utils::ClassAttribute

# @api private
# @since x.x.x
# @since 0.4.0
NESTED_ROUTES_SEPARATOR = '/'.freeze

# Set of default routes
Expand Down Expand Up @@ -48,7 +48,7 @@ class Resource
self.collection = Resource::CollectionAction

# @api private
# @since x.x.x
# @since 0.4.0
attr_reader :parent

# @api private
Expand All @@ -63,7 +63,7 @@ def initialize(router, name, options = {}, parent = nil, &blk)

# Allow nested resources inside resource or resources
#
# @since x.x.x
# @since 0.4.0
#
# @see Lotus::Router#resources
def resources(name, options = {}, &blk)
Expand All @@ -72,7 +72,7 @@ def resources(name, options = {}, &blk)

# Allow nested resource inside resource or resources
#
# @since x.x.x
# @since 0.4.0
#
# @see Lotus::Router#resource
def resource(name, options = {}, &blk)
Expand All @@ -82,15 +82,15 @@ def resource(name, options = {}, &blk)
# Return separator
#
# @api private
# @since x.x.x
# @since 0.4.0
def wildcard_param(route_param = nil)
NESTED_ROUTES_SEPARATOR
end

private

# @api private
# @since x.x.x
# @since 0.4.0
def _resource(klass, name, options, &blk)
options = options.merge(separator: @options[:separator], namespace: @options[:namespace])
klass.new(@router, [@name, name].join(NESTED_ROUTES_SEPARATOR), options, self, &blk)
Expand Down
8 changes: 4 additions & 4 deletions lib/lotus/routing/resource/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Action
# Nested routes separator
#
# @api private
# @since x.x.x
# @since 0.4.0
NESTED_ROUTES_SEPARATOR = '/'.freeze

# Ruby namespace where lookup for default subclasses.
Expand Down Expand Up @@ -246,7 +246,7 @@ def separator
# # Same for other action names
#
# @api private
# @since x.x.x
# @since 0.4.0
def controller_name
@options[:controller] || resource_name
end
Expand All @@ -256,7 +256,7 @@ def controller_name
# Singularize as (helper route)
#
# @api private
# @since x.x.x
# @since 0.4.0
def _singularized_as
resource_name.split(NESTED_ROUTES_SEPARATOR).map do |name|
Lotus::Utils::String.new(name).singularize
Expand All @@ -266,7 +266,7 @@ def _singularized_as
# Create nested rest path
#
# @api private
# @since x.x.x
# @since 0.4.0
def _nested_rest_path
Nested.new(resource_name, @resource).to_path
end
Expand Down
8 changes: 4 additions & 4 deletions lib/lotus/routing/resource/nested.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ class Resource
# Helper class to calculate nested path
#
# @api private
# @since x.x.x
# @since 0.4.0
class Nested
# @api private
# @since x.x.x
# @since 0.4.0
SEPARATOR = '/'.freeze

# @api private
# @since x.x.x
# @since 0.4.0
def initialize(resource_name, resource)
@resource_name = resource_name.to_s.split(SEPARATOR)
@resource = resource
Expand All @@ -20,7 +20,7 @@ def initialize(resource_name, resource)
end

# @api private
# @since x.x.x
# @since 0.4.0
def to_path
@path.reverse!.pop
@resource_name.zip(@path).flatten.join
Expand Down
4 changes: 2 additions & 2 deletions lib/lotus/routing/resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ class Resources < Resource
# @since 0.1.0
self.collection = Resources::CollectionAction

# Return wildcard param between slashs
# Return wildcard param between separators
#
# @api private
# @since x.x.x
# @since 0.4.0
def wildcard_param(route_param = nil)
"/:#{ Lotus::Utils::String.new(route_param).singularize }_id/"
end
Expand Down
4 changes: 2 additions & 2 deletions lib/lotus/routing/resources/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ class Action < Resource::Action
# Pluralize concrete actions
#
# @api private
# @since x.x.x
# @since 0.4.0
module PluralizedAction
private
# The name of the RESTful action.
#
# @api private
# @since x.x.x
# @since 0.4.0
def as
Lotus::Utils::String.new(super).pluralize
end
Expand Down
2 changes: 1 addition & 1 deletion lotus-router.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = '>= 2.0.0'

spec.add_dependency 'http_router', '~> 0.11'
spec.add_dependency 'lotus-utils', '~> 0.4'
spec.add_dependency 'lotus-utils', '~> 0.4', '>= 0.4.1'

spec.add_development_dependency 'bundler', '~> 1.5'
spec.add_development_dependency 'minitest', '~> 5'
Expand Down
2 changes: 1 addition & 1 deletion test/version_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

describe Lotus::Router::VERSION do
it 'exposes version' do
Lotus::Router::VERSION.must_equal '0.3.0'
Lotus::Router::VERSION.must_equal '0.4.0'
end
end

0 comments on commit 8a9d801

Please sign in to comment.