diff options
author | Paul Duncan <pabs@pablotron.org> | 2016-07-27 21:09:51 -0400 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2016-07-27 21:09:51 -0400 |
commit | d17ca8a92f5c7b79061296eaed5d73d3837cb9b6 (patch) | |
tree | be0a6bcc8e180fbff4f528edc43dd8334d6ad41c /src | |
parent | 066895cea97d0b730bc9b56054c9691e5181dd3f (diff) | |
download | guff-d17ca8a92f5c7b79061296eaed5d73d3837cb9b6.tar.bz2 guff-d17ca8a92f5c7b79061296eaed5d73d3837cb9b6.zip |
add view/template-html, use templates for project view
Diffstat (limited to 'src')
-rw-r--r-- | src/guff/models/page.cr | 14 | ||||
-rw-r--r-- | src/guff/models/project.cr | 16 | ||||
-rw-r--r-- | src/guff/template.cr | 1 | ||||
-rw-r--r-- | src/guff/views/page.cr | 51 | ||||
-rw-r--r-- | src/guff/views/project.cr | 14 | ||||
-rw-r--r-- | src/guff/views/template-html.cr | 47 |
6 files changed, 94 insertions, 49 deletions
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 |