lib/scrivito/attribute_content.rb in scrivito_sdk-0.18.1 vs lib/scrivito/attribute_content.rb in scrivito_sdk-0.30.0.rc1

- old
+ new

@@ -39,11 +39,11 @@ end end def has_custom_attribute?(name) name = name.to_s - name != 'blob' && data_from_cms.has_custom_attribute?(name) + data_from_cms.has_custom_attribute?(name) end alias_method :has_attribute?, :has_custom_attribute? # @return [String] def type_of_attribute(field_name) @@ -54,15 +54,11 @@ # Passing an invalid key will not raise an error, but return +nil+. # @api public def [](key) key = key.to_s - if key == '_obj_class' - obj_class - else - has_attribute?(key) ? read_attribute(key) : nil - end + has_attribute?(key) ? read_attribute(key) : nil end # Hook method to control which widget classes should be available for this page. # Override it to allow only certain classes or none. # Must return either +NilClass+, or +Array+. @@ -122,10 +118,18 @@ if obj_class_data = CmsBackend.instance.find_obj_class_data_by_name(revision, obj_class_name) ObjClass.new(obj_class_data, revision.workspace) end end + def to_show_view_path + to_view_path('show') + end + + def to_details_view_path + to_view_path('details') + end + private attr_writer :data_from_cms def data_from_cms @@ -135,36 +139,48 @@ @data_from_cms end end def prepare_attribute_value(attribute_value, attribute_type, attribute_name) + if attribute_name == '_obj_class' + return obj_class + end + case attribute_type when "html" StringTagging.tag_as_html(attribute_value) when "date" DateAttribute.parse(attribute_value) if attribute_value when "linklist" build_links(attribute_value) when "link" build_link(attribute_value) when "reference" - BasicObj.find([attribute_value]).first + workspace.objs.find([attribute_value]).first when "referencelist" - BasicObj.find(attribute_value).compact + workspace.objs.find(attribute_value).compact when "widget" build_widgets(attribute_value, attribute_name) + when "binary" + build_binary(attribute_value) else attribute_value end end + def build_binary(binary_definition) + if binary_definition && binary_definition['id'] + Binary.new(binary_definition['id'], workspace.published?) + end + end + def build_links(link_definitions) if link_definitions.present? link_definitions = link_definitions.map(&:with_indifferent_access) object_ids = link_definitions.map { |link_data| link_data[:destination] }.compact.uniq - objects = object_ids.empty? ? [] : BasicObj.find(object_ids) + objects = object_ids.empty? ? [] : workspace.objs.find(object_ids) link_definitions.each_with_object([]) do |link_data, links| obj = objects.detect { |o| o && o.id == link_data[:destination] } link = Link.new(link_data.merge(obj: obj)) links << link if link.resolved? end @@ -183,11 +199,11 @@ end end def build_internal_link(attribute_value) properties = { - obj: BasicObj.find(attribute_value['destination']), + obj: workspace.objs.find(attribute_value['destination']), title: attribute_value['title'], query: attribute_value['query'], fragment: attribute_value['fragment'], target: attribute_value['target'], } @@ -219,10 +235,14 @@ widget end end + def to_view_path(view_name) + "#{obj_class_name.underscore}/#{view_name}" + end + module ClassMethods # Instantiate an Obj or Widget instance from obj_data. # If a subclass of Obj or Widget with the same name as the property +_obj_class+ exists, # the instantiated Obj or Widget will be an instance of that subclass. def instantiate(obj_data) @@ -240,11 +260,10 @@ attributes.merge("_obj_class" => self.to_s) end def descendants type_computer = TypeComputer.new(self, nil) - CmsRestApi.get("workspaces/#{Workspace.current.id}/obj_classes")['results'] - .map { |obj_class_spec| obj_class_spec['name'] } + Workspace.current.obj_classes.map(&:name) .sort .map { |obj_class_name| type_computer.compute_type(obj_class_name) } .compact end end