@@ -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 ,
@@ -47,7 +47,6 @@ class Resource
47
47
self . relationship_linker = Relationships ::Relations
48
48
self . read_only_attributes = [ :id , :type , :links , :meta , :relationships ]
49
49
self . requestor_class = Query ::Requestor
50
- self . associations = [ ]
51
50
self . request_params_class = RequestParams
52
51
self . keep_request_params = false
53
52
self . add_defaults_to_changes = false
@@ -58,10 +57,6 @@ class Resource
58
57
#:underscored_route, :camelized_route, :dasherized_route, or custom
59
58
self . route_format = :underscored_route
60
59
61
- include Associations ::BelongsTo
62
- include Associations ::HasMany
63
- include Associations ::HasOne
64
-
65
60
class << self
66
61
extend Forwardable
67
62
def_delegators :_new_scope , :where , :order , :includes , :select , :all , :paginate , :page , :with_params , :first , :find , :last
@@ -253,6 +248,12 @@ def member_endpoint(name, options = {})
253
248
# @option options [Symbol] :default The default value for the property
254
249
def property ( name , options = { } )
255
250
schema . add ( name , options )
251
+ define_method ( name ) do
252
+ attributes [ name ]
253
+ end
254
+ define_method ( "#{ name } =" ) do |value |
255
+ set_attribute ( name , value )
256
+ end
256
257
end
257
258
258
259
# Declare multiple properties with the same optional options
@@ -430,8 +431,10 @@ def save
430
431
self . attributes = updated . attributes
431
432
self . links . attributes = updated . links . attributes
432
433
self . relationships . attributes = updated . relationships . attributes
434
+ self . relationships . last_result_set = last_result_set
433
435
clear_changes_information
434
436
self . relationships . clear_changes_information
437
+ _clear_cached_relationships
435
438
end
436
439
true
437
440
end
@@ -447,6 +450,9 @@ def destroy
447
450
false
448
451
else
449
452
self . attributes . clear
453
+ self . relationships . attributes . clear
454
+ self . relationships . last_result_set = nil
455
+ _clear_cached_relationships
450
456
true
451
457
end
452
458
end
@@ -491,27 +497,36 @@ def setup_default_properties
491
497
end
492
498
end
493
499
494
- def method_missing ( method , *args )
495
- association = association_for ( method )
496
-
497
- return super unless association || ( relationships && relationships . has_attribute? ( method ) )
500
+ def relationship_definition_for ( name )
501
+ relationships [ name ] if relationships && relationships . has_attribute? ( name )
502
+ end
498
503
499
- return nil unless relationship_definitions = relationships [ method ]
504
+ def included_data_for ( name , relationship_definition )
505
+ last_result_set . included . data_for ( name , relationship_definition )
506
+ end
500
507
508
+ def relationship_data_for ( name , relationship_definition )
501
509
# look in included data
502
- if relationship_definitions . key? ( "data" )
503
- return last_result_set . included . data_for ( method , relationship_definitions )
510
+ if relationship_definition . key? ( "data" )
511
+ return included_data_for ( name , relationship_definition )
504
512
end
505
513
506
- if association = association_for ( method )
507
- # look for a defined relationship url
508
- if relationship_definitions [ "links" ] && url = relationship_definitions [ "links" ] [ "related" ]
509
- return association . data ( url )
510
- end
514
+ url = relationship_definition [ "links" ] [ "related" ]
515
+ if relationship_definition [ "links" ] && url
516
+ return association_for ( name ) . data ( url )
511
517
end
518
+
512
519
nil
513
520
end
514
521
522
+ def method_missing ( method , *args )
523
+ relationship_definition = relationship_definition_for ( method )
524
+
525
+ return super unless relationship_definition
526
+
527
+ relationship_data_for ( method , relationship_definition )
528
+ end
529
+
515
530
def respond_to_missing? ( symbol , include_all = false )
516
531
return true if relationships && relationships . has_attribute? ( symbol )
517
532
return true if association_for ( symbol )
0 commit comments