gist: 2270 Download_button fork
public
Public Clone URL: git://gist.github.com/2270.git
Ruby
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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

Owner

dustym

Revisions