Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kanety/resource_url_helper
https://github.com/kanety/resource_url_helper
Last synced: 8 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/kanety/resource_url_helper
- Owner: kanety
- License: mit
- Created: 2021-07-23T07:34:17.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-07-23T09:04:42.000Z (over 3 years ago)
- Last Synced: 2024-12-25T04:04:44.881Z (23 days ago)
- Language: Ruby
- Size: 12.7 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# ResourceUrlHelper
Complement required url parameters from resource.
## Dependencies
* ruby 2.7+
* rails 6.0+## Installation
Add this line to your application's Gemfile:
```ruby
gem 'resource_url_helper'
```Then execute:
$ bundle
## Usage
Prepare model and it's route:
```ruby
class Comment < ActiveRecord::Base
endRails.application.routes.draw do
resources :comments, path: 'items/:item_id/comments' # :item_id is required
end
```Use `resource_url_for` as follows:
```ruby
comment = Comment.find(1)
comment.item_id = 2# `resource_url_for` complements :itemt_id from comment attributes automatically
Rails.application.routes.url_helpers.resource_url_for(comment, only_path: true)
#=> "/items/2/comments/1"# Against upper example, `url_for` needs :item_id explicitly
Rails.application.routes.url_helpers.url_for(comment, item_id: comment.item_id, only_path: true)
```You can use `resource_url_for` in your controller or view:
```ruby
class CommentsController < ActionController::Base
def show
comment = Comment.find(1)
comment.item_id = 2resource_url_for(comment)
#=> "/items/2/comments/1"
end
end
```### Complementation from resource association
This gem supports complementation from resource association:
```ruby
class Association::Comment
belongs_to :item
endRails.application.routes.draw do
resources :association_comments, path: 'items/:item/comments' # :item is required
endcomment = Association::Comment.find(1)
comment.item = Item.find(2)Rails.application.routes.url_helpers.resource_url_for(comment, only_path: true)
#=> "/items/2/comments/1"
```In case associated object is not found, `0` is filled as follows:
```ruby
comment = Association::Comment.find(1)
comment.item = nilRails.application.routes.url_helpers.resource_url_for(comment, only_path: true)
#=> "/items/0/comments/1"
```## Contributing
Bug reports and pull requests are welcome at https://github.com/kanety/resource_url_helper.
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).