Project

snake-eyes

0.0
No commit activity in last 3 years
No release in over 3 years
Automatically convert params in your controllers from camel case to snake case in all or a select few controllers
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 4.2.5
 Project Readme


SnakeEyes

Gem Build Status GitHub license

Automatically convert between camel case APIs to snake case for your Rails code

Installation

Add this line to your application's Gemfile:

gem 'snake-eyes'

And then execute:

$ bundle

Usage

To use SnakeEyes, simply add the following to the top of any controller in which you wish to have snake case parameters. All controllers that inherit from it shall also have the behaviour

class JsonController < ApplicationController
  snake_eyes_params

  def show
    #reference the params method as normal
  end
end

Dealing with nested params

Once snake_eyes_params has been enabled for a controller, params accepts an options hash, which can be used to specify which attributes should have the _attributes suffix appended.

class WithoutSnakeEyesController < ApplicationController

   def show
      params # results in:
      # {
      #    'user' => {
      #      'name' => 'John Smith',
      #      'favouriteColor' => 'blue',
      #      'address' => { line1: '123 street' },
      #      'billingAddress' => { line1: '456 road' }
      #    }
      #}
   end
end

class WithSnakeEyesController < ApplicationController
    snake_eyes_params

    def show
       params(nested_attributes: { user: [ :address, :billing_address ] }) # results in:
       # {
       #    'user_attributes' => {
       #      'name' => 'John Smith',
       #      'favourite_color' => 'blue',
       #      'address_attributes' => { line1: '123 street' },
       #      'billing_address_attributes' => { line1: '456 road' }
       #    }
       #}
    end
 end

Avoid _attributes suffix on parents: the _ prefix

To specify nested objects that should not have the _attributes suffix (but contain attributes that should), you can prefix them with an underscore:

 class WithSnakeEyesController < ApplicationController
     snake_eyes_params

     def show
        params(nested_attributes: { _user: [ :address, :billing_address ] }) # results in:
        # {
        #    'user' => {
        #      'name' => 'John Smith',
        #      'favourite_color' => 'blue',
        #      'address_attributes' => { 'line1: 123 street' },
        #      'billing_address_attributes' => { line1: '456 road' }
        #    }
        #}
     end
  end

Reference any element of an array: the '*' wildcard

To apply the _attributes suffix to all elements of an array, use the '*' wildcard in place of the array index:

class WithSnakeEyesController < ApplicationController
    snake_eyes_params

    def show
        # Given
        params(nested_attributes: [ _array: { '*' => :string } ])

        # If the params are:
        #
        # 'array' => [
        #      { 'string' => 'string' },
        #      { 'string' => 'string2' },
        #  ]
        #
        # What will be returned:
        #
        # 'array' => [
        #      { 'string_attributes' => 'string' },
        #      { 'string_attributes' => 'string2' },
        #  ]
    end
end

Substitutions

If you want to substitute alternative values for the ones that the controller actually receives, you can do that using the substitutions option:

class WithSnakeEyesController < ApplicationController
    snake_eyes_params

    def show
        # Given
        params(substitutions: {
            shallow_object: {
                price: { replace: 'FREE', with: 0.00 }
            }
        })

        # If params is:
        #
        # 'shallowObject' => {
        #      'price' => 'FREE'
        #  }
        #
        # What will be returned:
        #
        # 'shallow_object' => {
        #     'price' => 0.00
        # }
    end
end

You can also provide multiple substitutions as an array. They are matched left-to-right, and the first matching substitution is the one that is used.

class WithSnakeEyesController < ApplicationController
    snake_eyes_params

    def show
        # Given
        params(substitutions: {
            shallow_object: {
                price: [
                    { replace: 'FREE', with: 0.00 } ,
                    { replace: 'EXPENSIVE', with: 999.00 }
                ]
            }
        })

        # If params is:
        #
        # 'shallowObject' => {
        #      'price' => 'FREE'
        #  }
        #
        # What will be returned:
        #
        # 'shallow_object' => {
        #     'price' => 0.00
        # }

        # If params is:
        #
        # 'shallowObject' => {
        #      'price' => 'EXPENSIVE'
        #  }
        #
        # What will be returned:
        #
        # 'shallow_object' => {
        #     'price' => 999.00
        # }
    end
end

Configuration

By default SnakeEyes logs the snake case parameters to the Rails console. You can prevent this behaviour by configuring the gem:

SnakeEyes.configuration do |config|
  config.log_snake_eyes_parameters = false
end

Contributing

  1. Fork it ( https://github.com/greena13/snake-eyes/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request