lib/scrivito/attribute_content.rb in scrivito_sdk-0.70.2 vs lib/scrivito/attribute_content.rb in scrivito_sdk-0.71.0.rc1

- old
+ new

@@ -112,13 +112,11 @@ end def valid_widget_ruby_classes_for(field) computed_classes = self.class.assert_classes(valid_widget_classes_for(field), '#valid_widget_classes_for') - - ruby_classes = computed_classes || Scrivito.models.widgets.to_a - + ruby_classes = (computed_classes || Scrivito.models.widgets.to_a).uniq ruby_classes.select { |ruby_class| ruby_class.valid_inside_container?(self.class) } end def modification_for_attribute(attribute_name, revision=Workspace.current.base_revision) @@ -145,13 +143,37 @@ end # # Returns the object class name of this CMS object. # @api public + # @deprecated Use {Scrivito::AttributeContent#obj_class} instead. # @return [String] # def obj_class_name + Scrivito::Deprecation.warn( + 'Scrivito::BasicObj#obj_class_name and Scrivito::BasicWidget#obj_class_name are deprecated'\ + ' and will be removed in a future version.'\ + ' Please use Scrivito::BasicObj#obj_class and Scrivito::BasicWidget#obj_class instead.') + obj_class + end + + # + # Returns the object class name of this CMS object. + # @api public + # @return [String] + # @example + # BlogPost.all.first.obj_class + # #=> "BlogPost" + # + # @example + # FrenchBlog::BlogPost.all.first.obj_class + # #=> "FrenchBlog::BlogPost" + # + # GermanBlog::BlogPost.all.first.obj_class + # #=> "GermanBlog::BlogPost" + # + def obj_class data_from_cms.value_of('_obj_class') end def find_attribute_containing_widget(widget_id) attribute_definitions.each do |attribute_definition| @@ -186,18 +208,22 @@ def update_data(data) self.data_from_cms = data @attribute_cache = {} end - def to_show_view_path - to_view_path('show') + def show_view_path + view_path('show') end - def to_details_view_path - to_view_path('details') + def details_view_path + view_path('details') end + def embed_view_path + view_path('embed') + end + def data_from_cms if @data_from_cms.respond_to?(:call) @data_from_cms = @data_from_cms.call else @data_from_cms @@ -253,22 +279,22 @@ if attribute_value = DEFAULT_ATTRIBUTE_VALUES[attribute_definition.type] attribute_value.dup end end - def to_view_path(view_name) - "#{obj_class_name.underscore}/#{view_name}" + def view_path(view_name) + "#{obj_class.underscore}/#{view_name}" end # # @api public # module ClassMethods # - # Instantiate an {BasicObj Obj} or {BasicWidget Widget} instance from +obj_data+. - # If a subclass of +Obj+ or +Widget+ with the same name as the +_obj_class+ property exists, - # the instantiated +Obj+ or +Widget+ will be an instance of that subclass. + # Instantiate an {Scrivito::BasicObj Obj} or {Scrivito::BasicWidget Widget} instance from + # +obj_data+. If a subclass of +Obj+ or +Widget+ with the same name as the +_obj_class+ property + # exists, the instantiated +Obj+ or +Widget+ will be an instance of that subclass. # def instantiate(obj_data) obj_class = obj_data.value_of('_obj_class') instance = type_computer.compute_type(obj_class).allocate @@ -282,12 +308,12 @@ prepare_obj_class_attribute(attributes) unless special_class? end end def extract_obj_class_from_attributes(attributes) - if special_class? && (obj_class_name = attributes[:_obj_class] || attributes['_obj_class']) - type_computer.compute_type_without_fallback(obj_class_name) + if special_class? && (obj_class = attributes[:_obj_class] || attributes['_obj_class']) + type_computer.compute_type_without_fallback(obj_class) end end # # Defines an attribute. @@ -499,12 +525,12 @@ # @api public # # Short description of a CMS object or widget type for the UI. The description is displayed # when adding new pages or widgets, for example. As a general rule, it is used whenever no - # widget or object instance is available. If there is, the +BasicObj#description_for_editor+ - # and, respectively, +BasicWidget#description_for_editor+ instance methods are used instead. + # widget or object instance is available. If there is, the {Scrivito::BasicObj#description_for_editor} + # and, respectively, {Scrivito::BasicWidget#description_for_editor} instance methods are used instead. # # This method can be overridden to customize the description displayed to editors. # # @return [String] the +Class+ name def description_for_editor @@ -513,11 +539,11 @@ # # Returns the attribute definitions. # # @api public - # @see Scrivito::AttributeContent.attribute + # @see Scrivito::AttributeContent::ClassMethods#attribute # @return [Scrivito::AttributeDefinitionCollection] # def attribute_definitions AttributeDefinitionCollection.new(all_attribute_definitions) end @@ -580,9 +606,20 @@ if (classes_expected || []).any?{ |a| !a.is_a?(Class) } raise "Overridden method #{callback_method_name} must return actual classes!" end classes_expected + end + + def reset_blank_attributes(attributes) + obj_class = type_computer.compute_type_without_fallback(attributes['_obj_class']) + missing_attributes = obj_class.attribute_definitions.map(&:name) - attributes.keys + + missing_attributes.each do |attribute_name| + attributes[attribute_name] = nil + end + + attributes end protected def assert_valid_attribute_name(name)