Skip to content

Instantly share code, notes, and snippets.

@dustym
Created July 24, 2008 19:50
Show Gist options
  • Save dustym/2270 to your computer and use it in GitHub Desktop.
Save dustym/2270 to your computer and use it in GitHub Desktop.
class Array
def to_json(options = {}, &block) #:nodoc:
"[#{map { |value| ActiveSupport::JSON.encode(value, options, &block) } * ', '}]"
end
end
module ActiveSupport
module JSON
class << self
# Converts a Ruby object into a JSON string.
def encode(value, options = {}, &block)
raise_on_circular_reference(value) do
value.send(:to_json, options, &block)
end
end
end
end
end
class SomeModel
def to_json(options, &block)
values = {}
values.merge!(attributes.except(:private_stuff, :other_private_stuff))
block.call(values) if block
values.to_json
end
end
class SomeController
def index
render :json => SomeModel.find(:all).to_json do |model_json_hash|
model_json_hash[:url] = some_model_url(some_model)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment