From d17ca8a92f5c7b79061296eaed5d73d3837cb9b6 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Wed, 27 Jul 2016 21:09:51 -0400 Subject: add view/template-html, use templates for project view --- data/themes/default/templates/page.html | 2 +- data/themes/default/templates/project.html | 24 ++++++++++++++ src/guff/models/page.cr | 14 ++++++-- src/guff/models/project.cr | 16 ++++++++-- src/guff/template.cr | 1 - src/guff/views/page.cr | 51 +++++++----------------------- src/guff/views/project.cr | 14 +++++--- src/guff/views/template-html.cr | 47 +++++++++++++++++++++++++++ 8 files changed, 119 insertions(+), 50 deletions(-) create mode 100644 data/themes/default/templates/project.html create mode 100644 src/guff/views/template-html.cr diff --git a/data/themes/default/templates/page.html b/data/themes/default/templates/page.html index 5b64509..24f2290 100644 --- a/data/themes/default/templates/page.html +++ b/data/themes/default/templates/page.html @@ -9,7 +9,7 @@ %{styles} - +
page: %{name | h}
diff --git a/data/themes/default/templates/project.html b/data/themes/default/templates/project.html new file mode 100644 index 0000000..c62227a --- /dev/null +++ b/data/themes/default/templates/project.html @@ -0,0 +1,24 @@ + + + + + Project: %{name | h} + + %{styles} + + + + Project: %{name | h}
+ %{body} + +

+ Repo URL: %{repo_url|h} +

+
+ + + %{scripts} + diff --git a/src/guff/models/page.cr b/src/guff/models/page.cr index e85c484..6cca2be 100644 --- a/src/guff/models/page.cr +++ b/src/guff/models/page.cr @@ -33,7 +33,8 @@ class Guff::Models::PageModel < Guff::Models::Model e.user_id, e.name AS user_name, c.theme_id, - a.theme_id AS site_theme_id + COALESCE(c.theme_id, a.theme_id) AS active_theme_id, + COALESCE(f.theme_slug, g.theme_slug) active_theme_slug FROM sites a JOIN posts b @@ -44,6 +45,10 @@ class Guff::Models::PageModel < Guff::Models::Model ON (d.state_id = b.state_id) JOIN users e ON (e.user_id = b.created_by) + LEFT JOIN themes f + ON (f.theme_id = c.theme_id) + JOIN themes g + ON (g.theme_id = a.theme_id) WHERE a.site_id = ? AND b.slug = ? @@ -85,7 +90,8 @@ class Guff::Models::PageModel < Guff::Models::Model a.name, a.body, b.theme_id, - d.theme_id AS site_theme_id + COALESCE(b.theme_id, d.theme_id) AS active_theme_id, + COALESCE(e.theme_slug, f.theme_slug) active_theme_slug FROM posts a JOIN pages b @@ -94,6 +100,10 @@ class Guff::Models::PageModel < Guff::Models::Model ON (c.state_id = a.state_id) JOIN sites d ON (d.site_id = a.site_id) + LEFT JOIN themes e + ON (e.theme_id = b.theme_id) + JOIN themes f + ON (f.theme_id = d.theme_id) WHERE a.post_id = ? ", diff --git a/src/guff/models/project.cr b/src/guff/models/project.cr index 32be435..2aa08ae 100644 --- a/src/guff/models/project.cr +++ b/src/guff/models/project.cr @@ -12,6 +12,8 @@ class Guff::Models::ProjectModel < Guff::Models::Model ON (d.state_id = b.state_id) JOIN users e ON (e.user_id = b.created_by) + JOIN themes f + ON (f.theme_id = a.theme_id) WHERE a.site_id = ? AND b.slug = ? @@ -30,7 +32,9 @@ class Guff::Models::ProjectModel < Guff::Models::Model b.body, c.repo_url, e.user_id, - e.name AS user_name + e.name AS user_name, + a.theme_id, + f.theme_slug FROM sites a JOIN posts b @@ -41,6 +45,8 @@ class Guff::Models::ProjectModel < Guff::Models::Model ON (d.state_id = b.state_id) JOIN users e ON (e.user_id = b.created_by) + JOIN themes f + ON (f.theme_id = a.theme_id) WHERE a.site_id = ? AND b.slug = ? @@ -74,13 +80,19 @@ class Guff::Models::ProjectModel < Guff::Models::Model a.slug_lock, a.name, a.body, - b.repo_url + b.repo_url, + d.theme_id, + e.theme_slug FROM posts a JOIN projects b ON (b.post_id = a.post_id) JOIN states c ON (c.state_id = a.state_id) + JOIN sites d + ON (d.site_id = a.site_id) + JOIN themes e + ON (e.theme_id = d.theme_id) WHERE a.post_id = ? ", diff --git a/src/guff/template.cr b/src/guff/template.cr index c784a90..c69ec90 100644 --- a/src/guff/template.cr +++ b/src/guff/template.cr @@ -166,7 +166,6 @@ class Guff::Template private def parse(string : String) : Array(Ops::Op) r = string.scan(SCAN_RE).map do |md| - p md if md["text"]? Ops::LiteralOp.new(md["text"]) else diff --git a/src/guff/views/page.cr b/src/guff/views/page.cr index dc628b3..7d386c2 100644 --- a/src/guff/views/page.cr +++ b/src/guff/views/page.cr @@ -1,43 +1,14 @@ -class Guff::Views::PageView < Guff::Views::HTMLView - def initialize(context : Context, @item : Hash(String, String)) - super(context) - - # get theme id - @theme_id = @item[%w{theme_id site_theme_id}.find { |k| - @item[k]? && @item[k].size > 0 - }].not_nil!.to_i32 as Int32 - - # get theme slug, assets, and templates - @theme_slug = @context.models.theme.get(@theme_id)["theme_slug"] as String - @theme_assets = @context.models.theme.assets(@theme_id) as Theme::Assets - @theme_templates = @context.models.theme.templates(@theme_id) as Template::Cache - - # get page assets - @page_assets = @context.models.page.assets(@item["post_id"].to_i64) as Theme::Assets - end - - private def scripts - super(@theme_assets.scripts.map { |val| - theme_asset_path(val) - } + @page_assets.scripts) - end +require "./template-html" - private def styles - super(@theme_assets.styles.map { |val| - theme_asset_path(val) - } + @page_assets.styles) - end - - THEME_ASSET_PATH = "/guff/themes/%s/%s" - - private def theme_asset_path(asset_path : String) - THEME_ASSET_PATH % [@theme_slug, asset_path] - end - - def to_s(io) - @theme_templates["page.html"].run(io, @item.merge({ - "scripts" => scripts, - "styles" => styles, - })) +class Guff::Views::PageView < Guff::Views::TemplateHTMLView + def initialize(context : Context, @item : Hash(String, String)) + super( + context: context, + theme_id: @item["active_theme_id"].not_nil!.to_i32 as Int32, + theme_slug: @item["active_theme_slug"].not_nil!.to_s as String, + template_id: "page.html", + data: @item, + extra_assets: context.models.page.assets(@item["post_id"].to_i64) as Theme::Assets, + ) end end diff --git a/src/guff/views/project.cr b/src/guff/views/project.cr index e0d9bc0..fc91b5c 100644 --- a/src/guff/views/project.cr +++ b/src/guff/views/project.cr @@ -1,7 +1,13 @@ -class Guff::Views::ProjectView < Guff::Views::HTMLView +require "./template-html" + +class Guff::Views::ProjectView < Guff::Views::TemplateHTMLView def initialize(context : Context, @item : Hash(String, String)) - super(context) + super( + context: context, + theme_id: @item["theme_id"].not_nil!.to_i32 as Int32, + theme_slug: @item["theme_slug"].not_nil!.to_s as String, + template_id: "project.html", + data: @item, + ) end - - ECR.def_to_s("src/views/project.ecr") end diff --git a/src/guff/views/template-html.cr b/src/guff/views/template-html.cr new file mode 100644 index 0000000..6ad5859 --- /dev/null +++ b/src/guff/views/template-html.cr @@ -0,0 +1,47 @@ +class Guff::Views::TemplateHTMLView < Guff::Views::HTMLView + getter :theme_templates + + def initialize( + context : Context, + @theme_id : Int32, + @theme_slug : String, + @template_id : String, + @data : Hash(String, String), + @extra_assets : Theme::Assets? = nil, + ) + super(context) + + # get theme assets and templates + @theme_assets = @context.models.theme.assets(@theme_id) as Theme::Assets + @theme_templates = @context.models.theme.templates(@theme_id) as Template::Cache + end + + def scripts + theme_scripts = @theme_assets.scripts.map { |val| + theme_asset_path(val) + } + + super(@extra_assets ? theme_scripts + @extra_assets.not_nil!.scripts : theme_scripts) + end + + def styles + theme_styles = @theme_assets.styles.map { |val| + theme_asset_path(val) + } + + super(@extra_assets ? theme_styles + @extra_assets.not_nil!.styles : theme_styles) + end + + THEME_ASSET_PATH = "/guff/themes/%s/%s" + + private def theme_asset_path(asset_path : String) + THEME_ASSET_PATH % [@theme_slug, asset_path] + end + + def to_s(io : IO) + @theme_templates[@template_id].run(io, { + "scripts" => scripts, + "styles" => styles, + }.merge!(@data)) + end +end -- cgit v1.2.3