Project

params_for

0.04
No commit activity in last 3 years
No release in over 3 years
With Params validator oyu should be able to perform controller params validation easy and with any kind of type, format, or custom validation you already know and use in oyur models.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 3.0.0
~> 1.7
>= 0
>= 3.0.0
~> 10.0
~> 3.0

Runtime

 Project Readme

Build Status Code Climate Test Coverage Inline docs

ParamsFor

Use service objects and the power of ActiveModel::Validations to easy validate params in controller.

Installation

Add this line to your application's Gemfile:

gem 'params_for'

And then execute:

$ bundle

Or install it yourself as:

$ gem install params_for

Usage

In your controller:

# app/controllers/fancy_controller.rb

class FancyController < ApplicationController
  include ParamsFor::Connectors::Glue

  params_for :fancy, only: [:create]

  # Creates a Fancy object by checking and validating params
  # before that
  #
  def create
    ...
    @fancy = Fancy.new(fancy_params)
    ...
  end
end

Or you can play with it yourself

# app/controllers/fancy_controller.rb

class FancyController < ApplicationController
  include ParamsFor::Connectors::Glue

  # Creates a Fancy object by checking and validating params
  # before that
  #
  def create
    ...
    @fancy = Fancy.new(fancy_params)
    ...
  end

  protected

  # Strong params delegated to ParamsFor::Fancy
  # and memoized in @fancy_params var returned by this method
  #
  # @return [HashwithIndifferentAccess]
  def fancy_params
    params_for :fancy
  end
end

Some place in your application ( suggested app/validators/params_for/ )

  # app/validators/params_for/fancy.rb

  class ParamsFor::Fancy < ParamsFor::Base
    attr_accessor :user_id, :fancy_name, :fancy_description

    validates :user_id, :fancy_name, presence: true
    validates :user_id, integer: true
  end

Contributing

  1. Fork it ( https://github.com/[my-github-username]/params_for/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