require "fiona7/obj_class_name_mangler" require "fiona7/obj_class_name_demangler" module Fiona7 class TypeLoader class AttributeTypeHelper attr_accessor :cms_attribute def initialize(cms_attribute) self.cms_attribute = cms_attribute end def real_type self.cms_attribute.attribute_type end def virtual_type ::JSON.parse(cms_attribute.help_text(:de))['type'].to_sym rescue nil end end attr_reader :obj_class, :rc_obj_class def initialize(obj_class) self.obj_class = obj_class self.rc_obj_class = RailsConnector::Meta::EagerLoader.instance.obj_class(Fiona7::ObjClassNameMangler.new(self.obj_class).mangle) end def load(type_definition) if Fiona7.mode == :legacy type_definition.add_attr('title', :string, 'title', :string) type_definition.add_attr('valid_from', :date, 'valid_from', :date) type_definition.add_attr('valid_until', :date, 'valid_until', :date) type_definition.add_attr('channels', :stringlist, 'channels', :stringlist) type_definition.add_attr('suppress_export', :enum, 'suppress_export', :enum, ['0', '1']) if self.rc_obj_class.obj_type == 'publication' || self.rc_obj_class.obj_type == 'document' type_definition.add_attr('body', :html, 'body', :html) elsif self.rc_obj_class.obj_type == 'image' || self.rc_obj_class.obj_type == 'generic' type_definition.add_attr('blob', :binary, 'blob', :binary) end end self.rc_obj_class.custom_attributes.each do |real_name, cms_attribute| next if Fiona7::Initializer::ATTRIBUTES_MAP.key?(real_name) # TODO: move this to a better place ~> AttributeNameDemangler if Fiona7.mode == :standalone virtual_name = real_name.sub("s_#{self.rc_obj_class.name}__", "") else virtual_name = real_name end type_helper = AttributeTypeHelper.new(cms_attribute) real_type = type_helper.real_type virtual_type = type_helper.virtual_type || real_type values = cms_attribute.values type_definition.add_attr(virtual_name, virtual_type, real_name, real_type, values) end end protected attr_writer :obj_class, :rc_obj_class end end