app/cms/scrivito/obj_collection.rb in scrivito_sdk-1.11.0 vs app/cms/scrivito/obj_collection.rb in scrivito_sdk-1.12.0.rc1

- old
+ new

@@ -41,11 +41,11 @@ # # @return [Scrivito::BasicObj] if an {Scrivito::BasicObj Obj} with the given +path+ exists. # @return [NilClass] if no {Scrivito::BasicObj Obj} with the given +path+ exists. # def find_by_path(path) - find_by(:path, [path]).first.min_by(&:id) + find_one_by(:path, path) end # # Returns the {Scrivito::BasicObj Obj} that has the given +permalink+. # @@ -58,11 +58,11 @@ # # @return [Scrivito::BasicObj] if an {Scrivito::BasicObj Obj} with the given +permalink+ exists. # @return [NilClass] if no {Scrivito::BasicObj Obj} with the given +permalink+ exists. # def find_by_permalink(permalink) - find_by(:permalink, [permalink]).first.min_by(&:id) + find_one_by(:permalink, permalink) end # Returns an {ObjSearchEnumerator} with the given initial subquery consisting of the # four arguments. # @@ -96,10 +96,14 @@ # @api public def find_all_by_obj_class(obj_class) all.and(:_obj_class, :equals, obj_class) end + def find_one_by(view, value, **options) + find_by(view, [value], options).first.min_by(&:id) + end + def find_by_parent_path(path) find_by(:ppath, [path]).first end def find_by_parent_path_including_deleted(path) @@ -132,27 +136,27 @@ obj or raise ResourceNotFound, "Could not find Obj with id #{id_or_list}" end end - def find_by(view, keys) - find_by_including_deleted(view, keys).map do |list| - list.reject(&:deleted?) - end + def find_by(view, keys, **options) + find_by_including_deleted(view, keys, options).each { |objs| objs.reject!(&:deleted?) } end # Accepts the name of an "obj_by" view, a list of keys. # Returns a list of lists of Objs: a list of Objs for each given key. - def find_by_including_deleted(view, keys) - result = CmsBackend.find_obj_data_by( - workspace.revision, - view, - keys) + def find_by_including_deleted(view, keys, obj_class_name: nil) + revision = workspace.revision + CmsBackend.find_obj_data_by(revision, view, keys).map do |obj_datas| + if obj_class_name + obj_datas = obj_datas.select do |obj_data| + obj_class_name == obj_data.value_of('_obj_class') + end + end - result.map do |list| - list.map do |obj_data| + obj_datas.map do |obj_data| obj = BasicObj.instantiate(obj_data) - obj.revision = workspace.revision + obj.revision = revision obj end end end