{"id":15288844,"url":"https://github.com/pienkowb/preserve","last_synced_at":"2025-04-13T06:32:03.412Z","repository":{"id":51117906,"uuid":"51608342","full_name":"pienkowb/preserve","owner":"pienkowb","description":"Persist parameter values between requests","archived":false,"fork":false,"pushed_at":"2024-10-05T18:38:19.000Z","size":150,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"develop","last_synced_at":"2024-10-15T19:41:06.828Z","etag":null,"topics":["rails","ruby","ruby-on-rails"],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pienkowb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-02-12T18:39:19.000Z","updated_at":"2024-10-05T18:38:03.000Z","dependencies_parsed_at":"2024-10-26T13:57:17.125Z","dependency_job_id":"f47d54c8-4a09-4c2e-99a4-cf17e1555a94","html_url":"https://github.com/pienkowb/preserve","commit_stats":{"total_commits":97,"total_committers":2,"mean_commits":48.5,"dds":"0.010309278350515427","last_synced_commit":"ac5c883f4e530d6488a0e3c91d14d794ffce7f69"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pienkowb%2Fpreserve","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pienkowb%2Fpreserve/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pienkowb%2Fpreserve/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pienkowb%2Fpreserve/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pienkowb","download_url":"https://codeload.github.com/pienkowb/preserve/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248674659,"owners_count":21143760,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["rails","ruby","ruby-on-rails"],"created_at":"2024-09-30T15:53:22.420Z","updated_at":"2025-04-13T06:32:03.365Z","avatar_url":"https://github.com/pienkowb.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Preserve [![Gem version](https://img.shields.io/gem/v/preserve)](https://rubygems.org/gems/preserve) [![Build status](https://img.shields.io/github/actions/workflow/status/pienkowb/preserve/test.yml?branch=develop)](https://github.com/pienkowb/preserve/actions/workflows/test.yml?query=branch%3Adevelop) [![Coverage status](https://img.shields.io/coveralls/github/pienkowb/preserve/develop)](https://coveralls.io/github/pienkowb/preserve)\n\nPreserve is a Rails plugin which stores selected parameters in the session to make them available in subsequent requests.\n\n## Installation\n\nAdd the following line to your application's Gemfile:\n\n```ruby\ngem 'preserve'\n```\n\nInstall the gem with Bundler:\n\n```\n$ bundle install\n```\n\nOr do it manually by running:\n\n```\n$ gem install preserve\n```\n\n## Usage\n\nFor the sole purpose of this example, let's assume we have a Rails application with a controller showing all parameters sent in a request.\n\n```ruby\nclass ParametersController \u003c ApplicationController\n  def index\n    render json: parameters\n  end\n\n  private\n\n  def parameters\n    params.except(:controller, :action)\n  end\nend\n```\n\nRoutes are declared as following:\n\n```ruby\nRails.application.routes.draw do\n  resources :parameters, only: :index\nend\n```\n\nLet's start the application and test its behavior using [cURL](https://curl.haxx.se/).\nThe whole concept is based on the session, so in order for this to work the cookie engine must be enabled (hence the `-c` and `-b` options).\n\nIn the first request, the `status` parameter is set to `active`.\n\n```\n$ curl -c cookies http://localhost:3000/parameters?status=active\n```\n```json\n{\"status\":\"active\"}\n```\n\nAs expected, the application returns the parameter and its value.\n\nThe next request is sent without any parameters.\n\n```\n$ curl -b cookies http://localhost:3000/parameters\n```\n```json\n{}\n```\n\nObviously, the `status` parameter is no longer available.\n\nNow, let's call the `preserve` macro inside the `ParametersController` class with the parameter name as an argument.\n\n```ruby\nclass ParametersController \u003c ApplicationController\n  preserve :status\n\n  # ...\nend\n```\n\nSending the same two requests again yields a different result.\n\n```\n$ curl -c cookies http://localhost:3000/parameters?status=active\n```\n```json\n{\"status\":\"active\"}\n```\n\n```\n$ curl -b cookies http://localhost:3000/parameters\n```\n```json\n{\"status\":\"active\"}\n```\n\nThis time, the `status` parameter is still available when the second request is made, even though it wasn't sent particularly in that request.\n\n### Multiple arguments\n\nIf more than one parameter needs to be preserved within the same controller, its name can be passed as a succeeding argument to the `preserve` method.\n\n```ruby\npreserve :page, :per_page\n```\n\n### Action restrictions\n\nLimiting functionality provided by the gem to a certain set of controller actions can be achieved by applying the `only` (or `except`) option.\nFor example:\n\n```ruby\npreserve :status, only: :index\n```\n\nThe behavior is exactly the same as with an [Action Controller filter](https://guides.rubyonrails.org/action_controller_overview.html#filters).\n\n### Application-wide parameters\n\nParameters used across the whole application can be persisted by adding the `preserve` macro to the `ApplicationController`.\n\n```ruby\nclass ApplicationController \u003c ActionController::Base\n  preserve :locale\nend\n```\n\nIn more complex scenarios, controller inheritance can be utilized to further adjust the scope.\n\n### Nested parameters\n\nStoring a value of a nested parameter requires calling the `preserve` macro with an array of consecutive keys leading to that parameter in the parameters hash (similar to the [`Hash#dig`](https://apidock.com/ruby/Hash/dig) method).\n\nFor example, in case of the JSON payload below:\n\n```json\n{\n  \"sort\": {\n    \"column\": \"name\",\n    \"direction\": \"desc\"\n  }\n}\n```\n\nThe `column` parameter can be persisted with the following line:\n\n```ruby\npreserve [:sort, :column]\n```\n\n### Default parameter value\n\nThere might be a situation where neither the parameters hash nor the session contains a parameter value.\nTo provide a fallback value for such a scenario, you can use the `default` option.\nFor instance:\n\n```ruby\npreserve :status, default: 'active'\n```\n\nNote that default values are not stored in the session, but assigned on every request instead.\n\n### Blank parameter values\n\nNormally, when a parameter value is an empty string it is overwritten by a value stored in the session.\nTo change this behavior, you can use the `allow_blank` option.\n\n```ruby\npreserve :status, allow_blank: true\n```\n\nAs a result, the parameter value would be restored from the session only when the parameter was not sent in a request.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpienkowb%2Fpreserve","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpienkowb%2Fpreserve","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpienkowb%2Fpreserve/lists"}