Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ActionController extension to fix Persian numerals
module ActionControllerParametersExtension
extend ActiveSupport::Concern
# A recursive method to traverse the params hash. It replaces
# persian numerals with their equivalent english numerals.
# It's not the best way to do it, but it works for now.
def fix_numerals(element=self)
case element
when Hash
Hash[ element.map{ |k,v| [k, fix_numerals(v)] } ]
when Array
element.map{ |v| fix_numerals(v) }
else
element.tr("۱۲۳۴۵۶۷۸۹۰", "1234567890")
end
end
end
# Includes the extension
ActionController::Parameters.send(:include, ActionControllerParametersExtension)
@Arvinje
Copy link
Author

Arvinje commented Dec 12, 2015

NOTE:
This method doesn't return an instance of ActionController::Parameters, so it always has to be called at the end of the chain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment