0

I am trying to pre-process the params with to_ar2en_i function in ApplicationController before any action processes the params, I have the following in my application_controller.rb:

# translates every params' entity from arabic to english 
before_action :param_convert_ar2en_i

private

def param_convert_ar2en_i h = nil, path = []
  h ||= params
  h.each_pair do |k, v|
    if v.respond_to?(:key?)
      param_convert_ar2en_i v, [path, k].flatten
    else
      # something like: 
      params[[path, k].flatten].to_ar2en_i 
    end
  end
end

The problem is I don't know how to apply to_ar2en_i to a nested params with the path of [[path, k].flatten].

Can anybody kindly help me on this?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
dariush
  • 3,191
  • 3
  • 24
  • 43
  • 1
    Hello, I have a couple of questions: Why do you flatten the path array? and Could you actually provide an example params? – Jeremie May 10 '17 at 02:55
  • @Jeremie I `flatten` the `params` because the `path` is already an array and I want to append `k` to the `path`; example: `{"utf8"=>"✓", "authenticity_token"=>"1ggs+n156aMGeur52NvBenFZLT1BGy8FuIqcyy4st1HZoCG0KlPgZCGycLJrGCvK4ErwfsWFphnfLVZWYuGijQ==", "admin_stuff_brand"=>{"name"=>"skam ", "comment"=>"sakmsakms"}, "commit"=>"create"}` Please not that if the params was a simple `hash` it wasn't any problem, I stuck because the I cannot modify the `Parameter` object. – dariush May 10 '17 at 18:39

1 Answers1

0

Silly me!! Instead of trying to access params in params[[path, k].flatten].to_ar2en_i all I needed to do is just to access h[k].to_ar2en_i and since the h is passed by reference in Ruby, it will do the job.

def param_convert_ar2en_i h = nil, l = [], depth: 0
  h ||= params
  h.each_pair do |k, v|
    if v.respond_to? :key?
      param_convert_ar2en_i v, [l, k].flatten, depth: depth + 1
    else
      if h[k].respond_to? :each
        h[k].each { |i| i.to_ar2en_i if i.respond_to? :to_ar2en_i }
      else
        h[k].to_ar2en_i if h[k].respond_to? :to_ar2en_i 
      end
    end
  end
end
dariush
  • 3,191
  • 3
  • 24
  • 43