lib/scrivito/basic_obj.rb in scrivito_sdk-0.60.0 vs lib/scrivito/basic_obj.rb in scrivito_sdk-0.65.0.rc1

- old
+ new

@@ -53,16 +53,25 @@ def self.reset_type_computer! @_type_computer = nil end # - # Create a new {Scrivito::BasicObj Obj} in the cms + # Create a new {Scrivito::BasicObj Obj} in the CMS. # - # This allows you to set the different attributes types of an obj by - # providing a hash with the attributes names as key and the values you want - # to set as values + # @api public # + # This allows you to set the different attributes types of an obj by providing a hash with the + # attributes names as key and the values you want to set as values. It also considers the + # defaults set via {Scrivito::AttributeContent::ClassMethods#default_for Obj.default_for}. + # + # @param [Hash] attributes for the new obj + # @param [Hash] context in which the object creating should happen + # @option context [Scrivito::User] :scrivito_user current visitor + # @return [Obj] the newly created {Scrivito::BasicObj Obj} + # + # @see Scrivito::AttributeContent::ClassMethods#default_for + # # @example Reference lists have to be provided as an Array of {Scrivito::BasicObj Objs} # Obj.create(:reference_list => [other_obj]) # # @example Passing an {Scrivito::BasicObj Obj} allows you to set a reference # Obj.create(:reference => other_obj) @@ -105,18 +114,15 @@ # obj.update(:widgets => [obj.widgets.first]) # # # Clear a widget field # obj.update(:widgets => []) # - # @api public - # @param [Hash] attributes - # @return [Obj] the newly created {Scrivito::BasicObj Obj} - # - def self.create(attributes = {}) + def self.create(attributes = {}, context = {}) if obj_class = extract_obj_class_from_attributes(attributes) - obj_class.create(attributes) + obj_class.create(attributes, context) else + attributes = build_attributes_with_defaults(attributes, context) attributes = prepare_attributes_for_instantiation(attributes) api_attributes, widget_properties = prepare_attributes_for_rest_api(attributes) json = Workspace.current.api_request(:post, '/objs', obj: api_attributes) obj = find(json['_id']) CmsRestApi::WidgetExtractor.notify_persisted_widgets(obj, widget_properties) @@ -602,46 +608,63 @@ # @api public def last_changed read_attribute('_last_changed') end - def new?(revision=Workspace.current.base_revision) - return false unless revision + def new?(revision=workspace.base_revision) + quick_modification(revision) == "new" + end - if read_attribute('_modification') != 'deleted' - cms_data_for_revision(revision).nil? - else - false - end + def deleted?(revision=workspace.base_revision) + quick_modification(revision) == "deleted" end - def deleted?(revision=Workspace.current.base_revision) - return false unless revision + def modification(revision=workspace.base_revision) + quick_modification = quick_modification(revision) - if read_attribute('_modification') == 'deleted' - cms_data_for_revision(revision).present? + if ObjData === quick_modification + if data_from_cms == quick_modification + Modification::UNMODIFIED + else + Modification::EDITED + end + else + quick_modification end end - def modification(revision=workspace.base_revision) + # similar to modification, but faster if you are only interested in + # "new" and "deleted". + # this method sometimes does not return a string, but an instance of + # ObjData instead. this indicates that the modification is either + # UNMODIFIED or EDITED. Which one it is can be determined by comparing + # the returned ObjData. + def quick_modification(revision) return Modification::UNMODIFIED unless revision - return read_attribute('_modification') if revision == workspace.base_revision - if deleted?(revision) - Modification::DELETED - elsif new?(revision) - Modification::NEW - else # Edited - obj_data_from_revision = cms_data_for_revision(revision) - if obj_data_from_revision.present? - if data_from_cms == obj_data_from_revision - Modification::UNMODIFIED - else - Modification::EDITED - end + modification_attr = read_attribute('_modification') + + return modification_attr if revision == workspace.base_revision + + data_for_comparison = cms_data_for_revision(revision) + + if data_for_comparison.present? + if modification_attr == 'deleted' + # Obj exists in comparison revision, but not in current + Modification::DELETED else + # Obj exists in both revisions, leave the actual comparions + # up to the caller + data_for_comparison + end + else + if modification_attr == "deleted" + # Obj does not exist in either revision Modification::UNMODIFIED + else + # Obj exists in current, but not in comparision revision + Modification::NEW end end end def widget_data_for_revision(id, revision) @@ -881,12 +904,17 @@ end private def cms_data_for_revision(revision) - if revision - CmsBackend.instance.find_obj_data_by(revision, "id", [id]).first.first + return nil unless revision + + result = CmsBackend.instance.find_obj_data_by(revision, "id", [id]) + obj_data = result.first.first + + if obj_data && obj_data.value_of("_modification") != "deleted" + obj_data end end def read_widget_pool read_attribute('_widget_pool') @@ -1004,10 +1032,10 @@ serialized_attributes = CmsRestApi::LegacyAttributeSerializer.convert(obj_attributes) serialized_attributes['_widget_pool'] = CmsRestApi::LegacyAttributeSerializer .generate_widget_pool_changes(widget_pool_attributes) serialized_attributes else - serializer = AttributeSerializer.new + serializer = AttributeSerializer.new(obj_attributes['_obj_class'] || name) serialized_attributes = serialize_obj_attributes(serializer, obj_attributes) serialized_attributes['_widget_pool'] = serialize_widget_pool_attributes(serializer, widget_pool_attributes) serialized_attributes end