Skip to content

Commit

Permalink
Merge pull request #1344 from jmileham/serialization_includes
Browse files Browse the repository at this point in the history
AR Serialization shouldn't fall back to base opts for includes
  • Loading branch information
josevalim committed May 26, 2011
2 parents 5083d70 + 043b624 commit d341d16
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
8 changes: 2 additions & 6 deletions activerecord/lib/active_record/serialization.rb
Expand Up @@ -31,9 +31,6 @@ def serializable_hash(options = nil)
def serializable_add_includes(options = {})
return unless include_associations = options.delete(:include)

base_only_or_except = { :except => options[:except],
:only => options[:only] }

include_has_options = include_associations.is_a?(Hash)
associations = include_has_options ? include_associations.keys : Array.wrap(include_associations)

Expand All @@ -46,9 +43,8 @@ def serializable_add_includes(options = {})
end

if records
association_options = include_has_options ? include_associations[association] : base_only_or_except
opts = options.merge(association_options)
yield(association, records, opts)
association_options = include_has_options ? include_associations[association] : {}
yield(association, records, association_options)
end
end

Expand Down
9 changes: 9 additions & 0 deletions activerecord/test/cases/json_serialization_test.rb
Expand Up @@ -161,6 +161,15 @@ def test_includes_fetches_nth_level_associations
assert_match %r{"tag":\{"name":"General"\}}, json
end

def test_includes_doesnt_merge_opts_from_base
json = @david.to_json(
:only => :id,
:include => :posts
)

assert_match %{"title":"Welcome to the weblog"}, json
end

def test_should_not_call_methods_on_associations_that_dont_respond
def @david.favorite_quote; "Constraints are liberating"; end
json = @david.to_json(:include => :posts, :methods => :favorite_quote)
Expand Down

0 comments on commit d341d16

Please sign in to comment.