lib/scrivito/attribute_content.rb in scrivito_sdk-1.1.1 vs lib/scrivito/attribute_content.rb in scrivito_sdk-1.2.0.rc1

- old
+ new

@@ -198,15 +198,10 @@ end widgets end - def contained_widgets - referenced = referenced_widgets - referenced + referenced.map { |widget| widget.contained_widgets }.flatten - end - def update_data(data) self.data_from_cms = data @attribute_cache = {} end @@ -340,19 +335,28 @@ # +multienum+ attributes. If no values are provided for attributes of these types, the # resulting array of selectable values is empty. Empty string is not allowed as value. # @option options [Symbol, String] :default custom default value. # See {Scrivito::AttributeContent::DEFAULT_ATTRIBUTE_VALUES} for factory defaults. # See {Scrivito::AttributeContent::ClassMethods#default_for} for more advanced defaults. + # @option options [Class, Array<Class>] :only specifies (for use in the UI) the valid classes + # _of_ _the_ _values_ in a +reference+ or a +referencelist+ attribute, e.g. +Image+ or +Video+. + # Raises an error if the attribute type is neither +reference+ nor +referencelist+. If not + # specified, the UI assumes that any class is valid. # # @return nil + # # @raise [Scrivito::ScrivitoError] if the +type+ is unknown # @raise [Scrivito::ScrivitoError] if the +values+ include an empty string + # @raise [Scrivito::ScrivitoError] if the +only+ option is specified, but the +type+ is neither + # +reference+ nor +referencelist+ + # @raise [Scrivito::ScrivitoError] if the +only+ option includes invalid value(s) + # # @see Scrivito::AttributeContent::DEFAULT_ATTRIBUTE_VALUES # @see Scrivito::AttributeContent::ClassMethods#default_for # # @example Defining attributes: - # class Page < ::Obj + # class Page < Obj # attribute :my_string, :string # attribute 'my_html', 'my_html' # attribute :my_enum, :enum, values: %w[a b c], default: 'a' # attribute :my_multienum, :multienum # end @@ -378,22 +382,22 @@ # #=> "multienum" # Page.attribute_definitions[:my_multienum].values # #=> [] # # @example Inheriting attributes: - # class Page < ::Obj + # class Page < Obj # attribute :title, :string # end # # class SpecialPage < Page # end # # SpecialPage.attribute_definitions[:title].type # #=> "string" # # @example Overriding inherited attributes: - # class Page < ::Obj + # class Page < Obj # attribute :title, :string # end # # class SpecialPage < Page # attribute :title, :html @@ -403,10 +407,22 @@ # #=> "string" # # SpecialPage.attribute_definitions[:title].type # #=> "html" # + # @example Specifying valid classes for +reference+ attributes: + # class Page < Obj + # attribute :my_reference1, :reference, only: Image + # attribute :my_reference2, :reference, only: [Image, Video] + # end + # + # ImageWidget.attribute_definitions[:my_reference1].valid_classes + # #=> [Image] + # + # ImageWidget.attribute_definitions[:my_reference2].valid_classes + # #=> [Image, Video] + # # @see http://scrivito.com/default-attribute-values Specifying default attribute values # def attribute(name, type, options = {}) name, type, options = name.to_s, type.to_s, options assert_valid_attribute_name(name) @@ -419,11 +435,12 @@ own_attribute_definitions[name] = AttributeDefinition.new(name, type, options) nil end # - # Sets the default value of an attribute. + # Sets the default value of an attribute defined by + # {Scrivito::AttributeContent::ClassMethods#attribute}. # # @api public # # If {Scrivito::BasicObj.create Obj.create} or {Scrivito::BasicWidget.new Widget.new} are called # without providing a value for a specific custom attribute, the +block+ is called, and its @@ -522,22 +539,26 @@ raise ScrivitoError, 'No block given' unless block_given? attribute_defaults[attribute_name] = block nil end + # + # Short description of a CMS object or widget type for the UI. + # # @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 {Scrivito::BasicObj#description_for_editor} - # and, respectively, {Scrivito::BasicWidget#description_for_editor} instance methods are used instead. + # 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 + # {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 + # @return [String] short description of a CMS object or widget type for the UI + # def description_for_editor - name + name.titleize end # # Returns the attribute definitions. # @@ -618,9 +639,17 @@ missing_attributes.each do |attribute_name| attributes[attribute_name] = nil end attributes + end + + def as_json + { + name: name, + descriptionForEditor: description_for_editor, + attributes: attribute_definitions.map(&:as_json), + } end protected def assert_valid_attribute_name(name)