Coder Social home page Coder Social logo

preserve's Introduction

Preserve Gem version Build status Coverage status Maintainability status

Preserve is a Rails plugin which stores selected parameters in the session to make them available in subsequent requests.

Installation

Add the following line to your application's Gemfile:

gem 'preserve'

Install the gem with Bundler:

$ bundle install

Or do it manually by running:

$ gem install preserve

Usage

For the sole purpose of this example, let's assume we have a Rails application with a controller showing all parameters sent in a request.

class ParametersController < ApplicationController
  def index
    render json: parameters
  end

  private

  def parameters
    params.except(:controller, :action)
  end
end

Routes are declared as following:

Rails.application.routes.draw do
  resources :parameters, only: :index
end

Let's start the application and test its behavior using cURL. The 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).

In the first request, the status parameter is set to active.

$ curl -c cookies http://localhost:3000/parameters?status=active
{"status":"active"}

As expected, the application returns the parameter and its value.

The next request is sent without any parameters.

$ curl -b cookies http://localhost:3000/parameters
{}

Obviously, the status parameter is no longer available.

Now, let's call the preserve macro inside the ParametersController class with the parameter name as an argument.

class ParametersController < ApplicationController
  preserve :status

  # ...
end

Sending the same two requests again yields a different result.

$ curl -c cookies http://localhost:3000/parameters?status=active
{"status":"active"}
$ curl -b cookies http://localhost:3000/parameters
{"status":"active"}

This time, the status parameter is still available when the second request is made, even though it wasn't sent particularly in that request.

Multiple arguments

If 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.

preserve :page, :per_page

Action restrictions

Limiting functionality provided by the gem to a certain set of controller actions can be achieved by applying the only (or except) option. For example:

preserve :status, only: :index

The behavior is exactly the same as with an Action Controller filter.

Application-wide parameters

Parameters used across the whole application can be persisted by adding the preserve macro to the ApplicationController.

class ApplicationController < ActionController::Base
  preserve :locale
end

In more complex scenarios, controller inheritance can be utilized to further adjust the scope.

Nested parameters

Storing 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 method).

For example, in case of the JSON payload below:

{
  "sort": {
    "column": "name",
    "direction": "desc"
  }
}

The column parameter can be persisted with the following line:

preserve [:sort, :column]

Default parameter value

There might be a situation where neither the parameters hash nor the session contains a parameter value. To provide a fallback value for such a scenario, you can use the default option. For instance:

preserve :status, default: 'active'

Note that default values are not stored in the session, but assigned on every request instead.

Blank parameter values

Normally, when a parameter value is an empty string it is overwritten by a value stored in the session. To change this behavior, you can use the allow_blank option.

preserve :status, allow_blank: true

As a result, the parameter value would be restored from the session only when the parameter was not sent in a request.

preserve's People

Contributors

pienkowb avatar vicvega avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.