lib/scrivito/attribute_content.rb in scrivito_sdk-1.2.0 vs lib/scrivito/attribute_content.rb in scrivito_sdk-1.3.0.rc1
- old
+ new
@@ -9,11 +9,13 @@
module AttributeContent
ATTRIBUTE_TYPES = %w[
binary
date
enum
+ float
html
+ integer
link
linklist
multienum
reference
referencelist
@@ -25,10 +27,14 @@
COMPATIBLE_ATTRIBUTE_TYPES = [
%w[enum string],
%w[html string],
%w[multienum stringlist],
%w[string html],
+ %w[integer number],
+ %w[integer string],
+ %w[float number],
+ %w[float string],
]
#
# @api public
#
@@ -36,21 +42,33 @@
#
DEFAULT_ATTRIBUTE_VALUES = {
'binary' => nil,
'date' => nil,
'enum' => nil,
+ 'float' => FloatConversion::DEFAULT_VALUE,
'html' => '',
+ 'integer' => IntegerConversion::DEFAULT_VALUE,
'link' => nil,
'linklist' => [],
'multienum' => [],
'reference' => nil,
'referencelist' => [],
'string' => '',
'stringlist' => [],
'widgetlist' => [],
}
+ INLINE_DEFAULT_ALLOWED = %w[
+ enum
+ float
+ html
+ integer
+ multienum
+ string
+ stringlist
+ ]
+
delegate :attribute_definitions, to: :class
def has_attribute?(attribute_name)
has_public_system_attribute?(attribute_name) || has_custom_attribute?(attribute_name)
end
@@ -270,11 +288,16 @@
@attribute_deserializer ||= AttributeDeserializer.new(self, workspace)
end
def default_attribute_value(attribute_definition)
if attribute_value = DEFAULT_ATTRIBUTE_VALUES[attribute_definition.type]
- attribute_value.dup
+ case attribute_value
+ when Numeric
+ attribute_value
+ else
+ attribute_value.dup
+ end
end
end
def view_path(view_name)
"#{obj_class.underscore}/#{view_name}"
@@ -323,14 +346,16 @@
# Attributes are inherited. If, for example, the +Page+ model defines a +title+ attribute
# of the +string+ type, the +SpecialPage+ model, which inherits from +Page+, is also equipped
# with +title+. Inherited attributes can be overridden, i.e. you may redefine +title+ in
# +SpecialPage+ if you want its type to be +html+, for example.
#
- # @param [Symbol, String] name name of the attribute.
+ # @param [Symbol, String] name name of the attribute. The first character must be an ASCII
+ # lowercase letter, subsequent characters may also be digits or underscores.
+ # Also, the name must not be longer than 50 characters and must not be +"id"+.
# @param [Symbol, String] type type of the attribute. Scrivito supports the following types:
# +string+, +stringlist+, +html+, +enum+, +multienum+, +widgetlist+, +reference+,
- # +referencelist+, +link+, +linklist+, and +binary+.
+ # +referencelist+, +link+, +linklist+, +integer+, +float+ and +binary+.
# @param [Hash] options definition options.
#
# @option options [Array<Symbol>, Array<String>] :values selectable values for +enum+ and
# +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.
@@ -342,10 +367,11 @@
# 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 +name+ is invalid
# @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)
@@ -652,22 +678,31 @@
end
protected
def assert_valid_attribute_name(name)
- if name.starts_with?('_')
- raise ScrivitoError,
- "Invalid attribute name '#{name}'. Only system attributes can start with an underscore."
+ if name == 'id'
+ raise ScrivitoError, "Invalid attribute name 'id'. 'id' is reserved for the system."
end
+
+ if name !~ /\A[a-z][a-z0-9_]*\z/
+ raise ScrivitoError, "Invalid attribute name '#{name}'. " \
+ "The first character must be a lowercase ASCII letter, " \
+ "subsequent characters may also be digits or underscores."
+ end
+
+ if name.size > 50
+ raise ScrivitoError, "Invalid attribute name '#{name}'. " \
+ "Attribute names must not be longer than 50 characters."
+ end
end
def assert_valid_attribute_type(type)
raise ScrivitoError, "Unknown attribute type '#{type}'" unless ATTRIBUTE_TYPES.include?(type)
end
def assert_valid_attribute_default(name, type, default)
- inlined_default_allowed = ['string', 'stringlist', 'enum', 'multienum', 'html']
- unless inlined_default_allowed.include?(type)
+ unless INLINE_DEFAULT_ALLOWED.include?(type)
raise ScrivitoError, "Attribute '#{name}': " \
"Inline defaults for '#{type}' attributes are not supported. " \
"Please use 'default_for(:#{name})' for '#{type}' attributes instead."
end
end