P
Peter Ehrlich
Hey!
First, some background. The goal here is an experiment to make the best
code that I can. My challenge is with returning JSON in an ajax
request. In a standard http request, rails view templates would handle
preparing the data -- pluralizing, capitalizing, whatever. However,
there's no view in this case.
The simple approach looks like this:
def json_for_view
rounds['last_pushed']['c_score'] = time_ago_in_words
DateTime.parse(rounds[:last_pushed][:c_score])
rounds['last_pushed']['d_score'] = time_ago_in_words
DateTime.parse(rounds[:last_pushed][:d_score])
rounds['last_pushed']['c_score'] = Date.parse
rounds[:c_score].to_s
'January 1, 2011')
rounds['last_pushed']['d_score'] = Date.parse
rounds[:d_score].to_s
'January 1, 2011')
rounds.to_json
end
I see a couple of problems here:
- the field keys are specified first. This is not DRY. (etc all the
headaches of non dry code)
- A nicer feel would be if there was a method applied to each value,
like rounds[:last_pushed][:d_score].make_pretty
Here's what I've come up with:
# lib/toolbox.rb
class Object
def send_updates!(hash)
# updates values based on blocks passed in a hash
hash.each do |key, block|
self[key] = block.call(self[key]) if self[key]
end
end
end
# the model file
def json_for_view
rounds = JSON.parse(self.rounds)
pushed_pretty = lambda { |score|
time_ago_in_words(DateTime.parse(score)) }
created_pretty = lambda { |score| Date.parse(score).to_s
'January
1, 2011') }
rounds['last_pushed'].send_updates!({
'c_score' => pushed_pretty,
'd_score' => pushed_pretty
})
rounds['created_at'].send_updates!({
'c_score' => created_pretty,
'd_score' => created_pretty
})
rounds.to_json
end
Thoughts?
Is this a common problem?
Is there a common solution?
Am I doing something wrong which causes this to be an issue?
By the way -- is there any way to have json parse return keys as
symbols? I think that would be nice.
Cheers & Thanks,
--Peter
First, some background. The goal here is an experiment to make the best
code that I can. My challenge is with returning JSON in an ajax
request. In a standard http request, rails view templates would handle
preparing the data -- pluralizing, capitalizing, whatever. However,
there's no view in this case.
The simple approach looks like this:
def json_for_view
rounds['last_pushed']['c_score'] = time_ago_in_words
DateTime.parse(rounds[:last_pushed][:c_score])
rounds['last_pushed']['d_score'] = time_ago_in_words
DateTime.parse(rounds[:last_pushed][:d_score])
rounds['last_pushed']['c_score'] = Date.parse
rounds[:c_score].to_s
rounds['last_pushed']['d_score'] = Date.parse
rounds[:d_score].to_s
rounds.to_json
end
I see a couple of problems here:
- the field keys are specified first. This is not DRY. (etc all the
headaches of non dry code)
- A nicer feel would be if there was a method applied to each value,
like rounds[:last_pushed][:d_score].make_pretty
Here's what I've come up with:
# lib/toolbox.rb
class Object
def send_updates!(hash)
# updates values based on blocks passed in a hash
hash.each do |key, block|
self[key] = block.call(self[key]) if self[key]
end
end
end
# the model file
def json_for_view
rounds = JSON.parse(self.rounds)
pushed_pretty = lambda { |score|
time_ago_in_words(DateTime.parse(score)) }
created_pretty = lambda { |score| Date.parse(score).to_s
1, 2011') }
rounds['last_pushed'].send_updates!({
'c_score' => pushed_pretty,
'd_score' => pushed_pretty
})
rounds['created_at'].send_updates!({
'c_score' => created_pretty,
'd_score' => created_pretty
})
rounds.to_json
end
Thoughts?
Is this a common problem?
Is there a common solution?
Am I doing something wrong which causes this to be an issue?
By the way -- is there any way to have json parse return keys as
symbols? I think that would be nice.
Cheers & Thanks,
--Peter