@@ -12,6 +12,7 @@ class Resource
1212
1313 include Helpers ::DynamicAttributes
1414 include Helpers ::Dirty
15+ include Helpers ::Associatable
1516
1617 attr_accessor :last_result_set ,
1718 :links ,
@@ -29,7 +30,6 @@ class Resource
2930 :relationship_linker ,
3031 :read_only_attributes ,
3132 :requestor_class ,
32- :associations ,
3333 :json_key_format ,
3434 :route_format ,
3535 :request_params_class ,
@@ -47,7 +47,6 @@ class Resource
4747 self . relationship_linker = Relationships ::Relations
4848 self . read_only_attributes = [ :id , :type , :links , :meta , :relationships ]
4949 self . requestor_class = Query ::Requestor
50- self . associations = [ ]
5150 self . request_params_class = RequestParams
5251 self . keep_request_params = false
5352 self . add_defaults_to_changes = false
@@ -58,10 +57,6 @@ class Resource
5857 #:underscored_route, :camelized_route, :dasherized_route, or custom
5958 self . route_format = :underscored_route
6059
61- include Associations ::BelongsTo
62- include Associations ::HasMany
63- include Associations ::HasOne
64-
6560 class << self
6661 extend Forwardable
6762 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 = {})
253248 # @option options [Symbol] :default The default value for the property
254249 def property ( name , options = { } )
255250 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
256257 end
257258
258259 # Declare multiple properties with the same optional options
@@ -430,8 +431,10 @@ def save
430431 self . attributes = updated . attributes
431432 self . links . attributes = updated . links . attributes
432433 self . relationships . attributes = updated . relationships . attributes
434+ self . relationships . last_result_set = last_result_set
433435 clear_changes_information
434436 self . relationships . clear_changes_information
437+ _clear_cached_relationships
435438 end
436439 true
437440 end
@@ -447,6 +450,9 @@ def destroy
447450 false
448451 else
449452 self . attributes . clear
453+ self . relationships . attributes . clear
454+ self . relationships . last_result_set = nil
455+ _clear_cached_relationships
450456 true
451457 end
452458 end
@@ -491,27 +497,36 @@ def setup_default_properties
491497 end
492498 end
493499
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
498503
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
500507
508+ def relationship_data_for ( name , relationship_definition )
501509 # 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 )
504512 end
505513
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 )
511517 end
518+
512519 nil
513520 end
514521
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+
515530 def respond_to_missing? ( symbol , include_all = false )
516531 return true if relationships && relationships . has_attribute? ( symbol )
517532 return true if association_for ( symbol )
0 commit comments