aboutsummaryrefslogtreecommitdiff
path: root/src/guff/views/html/test/auth.cr
blob: e30a4a26f45d08ac51a57d8f22c5f3fec745ac14 (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
82
require "ecr/macros"
require "../page"

class Guff::TestAuthHTMLView
  TITLE = "Guff Auth Test"
  FEATURES = %w{bootstrap font-awesome guff/util}
  SCRIPTS = %w{
    /guff-stuff/js/search-field.js
    /guff-stuff/test/auth.js
  }

  TEMPLATES = TemplateCache.new({
    role: "
      <option
        value='%{name}'
        title='%{text}'
        %{selected}
      >
        %{name}
      </option>
    ",

    tab: "
      <li role='presentation' class='%{css}'>
        <a
          id='tab-%{id}'
          href='#tab-pane-%{id}'
          title='%{text}'
          aria-controls='tab-pane-%{id}'
          role='tab'
          data-toggle='tab'
        >
          <i class='fa %{icon}'></i>
          %{name}
        </a>
      </li>
    ",
  })

  def self.run(models, context : HTTP::Server::Context)
    new(models).run(context)
  end

  def initialize(@models : Models)
  end

  def run(context)
    page = PageHTMLView.new(TITLE, self.to_s)
    page.add_features(FEATURES)
    page.scripts.concat(SCRIPTS)
    context.response.content_type = page.content_type
    context.response.puts page
  end

  def h(s : String)
    HTML.escape(s || "")
  end

  def get_roles
    @models.role.get_roles.map { |row|
      name = row["role_name"] as String

      TEMPLATES[:role].run({
        "name":     h(name),
        "text":     h(row["role_desc"] as String),
        "selected": (name == "guest") ? "selected='selected'" : ""
      })
    }.join
  end

  def get_tabs
    @models.tab.get_tabs.map { |row|
      TEMPLATES[:tab].run(row.merge({
        "css":  row["css"]? || "",
        "name": h(row["name"]),
        "text": h(row["text"]),
      }))
    }.join
  end

  ECR.def_to_s("./src/guff/views/ecrs/test/auth.ecr")
end