@@ -12,6 +12,7 @@ class Resource
12
12
13
13
include Helpers ::DynamicAttributes
14
14
include Helpers ::Dirty
15
+ include Helpers ::Associatable
15
16
16
17
attr_accessor :last_result_set ,
17
18
:links ,
@@ -29,7 +30,6 @@ class Resource
29
30
:relationship_linker ,
30
31
:read_only_attributes ,
31
32
:requestor_class ,
32
- :associations ,
33
33
:json_key_format ,
34
34
:route_format ,
35
35
:request_params_class ,
@@ -45,7 +45,6 @@ class Resource
45
45
self . relationship_linker = Relationships ::Relations
46
46
self . read_only_attributes = [ :id , :type , :links , :meta , :relationships ]
47
47
self . requestor_class = Query ::Requestor
48
- self . associations = [ ]
49
48
self . request_params_class = RequestParams
50
49
self . keep_request_params = false
51
50
@@ -55,10 +54,6 @@ class Resource
55
54
#:underscored_route, :camelized_route, :dasherized_route, or custom
56
55
self . route_format = :underscored_route
57
56
58
- include Associations ::BelongsTo
59
- include Associations ::HasMany
60
- include Associations ::HasOne
61
-
62
57
class << self
63
58
extend Forwardable
64
59
def_delegators :_new_scope , :where , :order , :includes , :select , :all , :paginate , :page , :with_params , :first , :find , :last
@@ -250,6 +245,12 @@ def member_endpoint(name, options = {})
250
245
# @option options [Symbol] :default The default value for the property
251
246
def property ( name , options = { } )
252
247
schema . add ( name , options )
248
+ define_method ( name ) do
249
+ attributes [ name ]
250
+ end
251
+ define_method ( "#{ name } =" ) do |value |
252
+ set_attribute ( name , value )
253
+ end
253
254
end
254
255
255
256
# Declare multiple properties with the same optional options
@@ -428,8 +429,10 @@ def save
428
429
self . attributes = updated . attributes
429
430
self . links . attributes = updated . links . attributes
430
431
self . relationships . attributes = updated . relationships . attributes
432
+ self . relationships . last_result_set = last_result_set
431
433
clear_changes_information
432
434
self . relationships . clear_changes_information
435
+ _clear_cached_relationships
433
436
end
434
437
true
435
438
end
@@ -445,6 +448,9 @@ def destroy
445
448
false
446
449
else
447
450
self . attributes . clear
451
+ self . relationships . attributes . clear
452
+ self . relationships . last_result_set = nil
453
+ _clear_cached_relationships
448
454
true
449
455
end
450
456
end
@@ -480,27 +486,39 @@ def reset_request_select!(*resource_types)
480
486
481
487
protected
482
488
483
- def method_missing ( method , *args )
484
- association = association_for ( method )
489
+ def relationship_definition_for ( name )
490
+ relationships [ name ] if relationships && relationships . has_attribute? ( name )
491
+ end
485
492
486
- return super unless association || ( relationships && relationships . has_attribute? ( method ) )
493
+ def included_data_for ( name , relationship_definition )
494
+ last_result_set . included . data_for ( name , relationship_definition )
495
+ end
487
496
488
- return nil unless relationship_definitions = relationships [ method ]
497
+ def relationship_data_for ( name , relationship_definition , association )
498
+ return unless relationship_definition
489
499
490
500
# look in included data
491
- if relationship_definitions . key? ( "data" )
492
- return last_result_set . included . data_for ( method , relationship_definitions )
501
+ if relationship_definition . key? ( "data" )
502
+ return included_data_for ( name , relationship_definition )
493
503
end
494
504
495
- if association = association_for ( method )
496
- # look for a defined relationship url
497
- if relationship_definitions [ "links" ] && url = relationship_definitions [ "links" ] [ "related" ]
498
- return association . data ( url )
499
- end
505
+ url = relationship_definition [ "links" ] [ "related" ]
506
+ if relationship_definition [ "links" ] && url
507
+ return association . data ( url )
500
508
end
509
+
501
510
nil
502
511
end
503
512
513
+ def method_missing ( method , *args )
514
+ association = association_for ( method )
515
+ relationship_definition = relationship_definition_for ( method )
516
+
517
+ return super unless association || relationship_definition
518
+
519
+ relationship_data_for ( method , relationship_definition , association )
520
+ end
521
+
504
522
def respond_to_missing? ( symbol , include_all = false )
505
523
return true if relationships && relationships . has_attribute? ( symbol )
506
524
return true if association_for ( symbol )
0 commit comments