All Projects → hintmedia → moderate_parameters

hintmedia / moderate_parameters

Licence: MIT License
Moderate Parameters Gem

Programming Languages

ruby
36898 projects - #4 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to moderate parameters

gradle-upgrade-interactive
CLI to interactively upgrade gradle dependencies, inspired by yarn.
Stars: ✭ 44 (+266.67%)
Mutual labels:  upgrade
flora
Upgrade and version manager for terraform
Stars: ✭ 16 (+33.33%)
Mutual labels:  upgrade
gitea-auto-update
A script which can update gitea via crontab automatically to a new version.
Stars: ✭ 25 (+108.33%)
Mutual labels:  upgrade
kali-wsl
KALI LINUX : WINDOWS SUBSYSTEM FOR LINUX
Stars: ✭ 56 (+366.67%)
Mutual labels:  upgrade
autoupgrade
Upgrade module for PrestaShop
Stars: ✭ 71 (+491.67%)
Mutual labels:  upgrade
butterfly
Application transformation tool
Stars: ✭ 35 (+191.67%)
Mutual labels:  upgrade
fcmxmppserverv2
XMPP Connection Server for FCM using the latest version of the Smack library (4.3.4) + Connection Draining Implementation
Stars: ✭ 43 (+258.33%)
Mutual labels:  upgrade
entrepot
A list of free GitHub.com hosted WordPress plugins, themes & blocks
Stars: ✭ 29 (+141.67%)
Mutual labels:  upgrade
deblibs-gradle-plugin
A Gradle plugin that creates Github issue and Slack message for outdated dependencies so they can easily be tracked and manually upgraded.
Stars: ✭ 73 (+508.33%)
Mutual labels:  upgrade
rancher-plugin
rancher jenkins plugin deploy or upgrade service
Stars: ✭ 42 (+250%)
Mutual labels:  upgrade
ocp4upc
OCP4 Upgrade Paths Checker
Stars: ✭ 30 (+150%)
Mutual labels:  upgrade
dokuwiki-plugin-upgrade
Upgrade a DokuWiki installation automatically
Stars: ✭ 23 (+91.67%)
Mutual labels:  upgrade
Rector
Instant Upgrades and Automated Refactoring of any PHP 5.3+ code
Stars: ✭ 4,739 (+39391.67%)
Mutual labels:  upgrade

Moderate Parameters

By Hint.io

Gem Version CI Appraisals Maintainability

In our experience with UpgradeRails, the migration from protected_attributes to strong_parameters can leave more questions than answers. It can be difficult to determine what data is originating from within the app and what is coming from the internet.

Moderate Parameters is a set of tools providing logging of data sources in the controller by extending ActionController::Parameters functionality.

Installation

Add this line to your application's Gemfile:

gem 'moderate_parameters'

And then execute:

$ bundle

Or install it yourself as:

$ gem install moderate_parameters

Then add the initializer by running:

$ bundle exec rails g moderate_parameters:install

This will add an initializer to your rails app for turning on/off functionality.

Usage

Given a form at /people/new that submits data to the PeopleController#create action like so:

{ person: { name: 'Kyle', age: '26', height: '180' } }

With a model that looks like:

class Person < ActiveRecord::Base
  attr_accessible :name, :age, :height

  . . .

end

And a controller looks like this:

class PeopleController < ActionController::Base
  def create
    Person.create(params[:person])
  end

  . . .

end

We can add moderate_parameters by following the strong_parameters implementation method with a couple slight changes.

Add a private params method for the controller calling moderate (with controller_name and action_name as the first two args) instead of permit:

class PeopleController < ActionController::Base
  def create
    Person.create(person_params) # Was Person.create(params[:person])
  end

  . . .

  private

    def person_params
      params.require(:person).moderate(controller_name, action_name, :name)
    end
end

This will cause the person_params to flow the same way they did before (getting passed to the model without interruption), but the params that are not included in the argument of moderate will be logged to /log/moderate_params.log

Meaning that, after submitting the aforementioned data, our moderate_parameters.log will look like so:

people#create Top Level is missing: age
people#create Top Level is missing: height

We can fix this by adding age and height to person_params like so:

class PeopleController < ActionController::Base
  def create
    Person.create(person_params)
  end

  . . .

  private

    def person_params
      params.require(:person).moderate(controller_name, action_name, :name, :age, :height)
    end
end

We can then hit submit data from the form at /people/new and see that no new lines are added to the moderate_parameters.log file.

This means that we can remove moderate_parameters and move to using permit as the final migration step of strong_parameters:

class PeopleController < ActionController::Base
  def create
    Person.create(person_params)
  end

  . . .

  private

    def person_params
      params.require(:person).permit(:name, :age, :height)
    end
end

It is only AFTER this final step of the strong_parameters migration has been completed that you can safely remove the protected_attributes line in the model:

class Person < ActiveRecord::Base
  # attr_accessible :name, :age, :height

  . . .

end

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/hintmedia/moderate_parameters. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the moderate_parameters project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].