Permalink
Browse files

Merge pull request #3175 from nhocki/as-json-patch

Allow default options for `as_json` method on models
  • Loading branch information...
2 parents fe157a7 + da914fa commit afeb8976a34a3e3b4568357e08ea564d15d72d2f @josevalim josevalim committed Sep 30, 2011
@@ -206,4 +206,14 @@ def @contact.as_json(options); { :name => name, :created_at => created_at }; end
assert_no_match %r{"preferences":}, json
end
+ test "custom as_json options should be extendible" do
+ def @contact.as_json(options = {}); super(options.merge(:only => [:name])); end
+ json = @contact.to_json
+
+ assert_match %r{"name":"Konata Izumi"}, json
+ assert_no_match %r{"created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}}, json
+ assert_no_match %r{"awesome":}, json
+ assert_no_match %r{"preferences":}, json
+ end
+
end
@@ -12,7 +12,7 @@
[Object, Array, FalseClass, Float, Hash, Integer, NilClass, String, TrueClass].each do |klass|
klass.class_eval <<-RUBY, __FILE__, __LINE__
# Dumps object in JSON (JavaScript Object Notation). See www.json.org for more info.
- def to_json(options = nil)
+ def to_json(options = {})
ActiveSupport::JSON.encode(self, options)
end
RUBY

0 comments on commit afeb897

Please sign in to comment.