Skip to content

Commit bcf8e20

Browse files
committed
Convert attribute and relatioship names to symbols
Fixes issues when strings are provided #1019 & #1032 (cherry picked from commit e6240e0)
1 parent 970bc79 commit bcf8e20

2 files changed

Lines changed: 10 additions & 7 deletions

File tree

lib/jsonapi/resource.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,10 +507,12 @@ def attributes(*attrs)
507507
end
508508
end
509509

510-
def attribute(attr, options = {})
510+
def attribute(attribute_name, options = {})
511+
attr = attribute_name.to_sym
512+
511513
check_reserved_attribute_name(attr)
512514

513-
if (attr.to_sym == :id) && (options[:format].nil?)
515+
if (attr == :id) && (options[:format].nil?)
514516
ActiveSupport::Deprecation.warn('Id without format is no longer supported. Please remove ids from attributes, or specify a format.')
515517
end
516518

@@ -1041,7 +1043,8 @@ def _add_relationship(klass, *attrs)
10411043
options = attrs.extract_options!
10421044
options[:parent_resource] = self
10431045

1044-
attrs.each do |relationship_name|
1046+
attrs.each do |name|
1047+
relationship_name = name.to_sym
10451048
check_reserved_relationship_name(relationship_name)
10461049
check_duplicate_relationship_name(relationship_name)
10471050

test/fixtures/active_record.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,12 +1462,12 @@ class PersonResource < PersonResource; end
14621462
class PostResource < PostResource; end
14631463

14641464
class BookResource < JSONAPI::Resource
1465-
attribute :title
1465+
attribute "title"
14661466
attributes :isbn, :banned
14671467

1468-
has_many :authors
1468+
has_many "authors"
14691469

1470-
has_many :book_comments, relation_name: -> (options = {}) {
1470+
has_many "book_comments", relation_name: -> (options = {}) {
14711471
context = options[:context]
14721472
current_user = context ? context[:current_user] : nil
14731473

@@ -1478,7 +1478,7 @@ class BookResource < JSONAPI::Resource
14781478
end
14791479
}, reflect: true
14801480

1481-
has_many :aliased_comments, class_name: 'BookComments', relation_name: :approved_book_comments
1481+
has_many "aliased_comments", class_name: 'BookComments', relation_name: :approved_book_comments
14821482

14831483
filters :book_comments
14841484
filter :banned, apply: :apply_filter_banned

0 commit comments

Comments
 (0)