module Scrivito class ChildListTag < Struct.new(:tag_name, :obj, :field_name, :view) include TagRenderer def initialize(tag_name, obj, field_name, view) unless field_name == 'toclist' raise "#{field_name} is not (yet) supported. Currently only toclist is supported." end super end def options return {} unless user_present? options = { 'private-child-list-allowed-classes' => Obj.valid_page_classes_beneath(obj.path).to_json, 'private-child-list-path' => obj.path, 'private-obj-description-for-editor' => obj.description_for_editor, } if obj.has_attribute?(:child_order) options['private-child-list-order-obj-id'] = obj.id modification = comparison.modification_for_attribute(obj, :child_order) if modification == Modification::EDITED options['private-child-list-modification'] = modification end end options end def content(&block) return unless block_given? is_sortable = obj.has_attribute?(:child_order) && user_present? rendered_children = sorted_children.map do |child| obj_tag = ObjTag.new(child, is_sortable, comparison.modification(child), view) yield obj_tag, child obj_tag.rendered end view.safe_join(rendered_children) end # # This is a helper class for {ScrivitoHelper#scrivito_tag_list}. # @api public # class ObjTag < Struct.new(:obj, :is_sortable, :modification, :view) include TagRenderer attr_reader :tag_name, :rendered # # @api public # # @param tag_name [String, Symbol] Name of the html tag (e.g. +:div+ or +:span+). # @param html_options [Hash] Additional options, which are passed to +content_tag+. # Use them to add HTML attributes to the tag. # # @return [String] The rendered html tag # # @example Render a
tag containing the text "random content" and assigns the tag a css class called +very_important+. # <%= list.tag :div, class: "very_important" do %> # random content # <% end %> # def tag(tag_name, html_options = {}, &block) raise '"list.tag" can only be called once per iteration!' if @rendered @tag_name = tag_name @rendered = render(html_options, &block) nil end def options return {} unless is_sortable options = { 'private-obj-id' => obj.id, 'private-obj-description-for-editor' => obj.description_for_editor, } if modification == Modification::NEW || modification == Modification::DELETED options['private-obj-modification'] = modification end options end def content(&block) view.capture { yield } if block_given? end end private def user_present? view.scrivito_user.present? end def comparison EditingContextMiddleware.from_request(view.request).comparison end def sorted_children children = obj.toclist obj.has_attribute?(:child_order) ? Obj.sort_by_list(children, obj.child_order) : children end end end