Skip to content

Commit 86138a7

Browse files
authored
Merge pull request #710 from olleolleolle/feature/belongs-to-as-has-one-alias
Add belongs_to as DSL warning wrapper to has_one to help beginners
2 parents 8092f2d + 35ceb7f commit 86138a7

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

lib/jsonapi/resource.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,15 @@ def relationship(*attrs)
437437
def has_one(*attrs)
438438
_add_relationship(Relationship::ToOne, *attrs)
439439
end
440+
441+
def belongs_to(*attrs)
442+
ActiveSupport::Deprecation.warn "In #{name} you exposed a `has_one` relationship "\
443+
" using the `belongs_to` class method. We think `has_one`" \
444+
" is more appropriate. If you know what you're doing," \
445+
" and don't want to see this warning again, override the" \
446+
" `belongs_to` class method on your resource."
447+
_add_relationship(Relationship::ToOne, *attrs)
448+
end
440449

441450
def has_many(*attrs)
442451
_add_relationship(Relationship::ToMany, *attrs)

test/unit/jsonapi_request/jsonapi_request_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class CatResource < JSONAPI::Resource
44
attribute :name
55
attribute :breed
66

7-
has_one :mother, class_name: 'Cat'
7+
belongs_to :mother, class_name: 'Cat'
88
has_one :father, class_name: 'Cat'
99

1010
filters :name

0 commit comments

Comments
 (0)