app/controllers/fiona7/release_controller.rb in infopark_fiona7-0.71.0.7 vs app/controllers/fiona7/release_controller.rb in infopark_fiona7-0.71.1.0
- old
+ new
@@ -1,30 +1,63 @@
module Fiona7
class ReleaseController < ActionController::Base
def release
@releasable = Set.new
+ @errors = []
@obj = WriteObj.find(params[:id])
+ recursively_find_objects(@releasable, @obj, Set.new)
- @releasable.merge(referenced_objects(@obj))
- @releasable.merge(widgets(@obj))
- @releasable << @obj
-
@releasable.each do |obj|
if obj.really_edited?
- obj.take
- obj.release!
+ carefully_release(@errors, obj)
end
end
+ # TODO: display errors
render json: {}
end
protected
+
+ def carefully_release(errors, obj)
+ obj.take
+ obj.release!
+ rescue => e
+ Rails.logger.error("Error occured when releasing #{@obj.id}: unable to release Object with ID #{obj.id} (#{obj.path} because: #{e.inspect}")
+
+ # TODO: translate
+ error = "Unable to release Object with ID #{obj.id} because: #{e.message}"
+ errors << error
+ end
+
+ def recursively_find_objects(set, obj, already_encountered)
+ # already_encountered prevents loops
+ widgets_of_this_object = widgets(obj) - already_encountered
+ referenced_objects_of_this_object = referenced_objects(obj) - already_encountered
+
+ already_encountered.merge(widgets_of_this_object)
+ already_encountered.merge(referenced_objects_from_widgets)
+
+ widgets_of_this_object.each do |widget|
+ recursively_find_objects(set, widget, already_encountered)
+ end
+
+ referenced_objects_of_this_object.each do |referenced|
+ recursively_find_objects(set, referenced, already_encountered)
+ end
+
+ set.merge(widgets_of_this_object)
+ set.merge(referenced_objects_of_this_object)
+ set << obj
+
+ set
+ end
+
def widgets(obj)
links = @obj[:X_widget_pool] || []
- WriteObj.where(obj_id: links.map {|l| l.destination_object_id }).to_a
+ WriteObj.where(obj_id: links.map {|l| l.destination_object_id }).to_a.to_set
end
def referenced_objects(obj)
referenced = Set.new