aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--data/init.yaml45
-rw-r--r--src/guff/models/page.cr37
-rw-r--r--src/guff/models/theme.cr6
-rw-r--r--src/guff/views/page.cr26
4 files changed, 88 insertions, 26 deletions
diff --git a/data/init.yaml b/data/init.yaml
index 4f260b4..46a1e11 100644
--- a/data/init.yaml
+++ b/data/init.yaml
@@ -45,6 +45,18 @@ init_sql:
)
- |
+ CREATE TABLE asset_types (
+ type_id INTEGER PRIMARY KEY,
+ name TEXT UNIQUE NOT NULL
+ CHECK (LENGTH(name) > 0)
+ )
+
+ - |
+ INSERT INTO asset_types(type_id, name) VALUES
+ (1, 'script'),
+ (2, 'style')
+
+ - |
CREATE TABLE themes (
theme_id INTEGER PRIMARY KEY,
@@ -159,18 +171,6 @@ init_sql:
)
- |
- CREATE TABLE theme_asset_types (
- type_id INTEGER PRIMARY KEY,
- name TEXT UNIQUE NOT NULL
- CHECK (LENGTH(name) > 0)
- )
-
- - |
- INSERT INTO theme_asset_types(type_id, name) VALUES
- (1, 'script'),
- (2, 'style')
-
- - |
CREATE TABLE theme_assets (
-- theme file
file_id INTEGER NOT NULL
@@ -178,7 +178,7 @@ init_sql:
-- asset type
type_id INTEGER NOT NULL
- REFERENCES theme_asset_types(type_id),
+ REFERENCES asset_types(type_id),
-- load order
sort_order INTEGER NOT NULL,
@@ -322,6 +322,25 @@ init_sql:
)
- |
+ CREATE TABLE page_assets (
+ post_id INTEGER NOT NULL
+ REFERENCES posts(post_id),
+
+ -- asset type
+ type_id INTEGER NOT NULL
+ REFERENCES asset_types(type_id),
+
+ -- asset path
+ path TEXT NOT NULL
+ CHECK (LENGTH(path) > 0),
+
+ -- load order
+ sort_order INTEGER NOT NULL,
+
+ PRIMARY KEY (post_id, type_id, path)
+ )
+
+ - |
CREATE TABLE sessions (
id TEXT PRIMARY KEY,
diff --git a/src/guff/models/page.cr b/src/guff/models/page.cr
index 0ce8d5b..e85c484 100644
--- a/src/guff/models/page.cr
+++ b/src/guff/models/page.cr
@@ -97,6 +97,19 @@ class Guff::Models::PageModel < Guff::Models::Model
WHERE a.post_id = ?
",
+
+ assets: "
+ SELECT b.name AS type_name,
+ a.path
+
+ FROM page_assets a
+ JOIN asset_types b
+ ON (b.type_id = a.type_id)
+
+ WHERE a.post_id = ?
+
+ ORDER BY b.name, a.sort_order
+ ",
}
LIMIT = 50
@@ -210,4 +223,28 @@ class Guff::Models::PageModel < Guff::Models::Model
def get(post_id : Int64)
@context.dbs.ro.row(SQL[:get], [post_id.to_s]).not_nil!
end
+
+ def assets(post_id : Int64) : Theme::Assets
+ r = Theme::Assets.new
+ r.styles = [] of String
+ r.scripts = [] of String
+
+ # get theme assets
+ @context.dbs.ro.all(SQL[:assets], [post_id.to_s]) do |row|
+ key, val = %w{type_name path}.map { |k| row[k].to_s }
+
+ case key
+ when "style"
+ r.styles << val
+ when "script"
+ r.scripts << val
+ else
+ # never reached
+ raise "Unknown asset type: #{key}"
+ end
+ end
+
+ # return results
+ r
+ end
end
diff --git a/src/guff/models/theme.cr b/src/guff/models/theme.cr
index 9639adf..275ddff 100644
--- a/src/guff/models/theme.cr
+++ b/src/guff/models/theme.cr
@@ -93,7 +93,7 @@ class Guff::Models::ThemeModel < Guff::Models::Model
sort_order
) VALUES (
(SELECT file_id FROM theme_files WHERE theme_id = ? AND file_path = ?),
- (SELECT type_id FROM theme_asset_types WHERE name = ?),
+ (SELECT type_id FROM asset_types WHERE name = ?),
?
)
",
@@ -116,8 +116,8 @@ class Guff::Models::ThemeModel < Guff::Models::Model
FROM theme_files a
JOIN theme_assets b
ON (b.file_id = a.file_id)
- JOIN theme_asset_types c
- ON (b.type_id = a.type_id)
+ JOIN asset_types c
+ ON (c.type_id = b.type_id)
WHERE a.theme_id = ?
diff --git a/src/guff/views/page.cr b/src/guff/views/page.cr
index 262f30c..95f91bd 100644
--- a/src/guff/views/page.cr
+++ b/src/guff/views/page.cr
@@ -9,24 +9,30 @@ class Guff::Views::PageView < Guff::Views::HTMLView
# get theme slug
@theme_slug = @context.models.theme.get(@theme_id)["theme_slug"] as String
+
+ # get theme assets
+ @theme_assets = @context.models.theme.assets(@theme_id) as Theme::Assets
+
+ # 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|
- "/guff/themes/%s/%s" % [@theme_slug, val]
- })
- # TODO: allow page-specific scripts
+ super(@theme_assets.scripts.map { |val|
+ theme_asset_path(val)
+ } + @page_assets.scripts)
end
private def styles
- super(theme_assets.styles.map { |val|
- "/guff/themes/%s/%s" % [@theme_slug, val]
- })
- # TODO: allow page-specific styles
+ super(@theme_assets.styles.map { |val|
+ theme_asset_path(val)
+ } + @page_assets.styles)
end
- private def theme_assets
- @context.models.theme.assets(@theme_id)
+ THEME_ASSET_PATH = "/guff/themes/%s/%s"
+
+ private def theme_asset_path(asset_path : String)
+ THEME_ASSET_PATH % [@theme_slug, asset_path]
end
ECR.def_to_s("src/views/page.ecr")