summaryrefslogtreecommitdiff
path: root/app/helpers/nodes_helper.rb
blob: 5884c8cd255590877704f9d83847432bcc145f0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
module NodesHelper
  
  def title_for_node node
    if node.head
      node.head.title
    else
      if not node.draft or not node.draft.title
        logger.error "Missing title in node #{node.id}"
        return "NO TITLE"
      end
      node.draft.title
    end
  end
  
  def truncated_title_for_node node
    if (title = title_for_node node) && title.size > 20
      "<span title='#{title}'>#{truncate(title, 40)}</span>"
    else
      title
    end
  end
  
  def custom_page_templates
    Page.custom_templates.map {|x| [x.gsub("_", " ").titlecase, x]}
  end
  
  def user_list
    User.all.map {|u| [u.login, u.id]}
  end

  def node_last_editor(node)
    editor = node.draft&.editor || node.head&.editor
    return nil unless editor
    editor == current_user ? t("editor_self") : editor.login
  end

  def node_head_editor(node)
    editor = node.head&.editor
    return nil unless editor
    editor == current_user ? t("publisher_self") : editor.login
  end

  DEFAULT_EVENT_TAG_BY_PAGE_TAG = {
    'erfa-detail'       => 'open-day',
    'chaostreff-detail' => 'open-day'
  }.freeze

  def default_event_tag_mapping(page)
    page_tags = page.tag_list
    DEFAULT_EVENT_TAG_BY_PAGE_TAG.find { |page_tag, _| page_tags.include?(page_tag) }
  end

  def default_event_tag_list(page)
    default_event_tag_mapping(page)&.last
  end

  def event_schedule_text(event)
    if event.rrule.present?
      recurrence = event.humanize_rrule(I18n.locale)
      if recurrence
        time = event.start_time&.strftime("%H:%M")
        time ? "#{recurrence} #{t(:event_schedule_time, time: time)}" : recurrence
      else
        "#{event.rrule} (#{t(:event_schedule_unrecognized)})"
      end
    elsif event.start_time
      I18n.l(event.start_time, format: :long)
    else
      t(:event_schedule_none)
    end
  end

  def matching_node_kinds(node)
    path = node.unique_path
    CccConventions::NODE_KINDS.select { |_, config| config[:parent_match]&.call(path) }
  end

  def sitemap_node_open?(node)
    !CccConventions::SITEMAP_COLLAPSED_PATHS.include?(node.unique_name)
  end
end