require 'scrivito/attribute_content' require 'fiona7/type_register' module Scrivito module AttributeContent module ClassMethods # patch attribute to notify type register alias_method :orig_attribute, :attribute def attribute(name, type, options = {}) orig_attribute(name, type, options) ensure if self.name.present? # to_s is friendly to shadowclassing Fiona7::TypeRegister.instance.add_usr_attr(self.to_s, name, type, options[:values]) end end # support shadow classes def description_for_editor to_s.titleize end # support shadow classes def as_json { name: to_s, descriptionForEditor: description_for_editor, attributes: attribute_definitions.map(&:as_json), } end def register_attribute_definitions(obj_class) type_register = Fiona7::TypeRegister.instance self.attribute_definitions.each do |attribute_definition| # to_s instead of name for shadowclassing type_register.add_usr_attr(obj_class, attribute_definition.name, attribute_definition.type, attribute_definition.values.presence) end end # support legacy attribute names def assert_valid_attribute_name(name) if name == 'id' raise ScrivitoError, "Invalid attribute name 'id'. 'id' is reserved for the system." end if name !~ /\A[A-Za-z][A-Za-z0-9_]*\z/ # <-- PATCH HERE raise ScrivitoError, "Invalid attribute name '#{name}'. " \ "The first character must be an 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 end end end