Skip to content

Commit 3d41deb

Browse files
committed
Renamed has_one direction option to foreign_key_on. Options are :self and :related_resource
1 parent 03e765d commit 3d41deb

3 files changed

Lines changed: 5 additions & 5 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ The association methods support the following options:
247247
* `relation_name` - the name of the relation to use on the model. A lambda may be provided which allows conditional selection of the relation based on the context.
248248

249249
`has_one` associations support the additional option:
250-
* `direction` - defaults to `:belongs_to`. To indicate that the underlying model association is a `has_one` relationship specify `:reverse`.
250+
* `foreign_key_on` - defaults to `:self`. To indicate that the foreign key is on the related resource specify `:related_resource`.
251251

252252
Examples:
253253

lib/jsonapi/association.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,18 @@ def type_for_source(source)
4646
end
4747

4848
class HasOne < Association
49-
attr_reader :direction
49+
attr_reader :foreign_key_on
5050

5151
def initialize(name, options = {})
5252
super
5353
@class_name = options.fetch(:class_name, name.to_s.camelize)
5454
@type = class_name.underscore.pluralize.to_sym
5555
@foreign_key ||= "#{name}_id".to_sym
56-
@direction = options.fetch(:direction, :belongs_to)
56+
@foreign_key_on = options.fetch(:foreign_key_on, :self)
5757
end
5858

5959
def belongs_to?
60-
direction == :belongs_to
60+
foreign_key_on == :self
6161
end
6262

6363
def polymorphic_type

test/fixtures/active_record.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,7 @@ class AuthorResource < JSONAPI::Resource
10931093
attributes :name, :email
10941094
model_name 'Person'
10951095
has_many :posts
1096-
has_one :author_detail, direction: :reverse
1096+
has_one :author_detail, foreign_key_on: :related_resource
10971097

10981098
filter :name
10991099

0 commit comments

Comments
 (0)