Skip to content

Commit 4b75036

Browse files
authored
Merge pull request #1034 from cerebris/fix_string_attribute_names
Convert attribute and relationship names to symbols
2 parents 3487650 + e6240e0 commit 4b75036

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

lib/jsonapi/resource.rb

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

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

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

@@ -903,7 +905,8 @@ def _add_relationship(klass, *attrs)
903905
options = attrs.extract_options!
904906
options[:parent_resource] = self
905907

906-
attrs.each do |relationship_name|
908+
attrs.each do |name|
909+
relationship_name = name.to_sym
907910
check_reserved_relationship_name(relationship_name)
908911
check_duplicate_relationship_name(relationship_name)
909912

test/fixtures/active_record.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,12 +1470,12 @@ def records_for(rel_name)
14701470
end
14711471

14721472
class BookResource < JSONAPI::Resource
1473-
attribute :title
1473+
attribute "title"
14741474
attributes :isbn, :banned
14751475

1476-
has_many :authors
1476+
has_many "authors"
14771477

1478-
has_many :book_comments, relation_name: -> (options = {}) {
1478+
has_many "book_comments", relation_name: -> (options = {}) {
14791479
context = options[:context]
14801480
current_user = context ? context[:current_user] : nil
14811481

@@ -1486,7 +1486,7 @@ class BookResource < JSONAPI::Resource
14861486
end
14871487
}, reflect: true
14881488

1489-
has_many :aliased_comments, class_name: 'BookComments', relation_name: :approved_book_comments
1489+
has_many "aliased_comments", class_name: 'BookComments', relation_name: :approved_book_comments
14901490

14911491
filter :book_comments,
14921492
apply: ->(records, value, options) {

0 commit comments

Comments
 (0)