Last active
December 12, 2015 20:37
-
-
Save Arvinje/e911a57669708ee2e467 to your computer and use it in GitHub Desktop.
ActionController extension to fix Persian numerals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NOTE:
This method doesn't return an instance of ActionController::Parameters, so it always has to be called at the end of the chain.