lib/scrivito/basic_obj.rb in scrivito_sdk-0.65.2 vs lib/scrivito/basic_obj.rb in scrivito_sdk-0.66.0.rc1

- old
+ new

@@ -95,11 +95,11 @@ # # @example Dates attributes accept Time, Date and their subclasses (DateTime for example) # Obj.create(:date => Time.new) # Obj.create(:date => Date.now) # - # @example String, text, html and enum can be set by passing a {String} value + # @example String, html and enum can be set by passing a {String} value # Obj.create(:title => "My Title") # # @example Arrays of {String Strings} allow you to set multi enum fields # Obj.create(:tags => ["ruby", "rails"]) # @@ -183,17 +183,16 @@ # @note If invoked on a subclass of Obj, the result will be restricted to instances of that subclass. # # {ObjSearchEnumerator}s can be chained using one of the chainable methods # (e.g. {ObjSearchEnumerator#and} and {ObjSearchEnumerator#and_not}). # - # @example Look for the first 10 Objs whose ObjClass is "Pressrelease" and whose title contains "quarterly": + # @example Look for the first 10 Objs whose obj class is "Pressrelease" and whose title contains "quarterly": # Obj.where(:_obj_class, :equals, 'Pressrelease').and(:title, :contains, 'quarterly').take(10) # @param [Symbol, String, Array<Symbol, String>] field See {ObjSearchEnumerator#and} for details # @param [Symbol, String] operator See {ObjSearchEnumerator#and} for details # @param [String, Array<String>] value See {ObjSearchEnumerator#and} for details # @param [Hash] boost See {ObjSearchEnumerator#and} for details - # @raise [ScrivitoError] if called on a subclass of +Obj+ with no corresponding {ObjClass} # @raise [ScrivitoError] if called directly on +BasicObj+. Use +Obj.where+ instead. # @return [ObjSearchEnumerator] # @api public def self.where(field, operator, value, boost = nil) assert_not_basic_obj('.where') @@ -206,11 +205,10 @@ end # Returns a {ObjSearchEnumerator} of all {Scrivito::BasicObj Obj}s. # If invoked on a subclass of Obj, the result will be restricted to instances of that subclass. # @return [ObjSearchEnumerator] - # @raise [ScrivitoError] if called on a subclass of +Obj+ with no corresponding {ObjClass} # @raise [ScrivitoError] if called directly on +BasicObj+. Use +Obj.all+ instead. # @api public def self.all assert_not_basic_obj('.all') if self == ::Obj @@ -219,11 +217,11 @@ find_all_by_obj_class(name) end end # Returns a {ObjSearchEnumerator} of all Objs with the given +obj_class+. - # @param [String] obj_class Name of the ObjClass. + # @param [String] obj_class name of the obj class. # @return [ObjSearchEnumerator] # @api public def self.find_all_by_obj_class(obj_class) Workspace.current.objs.find_all_by_obj_class(obj_class) end @@ -278,34 +276,27 @@ # Override it to allow only certain classes or none. # Must return either +NilClass+, or +Array+. # # Be aware that the given argument is a parent path. # E.g. when creating a page with path +/products/shoes+ then the argument will be +/products+. + # The given parent path can also be +NilClass+. # # If +NilClass+ is returned, then all possible classes will be available. # By default +NilClass+ is returned. # - # If +Array+ is returned, then it should include desired class names. - # Each class name must be either a +String+ or a +Symbol+. - # Only this class names will be available. Order of the class names will be preserved. + # If +Array+ is returned, then it should include the desired classes. + # Only this classes will be available. Order of the classes will be preserved. # - # @param [String] parent_path Path of the parent obj - # @return [NilClass, Array<Symbol, String>] + # @param [String, NilClass] parent_path Path of the parent obj + # @return [NilClass, Array<Class>] # @api public def self.valid_page_classes_beneath(parent_path) end def self.valid_page_ruby_classes_beneath(parent_path) - computed_classes = valid_page_classes_beneath(parent_path) - - return Scrivito.models.pages.to_a unless computed_classes - - convert_to_obj_classes(computed_classes, Obj.type_computer) do |class_name| - Rails.logger.warn( - "Invalid page class #{class_name} returned by #valid_page_classes_beneath." - ) - end + assert_classes(valid_page_classes_beneath(parent_path), '.valid_page_classes_beneath') || + Scrivito.models.pages.to_a end # Update the {Scrivito::BasicObj Obj} with the attributes provided. # # For an overview of which values you can set via this method see the @@ -430,11 +421,11 @@ '"Obj.root" not found: There is no "Obj" with path "/". '\ 'Maybe you forgot the migration when setting up your Scrivito application? '\ 'Try "bundle exec rake scrivito:migrate scrivito:migrate:publish".' end - # Returns the homepage obj. This can be overwritten in your application's +Obj+. + # Returns the homepage obj. This can be overridden in your application's +Obj+. # Use {#homepage?} to check if an obj is the homepage. # @return [Obj] # @api public def self.homepage root @@ -451,20 +442,20 @@ end # This method determines the controller that should be invoked when the +Obj+ is requested. # By default a controller matching the Obj's obj_class will be used. # If the controller does not exist, the CmsController will be used as a fallback. - # Overwrite this method to force a different controller to be used. + # Override this method to force a different controller to be used. # @return [String] # @api public def controller_name obj_class_name end # This method determines the action that should be invoked when the +Obj+ is requested. # The default action is 'index'. - # Overwrite this method to force a different action to be used. + # Override this method to force a different action to be used. # @return [String] # @api public def controller_action_name "index" end @@ -480,20 +471,20 @@ # The routing schema: <code><em><obj.id></em>/<em><obj.slug></em></code> # # The default is {http://apidock.com/rails/ActiveSupport/Inflector/parameterize parameterize} # on +obj.title+. # - # You can customize this part by overwriting {#slug}. + # You can customize this part by overriding {#slug}. # @return [String] # @api public def slug (title || '').parameterize end # # This method determines the description that is shown in the UI - # and defaults to {Scrivito::BasicObj#display_title}. It can be overriden by a custom value. + # and defaults to {Scrivito::BasicObj#display_title}. It can be overridden by a custom value. # # @api public # def description_for_editor display_title @@ -510,11 +501,23 @@ # def display_title (binary_title || title).presence || self.class.description_for_editor end + # The alt description of an +Obj+ used for {ScrivitoHelper#scrivito_image_tag}. + # + # By default this method returns the +title+ of this +Obj+. + # + # You can customize this part by overriding {#alt_description}. + # + # @return [String] # @api public + def alt_description + title + end + + # @api public def title read_attribute('title') end # @api public @@ -529,10 +532,27 @@ def binary? blob_attribute_definition = attribute_definitions['blob'] blob_attribute_definition.present? && blob_attribute_definition.type == 'binary' end + # + # When delivering binary Objs, this method decides whether the image transformations should be + # applied by default. + # + # @api beta + # + # By default this method returns +false+. + # Override in subclasses to fit your needs. + # + # @note Only relevant for binary Objs + # @see Scrivito::Configuration.default_image_transformation= + # @see Scrivito::Binary#transform + # + def apply_image_transformation? + false + end + # Returns true if this object is the root object. # @api public def root? path == "/" end @@ -800,11 +820,10 @@ # Please use {Scrivito::BasicObj#destroy Obj#destroy} to destroy them. # @note This method does not support +Obj+s, which are +deleted+. # Please use {Scrivito::BasicObj.restore Obj.restore} to restore them. # # @raise [ScrivitoError] If the current workspace is +published+. - # @raise [ScrivitoError] If the current workspace is the +rtc+ workspace. # @raise [ScrivitoError] If the +Obj+ is +new+. # @raise [ScrivitoError] If the +Obj+ is +deleted+. # def revert assert_revertable @@ -892,19 +911,29 @@ raise ScrivitoError.new('Could not generate a new unused widget id') end def parent_path - unless root? || path.nil? - path.gsub(/\/[^\/]+$/, '').presence || '/' - end + ParentPath.of(path) unless root? end def as_client_json data_from_cms.to_h.except(*GENERATED_ATTRIBUTES) end + def outdated? + return false if workspace.published? + + base_revision = workspace.base_revision + published_revision = Workspace.published.revision + + return false if base_revision == published_revision + return true if has_conflict? + + cms_data_for_revision(base_revision) != cms_data_for_revision(published_revision) + end + private def cms_data_for_revision(revision) return nil unless revision @@ -1001,12 +1030,10 @@ # Restores a previously deleted +Obj+. # # @api public # # @raise [ScrivitoError] If the current workspace is +published+. - # @raise [ScrivitoError] If the current workspace is the +rtc+ workspace. - # def restore(obj_id) Workspace.current.assert_revertable base_revision_path = "revisions/#{Workspace.current.base_revision_id}/objs/#{obj_id}" obj_attributes = CmsRestApi.get(base_revision_path).merge('_id' => obj_id) Workspace.current.api_request(:post, '/objs', obj: obj_attributes) @@ -1026,21 +1053,14 @@ [api_attributes, widget_pool_attributes] end def serialize_attributes(obj_attributes, widget_pool_attributes, workspace) - if workspace.uses_obj_classes - 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(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 + 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 def serialize_obj_attributes(serializer, obj_attributes) serializer.serialize(obj_attributes, find_attribute_definitions(obj_attributes['_obj_class']) || attribute_definitions)