Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kanety/params_keeper_rails
A rails gem for keeping specific parameters through links
https://github.com/kanety/params_keeper_rails
Last synced: 2 months ago
JSON representation
A rails gem for keeping specific parameters through links
- Host: GitHub
- URL: https://github.com/kanety/params_keeper_rails
- Owner: kanety
- License: mit
- Created: 2017-08-28T13:41:24.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-10-21T00:43:20.000Z (3 months ago)
- Last Synced: 2024-10-21T04:07:34.621Z (3 months ago)
- Language: Ruby
- Size: 49.8 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# ParamsKeeperRails
A rails controller extension for keeping specific parameters through links.
## Dependencies
* ruby 2.3+
* rails 5.0+## Installation
Add this line to your application's Gemfile:
```ruby
gem 'params_keeper_rails'
```And then execute:
$ bundle
## Usage
Include modules in your controller and specify parameter keys you want to keep:
```ruby
class ExamplesController < ApplicationController
include ParamsKeeper::Controller
keep_params :key1, :key2
```Parameters are kept via `url_for` if destination controller is same as current controller.
For example:```ruby
GET "/examples?key1=**&key2=**"# hash arg
url_for(action: :index) #=> '/examples?key1=**&key2=**'# parameters are not kept if string or model arg is specified
url_for('/examples') #=> '/examples'
url_for(@example) #=> '/examples/:id'# parameters are not kept if destination controller is different from current controller
url_for(controller: 'examples2', action: :index) #=> '/examples2'# parameters are not kept if you put "keep_params: false" into args
url_for(action: :show, keep_params: false) #=> '/examples/:id'
```Parameters are kept for form with GET method via hidden fields.
This feature is supported by `form_with` for rails >= 5.1.```ruby
<%= form_with url: { action: :index}, method: :get do %>
<%= submit_tag 'submit' %>
<% end %>
#=> ...
#
```## Options
### Argument type
Enable only specific argument type of url_for:
```ruby
# hash arg (same as default behaviour)
keep_params :key1, :key2, for: :hash
url_for(action: :index) #=> '/examples?key1=**&key2=**'# string arg
keep_params :key1, :key2, for: :string
url_for('/examples') #=> '/examples?key1=**&key2=**'# model arg
keep_params :key1, :key2, for: :model
url_for(@example) #=> '/examples/:id?key1=**&key2=**'
````:for` allows to set multiple argument type as follows:
```ruby
keep_params :key1, :key2, for: [:hash, :model]
```### Multiple controllers
Keep parameters throught multiple controllers:
```ruby
class ExamplesController < ApplicationController
include ParamsKeeper::Controller
keep_params :key1, :key2, to: %w(examples examples2)
endclass Examples2Controller < ApplicationController
include ParamsKeeper::Controller
keep_params :key1, :key2, to: %w(examples examples2)
end
```### Default parameters
Specify default parameters:
```ruby
keep_params :key1, :key2, url_options: { fixed_param: :something }
```## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/kanety/params_keeper_rails.
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).