lib/jsonapi_compliable/util/persistence.rb in jsonapi_compliable-0.11.22 vs lib/jsonapi_compliable/util/persistence.rb in jsonapi_compliable-0.11.23

- old
+ new

@@ -25,14 +25,15 @@ # * persist current object # * associate temp id with current object # * associate parent objects with current object # * process children # * associate children + # * record hooks for later playback # * run post-process sideload hooks # * return current object # - # @return the persisted model instance + # @return a model instance def run parents = process_belongs_to(@relationships) update_foreign_key_for_parents(parents) persisted = persist_object(@meta[:method], @attributes) @@ -43,17 +44,24 @@ children = process_has_many(@relationships, persisted) do |x| update_foreign_key(persisted, x[:attributes], x) end associate_children(persisted, children) unless @meta[:method] == :destroy + post_process(persisted, parents) post_process(persisted, children) + before_commit = -> { @resource.before_commit(persisted, @meta[:method]) } + add_hook(before_commit) persisted end private + def add_hook(prc) + ::JsonapiCompliable::Util::Hooks.add(prc) + end + # The child's attributes should be modified to nil-out the # foreign_key when the parent is being destroyed or disassociated # # This is not the case for HABTM, whose "foreign key" is a join table def update_foreign_key(parent_object, attrs, x) @@ -145,10 +153,11 @@ def post_process(caller_model, processed) groups = processed.group_by { |x| x[:meta][:method] } groups.each_pair do |method, group| group.group_by { |g| g[:sideload] }.each_pair do |sideload, members| objects = members.map { |x| x[:object] } - sideload.fire_hooks!(caller_model, objects, method) + hook = -> { sideload.fire_hooks!(caller_model, objects, method) } + add_hook(hook) end end end def assign_temp_id(object, temp_id)