Skip to content

Commit 02a41b5

Browse files
committed
Merge pull request #692 from cerebris/pr/651
Pr/651
2 parents a8b698e + 2364a0a commit 02a41b5

3 files changed

Lines changed: 18 additions & 5 deletions

File tree

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,18 @@ class ContactResource < JSONAPI::Resource
214214
end
215215
```
216216

217+
##### Attribute Delegation
218+
219+
Normally resource attributes map to an attribute on the model of the same name. Using the `delegate` option allows a resource
220+
attribute to map to a differently named model attribute. For example:
221+
222+
```ruby
223+
class ContactResource < JSONAPI::Resource
224+
attribute :name_first, delegate: :first_name
225+
attribute :name_last, delegate: :last_name
226+
end
227+
```
228+
217229
##### Fetchable Attributes
218230

219231
By default all attributes are assumed to be fetchable. The list of fetchable attributes can be filtered by overriding

lib/jsonapi/resource.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,11 +399,11 @@ def attribute(attr, options = {})
399399
@_attributes ||= {}
400400
@_attributes[attr] = options
401401
define_method attr do
402-
@model.public_send(attr)
402+
@model.public_send(options[:delegate] ? options[:delegate].to_sym : attr)
403403
end unless method_defined?(attr)
404404

405405
define_method "#{attr}=" do |value|
406-
@model.public_send "#{attr}=", value
406+
@model.public_send("#{options[:delegate] ? options[:delegate].to_sym : attr}=", value)
407407
end unless method_defined?("#{attr}=")
408408
end
409409

test/unit/resource/resource_test.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class NoMatchAbstractResource < JSONAPI::Resource
3333
class CatResource < JSONAPI::Resource
3434
attribute :name
3535
attribute :breed
36+
attribute :kind, :delegate => :breed
3637

3738
has_one :mother, class_name: 'Cat'
3839
has_one :father, class_name: 'Cat'
@@ -175,7 +176,7 @@ def test_model_alternate
175176
def test_class_attributes
176177
attrs = CatResource._attributes
177178
assert_kind_of(Hash, attrs)
178-
assert_equal(attrs.keys.size, 3)
179+
assert_equal(attrs.keys.size, 4)
179180
end
180181

181182
def test_class_relationships
@@ -255,14 +256,14 @@ def test_updatable_fields_does_not_include_id
255256
# TODO: Please remove after `updateable_fields` is removed
256257
def test_updateable_fields_delegates_to_updatable_fields_with_deprecation
257258
ActiveSupport::Deprecation.silence do
258-
assert_empty(CatResource.updateable_fields(nil) - [:mother, :father, :name, :breed])
259+
assert_empty(CatResource.updateable_fields(nil) - [:mother, :father, :name, :breed, :kind])
259260
end
260261
end
261262

262263
# TODO: Please remove after `createable_fields` is removed
263264
def test_createable_fields_delegates_to_creatable_fields_with_deprecation
264265
ActiveSupport::Deprecation.silence do
265-
assert_empty(CatResource.createable_fields(nil) - [:mother, :father, :name, :breed, :id])
266+
assert_empty(CatResource.createable_fields(nil) - [:mother, :father, :name, :breed, :id, :kind])
266267
end
267268
end
268269

0 commit comments

Comments
 (0)