0.0
No commit activity in last 3 years
No release in over 3 years
StrongLocals is a gem for verifying the local variables passed to partials.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 4.0
 Project Readme

StrongLocals

Methods have signatures, React has PropTypes, but Rails partials don't provide a way to declare what local variables they expect to receive. StrongLocals aims to help with that, by providing validations for the incoming locals to the partial.

Example Usage

In HAML:

:ruby
  StrongLocals.
    add(:currency).
    add(:campaign, presence: false).
    validate!(local_assigns)

In ERB:

<%
StrongLocals.
  add(:currency).
  add(:campaign, presence: false).
  validate!(local_assigns)
%>

If the locals passed to the partial don't pass the validation, a StrongLocals::LocalsException exception will be raised which contains the error information.

Creating Custom Validations

First define a validation class which extends from StrongLocals::Validations::Base. It should contain a MESSAGE and a valid? method which checks to confirm that the value being validated conforms to the validation.

class StringValidation < StrongLocals::Validations::Base
  MESSAGE = 'not string'.freeze

  def valid?
    value.respond_to?(:to_s)
  end
end

Next, register your new validation so that StrongLocals knows about it.

StrongLocals::Locals.register :string, StringValidation