Class: GitBanner::ConfigEditor

Inherits:
Object
  • Object
show all
Defined in:
lib/git_banner/config_editor.rb

Class Method Summary collapse

Class Method Details

.call(env) ⇒ Object



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
# File 'lib/git_banner/config_editor.rb', line 6

def self.call(env)
  req = Rack::Request.new(env)
  file_path = Rails.root.join('config', 'git_banner_settings.json')

  if req.post?
    params = req.POST
    settings = {
      background: params["background"],
      color: params["color"],
      font_size: params["font_size"],
      font_family: params["font_family"],
      show_in: params["show_in"].split(",").map(&:strip)
    }
    File.write(file_path, JSON.pretty_generate(settings))
    return [302, { "Location" => "/git_banner/editor" }, ["Updated"]]
  else
    current = File.exist?(file_path) ? JSON.parse(File.read(file_path)) : {}
    html = "    <html><head><title>GitBanner Settings</title>\n    <script>\n    function updatePreview() {\n      let b = document.getElementById(\"preview\");\n      b.style.background = document.getElementsByName(\"background\")[0].value;\n      b.style.color = document.getElementsByName(\"color\")[0].value;\n      b.style.fontSize = document.getElementsByName(\"font_size\")[0].value;\n      b.style.fontFamily = document.getElementsByName(\"font_family\")[0].value;\n    }\n    window.onload = updatePreview;\n    </script></head><body oninput=\"updatePreview()\">\n    <h2>Configure GitBanner</h2>\n    <form method=\"POST\">\n      <label>Background: <input type=\"color\" name=\"background\" value=\"\#{current[\"background\"] || \"#222\"}\"></label><br>\n      <label>Color: <input type=\"color\" name=\"color\" value=\"\#{current[\"color\"] || \"#ccc\"}\"></label><br>\n      <label>Font size: <input type=\"text\" name=\"font_size\" value=\"\#{current[\"font_size\"] || \"12px\"}\"></label><br>\n      <label>Font family: <input type=\"text\" name=\"font_family\" value=\"\#{current[\"font_family\"] || \"monospace\"}\"></label><br>\n      <label>Show in (comma): <input type=\"text\" name=\"show_in\" value=\"\#{(current[\"show_in\"] || [\"development\"]).join(\",\")}\"></label><br>\n      <button type=\"submit\">Save</button>\n    </form>\n    <h3>Preview</h3>\n    <div id=\"preview\" style=\"padding:10px;\">Preview version: xxxxx</div>\n    </body></html>\n    HTML\n    return [200, { \"Content-Type\" => \"text/html\" }, [html]]\n  end\nend\n"