Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #as_json glitch caused by JSON and Rails #1318

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
language: ruby
rvm:
- 1.8.7
- 1.9.2
- 1.9.3
- 2.0.0
- ree
- jruby-18mode
- jruby-19mode
- rbx-2
gemfile:
Expand All @@ -14,15 +10,3 @@ gemfile:
matrix:
allow_failures:
- gemfile: Gemfile.edge
exclude:
# Edge Rails is only compatible with 1.9.3
- gemfile: Gemfile.edge
rvm: 1.8.7
- gemfile: Gemfile.edge
rvm: 1.9.2
- gemfile: Gemfile.edge
rvm: ree
- gemfile: Gemfile.edge
rvm: jruby-18mode
- gemfile: Gemfile.edge
rvm: rbx-18mode
3 changes: 3 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ task :bench do
end

task :default => :test

desc 'CI test task'
task :ci => :default
29 changes: 29 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
version: '{build}'

skip_tags: true

environment:
matrix:
- ruby_version: "193"
- ruby_version: "193-x64"
- ruby_version: "200"
- ruby_version: "200-x64"
- ruby_version: "21"
- ruby_version: "21-x64"

cache:
- vendor/bundle

install:
- SET PATH=C:\Ruby%ruby_version%\bin;%PATH%
- ruby --version
- gem --version
- gem install bundler
- bundler --version
- bundle platform
- bundle install --path=vendor/bundle --retry=3 --jobs=3

test_script:
- bundle exec rake ci

build: off
2 changes: 1 addition & 1 deletion lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def to_json(*args)
# object including the root.
def as_json(options={})
options ||= {}
if root = options.fetch(:root, @options.fetch(:root, root_name))
if root = options.to_hash.fetch(:root, @options.fetch(:root, root_name))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

failing test?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll check it out tomorrow.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, these tests are already failing on 0.8.3 prior to this change.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the 0-8-stable branch has been failing since 2013, looks like this PR broke it: #477

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bf4 I fixed some of your gem's tests, so this is now in a better state than the current 0-8-stable branch. It's not passing on some older versions of ruby that you guys are testing on travis because:

mime-types requires Ruby version >= 1.9.2

as seen here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mhuggins s/my/our gem. Thanks for looking into this. Sorry for being slow here...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, hopefully this is helpful and looks good. :)

@options[:hash] = hash = {}
@options[:unique_values] = {}

Expand Down
2 changes: 1 addition & 1 deletion test/serialization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def test_render_json_with_status

def test_render_json_with_callback
get :render_json_hello_world_with_callback
assert_equal 'alert({"hello":"world"})', @response.body
assert_equal '/**/alert({"hello":"world"})', @response.body
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh weird, shouldn't it have been this the whole time?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, it is currently failing on the 0.8 branch. This fixes it.

# For JSONP, Rails 3 uses application/json, but Rails 4 uses text/javascript
assert_match %r(application/json|text/javascript), @response.content_type.to_s
end
Expand Down
2 changes: 2 additions & 0 deletions test/serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,7 @@ def name
def test_inheritance_does_not_used_cached_attributes
parent = Class.new(ActiveModel::Serializer) do
attributes :title
alias :read_attribute_for_serialization :send
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed? If so, it probably shouldn't just be in the test, but generally in the serializer, no?

Also, see https://github.com/rails/rails/blob/4-2-stable/activemodel/lib/active_model/serialization.rb#L141

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good callout. Happy to change it if this is the desired fix.

end

child = Class.new(parent) do
Expand All @@ -1371,6 +1372,7 @@ def test_inheritance_does_not_used_cached_attributes

data_class = Class.new do
attr_accessor :title, :body
alias :read_attribute_for_serialization :send
end

item = data_class.new
Expand Down
1 change: 1 addition & 0 deletions test/test_fakes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ class SomeSerializer < ActiveModel::Serializer
end

class SomeObject < Struct.new(:some)
alias :read_attribute_for_serialization :send
end

# Set up some classes for polymorphic testing
Expand Down