Skip to content

Commit 4b39ded

Browse files
committed
Add relationship method as alternate to has_one and has_many
1 parent d90ff46 commit 4b39ded

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

lib/jsonapi/resource.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,21 @@ def default_attribute_options
326326
{ format: :default }
327327
end
328328

329+
def relationship(*attrs)
330+
options = attrs.last.is_a?(Hash) ? attrs.last : {}
331+
klass = case options[:to]
332+
when :one
333+
Association::HasOne
334+
when :many
335+
Association::HasMany
336+
else
337+
#:nocov:#
338+
fail ArgumentError.new('to: must be either :one or :many')
339+
#:nocov:#
340+
end
341+
_associate(klass, *attrs)
342+
end
343+
329344
def has_one(*attrs)
330345
_associate(Association::HasOne, *attrs)
331346
end

0 commit comments

Comments
 (0)