Module: Scrivito::AttributeContent::ClassMethods

Defined in:
lib/fiona7/scrivito_patches/attribute_content.rb

Instance Method Summary collapse

Instance Method Details

#as_jsonObject

support shadow classes



24
25
26
27
28
29
30
# File 'lib/fiona7/scrivito_patches/attribute_content.rb', line 24

def as_json
  {
    name: to_s,
    descriptionForEditor: description_for_editor,
    attributes: attribute_definitions.map(&:as_json),
  }
end

#assert_valid_attribute_name(name) ⇒ Object

support legacy attribute names



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fiona7/scrivito_patches/attribute_content.rb', line 42

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

#attribute(name, type, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/fiona7/scrivito_patches/attribute_content.rb', line 9

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

#description_for_editorObject

support shadow classes



19
20
21
# File 'lib/fiona7/scrivito_patches/attribute_content.rb', line 19

def description_for_editor
  to_s.titleize
end

#orig_attributeObject

patch attribute to notify type register



8
# File 'lib/fiona7/scrivito_patches/attribute_content.rb', line 8

alias_method :orig_attribute, :attribute

#register_attribute_definitions(obj_class) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/fiona7/scrivito_patches/attribute_content.rb', line 33

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