lib/fiona7/builder/obj_builder.rb in infopark_fiona7-1.5.5.5.1 vs lib/fiona7/builder/obj_builder.rb in infopark_fiona7-1.6.1.1.0
- old
+ new
@@ -16,11 +16,15 @@
require 'fiona7/attribute_writers/factory'
require 'fiona7/widget_gc/garbage_collector'
+require 'fiona7/name_and_parent_from_path'
+require 'fiona7/builder/container_chain_builder'
+
+
module Fiona7
module Builder
class ParamPreprocessor
def self.call(*args)
self.new.call(*args)
@@ -63,10 +67,11 @@
end
end
end
class ObjBuilder
+ include NameAndParentFromPath
def initialize(values)
@values = ParamPreprocessor.call(values)
# garbage
@values.delete(:_modification)
@@ -106,11 +111,11 @@
@widget_pool = @values.delete(:_widget_pool)
@permalink = @values.key?(:_permalink) ? (@values.delete(:_permalink) || "") : nil
@path = "/#{@path}" unless @path.start_with?('/')
@name, parent_path = name_and_parent_path_from_path(@path)
- @parent = ensure_parent_exists(parent_path) if parent_path
+ @parent = ContainerChainBuilder.new(parent_path).call
ensure_obj_class_exists
@obj = WriteObj.create!(name: @name, parent_obj_id: @parent.id, obj_class: @real_obj_class)
end
@@ -225,40 +230,9 @@
if values.kind_of?(Hash)
values.delete_if {|k, v| !(v.respond_to?(:first) && ["string", "enum", "stringlist", "text", "html", "multienum"].include?(v.first)) }
end
end
end.as_json
- end
-
- def name_and_parent_path_from_path(path)
- components = path.split('/')
- name = components.pop.presence
- parent_path= components.join('/').presence || '/'
-
- return name, parent_path
- end
-
- def ensure_parent_exists(path)
- remaining = path.split("/")
- current = []
- paths = []
- original = path
-
- while !remaining.empty?
- current.push(remaining.shift)
-
- path = current.join('/').presence || '/'
- paths.push(path)
- end
-
- paths.each do |path|
- if !WriteObj.exists?(path: path)
- name, parent_path = name_and_parent_path_from_path(path)
- WriteObj.create!(name: name, parent_obj_id: WriteObj.find_by_path(parent_path).id, obj_class: 'X_Container')
- end
- end
-
- WriteObj.find_by_path(original) || (raise "Tried to make sure that the parent under '#{original}' exist, but it does not :(")
end
def generate_orphaned_path
return nil if update?
"_orphaned/#{SecureRandom.hex(16)}"