Skip to content

Commit 7fa123b

Browse files
committed
Merge pull request #953 from lsylvester/use-active-model-name-for-type
use model name to determine the type
2 parents de23501 + 97e82c3 commit 7fa123b

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

lib/active_model/serializer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def id
188188
end
189189

190190
def type
191-
object.class.to_s.demodulize.underscore.pluralize
191+
object.class.model_name.plural
192192
end
193193

194194
def attributes(options = {})

test/adapter/json_api/linked_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def test_include_multiple_posts_and_linked
203203
assert_equal expected, alt_adapter.serializable_hash[:included]
204204
end
205205

206-
def test_ignore_model_namespace_for_linked_resource_type
206+
def test_underscore_model_namespace_for_linked_resource_type
207207
spammy_post = Post.new(id: 123)
208208
spammy_post.related = [Spam::UnrelatedLink.new(id: 456)]
209209
serializer = SpammyPostSerializer.new(spammy_post)
@@ -212,7 +212,7 @@ def test_ignore_model_namespace_for_linked_resource_type
212212
expected = {
213213
related: {
214214
data: [{
215-
type: 'unrelated_links',
215+
type: 'spam_unrelated_links',
216216
id: '456'
217217
}]
218218
}

test/fixtures/poro.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
class Model
22
FILE_DIGEST = Digest::MD5.hexdigest(File.open(__FILE__).read)
33

4+
def self.model_name
5+
@_model_name ||= ActiveModel::Name.new(self)
6+
end
7+
48
def initialize(hash={})
59
@attributes = hash
610
end

0 commit comments

Comments
 (0)