lib/scrivito/attribute_content.rb in scrivito_sdk-0.41.1 vs lib/scrivito/attribute_content.rb in scrivito_sdk-0.42.0

- old
+ new

@@ -264,9 +264,113 @@ Workspace.current.obj_classes.map(&:name) .sort .map { |obj_class_name| type_computer.compute_type(obj_class_name) } .compact end + + # + # Defines an attribute. + # + # @api private + # + # In order to be able to persist model data in CMS you have to define its attributes. + # By defining an attribute you tell Scrivito under which name its value should be persisted, + # which type of content it will contain etc, which values are allowed etc. + # + # Attributes are inherited, e.g. if a model +Page+ defines an attribute +title+ of type +string+ + # and a model +SpecialPage+ inherits from +Page+, then the model +SpecialPage+ will also have + # the attribute +title+. Inherited attributes can be overridden, e.g. +SpecialPage+ can override + # the inherited attribute +title+ by defining its own +title+ with a different type for example. + # + # @param [Symbol, String] name name of the attribute. + # @param [Symbol, String] type type of the attribute. Scrivito supports following types: +string+, + # +html+, +enum+, +multienum+, +widget+, +reference+, +referencelist+ and +binary+. + # @param [Hash] options definition options. + # + # @option options [Symbol, String] :values allowed values for types +enum+ and +multienum+. + # If no values are given for that types, then an empty array will be assumed. + # + # @return nil + # + # @example Defining attributes + # class Page < ::Obj + # attribute :my_string, :string + # attribute 'my_html', 'my_html' + # attribute :my_enum, :enum, values: %w[a b c] + # attribute :my_multienum, :multienum + # end + # + # Page.attribute_definitions + # #=> #<Scrivito::AttributeDefinitionCollection> + # + # Page.attribute_definitions[:my_string] + # #=> #<Scrivito::AttributeDefinition> + # + # Page.attribute_definitions[:my_string].type + # #=> :string + # + # Page.attribute_definitions[:my_html].type + # #=> :html + # + # Page.attribute_definitions[:my_enum].type + # #=> :enum + # Page.attribute_definitions[:my_enum].values + # #=> ["a", "b", "c"] + # + # Page.attribute_definitions[:my_multienum].type + # #=> :multienum + # Page.attribute_definitions[:my_multienum].values + # #=> [] + # + # @example Inheriting attributes + # class Page < ::Obj + # attributes :title, :string + # end + # + # class SpecialPage < Page + # end + # + # SpecialPage.attribute_definitions[:title].type + # #=> :string + # + # @example Overriding inherited attributes + # class Page < ::Obj + # attributes :title, :string + # end + # + # class SpecialPage < Page + # attribute :title, :html + # end + # + # Page.attribute_definitions[:title].type + # #=> :string + # + # SpecialPage.attribute_definitions[:title].type + # #=> :html + # + def attribute(name, type, options = {}) + name, type, options = name.to_sym, type.to_sym, options.with_indifferent_access + own_attribute_definitions[name] = AttributeDefinition.new(name, type, options) + nil + end + + def attribute_definitions + AttributeDefinitionCollection.new(all_attribute_definitions) + end + + protected + + def all_attribute_definitions + if superclass.respond_to?(:all_attribute_definitions, true) + superclass.all_attribute_definitions.merge(own_attribute_definitions) + else + own_attribute_definitions + end + end + + def own_attribute_definitions + @own_attribute_definitions ||= {} + end end end end