{"id":14955714,"url":"https://github.com/cedarcode/rename_params","last_synced_at":"2025-11-11T18:38:30.580Z","repository":{"id":56891787,"uuid":"72491651","full_name":"cedarcode/rename_params","owner":"cedarcode","description":"Simple params renaming for Rails applications","archived":false,"fork":false,"pushed_at":"2018-03-12T20:35:36.000Z","size":64,"stargazers_count":17,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-10-08T04:29:12.808Z","etag":null,"topics":["controller","params","rails-application","rename","transformations"],"latest_commit_sha":null,"homepage":"","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/cedarcode.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-11-01T01:11:20.000Z","updated_at":"2025-10-05T15:28:45.000Z","dependencies_parsed_at":"2022-08-21T00:20:47.183Z","dependency_job_id":null,"html_url":"https://github.com/cedarcode/rename_params","commit_stats":null,"previous_names":["marceloeloelo/rename_params"],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/cedarcode/rename_params","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cedarcode%2Frename_params","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cedarcode%2Frename_params/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cedarcode%2Frename_params/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cedarcode%2Frename_params/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cedarcode","download_url":"https://codeload.github.com/cedarcode/rename_params/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cedarcode%2Frename_params/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":283910127,"owners_count":26915128,"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","status":"online","status_checked_at":"2025-11-11T02:00:06.610Z","response_time":65,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["controller","params","rails-application","rename","transformations"],"created_at":"2024-09-24T13:11:36.501Z","updated_at":"2025-11-11T18:38:30.549Z","avatar_url":"https://github.com/cedarcode.png","language":"Ruby","readme":"# rename_params\n\n[![Build Status](https://travis-ci.org/cedarcode/rename_params.svg?branch=master)](https://travis-ci.org/cedarcode/rename_params)\n[![Code Climate](https://codeclimate.com/github/cedarcode/rename_params/badges/gpa.svg)](https://codeclimate.com/github/cedarcode/rename_params)\n[![Gem Version](https://badge.fury.io/rb/rename_params.svg)](https://badge.fury.io/rb/rename_params)\n\nSimple params renaming for Rails applications.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'rename_params'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install rename_params\n\n## Usage\n\nSpecify the input params that you would like to rename in your controller:\n\n\n```ruby\nclass UsersController \u003c ApplicationController\n  # This will rename \"username\" to \"login\" in the params\n  rename :username, to: :login, only: :index\n  \n  def index\n    @users = User.where(query_params)\n  end\n  \n  private\n  \n  def query_params\n    params.permit(:login, :age)\n  end\nend\n```\n\nThis means that for a request like the following:\n```\nGET /users?username=aperson\u0026age=28\n```\n\nThe `params` method will now have a key called `login` instead of `username` within the context of the `index` action:\n\n```\n\u003e puts params\n{ login: 'aperson', age: '28' }\n```\n\n## Nested params\n\nIf you need to rename a parameter which is nested into a certain key, you can use the\n`namespace` option. Just specify the nesting using an array format.\n\n\n```ruby\nclass UsersController \u003c ApplicationController\n  # Renames params[:user][:username] to params[:user][:login]\n  rename :username, to: :login, namespace: :user, only: :create\n\n  def create\n    @user = User.new(user_params)\n    respond_to do |format|\n      format.html { redirect_to(@user, notice: 'User was successfully created.') }\n    end\n  end\n\n  private\n\n  def user_params\n    params.require(:user).permit(:login)\n  end\nend\n```\n\nIf the parameter you need to change is nested under more than one namespace, you can use the array\nsyntax like this:\n\n```ruby\nclass UsersController \u003c ApplicationController\n  # Renames params[:session][:credentials][:username] to params[:session][:credentials][:login]\n  rename :username, to: :login, namespace: [:session, :credentials]\nend\n```\n\n## Converting values\n\nThere are cases where just renaming the parameter key won't be enough and you will want to also\ntransform the values that were sent. The `convert` option will give you some more flexibility as to\nwhat to do with those values.\n\n### Enum converter\n\nIf the request will only take a finite number of values you can use an Enum converter to define the\nconversion rules. This will be just a mapper in the form of a hash.\n\n\n```ruby\nclass TicketsController \u003c ApplicationController\n  # Converts params[:status] value from \"open\"/\"in_progress\"/\"closed\" to 0/1/2\n  rename :status, to: :status, convert: { open: 0, in_progress: 1, closed: 2 }\nend\n```\n\nThe example above will convert the `status` parameter from `open`/`in_progress`/`closed` to `0`/`1`/`2`:\n```\n# If params came with { status: 'in_progress' }, then after the transformation:\n\u003e puts params\n{ status: 1 }\n```\n\n### Proc converter\n\nYou can also use a `Proc` or a private method to convert the value to whatever makes sense by executing ruby code.\n\n```ruby\nclass UsersController \u003c ApplicationController\n  # Converts params[:age] value into year_of_birth\n  rename :age, to: :year_of_birth, convert: -\u003e (value) { Date.today.year - value }\nend\n```\n\nAssuming `Time.current` is in 2016, this will result in the following conversion:\n```\n# If params came with { age: 28 }, then after the transformation:\n\u003e puts params\n{ year_of_birth: 1988 }\n```\n\nIf you want to use private method instead of a proc, you can just declare it like this:\n\n```ruby\nclass UsersController \u003c ApplicationController\n  rename :age, to: :year_of_birth, convert: :to_year\n\n  private\n\n  def to_year(value)\n    Date.today.year - value\n  end\nend\n````\n\n## Multiple renaming\n\nIf you need to rename more than one parameter, just specify as many `rename` declarations\nas you need. Normally you will want to put the `rename` declarations at the top of the file, before\nany `before_action` in your controller.\n\n```ruby\nclass UsersController \u003c ApplicationController\n  rename :username, to: :login, namespace: :user, only: :create\n  rename :age, to: :year_of_birth, convert: -\u003e (value) { Date.today.year - value }, only: :index\nend\n```\n\nYou can think of the transformations as if they were ran in sequence in the same order\nthey were defined. So keep this in mind if one transformation depends on a previous one.\n\n## Moving params\n\nThere will be some cases where you will need to move a param from one namespace to another. For those\ncases, you can use the `move` macro.\n\n```ruby\nclass UsersController \u003c ApplicationController\n  # Moves params[:username] to params[:user][:username]\n  move :username, to: [:user], only: :create\n\n  def create\n    #...\n  end\nend\n```\n\nIn this case, the params were sent like this:\n```\n\u003e puts params\n{ username: 'aperson' }\n```\n\nAnd they were transformed to:\n```\n\u003e puts params\n{\n  user: {\n    username: 'aperson'\n  }\n}\n```\n\nYou can specify deeper nesting using the array notation. Example:\n```ruby\nclass UsersController \u003c ApplicationController\n  # Moves params[:street] to params[:contact][:address][:street]\n  move :street, to: [:contact, :address]\nend\n```\n\nThis will be renamed to:\n```\n\u003e puts params\n{\n  contact: {\n    address: {\n      street: '123 St.'\n    }\n  }\n}\n```\n\n### Using a namespace with move\nThe `move` option also accepts a `namespace` just like `rename`. If you want to move something that is not at the root\nlevel, you can always specify the path to it using a namespace.\n\nLet's say you have a UsersController\n\n\n```ruby\nclass UsersController \u003c ApplicationController\n  # Moves params[:user][:address][:street] to params[:user][:street]\n  move :street, namespace: [:user, :address], to: :user\nend\n```\n\nThis will be renamed from this:\n```\n\u003e puts params\n{\n  user: {\n    address: {\n      street: '123 St.'\n    }\n  }\n}\n```\nTo this:\n```\n\u003e puts params\n{\n  user: {\n    street: '123 St.'\n  }\n}\n```\n\n### The root option\n\nIf you need to move a param to the root level, you can do that by using the `:root` keyword:\n\n```ruby\nclass UsersController \u003c ApplicationController\n  # Moves params[:user][:login] to params[:login]\n  move :login, namespace: :user, to: :root\nend\n```\n\nDoing a request like the following:\n```\nGET `/users?user[login]=aperson`\n```\n\nWill rename the params to:\n```\n\u003e puts params\n{ login: 'aperson' }\n```\n\n### Combining move and rename\n\nIf you want to rename something and move it to a different namespace, you can do that by either first calling `rename`\nand then `move` in the line below, or you can use the `move_to` option within the same `rename` clause.\n\n```ruby\nclass UsersController \u003c ApplicationController\n  # Renames params[:username] to params[:user][:login]\n  rename :username, to: :login, move_to: :user, only: :create\n\n  def create\n    #...\n  end\nend\n```\n\nIn this case, the params were sent like\n```\n\u003e puts params\n{ username: 'aperson' }\n```\n\nBut they were transformed to:\n```\n\u003e puts params\n{\n  user: {\n    login: 'aperson'\n  }\n}\n```\n\nThis is the same than doing:\n```ruby\nclass UsersController \u003c ApplicationController\n  # Renames params[:username] to params[:user][:login]\n  rename :username, to: :login, only: :create\n  move :login, to: :user, only: :create\n\n  def create\n    #...\n  end\nend\n```\n\n\n## Contributing\n\n1. Fork it ( https://github.com/cedarcode/rename_params/ )\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create a new Pull Request\n\nSee the [Running Tests](RUNNING_TESTS.md) guide for details on how to run the test suite.\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcedarcode%2Frename_params","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcedarcode%2Frename_params","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcedarcode%2Frename_params/lists"}