Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Arvinje
Last active December 12, 2015 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arvinje/e911a57669708ee2e467 to your computer and use it in GitHub Desktop.
Save Arvinje/e911a57669708ee2e467 to your computer and use it in GitHub Desktop.
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