Skip to content

Commit b6b8dff

Browse files
committed
Merge remote-tracking branch 'upstream/master' into improve-tests
2 parents 4bba16b + e0b74d8 commit b6b8dff

File tree

11 files changed

+158
-50
lines changed

11 files changed

+158
-50
lines changed

Gemfile

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,32 @@ source 'https://rubygems.org'
33
# Specify your gem's dependencies in active_model_serializers.gemspec
44
gemspec
55

6-
gem "minitest"
6+
version = ENV['RAILS_VERSION'] || '4.2'
77

8-
version = ENV["RAILS_VERSION"] || "4.2"
9-
10-
if version == "master"
11-
gem "rails", github: "rails/rails"
12-
13-
# ugh https://github.com/rails/rails/issues/16063#issuecomment-48090125
14-
gem "arel", github: "rails/arel"
8+
if version == 'master'
9+
gem 'rack', github: 'rack/rack'
10+
git 'https://github.com/rails/rails.git' do
11+
gem 'railties'
12+
gem 'activesupport'
13+
gem 'activemodel'
14+
gem 'actionpack'
15+
# Rails 5
16+
gem 'actionview'
17+
end
18+
# Rails 5
19+
gem 'rails-controller-testing', github: 'rails/rails-controller-testing'
1520
else
16-
gem "rails", "~> #{version}.0"
21+
gem_version = "~> #{version}.0"
22+
gem 'railties', gem_version
23+
gem 'activesupport', gem_version
24+
gem 'activemodel', gem_version
25+
gem 'actionpack', gem_version
26+
end
27+
28+
group :test do
29+
gem 'activerecord'
30+
gem 'sqlite3', platform: :ruby
31+
gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby
1732
end
1833

1934
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem

active_model_serializers.gemspec

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,47 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
44
require 'active_model/serializer/version'
55

66
Gem::Specification.new do |spec|
7-
spec.name = "active_model_serializers"
7+
spec.name = 'active_model_serializers'
88
spec.version = ActiveModel::Serializer::VERSION
9-
spec.authors = ["Steve Klabnik"]
10-
spec.email = ["[email protected]"]
9+
spec.platform = Gem::Platform::RUBY
10+
spec.authors = ['Steve Klabnik']
11+
spec.email = ['[email protected]']
1112
spec.summary = %q{Conventions-based JSON generation for Rails.}
1213
spec.description = %q{ActiveModel::Serializers allows you to generate your JSON in an object-oriented and convention-driven manner.}
13-
spec.homepage = "https://github.com/rails-api/active_model_serializers"
14-
spec.license = "MIT"
14+
spec.homepage = 'https://github.com/rails-api/active_model_serializers'
15+
spec.license = 'MIT'
1516

1617
spec.files = `git ls-files -z`.split("\x0")
17-
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
1818
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19-
spec.require_paths = ["lib"]
19+
spec.require_paths = ['lib']
2020

21-
spec.add_dependency "activemodel", ">= 4.0"
21+
spec.required_ruby_version = '>= 1.9.3'
2222

23-
spec.add_development_dependency "rails", ">= 4.0"
24-
spec.add_development_dependency "bundler", "~> 1.6"
25-
spec.add_development_dependency "timecop", ">= 0.7"
26-
spec.add_development_dependency "rake"
27-
spec.add_development_dependency "kaminari"
28-
spec.add_development_dependency "will_paginate"
23+
rails_versions = '>= 4.0'
24+
spec.add_runtime_dependency 'activemodel', rails_versions
25+
# 'activesupport', rails_versions
26+
# 'builder'
27+
28+
spec.add_runtime_dependency 'actionpack', rails_versions
29+
# 'activesupport', rails_versions
30+
# 'rack'
31+
# 'rack-test', '~> 0.6.2'
32+
33+
spec.add_runtime_dependency 'railties', rails_versions
34+
# 'activesupport', rails_versions
35+
# 'actionpack', rails_versions
36+
# 'rake', '>= 0.8.7'
37+
38+
# 'activesupport', rails_versions
39+
# 'i18n,
40+
# 'tzinfo'
41+
# 'minitest'
42+
# 'thread_safe'
43+
44+
# Soft dependency for pagination
45+
spec.add_development_dependency 'kaminari'
46+
spec.add_development_dependency 'will_paginate'
47+
48+
spec.add_development_dependency 'bundler', '~> 1.6'
49+
spec.add_development_dependency 'timecop', '>= 0.7'
2950
end

lib/active_model/serializer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def self.attribute(attr, options = {})
7070
ActiveModelSerializers.silence_warnings do
7171
define_method key do
7272
object.read_attribute_for_serialization(attr)
73-
end unless respond_to?(key, false) || _fragmented.respond_to?(attr)
73+
end unless (key != :id && method_defined?(key)) || _fragmented.respond_to?(attr)
7474
end
7575
end
7676

lib/active_model/serializer/association.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Serializer
77
# @param [Hash{Symbol => Object}] options
88
#
99
# @example
10-
# Association.new(:comments, CommentSummarySerializer, embed: :ids)
10+
# Association.new(:comments, CommentSummarySerializer)
1111
#
1212
Association = Struct.new(:name, :serializer, :options) do
1313

lib/active_model/serializer/lint.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,17 @@ def test_serializable_hash
3636
# Typically, it is implemented by including ActiveModel::Serialization.
3737
def test_read_attribute_for_serialization
3838
assert_respond_to resource, :read_attribute_for_serialization, "The resource should respond to read_attribute_for_serialization"
39-
assert_equal resource.method(:read_attribute_for_serialization).arity, 1
39+
actual_arity = resource.method(:read_attribute_for_serialization).arity
40+
if defined?(::Rubinius)
41+
# 1 for def read_attribute_for_serialization(name); end
42+
# -2 for alias :read_attribute_for_serialization :send for rbx because :shrug:
43+
assert_includes [1, -2], actual_arity, "expected #{actual_arity.inspect} to be 1 or -2"
44+
else
45+
# using absolute value since arity is:
46+
# 1 for def read_attribute_for_serialization(name); end
47+
# -1 for alias :read_attribute_for_serialization :send
48+
assert_includes [1, -1], actual_arity, "expected #{actual_arity.inspect} to be 1 or -1"
49+
end
4050
end
4151

4252
# Passes if the object responds to <tt>as_json</tt> and if it takes
@@ -68,15 +78,19 @@ def test_to_json
6878
end
6979

7080
# Passes if the object responds to <tt>cache_key</tt> and if it takes no
71-
# arguments.
81+
# arguments (Rails 4.0) or a splat (Rails 4.1+).
7282
# Fails otherwise.
7383
#
7484
# <tt>cache_key</tt> returns a (self-expiring) unique key for the object,
7585
# which is used by the adapter.
7686
# It is not required unless caching is enabled.
7787
def test_cache_key
7888
assert_respond_to resource, :cache_key
79-
assert_equal resource.method(:cache_key).arity, 0
89+
actual_arity = resource.method(:cache_key).arity
90+
# using absolute value since arity is:
91+
# 0 for Rails 4.1+, *timestamp_names
92+
# -1 for Rails 4.0, no arguments
93+
assert_includes [-1, 0], actual_arity, "expected #{actual_arity.inspect} to be 0 or -1"
8094
end
8195

8296
# Passes if the object responds to <tt>id</tt> and if it takes no

lib/active_model_serializers.rb

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,20 @@ def silence_warnings
1010
end
1111

1212
end
13+
1314
require 'active_model'
14-
require 'active_model/serializer/version'
15+
require 'action_controller'
16+
1517
require 'active_model/serializer'
1618
require 'active_model/serializable_resource'
19+
require 'active_model/serializer/version'
1720

18-
begin
19-
require 'active_model/serializer/railtie'
20-
require 'action_controller'
21-
require 'action_controller/serialization'
22-
23-
ActiveSupport.on_load(:action_controller) do
24-
include ::ActionController::Serialization
25-
ActionDispatch::Reloader.to_prepare do
26-
ActiveModel::Serializer.serializers_cache.clear
27-
end
21+
require 'action_controller/serialization'
22+
ActiveSupport.on_load(:action_controller) do
23+
include ::ActionController::Serialization
24+
ActionDispatch::Reloader.to_prepare do
25+
ActiveModel::Serializer.serializers_cache.clear
2826
end
29-
rescue LoadError
30-
# rails not installed, continuing
3127
end
28+
29+
require 'active_model/serializer/railtie'

test/fixtures/active_record.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
require 'active_record'
2+
3+
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
4+
ActiveRecord::Schema.define do
5+
create_table :posts, force: true do |t|
6+
t.string :title
7+
t.text :body
8+
t.references :author
9+
t.timestamps null: false
10+
end
11+
create_table :authors, force: true do |t|
12+
t.string :name
13+
t.timestamps null: false
14+
end
15+
create_table :comments, force: true do |t|
16+
t.text :contents
17+
t.references :author
18+
t.references :post
19+
t.timestamp null: false
20+
end
21+
end
22+
23+
module ARModels
24+
class Post < ActiveRecord::Base
25+
has_many :comments
26+
belongs_to :author
27+
end
28+
29+
class Comment < ActiveRecord::Base
30+
belongs_to :post
31+
belongs_to :author
32+
end
33+
34+
class Author < ActiveRecord::Base
35+
has_many :posts
36+
end
37+
38+
class PostSerializer < ActiveModel::Serializer
39+
attributes :id, :title, :body
40+
41+
has_many :comments
42+
belongs_to :author
43+
url :comments
44+
end
45+
46+
class CommentSerializer < ActiveModel::Serializer
47+
attributes :id, :contents
48+
49+
belongs_to :author
50+
end
51+
52+
class AuthorSerializer < ActiveModel::Serializer
53+
attributes :id, :name
54+
55+
has_many :posts
56+
end
57+
end

test/fixtures/poro.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,11 @@ def custom_options
134134

135135
AuthorSerializer = Class.new(ActiveModel::Serializer) do
136136
cache key:'writer', skip_digest: true
137-
attributes :id, :name
137+
attribute :id
138+
attribute :name
138139

139-
has_many :posts, embed: :ids
140-
has_many :roles, embed: :ids
140+
has_many :posts
141+
has_many :roles
141142
has_one :bio
142143
end
143144

test/serializers/association_macros_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class AssociationMacrosTest < Minitest::Test
66
AuthorSummarySerializer = Class.new
77
class AssociationsTestSerializer < Serializer
88
belongs_to :author, serializer: AuthorSummarySerializer
9-
has_many :comments, embed: :ids
9+
has_many :comments
1010
has_one :category
1111
end
1212

@@ -21,7 +21,7 @@ def test_has_one_defines_reflection
2121
end
2222

2323
def test_has_many_defines_reflection
24-
has_many_reflection = HasManyReflection.new(:comments, embed: :ids)
24+
has_many_reflection = HasManyReflection.new(:comments, {})
2525

2626
assert_includes(@reflections, has_many_reflection)
2727
end

test/serializers/associations_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ def test_has_many_and_has_one
5252

5353
case key
5454
when :posts
55-
assert_equal({ embed: :ids }, options)
55+
assert_equal({}, options)
5656
assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer)
5757
when :bio
5858
assert_equal({}, options)
5959
assert_nil serializer
6060
when :roles
61-
assert_equal({ embed: :ids }, options)
61+
assert_equal({}, options)
6262
assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer)
6363
else
6464
flunk "Unknown association: #{key}"

0 commit comments

Comments
 (0)