Hash, Eval, or Set Variable on dynamic variable names?

Here is the current gist containing the sample test methods.

I’m creating javascript variables that can be used with the gon gem. As
you can see by the methods, the variable names change based on the race.
Unfortunately, I’d like to clean up the test methods a bit and DRY up
the code.

I’m not sure which route to go - hash, some type of set_variable method,
or eval. Can anyone provide some input on how you would dry up this
code given that the variables will have to change based on how they are
used and called from JS.

Many thanks in advance.

JD.

I simplified the code with something very simple:

def find_races
races = Race.all
gon.race,gon.bon_con = {},{}
races.each_with_index do |race,i|
gon.race[i] = race.name
gon.bon_con[i] = race.bon_con
end
end

Which allows me to access the data in JS through:

console.log(“gon.race[0]”) etc.

I could have just requested a hash be returned using:

gon.races = Race.all

but in IE9, all gon variables are shown in the script tags at the very
top of the page. I found this to be a rather intrusive security breach
because anyone could see the full table schema. By just isolating
values, I achieve the same result.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs